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
This commit is contained in:
Christopher Haster
2018-10-08 14:12:20 -05:00
parent 97d8d5e96a
commit 195075819e
2 changed files with 25 additions and 14 deletions

31
lfs.c
View File

@@ -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;
}
file->pos = file->size + off;
npos = file->size + off;
}
return file->pos;
if (npos < 0 || npos > LFS_FILE_MAX) {
// file position out of range
return LFS_ERR_INVAL;
}
// update pos
file->pos = npos;
return npos;
}
int lfs_file_truncate(lfs_t *lfs, lfs_file_t *file, lfs_off_t size) {