2019-08-01 02:07:26 -03:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
ArduPilot filesystem interface for posix systems
|
|
|
|
*/
|
2023-05-14 22:44:22 -03:00
|
|
|
|
|
|
|
#include "AP_Filesystem_config.h"
|
|
|
|
|
|
|
|
#if AP_FILESYSTEM_POSIX_ENABLED
|
|
|
|
|
2019-08-01 02:07:26 -03:00
|
|
|
#include "AP_Filesystem.h"
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2020-11-16 00:54:49 -04:00
|
|
|
#include <AP_Vehicle/AP_Vehicle_Type.h>
|
2019-08-01 02:07:26 -03:00
|
|
|
|
2019-09-09 05:30:59 -03:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#else
|
2019-08-01 02:07:26 -03:00
|
|
|
#include <sys/vfs.h>
|
2019-09-09 05:30:59 -03:00
|
|
|
#endif
|
2019-08-26 02:55:28 -03:00
|
|
|
#include <utime.h>
|
2019-08-01 02:07:26 -03:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2020-02-10 22:15:03 -04:00
|
|
|
/*
|
|
|
|
map a filename for SITL so operations are relative to the current directory
|
|
|
|
*/
|
|
|
|
static const char *map_filename(const char *fname)
|
|
|
|
{
|
2020-11-16 00:54:49 -04:00
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL && !APM_BUILD_TYPE(APM_BUILD_Replay)
|
2020-02-10 22:15:03 -04:00
|
|
|
// on SITL only allow paths under subdirectory. Users can still
|
|
|
|
// escape with .. if they want to
|
|
|
|
if (strcmp(fname, "/") == 0) {
|
|
|
|
return ".";
|
|
|
|
}
|
|
|
|
if (*fname == '/') {
|
|
|
|
fname++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// on Linux allow original name
|
|
|
|
return fname;
|
|
|
|
}
|
|
|
|
|
2022-03-14 11:33:32 -03:00
|
|
|
int AP_Filesystem_Posix::open(const char *fname, int flags, bool allow_absolute_paths)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2022-03-14 11:33:32 -03:00
|
|
|
if (! allow_absolute_paths) {
|
|
|
|
fname = map_filename(fname);
|
|
|
|
}
|
2019-08-01 02:07:26 -03:00
|
|
|
// we automatically add O_CLOEXEC as we always want it for ArduPilot FS usage
|
|
|
|
return ::open(fname, flags | O_CLOEXEC, 0644);
|
|
|
|
}
|
|
|
|
|
2020-03-11 03:48:09 -03:00
|
|
|
int AP_Filesystem_Posix::close(int fd)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2019-08-01 02:07:26 -03:00
|
|
|
return ::close(fd);
|
|
|
|
}
|
|
|
|
|
2020-03-30 01:14:35 -03:00
|
|
|
int32_t AP_Filesystem_Posix::read(int fd, void *buf, uint32_t count)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2019-08-01 02:07:26 -03:00
|
|
|
return ::read(fd, buf, count);
|
|
|
|
}
|
|
|
|
|
2020-03-30 01:14:35 -03:00
|
|
|
int32_t AP_Filesystem_Posix::write(int fd, const void *buf, uint32_t count)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2019-08-01 02:07:26 -03:00
|
|
|
return ::write(fd, buf, count);
|
|
|
|
}
|
|
|
|
|
2020-03-11 03:48:09 -03:00
|
|
|
int AP_Filesystem_Posix::fsync(int fd)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2019-08-01 02:07:26 -03:00
|
|
|
return ::fsync(fd);
|
|
|
|
}
|
|
|
|
|
2020-03-29 22:53:29 -03:00
|
|
|
int32_t AP_Filesystem_Posix::lseek(int fd, int32_t offset, int seek_from)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2019-08-01 02:07:26 -03:00
|
|
|
return ::lseek(fd, offset, seek_from);
|
|
|
|
}
|
|
|
|
|
2020-03-11 03:48:09 -03:00
|
|
|
int AP_Filesystem_Posix::stat(const char *pathname, struct stat *stbuf)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2020-02-10 22:15:03 -04:00
|
|
|
pathname = map_filename(pathname);
|
2019-08-01 02:07:26 -03:00
|
|
|
return ::stat(pathname, stbuf);
|
|
|
|
}
|
|
|
|
|
2020-03-11 03:48:09 -03:00
|
|
|
int AP_Filesystem_Posix::unlink(const char *pathname)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2020-02-10 22:15:03 -04:00
|
|
|
pathname = map_filename(pathname);
|
2019-11-02 07:11:29 -03:00
|
|
|
// we match the FATFS interface and use unlink
|
|
|
|
// for both files and directories
|
|
|
|
int ret = ::rmdir(pathname);
|
|
|
|
if (ret == -1) {
|
|
|
|
ret = ::unlink(pathname);
|
|
|
|
}
|
|
|
|
return ret;
|
2019-08-01 02:07:26 -03:00
|
|
|
}
|
|
|
|
|
2020-03-11 03:48:09 -03:00
|
|
|
int AP_Filesystem_Posix::mkdir(const char *pathname)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2020-02-10 22:15:03 -04:00
|
|
|
pathname = map_filename(pathname);
|
2019-08-01 02:07:26 -03:00
|
|
|
return ::mkdir(pathname, 0775);
|
|
|
|
}
|
|
|
|
|
2020-03-11 03:48:09 -03:00
|
|
|
void *AP_Filesystem_Posix::opendir(const char *pathname)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(nullptr);
|
2020-02-10 22:15:03 -04:00
|
|
|
pathname = map_filename(pathname);
|
2020-03-11 03:48:09 -03:00
|
|
|
return (void*)::opendir(pathname);
|
2019-08-01 02:07:26 -03:00
|
|
|
}
|
|
|
|
|
2020-03-11 03:48:09 -03:00
|
|
|
struct dirent *AP_Filesystem_Posix::readdir(void *dirp)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(nullptr);
|
2020-03-11 03:48:09 -03:00
|
|
|
return ::readdir((DIR *)dirp);
|
2019-08-01 02:07:26 -03:00
|
|
|
}
|
|
|
|
|
2020-03-11 03:48:09 -03:00
|
|
|
int AP_Filesystem_Posix::closedir(void *dirp)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2020-03-11 03:48:09 -03:00
|
|
|
return ::closedir((DIR *)dirp);
|
2019-08-01 02:07:26 -03:00
|
|
|
}
|
|
|
|
|
2023-02-21 17:23:28 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-08-01 02:07:26 -03:00
|
|
|
// return free disk space in bytes
|
2020-03-11 03:48:09 -03:00
|
|
|
int64_t AP_Filesystem_Posix::disk_free(const char *path)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2020-02-10 22:15:03 -04:00
|
|
|
path = map_filename(path);
|
2019-08-01 02:07:26 -03:00
|
|
|
struct statfs stats;
|
|
|
|
if (::statfs(path, &stats) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return (((int64_t)stats.f_bavail) * stats.f_bsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return total disk space in bytes
|
2020-03-11 03:48:09 -03:00
|
|
|
int64_t AP_Filesystem_Posix::disk_space(const char *path)
|
2019-08-01 02:07:26 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(-1);
|
2020-02-10 22:15:03 -04:00
|
|
|
path = map_filename(path);
|
2019-08-01 02:07:26 -03:00
|
|
|
struct statfs stats;
|
|
|
|
if (::statfs(path, &stats) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return (((int64_t)stats.f_blocks) * stats.f_bsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-26 02:55:28 -03:00
|
|
|
/*
|
|
|
|
set mtime on a file
|
|
|
|
*/
|
2020-03-29 22:53:29 -03:00
|
|
|
bool AP_Filesystem_Posix::set_mtime(const char *filename, const uint32_t mtime_sec)
|
2019-08-26 02:55:28 -03:00
|
|
|
{
|
2021-06-08 22:59:23 -03:00
|
|
|
FS_CHECK_ALLOWED(false);
|
2020-02-10 22:15:03 -04:00
|
|
|
filename = map_filename(filename);
|
2019-08-26 02:55:28 -03:00
|
|
|
struct utimbuf times {};
|
|
|
|
times.actime = mtime_sec;
|
|
|
|
times.modtime = mtime_sec;
|
|
|
|
|
|
|
|
return utime(filename, ×) == 0;
|
|
|
|
}
|
|
|
|
|
2023-05-14 22:44:22 -03:00
|
|
|
#endif // AP_FILESYSTEM_POSIX_ENABLED
|