Skip to content

Commit e22e9cd

Browse files
gekykjbracey
authored andcommitted
Added filesystem implementations of truncate
- File::truncate - FileSystem::file_truncate - FATFileSystem::file_truncate - LittleFileSystem::file_truncate
1 parent 571a771 commit e22e9cd

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
@@ -720,6 +720,37 @@ off_t FATFileSystem::file_size(fs_file_t file)
720720
return res;
721721
}
722722

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

724755
////// Dir operations //////
725756
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
@@ -217,6 +217,19 @@ class FATFileSystem : public mbed::FileSystem {
217217
*/
218218
virtual off_t file_size(mbed::fs_file_t file);
219219

220+
/** Truncate or extend a file.
221+
*
222+
* The file's length is set to the specified value. The seek pointer is
223+
* not changed. If the file is extended, the extended area appears as if
224+
* it were zero-filled.
225+
*
226+
* @param file File handle
227+
* @param length The requested new length for the file
228+
*
229+
* @return Zero on success, negative error code on failure
230+
*/
231+
virtual int file_truncate(mbed::fs_file_t file, off_t length);
232+
220233
/** Open a directory on the filesystem
221234
*
222235
* @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
@@ -474,6 +474,17 @@ off_t LittleFileSystem::file_size(fs_file_t file)
474474
return lfs_toerror(res);
475475
}
476476

477+
int LittleFileSystem::file_truncate(fs_file_t file, off_t length)
478+
{
479+
lfs_file_t *f = (lfs_file_t *)file;
480+
_mutex.lock();
481+
LFS_INFO("file_truncate(%p)", file);
482+
int err = lfs_file_truncate(&_lfs, f, length);
483+
LFS_INFO("file_truncate -> %d", lfs_toerror(err));
484+
_mutex.unlock();
485+
return lfs_toerror(err);
486+
}
487+
477488

478489
////// Dir operations //////
479490
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
@@ -220,6 +220,19 @@ class LittleFileSystem : public mbed::FileSystem {
220220
*/
221221
virtual off_t file_size(mbed::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(mbed::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

0 commit comments

Comments
 (0)