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 @@ -720,6 +720,37 @@ off_t FATFileSystem::file_size(fs_file_t file)
720
720
return res;
721
721
}
722
722
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
+
723
754
724
755
// //// Dir operations //////
725
756
int FATFileSystem::dir_open (fs_dir_t *dir, const char *path)
Original file line number Diff line number Diff line change @@ -217,6 +217,19 @@ class FATFileSystem : public mbed::FileSystem {
217
217
*/
218
218
virtual off_t file_size (mbed::fs_file_t file);
219
219
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
+
220
233
/* * Open a directory on the filesystem
221
234
*
222
235
* @param dir Destination for the handle to the directory
Original file line number Diff line number Diff line change @@ -474,6 +474,17 @@ off_t LittleFileSystem::file_size(fs_file_t file)
474
474
return lfs_toerror (res);
475
475
}
476
476
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
+
477
488
478
489
// //// Dir operations //////
479
490
int LittleFileSystem::dir_open (fs_dir_t *dir, const char *path)
Original file line number Diff line number Diff line change @@ -220,6 +220,19 @@ class LittleFileSystem : public mbed::FileSystem {
220
220
*/
221
221
virtual off_t file_size (mbed::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 (mbed::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
You can’t perform that action at this time.
0 commit comments