Skip to content

Commit 116c1e7

Browse files
committed
Adopted EISDIR as internal error for root path as argument
Unfortunately, it's hard to make directory lookups for root not a special case, and we don't like special cases when we're trying to keep code size small. Since there are a handful of code paths where opening root should return EISDIR (such as lfs_file_open("/")), using EISDIR to note that the argument is in fact a path to the root. This is needed because we no longer look up an entries contents in lfs_dir_find for free, since entries are more ephemeral.
1 parent f458da4 commit 116c1e7

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

lfs.c

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,13 +1290,6 @@ static int lfs_dir_getentry(lfs_t *lfs, lfs_mdir_t *dir,
12901290

12911291
static int lfs_dir_getinfo(lfs_t *lfs, lfs_mdir_t *dir,
12921292
int16_t id, struct lfs_info *info) {
1293-
if (id < 0) {
1294-
// special case for root
1295-
strcpy(info->name, "/");
1296-
info->type = LFS_TYPE_DIR;
1297-
return 0;
1298-
}
1299-
13001293
if (id == dir->moveid) {
13011294
int moved = lfs_moved(lfs, dir, dir->moveid);
13021295
if (moved < 0) {
@@ -1366,7 +1359,7 @@ static int lfs_dir_finder(lfs_t *lfs, void *p, lfs_mattr_t attr) {
13661359

13671360
// TODO drop others, make this only return id, also make get take in only entry to populate (with embedded tag)
13681361
static int lfs_dir_find(lfs_t *lfs, lfs_mdir_t *dir,
1369-
const char **path, int16_t *id) {
1362+
const char **path, uint16_t *id) {
13701363
lfs_mattr_t attr = {
13711364
.u.pair[0] = lfs->root[0],
13721365
.u.pair[1] = lfs->root[1],
@@ -1384,9 +1377,8 @@ static int lfs_dir_find(lfs_t *lfs, lfs_mdir_t *dir,
13841377

13851378
// special case for root dir
13861379
if (find.name[0] == '\0') {
1387-
// TODO set up root?
1388-
*id = -1;
1389-
return 0;
1380+
// Return ISDIR when we hit root
1381+
return LFS_ERR_ISDIR;
13901382
}
13911383

13921384
// skip '.' and root '..'
@@ -1492,9 +1484,9 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {
14921484
}
14931485

14941486
lfs_mdir_t cwd;
1495-
int err = lfs_dir_find(lfs, &cwd, &path, &(int16_t){0});
1487+
int err = lfs_dir_find(lfs, &cwd, &path, &(uint16_t){0});
14961488
if (err != LFS_ERR_NOENT || strchr(path, '/') != NULL) {
1497-
if (!err) {
1489+
if (!err || err == LFS_ERR_ISDIR) {
14981490
return LFS_ERR_EXIST;
14991491
}
15001492
return err;
@@ -1543,14 +1535,14 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {
15431535
}
15441536

15451537
int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
1546-
int16_t id;
1538+
uint16_t id;
15471539
int err = lfs_dir_find(lfs, &dir->m, &path, &id);
1548-
if (err) {
1540+
if (err && err != LFS_ERR_ISDIR) {
15491541
return err;
15501542
}
15511543

15521544
lfs_mattr_t attr;
1553-
if (id < 0) {
1545+
if (err == LFS_ERR_ISDIR) {
15541546
// handle root dir separately
15551547
attr.u.pair[0] = lfs->root[0];
15561548
attr.u.pair[1] = lfs->root[1];
@@ -1895,7 +1887,7 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
18951887

18961888
// allocate entry for file if it doesn't exist
18971889
lfs_mdir_t cwd;
1898-
int16_t id;
1890+
uint16_t id;
18991891
int err = lfs_dir_find(lfs, &cwd, &path, &id);
19001892
if (err && (err != LFS_ERR_NOENT || strchr(path, '/') != NULL)) {
19011893
return err;
@@ -2573,12 +2565,19 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
25732565
/// General fs operations ///
25742566
int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
25752567
lfs_mdir_t cwd;
2576-
int16_t id;
2568+
uint16_t id;
25772569
int err = lfs_dir_find(lfs, &cwd, &path, &id);
2578-
if (err) {
2570+
if (err && err != LFS_ERR_ISDIR) {
25792571
return err;
25802572
}
25812573

2574+
if (err == LFS_ERR_ISDIR) {
2575+
// special case for root
2576+
strcpy(info->name, "/");
2577+
info->type = LFS_TYPE_DIR;
2578+
return 0;
2579+
}
2580+
25822581
return lfs_dir_getinfo(lfs, &cwd, id, info);
25832582
}
25842583

@@ -2597,7 +2596,7 @@ int lfs_remove(lfs_t *lfs, const char *path) {
25972596
return err;
25982597
}
25992598

2600-
int16_t id;
2599+
uint16_t id;
26012600
err = lfs_dir_find(lfs, &cwd, &path, &id);
26022601
if (err) {
26032602
return err;
@@ -2669,7 +2668,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
26692668

26702669
// find old entry
26712670
lfs_mdir_t oldcwd;
2672-
int16_t oldid;
2671+
uint16_t oldid;
26732672
int err = lfs_dir_find(lfs, &oldcwd, &oldpath, &oldid);
26742673
if (err) {
26752674
return err;
@@ -2685,7 +2684,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
26852684

26862685
// find new entry
26872686
lfs_mdir_t newcwd;
2688-
int16_t newid;
2687+
uint16_t newid;
26892688
err = lfs_dir_find(lfs, &newcwd, &newpath, &newid);
26902689
if (err && err != LFS_ERR_NOENT) {
26912690
return err;
@@ -3746,18 +3745,18 @@ int lfs_deorphan(lfs_t *lfs) {
37463745
// return lfs_dir_setattrs(lfs, &dir, &entry, attrs, count);
37473746
//}
37483747

3749-
static int lfs_fs_size_count(void *p, lfs_block_t block) {
3750-
lfs_size_t *size = p;
3751-
*size += 1;
3752-
return 0;
3753-
}
3754-
3755-
lfs_ssize_t lfs_fs_size(lfs_t *lfs) {
3756-
lfs_size_t size = 0;
3757-
int err = lfs_fs_traverse(lfs, lfs_fs_size_count, &size);
3758-
if (err) {
3759-
return err;
3760-
}
3761-
3762-
return size;
3763-
}
3748+
//static int lfs_fs_size_count(void *p, lfs_block_t block) {
3749+
// lfs_size_t *size = p;
3750+
// *size += 1;
3751+
// return 0;
3752+
//}
3753+
//
3754+
//lfs_ssize_t lfs_fs_size(lfs_t *lfs) {
3755+
// lfs_size_t size = 0;
3756+
// int err = lfs_fs_traverse(lfs, lfs_fs_size_count, &size);
3757+
// if (err) {
3758+
// return err;
3759+
// }
3760+
//
3761+
// return size;
3762+
//}

0 commit comments

Comments
 (0)