Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/littlefs-project/littlefs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lfs.c
diff options
context:
space:
mode:
authorChristopher Haster <chaster@utexas.edu>2018-10-08 22:12:20 +0300
committerChristopher Haster <chaster@utexas.edu>2018-10-20 20:34:23 +0300
commit195075819e05a9ce8568d3d98363f2a6f19ed436 (patch)
treec408020e94292c47d9eb2a5eae932c5a7791660b /lfs.c
parent97d8d5e96a7781596708664f18f2ea6c3a179330 (diff)
Added 2GiB file size limit and EFBIG reporting
On disk, littlefs uses 32-bit integers to track file size. This sets a theoretical limit of 4GiB for files. However, the API passes file sizes around as signed numbers, with negative values representing error codes. This means that not all of the APIs will work with file sizes > 2GiB. Because of related complications over in FUSE land, I've added the LFS_FILE_MAX constant and proper error reporting if file writes/seeks exceed the 2GiB limit. In v2 this will join the other constants that get stored in the superblock to help portability. Since littlefs is targeting microcontrollers, it's likely this will be a sufficient solution. Note that it's still possible to enable partial-support for 4GiB files by defining LFS_FILE_MAX during compilation. This will work for most of the APIs, except lfs_file_seek, lfs_file_tell, and lfs_file_size. We can also consider improving support for 4GiB files, by making seek a bit more complicated and adding a lfs_file_stat function. I'll leave this for a future improvement if there's interest. Found by cgrozemuller
Diffstat (limited to 'lfs.c')
-rw-r--r--lfs.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/lfs.c b/lfs.c
index 438647e..ed7f687 100644
--- a/lfs.c
+++ b/lfs.c
@@ -1644,6 +1644,11 @@ lfs_ssize_t lfs_file_write(lfs_t *lfs, lfs_file_t *file,
file->pos = file->size;
}
+ if (file->pos + size > LFS_FILE_MAX) {
+ // larger than file limit?
+ return LFS_ERR_FBIG;
+ }
+
if (!(file->flags & LFS_F_WRITING) && file->pos > file->size) {
// fill with zeros
lfs_off_t pos = file->pos;
@@ -1730,24 +1735,24 @@ lfs_soff_t lfs_file_seek(lfs_t *lfs, lfs_file_t *file,
return err;
}
- // update pos
+ // find new pos
+ lfs_soff_t npos = file->pos;
if (whence == LFS_SEEK_SET) {
- file->pos = off;
+ npos = off;
} else if (whence == LFS_SEEK_CUR) {
- if (off < 0 && (lfs_off_t)-off > file->pos) {
- return LFS_ERR_INVAL;
- }
-
- file->pos = file->pos + off;
+ npos = file->pos + off;
} else if (whence == LFS_SEEK_END) {
- if (off < 0 && (lfs_off_t)-off > file->size) {
- return LFS_ERR_INVAL;
- }
+ npos = file->size + off;
+ }
- file->pos = file->size + off;
+ if (npos < 0 || npos > LFS_FILE_MAX) {
+ // file position out of range
+ return LFS_ERR_INVAL;
}
- return file->pos;
+ // update pos
+ file->pos = npos;
+ return npos;
}
int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {