| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title Os/Posix/FileSystem.cpp | ||
| 3 | // \brief Posix implementation for Os::FileSystem | ||
| 4 | // ====================================================================== | ||
| 5 | #include "Os/Posix/FileSystem.hpp" | ||
| 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 | |||
| 22 | 171 | PosixFileSystem::Status PosixFileSystem::_removeDirectory(const char* path) { | |
| 23 | 171 | Status status = OP_OK; | |
| 24 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 171 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 170 times.
|
171 | if (::rmdir(path) == -1) { |
| 25 | 1 | status = errno_to_filesystem_status(errno); | |
| 26 | } | ||
| 27 | 171 | return status; | |
| 28 | } | ||
| 29 | |||
| 30 | 638 | PosixFileSystem::Status PosixFileSystem::_removeFile(const char* path) { | |
| 31 | 638 | Status status = OP_OK; | |
| 32 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 638 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 638 times.
|
638 | if (::unlink(path) == -1) { |
| 33 | ✗ | status = errno_to_filesystem_status(errno); | |
| 34 | } | ||
| 35 | 638 | return status; | |
| 36 | } | ||
| 37 | |||
| 38 | 146 | PosixFileSystem::Status PosixFileSystem::_rename(const char* originPath, const char* destPath) { | |
| 39 | 146 | Status status = OP_OK; | |
| 40 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 146 times.
|
146 | if (::rename(originPath, destPath) == -1) { |
| 41 | ✗ | status = errno_to_filesystem_status(errno); | |
| 42 | } | ||
| 43 | 146 | return status; | |
| 44 | } | ||
| 45 | |||
| 46 | 122 | PosixFileSystem::Status PosixFileSystem::_getWorkingDirectory(char* path, FwSizeType bufferSize) { | |
| 47 | 122 | Status status = OP_OK; | |
| 48 | 122 | const char* cwd = ::getcwd(path, static_cast<size_t>(bufferSize)); | |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
|
122 | if (cwd == nullptr) { |
| 50 | ✗ | status = errno_to_filesystem_status(errno); | |
| 51 | } | ||
| 52 | 122 | return status; | |
| 53 | } | ||
| 54 | |||
| 55 | 122 | PosixFileSystem::Status PosixFileSystem::_changeWorkingDirectory(const char* path) { | |
| 56 | 122 | Status status = OP_OK; | |
| 57 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 122 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 122 times.
|
122 | if (::chdir(path) == -1) { |
| 58 | ✗ | status = errno_to_filesystem_status(errno); | |
| 59 | } | ||
| 60 | 122 | return status; | |
| 61 | } | ||
| 62 | |||
| 63 | 58 | PosixFileSystem::Status PosixFileSystem::_getFreeSpace(const char* path, | |
| 64 | FwSizeType& totalBytes, | ||
| 65 | FwSizeType& freeBytes) { | ||
| 66 | #ifdef TGT_OS_TYPE_VXWORKS | ||
| 67 | return Status::NOT_SUPPORTED; | ||
| 68 | #else | ||
| 69 | 58 | Status stat = OP_OK; | |
| 70 | static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<fsblkcnt_t>::max(), | ||
| 71 | "FwSizeType must be able to hold fsblkcnt_t"); | ||
| 72 | static_assert(std::numeric_limits<FwSizeType>::min() <= std::numeric_limits<fsblkcnt_t>::min(), | ||
| 73 | "FwSizeType must be able to hold fsblkcnt_t"); | ||
| 74 | static_assert(std::numeric_limits<FwSizeType>::max() >= std::numeric_limits<unsigned long>::max(), | ||
| 75 | "FwSizeType must be able to hold unsigned long"); | ||
| 76 | 58 | struct statvfs fsStat; | |
| 77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | int ret = statvfs(path, &fsStat); |
| 78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (ret) { |
| 79 | ✗ | return errno_to_filesystem_status(errno); | |
| 80 | } | ||
| 81 | |||
| 82 | 58 | const FwSizeType block_size = static_cast<FwSizeType>(fsStat.f_frsize); | |
| 83 | 58 | const FwSizeType free_blocks = static_cast<FwSizeType>(fsStat.f_bfree); | |
| 84 | 58 | const FwSizeType total_blocks = static_cast<FwSizeType>(fsStat.f_blocks); | |
| 85 | |||
| 86 | // Guard against divide-by-zero if filesystem reports invalid block size | ||
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
|
58 | if (block_size == 0) { |
| 88 | ✗ | return OTHER_ERROR; | |
| 89 | } | ||
| 90 | |||
| 91 | // Check for overflow in multiplication | ||
| 92 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 58 times.
|
116 | if (free_blocks > (std::numeric_limits<FwSizeType>::max() / block_size) || |
| 93 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 58 times.
|
58 | total_blocks > (std::numeric_limits<FwSizeType>::max() / block_size)) { |
| 94 | ✗ | return OVERFLOW_ERROR; | |
| 95 | } | ||
| 96 | 58 | freeBytes = free_blocks * block_size; | |
| 97 | 58 | totalBytes = total_blocks * block_size; | |
| 98 | 58 | return stat; | |
| 99 | #endif | ||
| 100 | } | ||
| 101 | |||
| 102 | ✗ | FileSystemHandle* PosixFileSystem::getHandle() { | |
| 103 | ✗ | return &this->m_handle; | |
| 104 | } | ||
| 105 | |||
| 106 | 1912 | PosixFileSystem::Status PosixFileSystem::_getPathType(const char* path, PathType& pathType) { | |
| 107 | 1912 | FW_ASSERT(path != nullptr); | |
| 108 | 1912 | struct stat path_stat; | |
| 109 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1912 times.
|
1912 | const I32 status = lstat(path, &path_stat); |
| 110 |
2/2✓ Branch 0 taken 1219 times.
✓ Branch 1 taken 693 times.
|
1912 | if (status == 0) { |
| 111 |
2/2✓ Branch 0 taken 206 times.
✓ Branch 1 taken 1013 times.
|
1219 | if (S_ISDIR(path_stat.st_mode)) { |
| 112 | 206 | pathType = PathType::DIRECTORY; | |
| 113 |
2/2✓ Branch 0 taken 1012 times.
✓ Branch 1 taken 1 times.
|
1013 | } else if (S_ISREG(path_stat.st_mode)) { |
| 114 | 1012 | pathType = PathType::FILE; | |
| 115 | } else { | ||
| 116 | 1 | pathType = PathType::OTHER; | |
| 117 | } | ||
| 118 | 1219 | return Status::OP_OK; | |
| 119 | } else { | ||
| 120 |
1/1✓ Branch 2 taken 693 times.
|
693 | return errno_to_filesystem_status(errno); |
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | } // namespace FileSystem | ||
| 125 | } // namespace Posix | ||
| 126 | } // namespace Os | ||
| 127 |