Standardized error values

Now matches the commonly used errno codes in name with the value
encoded as the negative errno code
This commit is contained in:
Christopher Haster 2017-04-23 22:10:16 -05:00
parent 5790ec2ce4
commit 287b54876e
5 changed files with 52 additions and 51 deletions

60
lfs.c
View File

@ -208,7 +208,7 @@ static int lfs_alloc_scan(lfs_t *lfs, lfs_block_t *block) {
lfs->free.start += lfs->cfg->lookahead;
lfs->free.off = 0;
if (lfs_scmp(lfs->free.start, end) > 0) {
return LFS_ERROR_NO_SPACE;
return LFS_ERR_NOSPC;
}
// find mask of free blocks from tree
@ -223,7 +223,7 @@ static int lfs_alloc_scan(lfs_t *lfs, lfs_block_t *block) {
static int lfs_alloc(lfs_t *lfs, lfs_block_t *block) {
// try to scan for free block
int err = lfs_alloc_scan(lfs, block);
if (err != LFS_ERROR_NO_SPACE) {
if (err != LFS_ERR_NOSPC) {
return err;
}
@ -329,7 +329,7 @@ static int lfs_dir_fetch(lfs_t *lfs,
if (!valid) {
LFS_ERROR("Corrupted dir pair at %d %d", tpair[0], tpair[1]);
return LFS_ERROR_CORRUPT;
return LFS_ERR_CORRUPT;
}
return 0;
@ -549,7 +549,7 @@ static int lfs_dir_next(lfs_t *lfs, lfs_dir_t *dir, lfs_entry_t *entry) {
entry->pair[0] = dir->pair[0];
entry->pair[1] = dir->pair[1];
entry->off = dir->off;
return LFS_ERROR_NO_ENTRY;
return LFS_ERR_NOENT;
}
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,
// continue on if we hit a directory
if (entry->d.type != LFS_TYPE_DIR) {
return LFS_ERROR_NOT_DIR;
return LFS_ERR_NOTDIR;
}
int err = lfs_dir_fetch(lfs, dir, entry->d.u.dir);
@ -679,8 +679,8 @@ int lfs_mkdir(lfs_t *lfs, const char *path) {
lfs_entry_t entry;
err = lfs_dir_find(lfs, &cwd, &entry, &path);
if (err != LFS_ERROR_NO_ENTRY) {
return err ? err : LFS_ERROR_EXISTS;
if (err != LFS_ERR_NOENT) {
return err ? err : LFS_ERR_EXISTS;
}
// Build up new directory
@ -731,7 +731,7 @@ int lfs_dir_open(lfs_t *lfs, lfs_dir_t *dir, const char *path) {
if (err) {
return err;
} else if (entry.d.type != LFS_TYPE_DIR) {
return LFS_ERROR_NOT_DIR;
return LFS_ERR_NOTDIR;
}
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) {
lfs_entry_t entry;
int err = lfs_dir_next(lfs, dir, &entry);
if (err) {
return (err == LFS_ERROR_NO_ENTRY) ? 0 : err;
return (err == LFS_ERR_NOENT) ? 0 : err;
}
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) {
while (off > (0x7fffffff & dir->d.size)) {
off -= 0x7fffffff & dir->d.size;
if (!(0x80000000 & dir->d.size)) {
return LFS_ERROR_INVALID;
return LFS_ERR_INVAL;
}
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,
}
err = lfs_dir_find(lfs, &cwd, &file->entry, &path);
if (err && err != LFS_ERROR_NO_ENTRY) {
if (err && err != LFS_ERR_NOENT) {
return err;
}
if (err == LFS_ERROR_NO_ENTRY) {
if (err == LFS_ERR_NOENT) {
if (!(flags & LFS_O_CREAT)) {
return LFS_ERROR_NO_ENTRY;
return LFS_ERR_NOENT;
}
// create entry to remember name
@ -1002,9 +1002,9 @@ int lfs_file_open(lfs_t *lfs, lfs_file_t *file,
return err;
}
} else if (file->entry.d.type == LFS_TYPE_DIR) {
return LFS_ERROR_IS_DIR;
return LFS_ERR_ISDIR;
} else if (flags & LFS_O_EXCL) {
return LFS_ERROR_EXISTS;
return LFS_ERR_EXISTS;
}
file->wpos = 0;
@ -1092,7 +1092,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
lfs_size_t nsize = size;
if ((file->flags & 3) == LFS_O_WRONLY) {
return LFS_ERROR_INVALID;
return LFS_ERR_INVAL;
}
while (nsize > 0) {
@ -1128,7 +1128,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
lfs_size_t nsize = size;
if ((file->flags & 3) == LFS_O_RDONLY) {
return LFS_ERROR_INVALID;
return LFS_ERR_INVAL;
}
while (nsize > 0) {
@ -1284,7 +1284,7 @@ int lfs_remove(lfs_t *lfs, const char *path) {
if (err) {
return err;
} else if (dir.d.size != sizeof(dir.d)) {
return LFS_ERROR_INVALID;
return LFS_ERR_INVAL;
}
}
@ -1329,14 +1329,14 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
lfs_entry_t preventry;
err = lfs_dir_find(lfs, &newcwd, &preventry, &newpath);
if (err && err != LFS_ERROR_NO_ENTRY) {
if (err && err != LFS_ERR_NOENT) {
return err;
}
bool prevexists = (err != LFS_ERROR_NO_ENTRY);
bool prevexists = (err != LFS_ERR_NOENT);
// must have same type
if (prevexists && preventry.d.type != oldentry.d.type) {
return LFS_ERROR_INVALID;
return LFS_ERR_INVAL;
}
lfs_dir_t dir;
@ -1348,7 +1348,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
if (err) {
return err;
} else if (dir.d.size != sizeof(dir.d)) {
return LFS_ERROR_INVALID;
return LFS_ERR_INVAL;
}
}
@ -1412,7 +1412,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
} else {
lfs->rcache.buffer = malloc(lfs->cfg->read_size);
if (!lfs->rcache.buffer) {
return LFS_ERROR_NO_MEM;
return LFS_ERR_NOMEM;
}
}
@ -1423,7 +1423,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
} else {
lfs->pcache.buffer = malloc(lfs->cfg->prog_size);
if (!lfs->pcache.buffer) {
return LFS_ERROR_NO_MEM;
return LFS_ERR_NOMEM;
}
}
@ -1433,7 +1433,7 @@ static int lfs_init(lfs_t *lfs, const struct lfs_config *cfg) {
} else {
lfs->free.lookahead = malloc(lfs->cfg->lookahead/8);
if (!lfs->free.lookahead) {
return LFS_ERROR_NO_MEM;
return LFS_ERR_NOMEM;
}
}
@ -1544,17 +1544,17 @@ int lfs_mount(lfs_t *lfs, const struct lfs_config *cfg) {
lfs->root[1] = superblock.d.root[1];
}
if (err == LFS_ERROR_CORRUPT ||
if (err == LFS_ERR_CORRUPT ||
memcmp(superblock.d.magic, "littlefs", 8) != 0) {
LFS_ERROR("Invalid superblock at %d %d", dir.pair[0], dir.pair[1]);
return LFS_ERROR_CORRUPT;
return LFS_ERR_CORRUPT;
}
if (superblock.d.version > 0x0000ffff) {
LFS_ERROR("Invalid version %d.%d\n",
0xffff & (superblock.d.version >> 16),
0xffff & (superblock.d.version >> 0));
return LFS_ERROR_INVALID;
return LFS_ERR_INVAL;
}
return err;
@ -1637,11 +1637,11 @@ static int lfs_parent(lfs_t *lfs, const lfs_block_t dir[2]) {
while (true) {
int err = lfs_dir_next(lfs, &parent, &entry);
if (err && err != LFS_ERROR_NO_ENTRY) {
if (err && err != LFS_ERR_NOENT) {
return err;
}
if (err == LFS_ERROR_NO_ENTRY) {
if (err == LFS_ERR_NOENT) {
break;
}

19
lfs.h
View File

@ -28,15 +28,16 @@ typedef uint32_t lfs_block_t;
// The littefs constants
enum lfs_error {
LFS_ERROR_OK = 0,
LFS_ERROR_CORRUPT = -3,
LFS_ERROR_NO_ENTRY = -4,
LFS_ERROR_EXISTS = -5,
LFS_ERROR_NOT_DIR = -6,
LFS_ERROR_IS_DIR = -7,
LFS_ERROR_INVALID = -8,
LFS_ERROR_NO_SPACE = -9,
LFS_ERROR_NO_MEM = -10,
LFS_ERR_OK = 0,
LFS_ERR_IO = -5,
LFS_ERR_CORRUPT = -77,
LFS_ERR_NOENT = -2,
LFS_ERR_EXISTS = -17,
LFS_ERR_NOTDIR = -20,
LFS_ERR_ISDIR = -21,
LFS_ERR_INVAL = -22,
LFS_ERR_NOSPC = -28,
LFS_ERR_NOMEM = -12,
};
enum lfs_type {

View File

@ -56,11 +56,11 @@ TEST
echo "--- Directory failures ---"
tests/test.py << TEST
lfs_mount(&lfs, &cfg) => 0;
lfs_mkdir(&lfs, "potato") => LFS_ERROR_EXISTS;
lfs_dir_open(&lfs, &dir[0], "tomato") => LFS_ERROR_NO_ENTRY;
lfs_dir_open(&lfs, &dir[0], "burito") => LFS_ERROR_NOT_DIR;
lfs_file_open(&lfs, &file[0], "tomato", LFS_O_RDONLY) => LFS_ERROR_NO_ENTRY;
lfs_file_open(&lfs, &file[0], "potato", LFS_O_RDONLY) => LFS_ERROR_IS_DIR;
lfs_mkdir(&lfs, "potato") => LFS_ERR_EXISTS;
lfs_dir_open(&lfs, &dir[0], "tomato") => LFS_ERR_NOENT;
lfs_dir_open(&lfs, &dir[0], "burito") => LFS_ERR_NOTDIR;
lfs_file_open(&lfs, &file[0], "tomato", LFS_O_RDONLY) => LFS_ERR_NOENT;
lfs_file_open(&lfs, &file[0], "potato", LFS_O_RDONLY) => LFS_ERR_ISDIR;
lfs_unmount(&lfs) => 0;
TEST
@ -126,7 +126,7 @@ TEST
echo "--- Directory remove ---"
tests/test.py << TEST
lfs_mount(&lfs, &cfg) => 0;
lfs_remove(&lfs, "potato") => LFS_ERROR_INVALID;
lfs_remove(&lfs, "potato") => LFS_ERR_INVAL;
lfs_remove(&lfs, "potato/sweet") => 0;
lfs_remove(&lfs, "potato/baked") => 0;
lfs_remove(&lfs, "potato/fried") => 0;
@ -220,7 +220,7 @@ tests/test.py << TEST
lfs_mount(&lfs, &cfg) => 0;
lfs_mkdir(&lfs, "warmpotato") => 0;
lfs_mkdir(&lfs, "warmpotato/mushy") => 0;
lfs_rename(&lfs, "hotpotato", "warmpotato") => LFS_ERROR_INVALID;
lfs_rename(&lfs, "hotpotato", "warmpotato") => LFS_ERR_INVAL;
lfs_remove(&lfs, "warmpotato/mushy") => 0;
lfs_rename(&lfs, "hotpotato", "warmpotato") => 0;
@ -255,7 +255,7 @@ tests/test.py << TEST
lfs_rename(&lfs, "warmpotato/baked", "coldpotato/baked") => 0;
lfs_rename(&lfs, "warmpotato/sweet", "coldpotato/sweet") => 0;
lfs_rename(&lfs, "warmpotato/fried", "coldpotato/fried") => 0;
lfs_remove(&lfs, "coldpotato") => LFS_ERROR_INVALID;
lfs_remove(&lfs, "coldpotato") => LFS_ERR_INVAL;
lfs_remove(&lfs, "warmpotato") => 0;
lfs_unmount(&lfs) => 0;
TEST

View File

@ -13,7 +13,7 @@ echo "--- Invalid superblocks ---"
ln -f -s /dev/null blocks/0
ln -f -s /dev/null blocks/1
tests/test.py << TEST
lfs_format(&lfs, &cfg) => LFS_ERROR_CORRUPT;
lfs_format(&lfs, &cfg) => LFS_ERR_CORRUPT;
TEST
rm blocks/0 blocks/1
@ -32,7 +32,7 @@ tests/test.py << TEST
TEST
rm blocks/0 blocks/1
tests/test.py << TEST
lfs_mount(&lfs, &cfg) => LFS_ERROR_CORRUPT;
lfs_mount(&lfs, &cfg) => LFS_ERR_CORRUPT;
TEST
echo "--- Valid corrupt mount ---"

View File

@ -20,14 +20,14 @@ TEST
rm -v blocks/8
tests/test.py << TEST
lfs_mount(&lfs, &cfg) => 0;
lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERROR_NO_ENTRY;
lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
unsigned before = 0;
lfs_traverse(&lfs, test_count, &before) => 0;
test_log("before", before);
lfs_deorphan(&lfs) => 0;
lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERROR_NO_ENTRY;
lfs_stat(&lfs, "parent/orphan", &info) => LFS_ERR_NOENT;
unsigned after = 0;
lfs_traverse(&lfs, test_count, &after) => 0;
test_log("after", after);