From a25743a82a5a3c074e492aeba912373ef2e74288 Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Sun, 4 Feb 2018 14:36:36 -0600 Subject: [PATCH] Fixed some minor error code differences - Write on read-only file to return LFS_ERR_BADF - Renaming directory onto file to return LFS_ERR_NOTEMPTY - Changed LFS_ERR_INVAL in lfs_file_seek to assert --- lfs.c | 16 ++++++---------- lfs.h | 1 + tests/test_dirs.sh | 2 +- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lfs.c b/lfs.c index 1e490c00839..b21d209380f 100644 --- a/lfs.c +++ b/lfs.c @@ -1463,11 +1463,7 @@ int lfs_file_sync(lfs_t *lfs, lfs_file_t *file) { return err; } - if (entry.d.type != LFS_TYPE_REG) { - // sanity check valid entry - return LFS_ERR_INVAL; - } - + assert(entry.d.type == LFS_TYPE_REG); entry.d.u.file.head = file->head; entry.d.u.file.size = file->size; @@ -1488,7 +1484,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_ERR_INVAL; + return LFS_ERR_BADF; } if (file->flags & LFS_F_WRITING) { @@ -1544,7 +1540,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_ERR_INVAL; + return LFS_ERR_BADF; } if (file->flags & LFS_F_READING) { @@ -1667,7 +1663,7 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file, int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) { if ((file->flags & 3) == LFS_O_RDONLY) { - return LFS_ERR_INVAL; + return LFS_ERR_BADF; } lfs_off_t oldsize = lfs_file_size(lfs, file); @@ -1879,7 +1875,7 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { // must have same type if (prevexists && preventry.d.type != oldentry.d.type) { - return LFS_ERR_INVAL; + return LFS_ERR_ISDIR; } lfs_dir_t dir; @@ -1891,7 +1887,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)+4) { - return LFS_ERR_INVAL; + return LFS_ERR_NOTEMPTY; } } diff --git a/lfs.h b/lfs.h index c9ff0adc566..4ce8878b3eb 100644 --- a/lfs.h +++ b/lfs.h @@ -66,6 +66,7 @@ enum lfs_error { LFS_ERR_NOTDIR = -20, // Entry is not a dir LFS_ERR_ISDIR = -21, // Entry is a dir LFS_ERR_NOTEMPTY = -39, // Dir is not empty + LFS_ERR_BADF = -9, // Bad file number LFS_ERR_INVAL = -22, // Invalid parameter LFS_ERR_NOSPC = -28, // No space left on device LFS_ERR_NOMEM = -12, // No more memory available diff --git a/tests/test_dirs.sh b/tests/test_dirs.sh index 197d0f5bf2d..9f9733dd712 100755 --- a/tests/test_dirs.sh +++ b/tests/test_dirs.sh @@ -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_ERR_INVAL; + lfs_rename(&lfs, "hotpotato", "warmpotato") => LFS_ERR_NOTEMPTY; lfs_remove(&lfs, "warmpotato/mushy") => 0; lfs_rename(&lfs, "hotpotato", "warmpotato") => 0;