AP_Filesystem_posix.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. ArduPilot filesystem interface for posix systems
  15. */
  16. #include "AP_Filesystem.h"
  17. #include <AP_HAL/AP_HAL.h>
  18. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX
  19. #if defined(__APPLE__)
  20. #include <sys/mount.h>
  21. #else
  22. #include <sys/vfs.h>
  23. #endif
  24. #include <utime.h>
  25. extern const AP_HAL::HAL& hal;
  26. int AP_Filesystem::open(const char *fname, int flags)
  27. {
  28. // we automatically add O_CLOEXEC as we always want it for ArduPilot FS usage
  29. return ::open(fname, flags | O_CLOEXEC, 0644);
  30. }
  31. int AP_Filesystem::close(int fd)
  32. {
  33. return ::close(fd);
  34. }
  35. ssize_t AP_Filesystem::read(int fd, void *buf, size_t count)
  36. {
  37. return ::read(fd, buf, count);
  38. }
  39. ssize_t AP_Filesystem::write(int fd, const void *buf, size_t count)
  40. {
  41. return ::write(fd, buf, count);
  42. }
  43. int AP_Filesystem::fsync(int fd)
  44. {
  45. return ::fsync(fd);
  46. }
  47. off_t AP_Filesystem::lseek(int fd, off_t offset, int seek_from)
  48. {
  49. return ::lseek(fd, offset, seek_from);
  50. }
  51. int AP_Filesystem::stat(const char *pathname, struct stat *stbuf)
  52. {
  53. return ::stat(pathname, stbuf);
  54. }
  55. int AP_Filesystem::unlink(const char *pathname)
  56. {
  57. return ::unlink(pathname);
  58. }
  59. int AP_Filesystem::mkdir(const char *pathname)
  60. {
  61. return ::mkdir(pathname, 0775);
  62. }
  63. DIR *AP_Filesystem::opendir(const char *pathname)
  64. {
  65. return ::opendir(pathname);
  66. }
  67. struct dirent *AP_Filesystem::readdir(DIR *dirp)
  68. {
  69. return ::readdir(dirp);
  70. }
  71. int AP_Filesystem::closedir(DIR *dirp)
  72. {
  73. return ::closedir(dirp);
  74. }
  75. // return free disk space in bytes
  76. int64_t AP_Filesystem::disk_free(const char *path)
  77. {
  78. struct statfs stats;
  79. if (::statfs(path, &stats) < 0) {
  80. return -1;
  81. }
  82. return (((int64_t)stats.f_bavail) * stats.f_bsize);
  83. }
  84. // return total disk space in bytes
  85. int64_t AP_Filesystem::disk_space(const char *path)
  86. {
  87. struct statfs stats;
  88. if (::statfs(path, &stats) < 0) {
  89. return -1;
  90. }
  91. return (((int64_t)stats.f_blocks) * stats.f_bsize);
  92. }
  93. /*
  94. set mtime on a file
  95. */
  96. bool AP_Filesystem::set_mtime(const char *filename, const time_t mtime_sec)
  97. {
  98. struct utimbuf times {};
  99. times.actime = mtime_sec;
  100. times.modtime = mtime_sec;
  101. return utime(filename, &times) == 0;
  102. }
  103. #endif // CONFIG_HAL_BOARD