Skip to content

Commit 8db2c0d

Browse files
gekykjbracey
authored andcommitted
Added filesystem implementations of truncate
- File::truncate - FileSystem::file_truncate - FATFileSystem::file_truncate - LittleFileSystem::file_truncate
1 parent 7b5939c commit 8db2c0d

File tree

8 files changed

+104
-0
lines changed

8 files changed

+104
-0
lines changed

features/storage/filesystem/File.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,10 @@ off_t File::size()
110110
return _fs->file_size(_file);
111111
}
112112

113+
int File::truncate(off_t length)
114+
{
115+
MBED_ASSERT(_fs);
116+
return _fs->file_truncate(_file, length);
117+
}
118+
113119
} // namespace mbed

features/storage/filesystem/File.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ class File : public FileHandle {
126126
*/
127127
virtual off_t size();
128128

129+
/** Truncate or extend a file.
130+
*
131+
* The file's length is set to the specified value. The seek pointer is
132+
* not changed. If the file is extended, the extended area appears as if
133+
* it were zero-filled.
134+
*
135+
* @param length The requested new length for the file
136+
*
137+
* @return Zero on success, negative error code on failure
138+
*/
139+
virtual int truncate(off_t length);
140+
129141
private:
130142
FileSystem *_fs;
131143
fs_file_t _file;

features/storage/filesystem/FileSystem.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ off_t FileSystem::file_size(fs_file_t file)
8484
return size;
8585
}
8686

87+
int FileSystem::file_truncate(fs_file_t file, off_t length)
88+
{
89+
return -ENOSYS;
90+
}
91+
8792
int FileSystem::dir_open(fs_dir_t *dir, const char *path)
8893
{
8994
return -ENOSYS;

features/storage/filesystem/FileSystem.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,19 @@ class FileSystem : public FileSystemLike {
220220
*/
221221
virtual off_t file_size(fs_file_t file);
222222

223+
/** Truncate or extend a file.
224+
*
225+
* The file's length is set to the specified value. The seek pointer is
226+
* not changed. If the file is extended, the extended area appears as if
227+
* it were zero-filled.
228+
*
229+
* @param file File handle
230+
* @param length The requested new length for the file
231+
*
232+
* @return Zero on success, negative error code on failure
233+
*/
234+
virtual int file_truncate(fs_file_t file, off_t length);
235+
223236
/** Open a directory on the filesystem
224237
*
225238
* @param dir Destination for the handle to the directory

features/storage/filesystem/fat/FATFileSystem.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,37 @@ off_t FATFileSystem::file_size(fs_file_t file)
721721
return res;
722722
}
723723

724+
int FATFileSystem::file_truncate(fs_file_t file, off_t length)
725+
{
726+
FIL *fh = static_cast<FIL *>(file);
727+
728+
lock();
729+
// save current position
730+
FSIZE_t oldoff = f_tell(fh);
731+
732+
// seek to new file size and truncate
733+
FRESULT res = f_lseek(fh, length);
734+
if (res) {
735+
unlock();
736+
return fat_error_remap(res);
737+
}
738+
739+
res = f_truncate(fh);
740+
if (res) {
741+
unlock();
742+
return fat_error_remap(res);
743+
}
744+
745+
// restore old position
746+
res = f_lseek(fh, oldoff);
747+
if (res) {
748+
unlock();
749+
return fat_error_remap(res);
750+
}
751+
752+
return 0;
753+
}
754+
724755

725756
////// Dir operations //////
726757
int FATFileSystem::dir_open(fs_dir_t *dir, const char *path)

features/storage/filesystem/fat/FATFileSystem.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,19 @@ class FATFileSystem : public FileSystem {
222222
*/
223223
virtual off_t file_size(fs_file_t file);
224224

225+
/** Truncate or extend a file.
226+
*
227+
* The file's length is set to the specified value. The seek pointer is
228+
* not changed. If the file is extended, the extended area appears as if
229+
* it were zero-filled.
230+
*
231+
* @param file File handle
232+
* @param length The requested new length for the file
233+
*
234+
* @return Zero on success, negative error code on failure
235+
*/
236+
virtual int file_truncate(mbed::fs_file_t file, off_t length);
237+
225238
/** Open a directory on the filesystem
226239
*
227240
* @param dir Destination for the handle to the directory

features/storage/filesystem/littlefs/LittleFileSystem.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,17 @@ off_t LittleFileSystem::file_size(fs_file_t file)
500500
return lfs_toerror(res);
501501
}
502502

503+
int LittleFileSystem::file_truncate(fs_file_t file, off_t length)
504+
{
505+
lfs_file_t *f = (lfs_file_t *)file;
506+
_mutex.lock();
507+
LFS_INFO("file_truncate(%p)", file);
508+
int err = lfs_file_truncate(&_lfs, f, length);
509+
LFS_INFO("file_truncate -> %d", lfs_toerror(err));
510+
_mutex.unlock();
511+
return lfs_toerror(err);
512+
}
513+
503514

504515
////// Dir operations //////
505516
int LittleFileSystem::dir_open(fs_dir_t *dir, const char *path)

features/storage/filesystem/littlefs/LittleFileSystem.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,19 @@ class LittleFileSystem : public mbed::FileSystem {
227227
*/
228228
virtual off_t file_size(mbed::fs_file_t file);
229229

230+
/** Truncate or extend a file.
231+
*
232+
* The file's length is set to the specified value. The seek pointer is
233+
* not changed. If the file is extended, the extended area appears as if
234+
* it were zero-filled.
235+
*
236+
* @param file File handle
237+
* @param length The requested new length for the file
238+
*
239+
* @return Zero on success, negative error code on failure
240+
*/
241+
virtual int file_truncate(mbed::fs_file_t file, off_t length);
242+
230243
/** Open a directory on the filesystem
231244
*
232245
* @param dir Destination for the handle to the directory

0 commit comments

Comments
 (0)