Changed rdonly/wronly mistakes to assert

Previously these returned LFS_ERR_BADF. But attempting to modify a file
opened read-only, or reading a write-only flie, is a user error and
should not occur in normal use.

Changing this to an assert allows the logic to be omitted if the user
disables asserts to reduce the code footprint (not suggested unless the
user really really knows what they're doing).
This commit is contained in:
Christopher Haster
2019-07-24 14:59:48 -05:00
parent 4ec4425272
commit 4850e01e14

15
lfs.c
View File

@@ -2631,10 +2631,7 @@ lfs_ssize_t lfs_file_read(lfs_t *lfs, lfs_file_t *file,
lfs_size_t nsize = size;
LFS_ASSERT(file->flags & LFS_F_OPENED);
if ((file->flags & 3) == LFS_O_WRONLY) {
return LFS_ERR_BADF;
}
LFS_ASSERT((file->flags & 3) != LFS_O_WRONLY);
if (file->flags & LFS_F_WRITING) {
// flush out any writes
@@ -2706,10 +2703,7 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
lfs_size_t nsize = size;
LFS_ASSERT(file->flags & LFS_F_OPENED);
if ((file->flags & 3) == LFS_O_RDONLY) {
return LFS_ERR_BADF;
}
LFS_ASSERT((file->flags & 3) != LFS_O_RDONLY);
if (file->flags & LFS_F_READING) {
// drop any reads
@@ -2857,10 +2851,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) {
LFS_ASSERT(file->flags & LFS_F_OPENED);
if ((file->flags & 3) == LFS_O_RDONLY) {
return LFS_ERR_BADF;
}
LFS_ASSERT((file->flags & 3) != LFS_O_RDONLY);
if (size > LFS_FILE_MAX) {
return LFS_ERR_INVAL;