From: batt Date: Sat, 20 Sep 2008 13:22:57 +0000 (+0000) Subject: Add max page offset allocated for the file; add some log and fix some typos. X-Git-Tag: 2.0.0~106 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=9110f3738d3613de62db86208f9833237dad66cf;p=bertos.git Add max page offset allocated for the file; add some log and fix some typos. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1828 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/fs/battfs.c b/bertos/fs/battfs.c index 5c039d34..f252695a 100644 --- a/bertos/fs/battfs.c +++ b/bertos/fs/battfs.c @@ -163,6 +163,8 @@ static bool setBufferHdr(struct BattFsSuper *disk, struct BattFsPageHeader *hdr) { uint8_t buf[BATTFS_HEADER_LEN]; + #warning FIXME:refactor computeFcs to save time and stack + hdr->fcs = computeFcs(hdr); /* Fill buffer */ battfs_to_disk(hdr, buf); @@ -201,6 +203,7 @@ static pgcnt_t countPages(pgoff_t *filelen_table, inode_t inode) static void movePages(struct BattFsSuper *disk, pgcnt_t src, int offset) { pgcnt_t dst = src + offset; + LOG_INFO("src %d, offset %d, size %d\n", src, offset, (disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t)); memmove(&disk->page_array[dst], &disk->page_array[src], (disk->page_count - MAX(dst, src)) * sizeof(pgcnt_t)); if (offset < 0) @@ -511,11 +514,12 @@ static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode, return false; } flushBuffer(disk); - LOG_INFO("Getting new page: %d\n", disk->page_array[disk->free_page_start]); + LOG_INFO("Getting new page %d, pos %d\n", disk->page_array[disk->free_page_start], new_pos); disk->curr_page = disk->page_array[disk->free_page_start++]; - movePages(disk, new_pos, +1); + memmove(&disk->page_array[new_pos + 1], &disk->page_array[new_pos], (disk->free_page_start - new_pos - 1) * sizeof(pgcnt_t)); #warning TODO: move other files! disk->page_array[new_pos] = disk->curr_page; + disk->cache_dirty = true; disk->curr_hdr.inode = inode; disk->curr_hdr.pgoff = pgoff; @@ -537,10 +541,8 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size) pgoff_t pg_offset; pgaddr_t addr_offset; pgaddr_t wr_len; - pgoff_t max_off = fd->size / (fdb->disk->page_size - BATTFS_HEADER_LEN); #warning TODO seek_pos > size? - size = MIN((kfile_off_t)size, fd->size - fd->seek_pos); while (size) { @@ -549,15 +551,14 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size) wr_len = MIN(size, (size_t)(fdb->disk->page_size - BATTFS_HEADER_LEN - addr_offset)); /* Handle write outside EOF */ - if (pg_offset > max_off) + if (pg_offset > fdb->max_off) { - if (!getNewPage(fdb->disk, (fdb->disk->page_array - fdb->start) + max_off, fdb->inode, pg_offset)) + if (!getNewPage(fdb->disk, (fdb->disk->page_array - fdb->start) + pg_offset, fdb->inode, pg_offset)) return total_write; - max_off = pg_offset; + fdb->max_off = pg_offset; } - /* Handle cache load of a new page*/ - if (fdb->start[pg_offset] != fdb->disk->curr_page) + else if (fdb->start[pg_offset] != fdb->disk->curr_page) { LOG_INFO("Re-writing page %d to %d\n", fdb->start[pg_offset], fdb->disk->page_array[fdb->disk->free_page_start]); if (!loadPage(fdb->disk, fdb->start[pg_offset])) @@ -593,7 +594,8 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size) fill_t fill_delta = MAX((int32_t)(addr_offset + wr_len) - fdb->disk->curr_hdr.fill, (int32_t)0); fdb->disk->free_bytes -= fill_delta; fdb->disk->curr_hdr.fill += fill_delta; - fdb->fd.size += fill_delta; + fd->size += fill_delta; + LOG_INFO("free_bytes %ld, seek_pos %ld, size %ld, curr_hdr.fill %ld\n", fdb->disk->free_bytes, fd->seek_pos, fd->size, fdb->disk->curr_hdr.fill); } return total_write; } @@ -729,6 +731,7 @@ bool battfs_fileopen(BattFsSuper *disk, BattFs *fd, inode_t inode, filemode_t mo /* Fill file size */ if ((fd->fd.size = countFileSize(disk, fd->start, inode)) == EOF) return false; + fd->max_off = fd->fd.size / (disk->page_size - BATTFS_HEADER_LEN); /* Reset seek position */ fd->fd.seek_pos = 0; diff --git a/bertos/fs/battfs.h b/bertos/fs/battfs.h index b14cb1c6..f46d8c8a 100644 --- a/bertos/fs/battfs.h +++ b/bertos/fs/battfs.h @@ -244,6 +244,7 @@ typedef struct BattFs BattFsSuper *disk; ///< Disk context filemode_t mode; ///< File open mode pgcnt_t *start; ///< Pointer to page_array file start position. + pgcnt_t max_off; ///< Max page offset allocated for the file. } BattFs; /**