From cf7984366131385f70a5c1367be3d0b6d7831c44 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 22 Feb 2023 08:23:28 +1100 Subject: [PATCH] AP_Filesystem: support file rename --- libraries/AP_Filesystem/AP_Filesystem.cpp | 6 ++++++ libraries/AP_Filesystem/AP_Filesystem.h | 1 + libraries/AP_Filesystem/AP_Filesystem_ESP32.cpp | 8 ++++++++ libraries/AP_Filesystem/AP_Filesystem_ESP32.h | 1 + libraries/AP_Filesystem/AP_Filesystem_FATFS.cpp | 16 ++++++++++++++++ libraries/AP_Filesystem/AP_Filesystem_FATFS.h | 1 + libraries/AP_Filesystem/AP_Filesystem_backend.h | 1 + libraries/AP_Filesystem/AP_Filesystem_posix.cpp | 8 ++++++++ libraries/AP_Filesystem/AP_Filesystem_posix.h | 1 + 9 files changed, 43 insertions(+) diff --git a/libraries/AP_Filesystem/AP_Filesystem.cpp b/libraries/AP_Filesystem/AP_Filesystem.cpp index 2a316dc5e8..4e4f129d3d 100644 --- a/libraries/AP_Filesystem/AP_Filesystem.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem.cpp @@ -182,6 +182,12 @@ int AP_Filesystem::mkdir(const char *pathname) return backend.fs.mkdir(pathname); } +int AP_Filesystem::rename(const char *oldpath, const char *newpath) +{ + const Backend &backend = backend_by_path(oldpath); + return backend.fs.rename(oldpath, newpath); +} + AP_Filesystem::DirHandle *AP_Filesystem::opendir(const char *pathname) { const Backend &backend = backend_by_path(pathname); diff --git a/libraries/AP_Filesystem/AP_Filesystem.h b/libraries/AP_Filesystem/AP_Filesystem.h index d9a5cc7374..193111de19 100644 --- a/libraries/AP_Filesystem/AP_Filesystem.h +++ b/libraries/AP_Filesystem/AP_Filesystem.h @@ -93,6 +93,7 @@ public: int stat(const char *pathname, struct stat *stbuf); int unlink(const char *pathname); int mkdir(const char *pathname); + int rename(const char *oldpath, const char *newpath); DirHandle *opendir(const char *pathname); struct dirent *readdir(DirHandle *dirp); diff --git a/libraries/AP_Filesystem/AP_Filesystem_ESP32.cpp b/libraries/AP_Filesystem/AP_Filesystem_ESP32.cpp index de285e358e..c41ad9179c 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_ESP32.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_ESP32.cpp @@ -91,6 +91,14 @@ int AP_Filesystem_ESP32::unlink(const char *pathname) return ::unlink(pathname); } +int AP_Filesystem_ESP32::rename(const char *oldpath, const char *newpath) +{ +#if FSDEBUG + printf("DO rename %s \n", oldpath, newpath); +#endif + return ::rename(oldpath, newpath); +} + int AP_Filesystem_ESP32::mkdir(const char *pathname) { #if FSDEBUG diff --git a/libraries/AP_Filesystem/AP_Filesystem_ESP32.h b/libraries/AP_Filesystem/AP_Filesystem_ESP32.h index 69877fb7f5..b117fce987 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_ESP32.h +++ b/libraries/AP_Filesystem/AP_Filesystem_ESP32.h @@ -33,6 +33,7 @@ public: void *opendir(const char *pathname) override; struct dirent *readdir(void *dirp) override; int closedir(void *dirp) override; + int rename(const char *oldpath, const char *newpath) override; // return free disk space in bytes, -1 on error int64_t disk_free(const char *path) override; diff --git a/libraries/AP_Filesystem/AP_Filesystem_FATFS.cpp b/libraries/AP_Filesystem/AP_Filesystem_FATFS.cpp index 115d49cb04..9cd93ceac4 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_FATFS.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_FATFS.cpp @@ -665,6 +665,22 @@ int AP_Filesystem_FATFS::mkdir(const char *pathname) return 0; } +int AP_Filesystem_FATFS::rename(const char *oldpath, const char *newpath) +{ + FS_CHECK_ALLOWED(-1); + WITH_SEMAPHORE(sem); + + errno = 0; + + int res = f_rename(oldpath, newpath); + if (res != FR_OK) { + errno = fatfs_to_errno((FRESULT)res); + return -1; + } + + return 0; +} + /* wrapper structure to associate a dirent with a DIR */ diff --git a/libraries/AP_Filesystem/AP_Filesystem_FATFS.h b/libraries/AP_Filesystem/AP_Filesystem_FATFS.h index 93e1c10edc..ef79a7c475 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_FATFS.h +++ b/libraries/AP_Filesystem/AP_Filesystem_FATFS.h @@ -30,6 +30,7 @@ public: int unlink(const char *pathname) override; int mkdir(const char *pathname) override; void *opendir(const char *pathname) override; + int rename(const char *oldpath, const char *newpath) override; struct dirent *readdir(void *dirp) override; int closedir(void *dirp) override; diff --git a/libraries/AP_Filesystem/AP_Filesystem_backend.h b/libraries/AP_Filesystem/AP_Filesystem_backend.h index 1ccb1100f6..187f9c48c3 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_backend.h +++ b/libraries/AP_Filesystem/AP_Filesystem_backend.h @@ -57,6 +57,7 @@ public: virtual void *opendir(const char *pathname) { return nullptr; } virtual struct dirent *readdir(void *dirp) { return nullptr; } virtual int closedir(void *dirp) { return -1; } + virtual int rename(const char *oldpath, const char *newpath) { return -1; } // return free disk space in bytes, -1 on error virtual int64_t disk_free(const char *path) { return 0; } diff --git a/libraries/AP_Filesystem/AP_Filesystem_posix.cpp b/libraries/AP_Filesystem/AP_Filesystem_posix.cpp index f0982159b2..658edbbf02 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_posix.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_posix.cpp @@ -135,6 +135,14 @@ int AP_Filesystem_Posix::closedir(void *dirp) return ::closedir((DIR *)dirp); } +int AP_Filesystem_Posix::rename(const char *oldpath, const char *newpath) +{ + FS_CHECK_ALLOWED(-1); + oldpath = map_filename(oldpath); + newpath = map_filename(newpath); + return ::rename(oldpath, newpath); +} + // return free disk space in bytes int64_t AP_Filesystem_Posix::disk_free(const char *path) { diff --git a/libraries/AP_Filesystem/AP_Filesystem_posix.h b/libraries/AP_Filesystem/AP_Filesystem_posix.h index 61d6bfdfd3..faea21d71d 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_posix.h +++ b/libraries/AP_Filesystem/AP_Filesystem_posix.h @@ -41,6 +41,7 @@ public: void *opendir(const char *pathname) override; struct dirent *readdir(void *dirp) override; int closedir(void *dirp) override; + int rename(const char *oldpath, const char *newpath) override; // return free disk space in bytes, -1 on error int64_t disk_free(const char *path) override;