mirror of https://github.com/ARMmbed/mbed-os.git
Added filesystem implementations of truncate
- File::truncate - FileSystem::file_truncate - FATFileSystem::file_truncate - LittleFileSystem::file_truncatepull/8972/head
parent
7b5939c05b
commit
8db2c0d00b
|
|
@ -110,4 +110,10 @@ off_t File::size()
|
|||
return _fs->file_size(_file);
|
||||
}
|
||||
|
||||
int File::truncate(off_t length)
|
||||
{
|
||||
MBED_ASSERT(_fs);
|
||||
return _fs->file_truncate(_file, length);
|
||||
}
|
||||
|
||||
} // namespace mbed
|
||||
|
|
|
|||
|
|
@ -126,6 +126,18 @@ public:
|
|||
*/
|
||||
virtual off_t size();
|
||||
|
||||
/** Truncate or extend a file.
|
||||
*
|
||||
* The file's length is set to the specified value. The seek pointer is
|
||||
* not changed. If the file is extended, the extended area appears as if
|
||||
* it were zero-filled.
|
||||
*
|
||||
* @param length The requested new length for the file
|
||||
*
|
||||
* @return Zero on success, negative error code on failure
|
||||
*/
|
||||
virtual int truncate(off_t length);
|
||||
|
||||
private:
|
||||
FileSystem *_fs;
|
||||
fs_file_t _file;
|
||||
|
|
|
|||
|
|
@ -84,6 +84,11 @@ off_t FileSystem::file_size(fs_file_t file)
|
|||
return size;
|
||||
}
|
||||
|
||||
int FileSystem::file_truncate(fs_file_t file, off_t length)
|
||||
{
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
int FileSystem::dir_open(fs_dir_t *dir, const char *path)
|
||||
{
|
||||
return -ENOSYS;
|
||||
|
|
|
|||
|
|
@ -220,6 +220,19 @@ protected:
|
|||
*/
|
||||
virtual off_t file_size(fs_file_t file);
|
||||
|
||||
/** Truncate or extend a file.
|
||||
*
|
||||
* The file's length is set to the specified value. The seek pointer is
|
||||
* not changed. If the file is extended, the extended area appears as if
|
||||
* it were zero-filled.
|
||||
*
|
||||
* @param file File handle
|
||||
* @param length The requested new length for the file
|
||||
*
|
||||
* @return Zero on success, negative error code on failure
|
||||
*/
|
||||
virtual int file_truncate(fs_file_t file, off_t length);
|
||||
|
||||
/** Open a directory on the filesystem
|
||||
*
|
||||
* @param dir Destination for the handle to the directory
|
||||
|
|
|
|||
|
|
@ -721,6 +721,37 @@ off_t FATFileSystem::file_size(fs_file_t file)
|
|||
return res;
|
||||
}
|
||||
|
||||
int FATFileSystem::file_truncate(fs_file_t file, off_t length)
|
||||
{
|
||||
FIL *fh = static_cast<FIL *>(file);
|
||||
|
||||
lock();
|
||||
// save current position
|
||||
FSIZE_t oldoff = f_tell(fh);
|
||||
|
||||
// seek to new file size and truncate
|
||||
FRESULT res = f_lseek(fh, length);
|
||||
if (res) {
|
||||
unlock();
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
res = f_truncate(fh);
|
||||
if (res) {
|
||||
unlock();
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
// restore old position
|
||||
res = f_lseek(fh, oldoff);
|
||||
if (res) {
|
||||
unlock();
|
||||
return fat_error_remap(res);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
////// Dir operations //////
|
||||
int FATFileSystem::dir_open(fs_dir_t *dir, const char *path)
|
||||
|
|
|
|||
|
|
@ -222,6 +222,19 @@ protected:
|
|||
*/
|
||||
virtual off_t file_size(fs_file_t file);
|
||||
|
||||
/** Truncate or extend a file.
|
||||
*
|
||||
* The file's length is set to the specified value. The seek pointer is
|
||||
* not changed. If the file is extended, the extended area appears as if
|
||||
* it were zero-filled.
|
||||
*
|
||||
* @param file File handle
|
||||
* @param length The requested new length for the file
|
||||
*
|
||||
* @return Zero on success, negative error code on failure
|
||||
*/
|
||||
virtual int file_truncate(mbed::fs_file_t file, off_t length);
|
||||
|
||||
/** Open a directory on the filesystem
|
||||
*
|
||||
* @param dir Destination for the handle to the directory
|
||||
|
|
|
|||
|
|
@ -500,6 +500,17 @@ off_t LittleFileSystem::file_size(fs_file_t file)
|
|||
return lfs_toerror(res);
|
||||
}
|
||||
|
||||
int LittleFileSystem::file_truncate(fs_file_t file, off_t length)
|
||||
{
|
||||
lfs_file_t *f = (lfs_file_t *)file;
|
||||
_mutex.lock();
|
||||
LFS_INFO("file_truncate(%p)", file);
|
||||
int err = lfs_file_truncate(&_lfs, f, length);
|
||||
LFS_INFO("file_truncate -> %d", lfs_toerror(err));
|
||||
_mutex.unlock();
|
||||
return lfs_toerror(err);
|
||||
}
|
||||
|
||||
|
||||
////// Dir operations //////
|
||||
int LittleFileSystem::dir_open(fs_dir_t *dir, const char *path)
|
||||
|
|
|
|||
|
|
@ -227,6 +227,19 @@ protected:
|
|||
*/
|
||||
virtual off_t file_size(mbed::fs_file_t file);
|
||||
|
||||
/** Truncate or extend a file.
|
||||
*
|
||||
* The file's length is set to the specified value. The seek pointer is
|
||||
* not changed. If the file is extended, the extended area appears as if
|
||||
* it were zero-filled.
|
||||
*
|
||||
* @param file File handle
|
||||
* @param length The requested new length for the file
|
||||
*
|
||||
* @return Zero on success, negative error code on failure
|
||||
*/
|
||||
virtual int file_truncate(mbed::fs_file_t file, off_t length);
|
||||
|
||||
/** Open a directory on the filesystem
|
||||
*
|
||||
* @param dir Destination for the handle to the directory
|
||||
|
|
|
|||
Loading…
Reference in New Issue