Skip to content

Commit d9c3637

Browse files
committed
Fixed handling of root as target for create operations
Before this patch, when calling lfs_mkdir or lfs_file_open with root as the target, littlefs wouldn't find the path properly and happily run into undefined behaviour. The fix is to populate a directory entry for root in the lfs_dir_find function. As an added plus, this allowed several special cases around root to be completely dropped.
1 parent 1476181 commit d9c3637

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

lfs.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,19 @@ static int lfs_dir_find(lfs_t *lfs, lfs_dir_t *dir,
780780
pathname += strspn(pathname, "/");
781781
pathlen = strcspn(pathname, "/");
782782

783+
// special case for root dir
784+
if (pathname[0] == '\0') {
785+
*entry = (lfs_entry_t){
786+
.d.type = LFS_TYPE_DIR,
787+
.d.elen = sizeof(entry->d) - 4,
788+
.d.alen = 0,
789+
.d.nlen = 0,
790+
.d.u.dir[0] = lfs->root[0],
791+
.d.u.dir[1] = lfs->root[1],
792+
};
793+
return 0;
794+
}
795+
783796
// skip '.' and root '..'
784797
if ((pathlen == 1 && memcmp(pathname, ".", 1) == 0) ||
785798
(pathlen == 2 && memcmp(pathname, "..", 2) == 0)) {
@@ -936,15 +949,6 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
936949
return err;
937950
}
938951

939-
// check for root, can only be something like '/././../.'
940-
if (strspn(path, "/.") == strlen(path)) {
941-
dir->head[0] = dir->pair[0];
942-
dir->head[1] = dir->pair[1];
943-
dir->pos = sizeof(dir->d) - 2;
944-
dir->off = sizeof(dir->d);
945-
return 0;
946-
}
947-
948952
lfs_entry_t entry;
949953
err = lfs_dir_find(lfs, dir, &entry, &path);
950954
if (err) {
@@ -1799,14 +1803,6 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
17991803

18001804
/// General fs operations ///
18011805
int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
1802-
// check for root, can only be something like '/././../.'
1803-
if (strspn(path, "/.") == strlen(path)) {
1804-
memset(info, 0, sizeof(*info));
1805-
info->type = LFS_TYPE_DIR;
1806-
strcpy(info->name, "/");
1807-
return 0;
1808-
}
1809-
18101806
lfs_dir_t cwd;
18111807
int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
18121808
if (err) {
@@ -1825,11 +1821,15 @@ int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
18251821
info->size = entry.d.u.file.size;
18261822
}
18271823

1828-
err = lfs_bd_read(lfs, cwd.pair[0],
1829-
entry.off + 4+entry.d.elen+entry.d.alen,
1830-
info->name, entry.d.nlen);
1831-
if (err) {
1832-
return err;
1824+
if (lfs_paircmp(entry.d.u.dir, lfs->root) == 0) {
1825+
strcpy(info->name, "/");
1826+
} else {
1827+
err = lfs_bd_read(lfs, cwd.pair[0],
1828+
entry.off + 4+entry.d.elen+entry.d.alen,
1829+
info->name, entry.d.nlen);
1830+
if (err) {
1831+
return err;
1832+
}
18331833
}
18341834

18351835
return 0;

tests/test_paths.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ tests/test.py << TEST
108108
lfs_stat(&lfs, "/", &info) => 0;
109109
info.type => LFS_TYPE_DIR;
110110
strcmp(info.name, "/") => 0;
111+
112+
lfs_mkdir(&lfs, "/") => LFS_ERR_EXIST;
113+
lfs_file_open(&lfs, &file[0], "/", LFS_O_WRONLY | LFS_O_CREAT)
114+
=> LFS_ERR_ISDIR;
111115
lfs_unmount(&lfs) => 0;
112116
TEST
113117

0 commit comments

Comments
 (0)