From: batt Date: Thu, 25 Sep 2008 16:11:55 +0000 (+0000) Subject: Refactor to fill sparse bytes with a known value (0). X-Git-Tag: 2.0.0~88 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=35a576d2832982a6860ff15f7e6f04ad7427e140;p=bertos.git Refactor to fill sparse bytes with a known value (0). git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1846 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/fs/battfs.c b/bertos/fs/battfs.c index 87b54019..2f4eee43 100644 --- a/bertos/fs/battfs.c +++ b/bertos/fs/battfs.c @@ -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; diff --git a/bertos/fs/battfs_test.c b/bertos/fs/battfs_test.c index 0df504cd..ca0ded69 100644 --- a/bertos/fs/battfs_test.c +++ b/bertos/fs/battfs_test.c @@ -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));