fat: write: fix broken write at non-zero file offset
authorMarek Szyprowski <m.szyprowski@samsung.com>
Tue, 24 Sep 2019 08:50:34 +0000 (10:50 +0200)
committerJaehoon Chung <jh80.chung@samsung.com>
Tue, 20 Oct 2020 06:13:47 +0000 (15:13 +0900)
Handling of the start file offset was broken in the current code. Although
the code skipped the needed clusters, it then tried to continue write with
current cluster set to EOF, what caused assertion. It also lacked adjusting
filesize in case of writing at the end of file and adjusting in-cluster
offset for partial overwrite.

This patch fixes all those issues.

Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Change-Id: I24cf908407ef951920291e560db28e603799ae77

fs/fat/fat_write.c

index d7fdc56c8259a303414c6509308da9eb35cbcc32..984fa32372f296bb16de17a7438e12e03c3eb98d 100644 (file)
@@ -773,14 +773,12 @@ set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
        /* go to cluster at pos */
        cur_pos = bytesperclust;
        while (1) {
+               newclust = get_fatent(mydata, curclust);
                if (pos <= cur_pos)
                        break;
-               if (IS_LAST_CLUST(curclust, mydata->fatsize))
+               if (IS_LAST_CLUST(newclust, mydata->fatsize))
                        break;
-
-               newclust = get_fatent(mydata, curclust);
-               if (!IS_LAST_CLUST(newclust, mydata->fatsize) &&
-                   CHECK_CLUST(newclust, mydata->fatsize)) {
+               if (CHECK_CLUST(newclust, mydata->fatsize)) {
                        debug("curclust: 0x%x\n", curclust);
                        debug("Invalid FAT entry\n");
                        return -1;
@@ -789,8 +787,9 @@ set_contents(fsdata *mydata, dir_entry *dentptr, loff_t pos, __u8 *buffer,
                cur_pos += bytesperclust;
                curclust = newclust;
        }
-       if (IS_LAST_CLUST(curclust, mydata->fatsize)) {
+       if (IS_LAST_CLUST(newclust, mydata->fatsize)) {
                assert(pos == cur_pos);
+               filesize -= pos;
                goto set_clusters;
        }