Refactor to fill sparse bytes with a known value (0).
authorbatt <batt@38d2e660-2303-0410-9eaa-f027e97ec537>
Thu, 25 Sep 2008 16:11:55 +0000 (16:11 +0000)
committerbatt <batt@38d2e660-2303-0410-9eaa-f027e97ec537>
Thu, 25 Sep 2008 16:11:55 +0000 (16:11 +0000)
git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1846 38d2e660-2303-0410-9eaa-f027e97ec537

bertos/fs/battfs.c
bertos/fs/battfs_test.c

index 87b540197661745f445983e9e149f09a179458be..2f4eee432bb7adce1614aeb07b471935d3af9d65 100644 (file)
@@ -589,51 +589,61 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size)
        if (fd->seek_pos < 0)
                return total_write;
 
-       if ((fd->seek_pos / fdb->disk->data_size) > fdb->max_off)
+       if (fd->seek_pos > fd->size)
        {
-               /*
-                * Handle writing when seek pos if far over EOF,
-                * We need to allocate the missing pages first.
-                */
-               pgoff_t missing_pages = fd->seek_pos / fdb->disk->data_size - fdb->max_off;
-
-               LOG_INFO("missing pages: %d\n", missing_pages);
+               /* Handle writing when seek pos if far over EOF */
                if (!loadPage(fdb->disk, fdb->start[fdb->max_off], &curr_hdr))
                {
                                #warning TODO set error?
                                return total_write;
                }
 
-               uint8_t dummy = 0xff;
-               /* Add missing pages to reach current seek_pos */
-               while (missing_pages--)
+               /* Fill unused space of first page with 0s */
+               uint8_t dummy = 0;
+               pgaddr_t zero_bytes = MIN(fd->seek_pos - fd->size, fdb->disk->data_size - curr_hdr.fill);
+               while (zero_bytes--)
                {
-                       /* Update size and free space left */
-                       fd->size += fdb->disk->data_size - curr_hdr.fill;
-                       fdb->disk->free_bytes -= fdb->disk->data_size - curr_hdr.fill;
+                       if (fdb->disk->bufferWrite(fdb->disk, curr_hdr.fill, &dummy, 1) != 1)
+                       {
+                               #warning TODO set error?
+                       }
+                       curr_hdr.fill++;
+                       fd->size++;
+                       fdb->disk->free_bytes--;
+                       fdb->disk->cache_dirty = true;
+               }
+               setBufferHdr(fdb->disk, &curr_hdr);
+               flushBuffer(fdb->disk);
 
-                       /* Fill empty space with 0xFF */
-                       for (addr_offset = curr_hdr.fill; addr_offset < fdb->disk->data_size; addr_offset++)
+               /* Fill page buffer with 0 to avoid filling unused pages with garbage */
+               for (pgaddr_t off = 0; off < fdb->disk->data_size; off++)
+               {
+                       if (fdb->disk->bufferWrite(fdb->disk, off, &dummy, 1) != 1)
                        {
-                               if (fdb->disk->bufferWrite(fdb->disk, addr_offset, &dummy, 1) != 1)
-                               {
-                                       #warning TODO set error?
-                               }
+                               #warning TODO set error?
                        }
-                       curr_hdr.fill = fdb->disk->data_size;
-                       setBufferHdr(fdb->disk, &curr_hdr);
+               }
 
+               /* Allocate the missing pages first. */
+               pgoff_t missing_pages = fd->seek_pos / fdb->disk->data_size - fdb->max_off;
+               LOG_INFO("missing pages: %d\n", missing_pages);
+
+               while (missing_pages--)
+               {
+                       zero_bytes = MIN((kfile_off_t)fdb->disk->data_size, fd->seek_pos - fd->size);
                        /* Get the new page needed */
                        if (!getNewPage(fdb->disk, (fdb->start - fdb->disk->page_array) + fdb->max_off + 1, fdb->inode, fdb->max_off + 1, &curr_hdr))
                                return total_write;
 
+                       /* Update size and free space left */
+                       fd->size += zero_bytes;
+                       fdb->disk->free_bytes -= zero_bytes;
+
+                       curr_hdr.fill = zero_bytes;
+                       setBufferHdr(fdb->disk, &curr_hdr);
+
                        fdb->max_off++;
                }
-               /* Fix sizes for the last new page (could be only partially full) */
-               curr_hdr.fill = fd->seek_pos % fdb->disk->data_size;
-               setBufferHdr(fdb->disk, &curr_hdr);
-               fd->size += curr_hdr.fill;
-               fdb->disk->free_bytes -= curr_hdr.fill;
        }
        else if (!getBufferHdr(fdb->disk, &curr_hdr))
                return total_write;
index 0df504cd72fc5cb31983deea18410fdc7638d6ae..ca0ded691e0b225f79c9bf00cea8f50cfa5ab416 100644 (file)
@@ -817,7 +817,7 @@ static void writeEOF(BattFsSuper *disk)
        memset(buf, 0, 2);
        ASSERT(kfile_read(&fd1.fd, buf, 2) == 2);
        for (int i = 0; i < 2; i++)
-               ASSERT(buf[i] == 0xff);
+               ASSERT(buf[i] == 0);
 
        memset(buf, 0, 2);
        ASSERT(kfile_read(&fd1.fd, buf, 2) == 2);
@@ -838,7 +838,7 @@ static void writeEOF(BattFsSuper *disk)
        ASSERT(kfile_read(&fd1.fd, buf, 2) == 2);
        ASSERT(fd1.fd.seek_pos == 2);
        for (int i = 0; i < 2; i++)
-               ASSERT(buf[i] == 0xff);
+               ASSERT(buf[i] == 0);
 
        memset(buf, 0, 2);
        ASSERT(kfile_read(&fd1.fd, buf, 2) == 2);
@@ -850,7 +850,7 @@ static void writeEOF(BattFsSuper *disk)
        ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));
        ASSERT(fd1.fd.seek_pos == sizeof(buf) + 4);
        for (int i = 0; i < sizeof(buf); i++)
-               ASSERT(buf[i] == 0xff);
+               ASSERT(buf[i] == 0);
 
        memset(buf, 0, sizeof(buf));
        ASSERT(kfile_read(&fd1.fd, buf, sizeof(buf)) == sizeof(buf));