Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix name ordering when names only differ in length #926

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1430,8 +1430,8 @@ static int lfs_dir_find_match(void *data,
}

// only equal if our size is still the same
if (name->size != lfs_tag_size(tag)) {
return (name->size < lfs_tag_size(tag)) ? LFS_CMP_LT : LFS_CMP_GT;
if (lfs_tag_size(tag) != name->size) {
return (lfs_tag_size(tag) < name->size) ? LFS_CMP_LT : LFS_CMP_GT;
}

// found a match!
Expand Down
99 changes: 99 additions & 0 deletions tests/test_dirs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,105 @@ code = '''
lfs_unmount(&lfs) => 0;
'''

# littlefs should keep directories in lexicographic order
[cases.test_dirs_ordering]
# ORDER=0 => inorder
# ORDER=1 => reversed
# ORDER=2 => random
defines.ORDER = [0, 1, 2]
code = '''
lfs_t lfs;
lfs_format(&lfs, cfg) => 0;
lfs_mount(&lfs, cfg) => 0;
if (ORDER == 0) {
lfs_mkdir(&lfs, "a") => 0;
lfs_mkdir(&lfs, "b") => 0;
lfs_mkdir(&lfs, "c") => 0;
} else if (ORDER == 1) {
lfs_mkdir(&lfs, "c") => 0;
lfs_mkdir(&lfs, "b") => 0;
lfs_mkdir(&lfs, "a") => 0;
} else if (ORDER == 2) {
// "random"
lfs_mkdir(&lfs, "a") => 0;
lfs_mkdir(&lfs, "c") => 0;
lfs_mkdir(&lfs, "b") => 0;
}

// check the order
lfs_dir_t dir;
lfs_dir_open(&lfs, &dir, "/") => 0;
struct lfs_info info;
lfs_dir_read(&lfs, &dir, &info) => 1;
assert(info.type == LFS_TYPE_DIR);
assert(strcmp(info.name, ".") == 0);
lfs_dir_read(&lfs, &dir, &info) => 1;
assert(info.type == LFS_TYPE_DIR);
assert(strcmp(info.name, "..") == 0);
lfs_dir_read(&lfs, &dir, &info) => 1;
assert(info.type == LFS_TYPE_DIR);
assert(strcmp(info.name, "a") == 0);
lfs_dir_read(&lfs, &dir, &info) => 1;
assert(info.type == LFS_TYPE_DIR);
assert(strcmp(info.name, "b") == 0);
lfs_dir_read(&lfs, &dir, &info) => 1;
assert(info.type == LFS_TYPE_DIR);
assert(strcmp(info.name, "c") == 0);
lfs_dir_read(&lfs, &dir, &info) => 0;
lfs_dir_close(&lfs, &dir) => 0;

lfs_unmount(&lfs) => 0;
'''

[cases.test_dirs_ordering_length]
# ORDER=0 => inorder
# ORDER=1 => reversed
# ORDER=2 => random
defines.ORDER = [0, 1, 2]
code = '''
lfs_t lfs;
lfs_format(&lfs, cfg) => 0;
lfs_mount(&lfs, cfg) => 0;
if (ORDER == 0) {
lfs_mkdir(&lfs, "a") => 0;
lfs_mkdir(&lfs, "aa") => 0;
lfs_mkdir(&lfs, "aaa") => 0;
} else if (ORDER == 1) {
lfs_mkdir(&lfs, "aaa") => 0;
lfs_mkdir(&lfs, "aa") => 0;
lfs_mkdir(&lfs, "a") => 0;
} else if (ORDER == 2) {
// "random"
lfs_mkdir(&lfs, "a") => 0;
lfs_mkdir(&lfs, "aaa") => 0;
lfs_mkdir(&lfs, "aa") => 0;
}

// check the order
lfs_dir_t dir;
lfs_dir_open(&lfs, &dir, "/") => 0;
struct lfs_info info;
lfs_dir_read(&lfs, &dir, &info) => 1;
assert(info.type == LFS_TYPE_DIR);
assert(strcmp(info.name, ".") == 0);
lfs_dir_read(&lfs, &dir, &info) => 1;
assert(info.type == LFS_TYPE_DIR);
assert(strcmp(info.name, "..") == 0);
lfs_dir_read(&lfs, &dir, &info) => 1;
assert(info.type == LFS_TYPE_DIR);
assert(strcmp(info.name, "a") == 0);
lfs_dir_read(&lfs, &dir, &info) => 1;
assert(info.type == LFS_TYPE_DIR);
assert(strcmp(info.name, "aa") == 0);
lfs_dir_read(&lfs, &dir, &info) => 1;
assert(info.type == LFS_TYPE_DIR);
assert(strcmp(info.name, "aaa") == 0);
lfs_dir_read(&lfs, &dir, &info) => 0;
lfs_dir_close(&lfs, &dir) => 0;

lfs_unmount(&lfs) => 0;
'''

[cases.test_dirs_other_errors]
code = '''
lfs_t lfs;
Expand Down
Loading