| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title Os/Posix/Directory.cpp | ||
| 3 | // \brief Posix implementation for Os::Directory | ||
| 4 | // ====================================================================== | ||
| 5 | #include <sys/stat.h> | ||
| 6 | #include <cerrno> | ||
| 7 | #include <cstring> | ||
| 8 | |||
| 9 | #include <Fw/Types/Assert.hpp> | ||
| 10 | #include <Fw/Types/StringUtils.hpp> | ||
| 11 | #include <Os/Posix/Directory.hpp> | ||
| 12 | #include <Os/Posix/error.hpp> | ||
| 13 | |||
| 14 | namespace Os { | ||
| 15 | namespace Posix { | ||
| 16 | namespace Directory { | ||
| 17 | |||
| 18 | 155 | PosixDirectory::PosixDirectory() : Os::DirectoryInterface(), m_handle() {} | |
| 19 | |||
| 20 | ✗ | DirectoryHandle* PosixDirectory::getHandle() { | |
| 21 | ✗ | return &this->m_handle; | |
| 22 | } | ||
| 23 | |||
| 24 | 252 | PosixDirectory::Status PosixDirectory::open(const char* path, OpenMode mode) { | |
| 25 | 252 | Status status = Status::OP_OK; | |
| 26 | |||
| 27 | // If one of the CREATE mode, attempt to create the directory | ||
| 28 |
4/4✓ Branch 0 taken 251 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 214 times.
✓ Branch 3 taken 37 times.
|
252 | if (mode == OpenMode::CREATE_EXCLUSIVE || mode == OpenMode::CREATE_IF_MISSING) { |
| 29 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 215 times.
✓ Branch 3 taken 45 times.
✓ Branch 4 taken 170 times.
|
215 | if (::mkdir(path, S_IRWXU) == -1) { |
| 30 | 45 | status = errno_to_directory_status(errno); | |
| 31 | // If error is not ALREADY_EXISTS, return the error | ||
| 32 | // If any error and mode CREATE_EXCLUSIVE, return the error | ||
| 33 | // Else, we keep going with OP_OK | ||
| 34 |
3/4✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 44 times.
|
45 | if (status != Status::ALREADY_EXISTS || mode == OpenMode::CREATE_EXCLUSIVE) { |
| 35 | 1 | return status; | |
| 36 | } else { | ||
| 37 | 44 | status = Status::OP_OK; | |
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 251 times.
|
251 | DIR* dir = ::opendir(path); |
| 43 | |||
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 251 times.
|
251 | if (dir == nullptr) { |
| 45 | ✗ | status = errno_to_directory_status(errno); | |
| 46 | } | ||
| 47 | |||
| 48 | 251 | this->m_handle.m_dir_descriptor = dir; | |
| 49 | 251 | return status; | |
| 50 | } | ||
| 51 | |||
| 52 | 437 | PosixDirectory::Status PosixDirectory::rewind() { | |
| 53 | 437 | Status status = Status::OP_OK; | |
| 54 | // no errors defined in man page for rewinddir | ||
| 55 |
1/2✗ Branch 4 not taken.
✓ Branch 5 taken 437 times.
|
437 | ::rewinddir(this->m_handle.m_dir_descriptor); |
| 56 | 437 | return status; | |
| 57 | } | ||
| 58 | |||
| 59 | 922 | PosixDirectory::Status PosixDirectory::read(char* fileNameBuffer, FwSizeType bufSize) { | |
| 60 | 922 | FW_ASSERT(fileNameBuffer != nullptr); | |
| 61 | |||
| 62 | 922 | Status status = Status::OP_OK; | |
| 63 | |||
| 64 | // Set errno to 0 so we know why we exited readdir | ||
| 65 | // This is recommended by the manual pages (man 3 readdir) | ||
| 66 | 922 | errno = 0; | |
| 67 | |||
| 68 |
1/2✗ Branch 4 not taken.
✓ Branch 5 taken 922 times.
|
922 | struct dirent* direntData = ::readdir(this->m_handle.m_dir_descriptor); |
| 69 |
2/2✓ Branch 0 taken 1310 times.
✓ Branch 1 taken 93 times.
|
1403 | while (direntData != nullptr) { |
| 70 | // Skip . and .. directory entries | ||
| 71 |
4/4✓ Branch 2 taken 481 times.
✓ Branch 3 taken 829 times.
✓ Branch 6 taken 206 times.
✓ Branch 7 taken 275 times.
|
1310 | if ((direntData->d_name[0] == '.' and direntData->d_name[1] == '\0') or |
| 72 |
4/6✓ Branch 2 taken 206 times.
✓ Branch 3 taken 829 times.
✓ Branch 6 taken 206 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 206 times.
✗ Branch 11 not taken.
|
1035 | (direntData->d_name[0] == '.' and direntData->d_name[1] == '.' and direntData->d_name[2] == '\0')) { |
| 73 |
1/2✗ Branch 4 not taken.
✓ Branch 5 taken 481 times.
|
481 | direntData = ::readdir(this->m_handle.m_dir_descriptor); |
| 74 | } else { | ||
| 75 | 829 | (void)Fw::StringUtils::string_copy(fileNameBuffer, direntData->d_name, bufSize); | |
| 76 | 829 | break; | |
| 77 | } | ||
| 78 | } | ||
| 79 |
2/2✓ Branch 0 taken 93 times.
✓ Branch 1 taken 829 times.
|
922 | if (direntData == nullptr) { |
| 80 | // loop ended because readdir failed, did it error or did we run out of files? | ||
| 81 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 93 times.
|
93 | if (errno != 0) { |
| 82 | // Only error from readdir is EBADF | ||
| 83 | ✗ | status = Status::BAD_DESCRIPTOR; | |
| 84 | } else { | ||
| 85 | 93 | status = Status::NO_MORE_FILES; | |
| 86 | } | ||
| 87 | } | ||
| 88 | 922 | return status; | |
| 89 | } | ||
| 90 | |||
| 91 | 333 | void PosixDirectory::close() { | |
| 92 | // ::closedir errors if dir descriptor is nullptr | ||
| 93 |
2/2✓ Branch 4 taken 251 times.
✓ Branch 5 taken 82 times.
|
333 | if (this->m_handle.m_dir_descriptor != nullptr) { |
| 94 |
1/2✗ Branch 4 not taken.
✓ Branch 5 taken 251 times.
|
251 | (void)::closedir(this->m_handle.m_dir_descriptor); |
| 95 | } | ||
| 96 | 333 | this->m_handle.m_dir_descriptor = nullptr; | |
| 97 | 333 | } | |
| 98 | |||
| 99 | } // namespace Directory | ||
| 100 | } // namespace Posix | ||
| 101 | } // namespace Os | ||
| 102 |