GCC Code Coverage Report


Directory: ./
File: Os/Directory.cpp
Date: 2026-09-23 21:11:01
Exec Total Coverage
Lines: 76 92 82.6%
Functions: 10 12 83.3%
Branches: 37 55 67.3%

Line Branch Exec Source
1 // ======================================================================
2 // \title Os/Directory.cpp
3 // \brief common function implementation for Os::Directory
4 // ======================================================================
5 #include <Fw/Types/Assert.hpp>
6 #include <Os/Directory.hpp>
7
8 namespace Os {
9
10 2478 Directory::Directory()
11
3/3
✓ Branch 13 taken 39648 times.
✓ Branch 14 taken 2478 times.
✓ Branch 20 taken 2478 times.
42126 : m_is_open(false), m_handle_storage(), m_delegate(*DirectoryInterface::getDelegate(m_handle_storage)) {
12 2478 FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
13 2478 }
14
15 4958 Directory::~Directory() {
16 4956 FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
17
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 2478 times.
✓ Branch 5 taken 226 times.
✓ Branch 6 taken 2252 times.
4956 if (this->m_is_open) {
18 452 this->close();
19 }
20 4956 this->m_delegate.~DirectoryInterface();
21 4958 }
22
23 // ------------------------------------------------------------
24 // Directory operations delegating to implementation
25 // ------------------------------------------------------------
26 ✗ DirectoryHandle* Directory::getHandle() {
27 ✗ FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
28 ✗ return this->m_delegate.getHandle();
29 }
30
31 743 Directory::Status Directory::open(const char* path, OpenMode mode) {
32 743 FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
33 743 FW_ASSERT(path != nullptr);
34 743 FW_ASSERT(mode >= 0 and mode < OpenMode::MAX_OPEN_MODE);
35 743 Status status = this->m_delegate.open(path, mode);
36
2/2
✓ Branch 0 taken 738 times.
✓ Branch 1 taken 5 times.
743 if (status == Status::OP_OK) {
37 738 this->m_is_open = true;
38 }
39 743 return status;
40 }
41
42 928 bool Directory::isOpen() {
43 928 FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
44
1/2
✗ Branch 3 not taken.
✓ Branch 4 taken 928 times.
928 return this->m_is_open;
45 }
46 915 Directory::Status Directory::rewind() {
47 915 FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
48
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 915 times.
✓ Branch 5 taken 72 times.
✓ Branch 6 taken 843 times.
915 if (not this->m_is_open) {
49 72 return Status::NOT_OPENED;
50 }
51 843 return this->m_delegate.rewind();
52 }
53
54 4066 Directory::Status Directory::read(char* fileNameBuffer, FwSizeType bufSize) {
55 4066 FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
56
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 4066 times.
✓ Branch 5 taken 74 times.
✓ Branch 6 taken 3992 times.
4066 if (not this->m_is_open) {
57 74 return Status::NOT_OPENED;
58 }
59 3992 FW_ASSERT(fileNameBuffer != nullptr);
60 3992 FW_ASSERT(bufSize > 0);
61 3992 Status status = this->m_delegate.read(fileNameBuffer, bufSize);
62 3992 fileNameBuffer[bufSize - 1] = '\0'; // Guarantee null-termination
63 3992 return status;
64 }
65
66 3726 Directory::Status Directory::read(Fw::StringBase& filename) {
67 3726 FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
68
3/4
✗ Branch 3 not taken.
✓ Branch 4 taken 3726 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 3725 times.
3726 if (not this->m_is_open) {
69 1 return Status::NOT_OPENED;
70 }
71 3725 return this->m_delegate.read(const_cast<char*>(filename.toChar()), filename.getCapacity());
72 }
73
74 821 void Directory::close() {
75 821 FW_ASSERT(&this->m_delegate == reinterpret_cast<DirectoryInterface*>(&this->m_handle_storage[0]));
76 821 this->m_is_open = false;
77 821 return this->m_delegate.close();
78 }
79
80 // ------------------------------------------------------------
81 // Common functions built on top of OS-specific functions
82 // ------------------------------------------------------------
83
84 308 Directory::Status Directory::getFileCount(FwSizeType& fileCount) {
85
2/4
✗ Branch 3 not taken.
✓ Branch 4 taken 308 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 308 times.
308 if (not this->m_is_open) {
86 ✗ return Status::NOT_OPENED;
87 }
88 // Rewind to ensure we start from the beginning of the stream
89
2/3
✓ Branch 4 taken 308 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 308 times.
308 if (this->rewind() != Status::OP_OK) {
90 ✗ return Status::OTHER_ERROR;
91 }
92 308 const FwSizeType loopLimit = std::numeric_limits<FwSizeType>::max();
93 308 FwSizeType count = 0;
94 308 char unusedBuffer[1]; // buffer must have size but is unused
95 308 Status readStatus = Status::OP_OK;
96 308 fileCount = 0;
97 // Count files by reading each file entry until there is NO_MORE_FILES
98
1/2
✓ Branch 0 taken 3903 times.
✗ Branch 1 not taken.
3903 for (FwSizeType iter = 0; iter < loopLimit; ++iter) {
99
1/1
✓ Branch 4 taken 3903 times.
3903 readStatus = this->read(unusedBuffer, sizeof(unusedBuffer));
100
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 3595 times.
3903 if (readStatus == Status::NO_MORE_FILES) {
101 308 break;
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3595 times.
3595 } else if (readStatus != Status::OP_OK) {
103 ✗ return Status::OTHER_ERROR;
104 }
105 3595 count++;
106 }
107 308 fileCount = count;
108
2/3
✓ Branch 4 taken 308 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 308 times.
308 if (this->rewind() != Status::OP_OK) {
109 ✗ return Status::OTHER_ERROR;
110 }
111 308 return Status::OP_OK;
112 }
113
114 74 Directory::Status Directory::readDirectory(Fw::ExternalArray<Fw::String>& filenameArray, FwSizeType& filenameCount) {
115 74 FW_ASSERT(filenameArray.getElements() != nullptr);
116 74 FW_ASSERT(filenameArray.getSize() > 0);
117
2/4
✗ Branch 3 not taken.
✓ Branch 4 taken 74 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 74 times.
74 if (not this->m_is_open) {
118 ✗ return Status::NOT_OPENED;
119 }
120 // Rewind to ensure we start reading from the beginning of the stream
121
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 74 times.
74 if (this->rewind() != Status::OP_OK) {
122 ✗ return Status::OTHER_ERROR;
123 }
124
125 74 Status readStatus = Status::OP_OK;
126 74 Status returnStatus = Status::OP_OK;
127 FwSizeType index;
128 74 filenameCount = 0;
129 // Iterate through the directory and read the filenames into the array
130
2/2
✓ Branch 2 taken 296 times.
✓ Branch 3 taken 74 times.
370 for (index = 0; index < filenameArray.getSize(); index++) {
131 296 readStatus = this->read(filenameArray[index]);
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 296 times.
296 if (readStatus == Status::NO_MORE_FILES) {
133 ✗ break;
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 296 times.
296 } else if (readStatus != Status::OP_OK) {
135 ✗ return Status::OTHER_ERROR;
136 }
137 }
138 74 filenameCount = index;
139
140
1/2
✗ Branch 4 not taken.
✓ Branch 5 taken 74 times.
74 if (this->rewind() != Status::OP_OK) {
141 ✗ return Status::OTHER_ERROR;
142 }
143
144 74 return returnStatus;
145 }
146
147 ✗ Directory::Status Directory::readDirectory(Fw::String filenameArray[],
148 const FwSizeType filenameArraySize,
149 FwSizeType& filenameCount) {
150 ✗ Fw::ExternalArray<Fw::String> array(filenameArray, filenameArraySize);
151 ✗ return this->readDirectory(array, filenameCount);
152 ✗ }
153
154 } // namespace Os
155