F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
FileSystem.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Posix/FileSystem.cpp
3 // \brief Posix implementation for Os::FileSystem
4 // ======================================================================
6 #include "Os/Posix/error.hpp"
7 
8 #include <dirent.h>
9 #ifndef TGT_OS_TYPE_VXWORKS
10 #include <sys/statvfs.h>
11 #endif
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <cstdio>
15 
16 #include <cerrno>
17 
18 namespace Os {
19 namespace Posix {
20 namespace FileSystem {
21 
23  Status status = OP_OK;
24  if (::rmdir(path) == -1) {
25  status = errno_to_filesystem_status(errno);
26  }
27  return status;
28 }
29 
31  Status status = OP_OK;
32  if (::unlink(path) == -1) {
33  status = errno_to_filesystem_status(errno);
34  }
35  return status;
36 }
37 
38 PosixFileSystem::Status PosixFileSystem::_rename(const char* originPath, const char* destPath) {
39  Status status = OP_OK;
40  if (::rename(originPath, destPath) == -1) {
41  status = errno_to_filesystem_status(errno);
42  }
43  return status;
44 }
45 
47  Status status = OP_OK;
48  if (::getcwd(path, bufferSize) == nullptr) {
49  status = errno_to_filesystem_status(errno);
50  }
51  return status;
52 }
53 
55  Status status = OP_OK;
56  if (::chdir(path) == -1) {
57  status = errno_to_filesystem_status(errno);
58  }
59  return status;
60 }
61 
63  FwSizeType& totalBytes,
64  FwSizeType& freeBytes) {
65 #ifdef TGT_OS_TYPE_VXWORKS
66  return Status::NOT_SUPPORTED;
67 #else
68  Status stat = OP_OK;
69  static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<fsblkcnt_t>::max(),
70  "FwSizeType must be able to hold fsblkcnt_t");
72  "FwSizeType must be able to hold fsblkcnt_t");
73  static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<unsigned long>::max(),
74  "FwSizeType must be able to hold unsigned long");
75  struct statvfs fsStat;
76  int ret = statvfs(path, &fsStat);
77  if (ret) {
78  return errno_to_filesystem_status(errno);
79  }
80 
81  const FwSizeType block_size = static_cast<FwSizeType>(fsStat.f_frsize);
82  const FwSizeType free_blocks = static_cast<FwSizeType>(fsStat.f_bfree);
83  const FwSizeType total_blocks = static_cast<FwSizeType>(fsStat.f_blocks);
84 
85  // Check for overflow in multiplication
86  if (free_blocks > (std::numeric_limits<FwSizeType>::max() / block_size) ||
87  total_blocks > (std::numeric_limits<FwSizeType>::max() / block_size)) {
88  return OVERFLOW_ERROR;
89  }
90  freeBytes = free_blocks * block_size;
91  totalBytes = total_blocks * block_size;
92  return stat;
93 #endif
94 }
95 
97  return &this->m_handle;
98 }
99 
100 } // namespace FileSystem
101 } // namespace Posix
102 } // namespace Os
static U32 min(const U32 a, const U32 b)
Definition: Checksum.cpp:16
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
FileSystem class.
Definition: FileSystem.hpp:117
@ OP_OK
Operation was successful.
Definition: FileSystem.hpp:25
Status _removeFile(const char *path) override
Remove a file at the specified path.
Definition: FileSystem.cpp:30
Status _changeWorkingDirectory(const char *path) override
Change the current working directory to the specified path.
Definition: FileSystem.cpp:54
Status _getWorkingDirectory(char *path, FwSizeType bufferSize) override
Get the current working directory.
Definition: FileSystem.cpp:46
FileSystemHandle * getHandle() override
Get the raw FileSystem handle.
Definition: FileSystem.cpp:96
Status _removeDirectory(const char *path) override
Remove a directory at the specified path.
Definition: FileSystem.cpp:22
Status _getFreeSpace(const char *path, FwSizeType &totalBytes, FwSizeType &freeBytes) override
Get filesystem free and total space in bytes on the filesystem containing the specified path.
Definition: FileSystem.cpp:62
Status _rename(const char *sourcePath, const char *destPath) override
Rename a file from source to destination.
Definition: FileSystem.cpp:38
FileSystem::Status errno_to_filesystem_status(PlatformIntType errno_input)
Definition: error.cpp:51