From: batt Date: Fri, 26 Sep 2008 15:00:21 +0000 (+0000) Subject: Add more tests; handle end of disk space correctly. X-Git-Tag: 2.0.0~81 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=4ce77ecc12cbc60cdb8717755006f6f461e6f6ad;p=bertos.git Add more tests; handle end of disk space correctly. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1853 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/fs/battfs.c b/bertos/fs/battfs.c index 671a1b91..7d30231f 100644 --- a/bertos/fs/battfs.c +++ b/bertos/fs/battfs.c @@ -593,10 +593,10 @@ static int battfs_fileclose(struct KFile *fd) static bool getNewPage(struct BattFsSuper *disk, pgcnt_t new_pos, inode_t inode, pgoff_t pgoff, BattFsPageHeader *new_hdr) { - if (disk->free_page_start >= disk->page_count) + if (SPACE_OVER(disk)) { #warning TODO space over! - LOG_INFO("No disk space available\n"); + LOG_ERR("No disk space available!\n"); return false; } flushBuffer(disk); @@ -724,6 +724,11 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size) /* Handle cache load of a new page*/ else if (fdb->start[pg_offset] != fdb->disk->curr_page) { + if (SPACE_OVER(fdb->disk)) + { + LOG_ERR("No disk space available!\n"); + return total_write; + } 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], &curr_hdr)) { @@ -743,7 +748,6 @@ static size_t battfs_write(struct KFile *fd, const void *_buf, size_t size) curr_hdr.seq++; } - //LOG_INFO("writing to buffer for page %d, offset %d, size %d\n", fdb->disk->curr_page, addr_offset, wr_len); if (fdb->disk->bufferWrite(fdb->disk, addr_offset, buf, wr_len) != wr_len) { diff --git a/bertos/fs/battfs.h b/bertos/fs/battfs.h index 20f38e07..bd530e81 100644 --- a/bertos/fs/battfs.h +++ b/bertos/fs/battfs.h @@ -220,6 +220,11 @@ typedef struct BattFsSuper /* TODO add other fields. */ } BattFsSuper; +/** + * True if space on \a disk is over. + */ +#define SPACE_OVER(disk) ((disk)->free_page_start >= (disk)->page_count) + typedef uint8_t filemode_t; ///< Type for file open modes. typedef int32_t file_size_t; ///< Type for file sizes. diff --git a/bertos/fs/battfs_test.c b/bertos/fs/battfs_test.c index da0e79e6..4d252b43 100644 --- a/bertos/fs/battfs_test.c +++ b/bertos/fs/battfs_test.c @@ -890,11 +890,124 @@ static void writeEOF(BattFsSuper *disk) } +static void endOfSpace(BattFsSuper *disk) +{ + TRACEMSG("20: what happens when disk space is over?\n"); + BattFs fd1; + uint8_t buf[(PAGE_SIZE - BATTFS_HEADER_LEN) * 5]; + + fp = fopen(test_filename, "w+"); + + unsigned int PAGE_FILL = PAGE_SIZE - BATTFS_HEADER_LEN; + inode_t INODE = 0; + unsigned int MODE = BATTFS_CREATE; + + disk->erase(disk, 0); + disk->erase(disk, 1); + disk->erase(disk, 2); + disk->erase(disk, 3); + fclose(fp); + + ASSERT(battfs_init(disk)); + ASSERT(battfs_fsck(disk)); + ASSERT(battfs_fileopen(disk, &fd1, INODE, MODE)); + ASSERT(kfile_write(&fd1.fd, buf, sizeof(buf)) == PAGE_FILL * 4); + ASSERT(fd1.fd.size == PAGE_FILL * 4); + ASSERT(fd1.fd.seek_pos == PAGE_FILL * 4); + ASSERT(disk->free_bytes == 0); + + ASSERT(kfile_close(&fd1.fd) == 0); + ASSERT(battfs_fsck(disk)); + ASSERT(battfs_close(disk)); + + TRACEMSG("20: passed\n"); +} + + +static void multipleFilesRW(BattFsSuper *disk) +{ + TRACEMSG("21: multiple files read/write test\n"); + + FILE *fpt = fopen(test_filename, "w+"); + + for (int i = 0; i < FILE_SIZE; i++) + fputc(0xff, fpt); + fclose(fpt); + + #define N_FILES 10 + BattFs fd[N_FILES]; + inode_t INODE = 0; + unsigned int MODE = BATTFS_CREATE; + uint32_t buf[FILE_SIZE / (4 * N_FILES * sizeof(uint32_t))]; + + ASSERT(battfs_init(disk)); + ASSERT(battfs_fsck(disk)); + for (inode_t i = 0; i < N_FILES; i++) + ASSERT(battfs_fileopen(disk, &fd[i], i, MODE)); + + for (int i = N_FILES - 1; i >= 0; i--) + { + for (uint32_t j = 0; j < countof(buf); j++) + buf[j] = j+i; + + ASSERT(kfile_write(&fd[i], buf, sizeof(buf)) == sizeof(buf)); + ASSERT(fd[i].fd.size == sizeof(buf)); + ASSERT(fd[i].fd.seek_pos == sizeof(buf)); + ASSERT(kfile_seek(&fd[i].fd, 0, SEEK_SET) == 0); + } + + for (inode_t i = 0; i < N_FILES; i++) + { + memset(buf, 0, sizeof(buf)); + ASSERT(kfile_read(&fd[i], buf, sizeof(buf)) == sizeof(buf)); + + + for (uint32_t j = 0; j < countof(buf); j++) + ASSERT(buf[j] == j+i); + + ASSERT(fd[i].fd.size == sizeof(buf)); + ASSERT(fd[i].fd.seek_pos == sizeof(buf)); + ASSERT(kfile_seek(&fd[i].fd, 0, SEEK_SET) == 0); + } + + for (inode_t i = 0; i < N_FILES; i++) + ASSERT(kfile_close(&fd[i].fd) == 0); + + ASSERT(battfs_fsck(disk)); + ASSERT(battfs_close(disk)); + + ASSERT(battfs_init(disk)); + ASSERT(battfs_fsck(disk)); + + for (inode_t i = 0; i < N_FILES; i++) + ASSERT(battfs_fileopen(disk, &fd[i], i, 0)); + + for (inode_t i = 0; i < N_FILES; i++) + { + memset(buf, 0, sizeof(buf)); + ASSERT(kfile_read(&fd[i], buf, sizeof(buf)) == sizeof(buf)); + + for (uint32_t j = 0; j < countof(buf); j++) + ASSERT(buf[j] == j+i); + + ASSERT(fd[i].fd.size == sizeof(buf)); + ASSERT(fd[i].fd.seek_pos == sizeof(buf)); + ASSERT(kfile_seek(&fd[i].fd, 0, SEEK_SET) == 0); + } + + for (inode_t i = 0; i < N_FILES; i++) + ASSERT(kfile_close(&fd[i].fd) == 0); + + ASSERT(battfs_close(disk)); + TRACEMSG("21: passed\n"); +} + int battfs_testRun(void) { BattFsSuper disk; + disk.page_size = PAGE_SIZE; disk.open = disk_open; disk.read = disk_page_read; disk.load = disk_page_load; @@ -921,6 +1034,8 @@ int battfs_testRun(void) increaseFile(&disk); readEOF(&disk); writeEOF(&disk); + endOfSpace(&disk); + multipleFilesRW(&disk); kprintf("All tests passed!\n");