File tree Expand file tree Collapse file tree 8 files changed +104
-0
lines changed
features/storage/filesystem Expand file tree Collapse file tree 8 files changed +104
-0
lines changed Original file line number Diff line number Diff line change @@ -110,4 +110,10 @@ off_t File::size()
110
110
return _fs->file_size (_file);
111
111
}
112
112
113
+ int File::truncate (off_t length)
114
+ {
115
+ MBED_ASSERT (_fs);
116
+ return _fs->file_truncate (_file, length);
117
+ }
118
+
113
119
} // namespace mbed
Original file line number Diff line number Diff line change @@ -126,6 +126,18 @@ class File : public FileHandle {
126
126
*/
127
127
virtual off_t size ();
128
128
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
+
129
141
private:
130
142
FileSystem *_fs;
131
143
fs_file_t _file;
Original file line number Diff line number Diff line change @@ -84,6 +84,11 @@ off_t FileSystem::file_size(fs_file_t file)
84
84
return size;
85
85
}
86
86
87
+ int FileSystem::file_truncate (fs_file_t file, off_t length)
88
+ {
89
+ return -ENOSYS;
90
+ }
91
+
87
92
int FileSystem::dir_open (fs_dir_t *dir, const char *path)
88
93
{
89
94
return -ENOSYS;
Original file line number Diff line number Diff line change @@ -220,6 +220,19 @@ class FileSystem : public FileSystemLike {
220
220
*/
221
221
virtual off_t file_size (fs_file_t file);
222
222
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
+
223
236
/* * Open a directory on the filesystem
224
237
*
225
238
* @param dir Destination for the handle to the directory
Original file line number Diff line number Diff line change @@ -721,6 +721,37 @@ off_t FATFileSystem::file_size(fs_file_t file)
721
721
return res;
722
722
}
723
723
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
+
724
755
725
756
// //// Dir operations //////
726
757
int FATFileSystem::dir_open (fs_dir_t *dir, const char *path)
Original file line number Diff line number Diff line change @@ -222,6 +222,19 @@ class FATFileSystem : public FileSystem {
222
222
*/
223
223
virtual off_t file_size (fs_file_t file);
224
224
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
+
225
238
/* * Open a directory on the filesystem
226
239
*
227
240
* @param dir Destination for the handle to the directory
Original file line number Diff line number Diff line change @@ -500,6 +500,17 @@ off_t LittleFileSystem::file_size(fs_file_t file)
500
500
return lfs_toerror (res);
501
501
}
502
502
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
+
503
514
504
515
// //// Dir operations //////
505
516
int LittleFileSystem::dir_open (fs_dir_t *dir, const char *path)
Original file line number Diff line number Diff line change @@ -227,6 +227,19 @@ class LittleFileSystem : public mbed::FileSystem {
227
227
*/
228
228
virtual off_t file_size (mbed::fs_file_t file);
229
229
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
+
230
243
/* * Open a directory on the filesystem
231
244
*
232
245
* @param dir Destination for the handle to the directory
You can’t perform that action at this time.
0 commit comments