Skip to content

Commit

Permalink
SPIFFS_eof() and SPIFFS_tell() return wrong value #72 (thanks @ArtemP…
Browse files Browse the repository at this point in the history
  • Loading branch information
pellepl committed Mar 7, 2016
1 parent 1d9f670 commit 24aac90
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/spiffs_hydrogen.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ s32_t SPIFFS_write(spiffs *fs, spiffs_file fh, void *buf, s32_t len) {
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
}

if ((fd->flags & SPIFFS_APPEND)) {
fd->fdoffset = fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size;
}

offset = fd->fdoffset;

#if SPIFFS_CACHE_WR
Expand Down Expand Up @@ -1053,7 +1057,7 @@ s32_t SPIFFS_eof(spiffs *fs, spiffs_file fh) {
SPIFFS_API_CHECK_RES_UNLOCK(fs, res);
#endif

res = (fd->fdoffset == fd->size);
res = (fd->fdoffset >= (fd->size == SPIFFS_UNDEFINED_LEN ? 0 : fd->size));

SPIFFS_UNLOCK(fs);
return res;
Expand Down
69 changes: 69 additions & 0 deletions src/test/test_bugreports.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,73 @@ TEST(truncate_48) {
} TEST_END(truncate_48)
#endif

TEST(eof_tell_72) {
fs_reset();

s32_t res;

spiffs_file fd = SPIFFS_open(FS, "file", SPIFFS_CREAT | SPIFFS_RDWR | SPIFFS_APPEND, 0);
TEST_CHECK_GT(fd, 0);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 0);

res = SPIFFS_write(FS, fd, "test", 4);
TEST_CHECK_EQ(res, 4);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 4);

res = SPIFFS_fflush(FS, fd);
TEST_CHECK_EQ(res, SPIFFS_OK);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 4);

res = SPIFFS_lseek(FS, fd, 2, SPIFFS_SEEK_SET);
TEST_CHECK_EQ(res, 2);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 2);

res = SPIFFS_write(FS, fd, "test", 4);
TEST_CHECK_EQ(res, 4);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 8);

res = SPIFFS_fflush(FS, fd);
TEST_CHECK_EQ(res, SPIFFS_OK);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 8);

res = SPIFFS_close(FS, fd);
TEST_CHECK_EQ(res, SPIFFS_OK);
TEST_CHECK_LT(SPIFFS_eof(FS, fd), SPIFFS_OK);
TEST_CHECK_LT(SPIFFS_tell(FS, fd), SPIFFS_OK);

fd = SPIFFS_open(FS, "file", SPIFFS_RDWR, 0);
TEST_CHECK_GT(fd, 0);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 0);

res = SPIFFS_lseek(FS, fd, 2, SPIFFS_SEEK_SET);
TEST_CHECK_EQ(res, 2);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 2);

res = SPIFFS_write(FS, fd, "test", 4);
TEST_CHECK_EQ(res, 4);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 6);

res = SPIFFS_fflush(FS, fd);
TEST_CHECK_EQ(res, SPIFFS_OK);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 0);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 6);

res = SPIFFS_lseek(FS, fd, 0, SPIFFS_SEEK_END);
TEST_CHECK_EQ(res, 8);
TEST_CHECK_EQ(SPIFFS_eof(FS, fd), 1);
TEST_CHECK_EQ(SPIFFS_tell(FS, fd), 8);

return TEST_RES_OK;
} TEST_END(eof_tell_72)


SUITE_END(bug_tests)

0 comments on commit 24aac90

Please sign in to comment.