Skip to content

Commit 287b548

Browse files
committed
Standardized error values
Now matches the commonly used errno codes in name with the value encoded as the negative errno code
1 parent 5790ec2 commit 287b548

File tree

5 files changed

+52
-51
lines changed

5 files changed

+52
-51
lines changed

lfs.c

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static int lfs_alloc_scan(lfs_t *lfs, lfs_block_t *block) {
208208
lfs->free.start += lfs->cfg->lookahead;
209209
lfs->free.off = 0;
210210
if (lfs_scmp(lfs->free.start, end) > 0) {
211-
return LFS_ERROR_NO_SPACE;
211+
return LFS_ERR_NOSPC;
212212
}
213213

214214
// find mask of free blocks from tree
@@ -223,7 +223,7 @@ static int lfs_alloc_scan(lfs_t *lfs, lfs_block_t *block) {
223223
static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
224224
// try to scan for free block
225225
int err = lfs_alloc_scan(lfs, block);
226-
if (err != LFS_ERROR_NO_SPACE) {
226+
if (err != LFS_ERR_NOSPC) {
227227
return err;
228228
}
229229

@@ -329,7 +329,7 @@ static int lfs_dir_fetch(lfs_t *lfs,
329329

330330
if (!valid) {
331331
LFS_ERROR("Corrupted dir pair at %d %d", tpair[0], tpair[1]);
332-
return LFS_ERROR_CORRUPT;
332+
return LFS_ERR_CORRUPT;
333333
}
334334

335335
return 0;
@@ -549,7 +549,7 @@ static int lfs_dir_next(lfs_t *lfs, lfs_dir_t *dir, lfs_entry_t *entry) {
549549
entry->pair[0] = dir->pair[0];
550550
entry->pair[1] = dir->pair[1];
551551
entry->off = dir->off;
552-
return LFS_ERROR_NO_ENTRY;
552+
return LFS_ERR_NOENT;
553553
}
554554

555555
int err = lfs_dir_fetch(lfs, dir, dir->d.tail);
@@ -653,7 +653,7 @@ static int lfs_dir_find(lfs_t *lfs, lfs_dir_t *dir,
653653

654654
// continue on if we hit a directory
655655
if (entry->d.type != LFS_TYPE_DIR) {
656-
return LFS_ERROR_NOT_DIR;
656+
return LFS_ERR_NOTDIR;
657657
}
658658

659659
int err = lfs_dir_fetch(lfs, dir, entry->d.u.dir);
@@ -679,8 +679,8 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {
679679

680680
lfs_entry_t entry;
681681
err = lfs_dir_find(lfs, &cwd, &entry, &path);
682-
if (err != LFS_ERROR_NO_ENTRY) {
683-
return err ? err : LFS_ERROR_EXISTS;
682+
if (err != LFS_ERR_NOENT) {
683+
return err ? err : LFS_ERR_EXISTS;
684684
}
685685

686686
// Build up new directory
@@ -731,7 +731,7 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
731731
if (err) {
732732
return err;
733733
} else if (entry.d.type != LFS_TYPE_DIR) {
734-
return LFS_ERROR_NOT_DIR;
734+
return LFS_ERR_NOTDIR;
735735
}
736736

737737
err = lfs_dir_fetch(lfs, dir, entry.d.u.dir);
@@ -772,7 +772,7 @@ int lfs_dir_read(lfs_t *lfs, lfs_dir_t *dir, struct lfs_info *info) {
772772
lfs_entry_t entry;
773773
int err = lfs_dir_next(lfs, dir, &entry);
774774
if (err) {
775-
return (err == LFS_ERROR_NO_ENTRY) ? 0 : err;
775+
return (err == LFS_ERR_NOENT) ? 0 : err;
776776
}
777777

778778
info->type = entry.d.type & 0xff;
@@ -800,7 +800,7 @@ int lfs_dir_seek(lfs_t *lfs, lfs_dir_t *dir, lfs_off_t off) {
800800
while (off > (0x7fffffff & dir->d.size)) {
801801
off -= 0x7fffffff & dir->d.size;
802802
if (!(0x80000000 & dir->d.size)) {
803-
return LFS_ERROR_INVALID;
803+
return LFS_ERR_INVAL;
804804
}
805805

806806
int err = lfs_dir_fetch(lfs, dir, dir->d.tail);
@@ -983,13 +983,13 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
983983
}
984984

985985
err = lfs_dir_find(lfs, &cwd, &file->entry, &path);
986-
if (err && err != LFS_ERROR_NO_ENTRY) {
986+
if (err && err != LFS_ERR_NOENT) {
987987
return err;
988988
}
989989

990-
if (err == LFS_ERROR_NO_ENTRY) {
990+
if (err == LFS_ERR_NOENT) {
991991
if (!(flags & LFS_O_CREAT)) {
992-
return LFS_ERROR_NO_ENTRY;
992+
return LFS_ERR_NOENT;
993993
}
994994

995995
// create entry to remember name
@@ -1002,9 +1002,9 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
10021002
return err;
10031003
}
10041004
} else if (file->entry.d.type == LFS_TYPE_DIR) {
1005-
return LFS_ERROR_IS_DIR;
1005+
return LFS_ERR_ISDIR;
10061006
} else if (flags & LFS_O_EXCL) {
1007-
return LFS_ERROR_EXISTS;
1007+
return LFS_ERR_EXISTS;
10081008
}
10091009

10101010
file->wpos = 0;
@@ -1092,7 +1092,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
10921092
lfs_size_t nsize = size;
10931093

10941094
if ((file->flags & 3) == LFS_O_WRONLY) {
1095-
return LFS_ERROR_INVALID;
1095+
return LFS_ERR_INVAL;
10961096
}
10971097

10981098
while (nsize > 0) {
@@ -1128,7 +1128,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
11281128
lfs_size_t nsize = size;
11291129

11301130
if ((file->flags & 3) == LFS_O_RDONLY) {
1131-
return LFS_ERROR_INVALID;
1131+
return LFS_ERR_INVAL;
11321132
}
11331133

11341134
while (nsize > 0) {
@@ -1284,7 +1284,7 @@ int lfs_remove(lfs_t *lfs, const char *path) {
12841284
if (err) {
12851285
return err;
12861286
} else if (dir.d.size != sizeof(dir.d)) {
1287-
return LFS_ERROR_INVALID;
1287+
return LFS_ERR_INVAL;
12881288
}
12891289
}
12901290

@@ -1329,14 +1329,14 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
13291329

13301330
lfs_entry_t preventry;
13311331
err = lfs_dir_find(lfs, &newcwd, &preventry, &newpath);
1332-
if (err && err != LFS_ERROR_NO_ENTRY) {
1332+
if (err && err != LFS_ERR_NOENT) {
13331333
return err;
13341334
}
1335-
bool prevexists = (err != LFS_ERROR_NO_ENTRY);
1335+
bool prevexists = (err != LFS_ERR_NOENT);
13361336

13371337
// must have same type
13381338
if (prevexists && preventry.d.type != oldentry.d.type) {
1339-
return LFS_ERROR_INVALID;
1339+
return LFS_ERR_INVAL;
13401340
}
13411341

13421342
lfs_dir_t dir;
@@ -1348,7 +1348,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
13481348
if (err) {
13491349
return err;
13501350
} else if (dir.d.size != sizeof(dir.d)) {
1351-
return LFS_ERROR_INVALID;
1351+
return LFS_ERR_INVAL;
13521352
}
13531353
}
13541354

@@ -1412,7 +1412,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
14121412
} else {
14131413
lfs->rcache.buffer = malloc(lfs->cfg->read_size);
14141414
if (!lfs->rcache.buffer) {
1415-
return LFS_ERROR_NO_MEM;
1415+
return LFS_ERR_NOMEM;
14161416
}
14171417
}
14181418

@@ -1423,7 +1423,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
14231423
} else {
14241424
lfs->pcache.buffer = malloc(lfs->cfg->prog_size);
14251425
if (!lfs->pcache.buffer) {
1426-
return LFS_ERROR_NO_MEM;
1426+
return LFS_ERR_NOMEM;
14271427
}
14281428
}
14291429

@@ -1433,7 +1433,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
14331433
} else {
14341434
lfs->free.lookahead = malloc(lfs->cfg->lookahead/8);
14351435
if (!lfs->free.lookahead) {
1436-
return LFS_ERROR_NO_MEM;
1436+
return LFS_ERR_NOMEM;
14371437
}
14381438
}
14391439

@@ -1544,17 +1544,17 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
15441544
lfs->root[1] = superblock.d.root[1];
15451545
}
15461546

1547-
if (err == LFS_ERROR_CORRUPT ||
1547+
if (err == LFS_ERR_CORRUPT ||
15481548
memcmp(superblock.d.magic, "littlefs", 8) != 0) {
15491549
LFS_ERROR("Invalid superblock at %d %d", dir.pair[0], dir.pair[1]);
1550-
return LFS_ERROR_CORRUPT;
1550+
return LFS_ERR_CORRUPT;
15511551
}
15521552

15531553
if (superblock.d.version > 0x0000ffff) {
15541554
LFS_ERROR("Invalid version %d.%d\n",
15551555
0xffff & (superblock.d.version >> 16),
15561556
0xffff & (superblock.d.version >> 0));
1557-
return LFS_ERROR_INVALID;
1557+
return LFS_ERR_INVAL;
15581558
}
15591559

15601560
return err;
@@ -1637,11 +1637,11 @@ static int lfs_parent(lfs_t *lfs, const lfs_block_t dir[2]) {
16371637

16381638
while (true) {
16391639
int err = lfs_dir_next(lfs, &parent, &entry);
1640-
if (err && err != LFS_ERROR_NO_ENTRY) {
1640+
if (err && err != LFS_ERR_NOENT) {
16411641
return err;
16421642
}
16431643

1644-
if (err == LFS_ERROR_NO_ENTRY) {
1644+
if (err == LFS_ERR_NOENT) {
16451645
break;
16461646
}
16471647

lfs.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,16 @@ typedef uint32_t lfs_block_t;
2828

2929
// The littefs constants
3030
enum lfs_error {
31-
LFS_ERROR_OK = 0,
32-
LFS_ERROR_CORRUPT = -3,
33-
LFS_ERROR_NO_ENTRY = -4,
34-
LFS_ERROR_EXISTS = -5,
35-
LFS_ERROR_NOT_DIR = -6,
36-
LFS_ERROR_IS_DIR = -7,
37-
LFS_ERROR_INVALID = -8,
38-
LFS_ERROR_NO_SPACE = -9,
39-
LFS_ERROR_NO_MEM = -10,
31+
LFS_ERR_OK = 0,
32+
LFS_ERR_IO = -5,
33+
LFS_ERR_CORRUPT = -77,
34+
LFS_ERR_NOENT = -2,
35+
LFS_ERR_EXISTS = -17,
36+
LFS_ERR_NOTDIR = -20,
37+
LFS_ERR_ISDIR = -21,
38+
LFS_ERR_INVAL = -22,
39+
LFS_ERR_NOSPC = -28,
40+
LFS_ERR_NOMEM = -12,
4041
};
4142

4243
enum lfs_type {

tests/test_dirs.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ TEST
5656
echo "--- Directory failures ---"
5757
tests/test.py << TEST
5858
lfs_mount(&lfs, &cfg) => 0;
59-
lfs_mkdir(&lfs, "potato") => LFS_ERROR_EXISTS;
60-
lfs_dir_open(&lfs, &dir[0], "tomato") => LFS_ERROR_NO_ENTRY;
61-
lfs_dir_open(&lfs, &dir[0], "burito") => LFS_ERROR_NOT_DIR;
62-
lfs_file_open(&lfs, &file[0], "tomato", LFS_O_RDONLY) => LFS_ERROR_NO_ENTRY;
63-
lfs_file_open(&lfs, &file[0], "potato", LFS_O_RDONLY) => LFS_ERROR_IS_DIR;
59+
lfs_mkdir(&lfs, "potato") => LFS_ERR_EXISTS;
60+
lfs_dir_open(&lfs, &dir[0], "tomato") => LFS_ERR_NOENT;
61+
lfs_dir_open(&lfs, &dir[0], "burito") => LFS_ERR_NOTDIR;
62+
lfs_file_open(&lfs, &file[0], "tomato", LFS_O_RDONLY) => LFS_ERR_NOENT;
63+
lfs_file_open(&lfs, &file[0], "potato", LFS_O_RDONLY) => LFS_ERR_ISDIR;
6464
lfs_unmount(&lfs) => 0;
6565
TEST
6666

@@ -126,7 +126,7 @@ TEST
126126
echo "--- Directory remove ---"
127127
tests/test.py << TEST
128128
lfs_mount(&lfs, &cfg) => 0;
129-
lfs_remove(&lfs, "potato") => LFS_ERROR_INVALID;
129+
lfs_remove(&lfs, "potato") => LFS_ERR_INVAL;
130130
lfs_remove(&lfs, "potato/sweet") => 0;
131131
lfs_remove(&lfs, "potato/baked") => 0;
132132
lfs_remove(&lfs, "potato/fried") => 0;
@@ -220,7 +220,7 @@ tests/test.py << TEST
220220
lfs_mount(&lfs, &cfg) => 0;
221221
lfs_mkdir(&lfs, "warmpotato") => 0;
222222
lfs_mkdir(&lfs, "warmpotato/mushy") => 0;
223-
lfs_rename(&lfs, "hotpotato", "warmpotato") => LFS_ERROR_INVALID;
223+
lfs_rename(&lfs, "hotpotato", "warmpotato") => LFS_ERR_INVAL;
224224
225225
lfs_remove(&lfs, "warmpotato/mushy") => 0;
226226
lfs_rename(&lfs, "hotpotato", "warmpotato") => 0;
@@ -255,7 +255,7 @@ tests/test.py << TEST
255255
lfs_rename(&lfs, "warmpotato/baked", "coldpotato/baked") => 0;
256256
lfs_rename(&lfs, "warmpotato/sweet", "coldpotato/sweet") => 0;
257257
lfs_rename(&lfs, "warmpotato/fried", "coldpotato/fried") => 0;
258-
lfs_remove(&lfs, "coldpotato") => LFS_ERROR_INVALID;
258+
lfs_remove(&lfs, "coldpotato") => LFS_ERR_INVAL;
259259
lfs_remove(&lfs, "warmpotato") => 0;
260260
lfs_unmount(&lfs) => 0;
261261
TEST

tests/test_format.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ echo "--- Invalid superblocks ---"
1313
ln -f -s /dev/null blocks/0
1414
ln -f -s /dev/null blocks/1
1515
tests/test.py << TEST
16-
lfs_format(&lfs, &cfg) => LFS_ERROR_CORRUPT;
16+
lfs_format(&lfs, &cfg) => LFS_ERR_CORRUPT;
1717
TEST
1818
rm blocks/0 blocks/1
1919

@@ -32,7 +32,7 @@ tests/test.py << TEST
3232
TEST
3333
rm blocks/0 blocks/1
3434
tests/test.py << TEST
35-
lfs_mount(&lfs, &cfg) => LFS_ERROR_CORRUPT;
35+
lfs_mount(&lfs, &cfg) => LFS_ERR_CORRUPT;
3636
TEST
3737

3838
echo "--- Valid corrupt mount ---"

tests/test_orphan.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ TEST
2020
rm -v blocks/8
2121
tests/test.py << TEST
2222
lfs_mount(&lfs, &cfg) => 0;
23-
lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERROR_NO_ENTRY;
23+
lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
2424
unsigned before = 0;
2525
lfs_traverse(&lfs, test_count, &before) => 0;
2626
test_log("before", before);
2727
2828
lfs_deorphan(&lfs) => 0;
2929
30-
lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERROR_NO_ENTRY;
30+
lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
3131
unsigned after = 0;
3232
lfs_traverse(&lfs, test_count, &after) => 0;
3333
test_log("after", after);

0 commit comments

Comments
 (0)