From e14a287292c79724d22360b857f0f07740db893b Mon Sep 17 00:00:00 2001 From: Willian Galvani Date: Mon, 14 Mar 2022 11:33:32 -0300 Subject: [PATCH] AP_Filesystem: add allow_absolute_paths to open(), implement it for posix backend --- libraries/AP_Filesystem/AP_Filesystem.cpp | 4 ++-- libraries/AP_Filesystem/AP_Filesystem.h | 2 +- libraries/AP_Filesystem/AP_Filesystem_ESP32.h | 2 +- libraries/AP_Filesystem/AP_Filesystem_FATFS.cpp | 2 +- libraries/AP_Filesystem/AP_Filesystem_FATFS.h | 2 +- libraries/AP_Filesystem/AP_Filesystem_Mission.cpp | 2 +- libraries/AP_Filesystem/AP_Filesystem_Mission.h | 2 +- libraries/AP_Filesystem/AP_Filesystem_Param.cpp | 2 +- libraries/AP_Filesystem/AP_Filesystem_Param.h | 2 +- libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp | 2 +- libraries/AP_Filesystem/AP_Filesystem_ROMFS.h | 2 +- libraries/AP_Filesystem/AP_Filesystem_Sys.cpp | 2 +- libraries/AP_Filesystem/AP_Filesystem_Sys.h | 2 +- libraries/AP_Filesystem/AP_Filesystem_backend.h | 2 +- libraries/AP_Filesystem/AP_Filesystem_posix.cpp | 6 ++++-- libraries/AP_Filesystem/AP_Filesystem_posix.h | 2 +- 16 files changed, 20 insertions(+), 18 deletions(-) diff --git a/libraries/AP_Filesystem/AP_Filesystem.cpp b/libraries/AP_Filesystem/AP_Filesystem.cpp index 1719a7b0c5..2d677bef8d 100644 --- a/libraries/AP_Filesystem/AP_Filesystem.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem.cpp @@ -106,10 +106,10 @@ const AP_Filesystem::Backend &AP_Filesystem::backend_by_fd(int &fd) const return backends[idx]; } -int AP_Filesystem::open(const char *fname, int flags) +int AP_Filesystem::open(const char *fname, int flags, bool allow_absolute_paths) { const Backend &backend = backend_by_path(fname); - int fd = backend.fs.open(fname, flags); + int fd = backend.fs.open(fname, flags, allow_absolute_paths); if (fd < 0) { return -1; } diff --git a/libraries/AP_Filesystem/AP_Filesystem.h b/libraries/AP_Filesystem/AP_Filesystem.h index 972b8e93dd..bbd02e78b5 100644 --- a/libraries/AP_Filesystem/AP_Filesystem.h +++ b/libraries/AP_Filesystem/AP_Filesystem.h @@ -80,7 +80,7 @@ public: AP_Filesystem() {} // functions that closely match the equivalent posix calls - int open(const char *fname, int flags); + int open(const char *fname, int flags, bool allow_absolute_paths = false); int close(int fd); int32_t read(int fd, void *buf, uint32_t count); int32_t write(int fd, const void *buf, uint32_t count); diff --git a/libraries/AP_Filesystem/AP_Filesystem_ESP32.h b/libraries/AP_Filesystem/AP_Filesystem_ESP32.h index bdc03fbd33..69877fb7f5 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_ESP32.h +++ b/libraries/AP_Filesystem/AP_Filesystem_ESP32.h @@ -21,7 +21,7 @@ class AP_Filesystem_ESP32 : public AP_Filesystem_Backend { public: // functions that closely match the equivalent posix calls - int open(const char *fname, int flags) override; + int open(const char *fname, int flags, bool allow_absolute_paths = false) override; int close(int fd) override; int32_t read(int fd, void *buf, uint32_t count) override; int32_t write(int fd, const void *buf, uint32_t count) override; diff --git a/libraries/AP_Filesystem/AP_Filesystem_FATFS.cpp b/libraries/AP_Filesystem/AP_Filesystem_FATFS.cpp index 887aabfd98..29a45d3aed 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_FATFS.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_FATFS.cpp @@ -279,7 +279,7 @@ static bool remount_file_system(void) return true; } -int AP_Filesystem_FATFS::open(const char *pathname, int flags) +int AP_Filesystem_FATFS::open(const char *pathname, int flags, bool allow_absolute_path) { int fileno; int fatfs_modes; diff --git a/libraries/AP_Filesystem/AP_Filesystem_FATFS.h b/libraries/AP_Filesystem/AP_Filesystem_FATFS.h index 5193555947..663dffc819 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_FATFS.h +++ b/libraries/AP_Filesystem/AP_Filesystem_FATFS.h @@ -20,7 +20,7 @@ class AP_Filesystem_FATFS : public AP_Filesystem_Backend { public: // functions that closely match the equivalent posix calls - int open(const char *fname, int flags) override; + int open(const char *fname, int flags, bool allow_absolute_paths = false) override; int close(int fd) override; int32_t read(int fd, void *buf, uint32_t count) override; int32_t write(int fd, const void *buf, uint32_t count) override; diff --git a/libraries/AP_Filesystem/AP_Filesystem_Mission.cpp b/libraries/AP_Filesystem/AP_Filesystem_Mission.cpp index d4cb9f34de..ab71298b9d 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_Mission.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_Mission.cpp @@ -30,7 +30,7 @@ extern int errno; #define IDLE_TIMEOUT_MS 30000 -int AP_Filesystem_Mission::open(const char *fname, int flags) +int AP_Filesystem_Mission::open(const char *fname, int flags, bool allow_absolute_paths) { enum MAV_MISSION_TYPE mtype; diff --git a/libraries/AP_Filesystem/AP_Filesystem_Mission.h b/libraries/AP_Filesystem/AP_Filesystem_Mission.h index b4ff4f53ca..a4f0f869fd 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_Mission.h +++ b/libraries/AP_Filesystem/AP_Filesystem_Mission.h @@ -23,7 +23,7 @@ class AP_Filesystem_Mission : public AP_Filesystem_Backend { public: // functions that closely match the equivalent posix calls - int open(const char *fname, int flags) override; + int open(const char *fname, int flags, bool allow_absolute_paths = false) override; int close(int fd) override; int32_t read(int fd, void *buf, uint32_t count) override; int32_t lseek(int fd, int32_t offset, int whence) override; diff --git a/libraries/AP_Filesystem/AP_Filesystem_Param.cpp b/libraries/AP_Filesystem/AP_Filesystem_Param.cpp index a10e4a6f9a..1bf5c22a11 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_Param.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_Param.cpp @@ -26,7 +26,7 @@ extern const AP_HAL::HAL& hal; extern int errno; -int AP_Filesystem_Param::open(const char *fname, int flags) +int AP_Filesystem_Param::open(const char *fname, int flags, bool allow_absolute_path) { if (!check_file_name(fname)) { errno = ENOENT; diff --git a/libraries/AP_Filesystem/AP_Filesystem_Param.h b/libraries/AP_Filesystem/AP_Filesystem_Param.h index 98e8dc6af1..6f3c30f046 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_Param.h +++ b/libraries/AP_Filesystem/AP_Filesystem_Param.h @@ -24,7 +24,7 @@ class AP_Filesystem_Param : public AP_Filesystem_Backend { public: // functions that closely match the equivalent posix calls - int open(const char *fname, int flags) override; + int open(const char *fname, int flags, bool allow_absolute_paths = false) override; int close(int fd) override; int32_t read(int fd, void *buf, uint32_t count) override; int32_t lseek(int fd, int32_t offset, int whence) override; diff --git a/libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp b/libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp index 5b23b4e898..d4e3dadb3f 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_ROMFS.cpp @@ -23,7 +23,7 @@ #if defined(HAL_HAVE_AP_ROMFS_EMBEDDED_H) -int AP_Filesystem_ROMFS::open(const char *fname, int flags) +int AP_Filesystem_ROMFS::open(const char *fname, int flags, bool allow_absolute_paths) { if ((flags & O_ACCMODE) != O_RDONLY) { errno = EROFS; diff --git a/libraries/AP_Filesystem/AP_Filesystem_ROMFS.h b/libraries/AP_Filesystem/AP_Filesystem_ROMFS.h index fb69e23360..897195915e 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_ROMFS.h +++ b/libraries/AP_Filesystem/AP_Filesystem_ROMFS.h @@ -21,7 +21,7 @@ class AP_Filesystem_ROMFS : public AP_Filesystem_Backend { public: // functions that closely match the equivalent posix calls - int open(const char *fname, int flags) override; + int open(const char *fname, int flags, bool allow_absolute_paths = false) override; int close(int fd) override; int32_t read(int fd, void *buf, uint32_t count) override; int32_t write(int fd, const void *buf, uint32_t count) override; diff --git a/libraries/AP_Filesystem/AP_Filesystem_Sys.cpp b/libraries/AP_Filesystem/AP_Filesystem_Sys.cpp index da14a5bfc5..0d0550c8f7 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_Sys.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_Sys.cpp @@ -60,7 +60,7 @@ int8_t AP_Filesystem_Sys::file_in_sysfs(const char *fname) { return -1; } -int AP_Filesystem_Sys::open(const char *fname, int flags) +int AP_Filesystem_Sys::open(const char *fname, int flags, bool allow_absolute_paths) { if ((flags & O_ACCMODE) != O_RDONLY) { errno = EROFS; diff --git a/libraries/AP_Filesystem/AP_Filesystem_Sys.h b/libraries/AP_Filesystem/AP_Filesystem_Sys.h index 1eef1fac35..e097b74d4a 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_Sys.h +++ b/libraries/AP_Filesystem/AP_Filesystem_Sys.h @@ -23,7 +23,7 @@ class AP_Filesystem_Sys : public AP_Filesystem_Backend { public: // functions that closely match the equivalent posix calls - int open(const char *fname, int flags) override; + int open(const char *fname, int flags, bool allow_absolute_paths = false) override; int close(int fd) override; int32_t read(int fd, void *buf, uint32_t count) override; int32_t lseek(int fd, int32_t offset, int whence) override; diff --git a/libraries/AP_Filesystem/AP_Filesystem_backend.h b/libraries/AP_Filesystem/AP_Filesystem_backend.h index a60fad1d17..f359891cf9 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_backend.h +++ b/libraries/AP_Filesystem/AP_Filesystem_backend.h @@ -43,7 +43,7 @@ class AP_Filesystem_Backend { public: // functions that closely match the equivalent posix calls - virtual int open(const char *fname, int flags) { + virtual int open(const char *fname, int flags, bool allow_absolute_paths = false) { return -1; } virtual int close(int fd) { return -1; } diff --git a/libraries/AP_Filesystem/AP_Filesystem_posix.cpp b/libraries/AP_Filesystem/AP_Filesystem_posix.cpp index 6454d86d60..f0982159b2 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_posix.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem_posix.cpp @@ -49,10 +49,12 @@ static const char *map_filename(const char *fname) return fname; } -int AP_Filesystem_Posix::open(const char *fname, int flags) +int AP_Filesystem_Posix::open(const char *fname, int flags, bool allow_absolute_paths) { FS_CHECK_ALLOWED(-1); - fname = map_filename(fname); + if (! allow_absolute_paths) { + fname = map_filename(fname); + } // we automatically add O_CLOEXEC as we always want it for ArduPilot FS usage return ::open(fname, flags | O_CLOEXEC, 0644); } diff --git a/libraries/AP_Filesystem/AP_Filesystem_posix.h b/libraries/AP_Filesystem/AP_Filesystem_posix.h index 42fdad3ec6..61d6bfdfd3 100644 --- a/libraries/AP_Filesystem/AP_Filesystem_posix.h +++ b/libraries/AP_Filesystem/AP_Filesystem_posix.h @@ -29,7 +29,7 @@ class AP_Filesystem_Posix : public AP_Filesystem_Backend { public: // functions that closely match the equivalent posix calls - int open(const char *fname, int flags) override; + int open(const char *fname, int flags, bool allow_absolute_paths = false) override; int close(int fd) override; int32_t read(int fd, void *buf, uint32_t count) override; int32_t write(int fd, const void *buf, uint32_t count) override;