Skip to content

Commit cde156c

Browse files
Alexander ValitovBogdan Marinescu
authored andcommitted
Added implementation for "rename" on FAT file system
1 parent 1409f2a commit cde156c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

libraries/fs/fat/FATFileSystem.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ int FATFileSystem::remove(const char *filename) {
108108
return 0;
109109
}
110110

111+
int FATFileSystem::rename(const char *oldname, const char *newname) {
112+
FRESULT res = f_rename(oldname, newname);
113+
if (res) {
114+
debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
115+
return -1;
116+
}
117+
return 0;
118+
}
119+
111120
int FATFileSystem::format() {
112121
FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
113122
if (res) {

libraries/fs/fat/FATFileSystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class FATFileSystem : public FileSystemLike {
4141

4242
virtual FileHandle *open(const char* name, int flags);
4343
virtual int remove(const char *filename);
44+
virtual int rename(const char *oldname, const char *newname);
4445
virtual int format();
4546
virtual DirHandle *opendir(const char *name);
4647
virtual int mkdir(const char *name, mode_t mode);

libraries/mbed/common/retarget.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,15 @@ extern "C" int remove(const char *path) {
323323
}
324324

325325
extern "C" int rename(const char *oldname, const char *newname) {
326-
return -1;
326+
FilePath fpOld(oldname);
327+
FilePath fpNew(newname);
328+
FileSystemLike *fsOld = fpOld.fileSystem();
329+
FileSystemLike *fsNew = fpNew.fileSystem();
330+
331+
/* rename only if both files are on the same FS */
332+
if (fsOld != fsNew || fsOld == NULL) return -1;
333+
334+
return fsOld->rename(fpOld.fileName(), fpNew.fileName());
327335
}
328336

329337
extern "C" char *tmpnam(char *s) {

0 commit comments

Comments
 (0)