mirror of
https://gitee.com/openharmony/third_party_littlefs
synced 2024-11-23 06:50:37 +00:00
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:
parent
97d8d5e96a
commit
195075819e
31
lfs.c
31
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
|
8
lfs.h
8
lfs.h
@ -21,7 +21,7 @@ extern "C"
|
||||
// Software library version
|
||||
// Major (top-nibble), incremented on backwards incompatible changes
|
||||
// Minor (bottom-nibble), incremented on feature additions
|
||||
#define LFS_VERSION 0x00010006
|
||||
#define LFS_VERSION 0x00010007
|
||||
#define LFS_VERSION_MAJOR (0xffff & (LFS_VERSION >> 16))
|
||||
#define LFS_VERSION_MINOR (0xffff & (LFS_VERSION >> 0))
|
||||
|
||||
@ -49,6 +49,11 @@ typedef uint32_t lfs_block_t;
|
||||
#define LFS_NAME_MAX 255
|
||||
#endif
|
||||
|
||||
// Max file size in bytes
|
||||
#ifndef LFS_FILE_MAX
|
||||
#define LFS_FILE_MAX 2147483647
|
||||
#endif
|
||||
|
||||
// Possible error codes, these are negative to allow
|
||||
// valid positive return values
|
||||
enum lfs_error {
|
||||
@ -61,6 +66,7 @@ enum lfs_error {
|
||||
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_FBIG = -27, // File too large
|
||||
LFS_ERR_INVAL = -22, // Invalid parameter
|
||||
LFS_ERR_NOSPC = -28, // No space left on device
|
||||
LFS_ERR_NOMEM = -12, // No more memory available
|
||||
|
Loading…
Reference in New Issue
Block a user