From 24795e6b74b3618ecb37742f98cff19a4344e88b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Mon, 13 Mar 2023 11:39:06 +0100 Subject: [PATCH 1/3] Add missing iterations in tests --- tests/test_dirs.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_dirs.toml b/tests/test_dirs.toml index 270f4f8e..6300f8c1 100644 --- a/tests/test_dirs.toml +++ b/tests/test_dirs.toml @@ -727,7 +727,7 @@ code = ''' } lfs_unmount(&lfs) => 0; - for (int j = 2; j < COUNT; j++) { + for (int j = 2; j < COUNT + 2; j++) { lfs_mount(&lfs, &cfg) => 0; lfs_dir_open(&lfs, &dir, "hello") => 0; lfs_dir_read(&lfs, &dir, &info) => 1; @@ -787,7 +787,7 @@ code = ''' } lfs_unmount(&lfs) => 0; - for (int j = 2; j < COUNT; j++) { + for (int j = 2; j < COUNT + 2; j++) { lfs_mount(&lfs, &cfg) => 0; lfs_dir_open(&lfs, &dir, "/") => 0; lfs_dir_read(&lfs, &dir, &info) => 1; From 384a498762dada6a7a3b1d3c91beaa8db412eb5a Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 18 Apr 2023 14:55:43 -0500 Subject: [PATCH 2/3] Extend dir seek tests to include seeking to end of directory --- tests/test_dirs.toml | 99 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 91 insertions(+), 8 deletions(-) diff --git a/tests/test_dirs.toml b/tests/test_dirs.toml index 6300f8c1..7ff46e61 100644 --- a/tests/test_dirs.toml +++ b/tests/test_dirs.toml @@ -727,7 +727,8 @@ code = ''' } lfs_unmount(&lfs) => 0; - for (int j = 2; j < COUNT + 2; j++) { + // try seeking to each dir entry + for (int j = 0; j < COUNT; j++) { lfs_mount(&lfs, &cfg) => 0; lfs_dir_open(&lfs, &dir, "hello") => 0; lfs_dir_read(&lfs, &dir, &info) => 1; @@ -737,15 +738,14 @@ code = ''' assert(strcmp(info.name, "..") == 0); assert(info.type == LFS_TYPE_DIR); - lfs_soff_t pos; for (int i = 0; i < j; i++) { sprintf(path, "kitty%03d", i); lfs_dir_read(&lfs, &dir, &info) => 1; assert(strcmp(info.name, path) == 0); assert(info.type == LFS_TYPE_DIR); - pos = lfs_dir_tell(&lfs, &dir); - assert(pos >= 0); } + lfs_soff_t pos = lfs_dir_tell(&lfs, &dir); + assert(pos >= 0); lfs_dir_seek(&lfs, &dir, pos) => 0; sprintf(path, "kitty%03d", j); @@ -774,6 +774,48 @@ code = ''' lfs_dir_close(&lfs, &dir) => 0; lfs_unmount(&lfs) => 0; } + + // try seeking to end of dir + lfs_mount(&lfs, &cfg) => 0; + lfs_dir_open(&lfs, &dir, "hello") => 0; + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, ".") == 0); + assert(info.type == LFS_TYPE_DIR); + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, "..") == 0); + assert(info.type == LFS_TYPE_DIR); + + for (int i = 0; i < COUNT; i++) { + sprintf(path, "kitty%03d", i); + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, path) == 0); + assert(info.type == LFS_TYPE_DIR); + } + lfs_soff_t pos = lfs_dir_tell(&lfs, &dir); + assert(pos >= 0); + + lfs_dir_read(&lfs, &dir, &info) => 0; + + lfs_dir_seek(&lfs, &dir, pos) => 0; + lfs_dir_read(&lfs, &dir, &info) => 0; + + lfs_dir_rewind(&lfs, &dir) => 0; + sprintf(path, "kitty%03d", 0); + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, ".") == 0); + assert(info.type == LFS_TYPE_DIR); + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, "..") == 0); + assert(info.type == LFS_TYPE_DIR); + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, path) == 0); + assert(info.type == LFS_TYPE_DIR); + + lfs_dir_seek(&lfs, &dir, pos) => 0; + lfs_dir_read(&lfs, &dir, &info) => 0; + + lfs_dir_close(&lfs, &dir) => 0; + lfs_unmount(&lfs) => 0; ''' [[case]] # root seek @@ -787,7 +829,7 @@ code = ''' } lfs_unmount(&lfs) => 0; - for (int j = 2; j < COUNT + 2; j++) { + for (int j = 0; j < COUNT; j++) { lfs_mount(&lfs, &cfg) => 0; lfs_dir_open(&lfs, &dir, "/") => 0; lfs_dir_read(&lfs, &dir, &info) => 1; @@ -797,15 +839,14 @@ code = ''' assert(strcmp(info.name, "..") == 0); assert(info.type == LFS_TYPE_DIR); - lfs_soff_t pos; for (int i = 0; i < j; i++) { sprintf(path, "hi%03d", i); lfs_dir_read(&lfs, &dir, &info) => 1; assert(strcmp(info.name, path) == 0); assert(info.type == LFS_TYPE_DIR); - pos = lfs_dir_tell(&lfs, &dir); - assert(pos >= 0); } + lfs_soff_t pos = lfs_dir_tell(&lfs, &dir); + assert(pos >= 0); lfs_dir_seek(&lfs, &dir, pos) => 0; sprintf(path, "hi%03d", j); @@ -834,5 +875,47 @@ code = ''' lfs_dir_close(&lfs, &dir) => 0; lfs_unmount(&lfs) => 0; } + + // try seeking to end of dir + lfs_mount(&lfs, &cfg) => 0; + lfs_dir_open(&lfs, &dir, "/") => 0; + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, ".") == 0); + assert(info.type == LFS_TYPE_DIR); + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, "..") == 0); + assert(info.type == LFS_TYPE_DIR); + + for (int i = 0; i < COUNT; i++) { + sprintf(path, "hi%03d", i); + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, path) == 0); + assert(info.type == LFS_TYPE_DIR); + } + lfs_soff_t pos = lfs_dir_tell(&lfs, &dir); + assert(pos >= 0); + + lfs_dir_read(&lfs, &dir, &info) => 0; + + lfs_dir_seek(&lfs, &dir, pos) => 0; + lfs_dir_read(&lfs, &dir, &info) => 0; + + lfs_dir_rewind(&lfs, &dir) => 0; + sprintf(path, "hi%03d", 0); + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, ".") == 0); + assert(info.type == LFS_TYPE_DIR); + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, "..") == 0); + assert(info.type == LFS_TYPE_DIR); + lfs_dir_read(&lfs, &dir, &info) => 1; + assert(strcmp(info.name, path) == 0); + assert(info.type == LFS_TYPE_DIR); + + lfs_dir_seek(&lfs, &dir, pos) => 0; + lfs_dir_read(&lfs, &dir, &info) => 0; + + lfs_dir_close(&lfs, &dir) => 0; + lfs_unmount(&lfs) => 0; ''' From b33a5b3f856db91e556bc13ad4b7a6c86f75e892 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Tue, 18 Apr 2023 14:56:46 -0500 Subject: [PATCH 3/3] Fixed issue where seeking to end-of-directory return LFS_ERR_INVAL This was just an oversight. Seeking to the end of the directory should not error, but instead restore the directory to the state where the next read returns 0. Note this matches the behavior of lfs_file_tell/lfs_file_seek. Found by sosthene-nitrokey --- lfs.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lfs.c b/lfs.c index 26280fa8..239ad87f 100644 --- a/lfs.c +++ b/lfs.c @@ -2595,11 +2595,6 @@ static int lfs_dir_rawseek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { dir->id = (off > 0 && lfs_pair_cmp(dir->head, lfs->root) == 0); while (off > 0) { - int diff = lfs_min(dir->m.count - dir->id, off); - dir->id += diff; - dir->pos += diff; - off -= diff; - if (dir->id == dir->m.count) { if (!dir->m.split) { return LFS_ERR_INVAL; @@ -2612,6 +2607,11 @@ static int lfs_dir_rawseek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) { dir->id = 0; } + + int diff = lfs_min(dir->m.count - dir->id, off); + dir->id += diff; + dir->pos += diff; + off -= diff; } return 0;