Skip to content

Commit

Permalink
Fixed some minor error code differences
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
geky committed Feb 4, 2018
1 parent 6716b55 commit a25743a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
16 changes: 6 additions & 10 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down
1 change: 1 addition & 0 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_dirs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a25743a

Please sign in to comment.