GCC Code Coverage Report


Directory: Os/
File: Directory.hpp
Date: 2026-09-03 21:15:10
Exec Total Coverage
Lines: 2 2 100.0%
Functions: 2 2 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 // ======================================================================
2 // \title Os/Directory.hpp
3 // \brief Os::Directory interface definition
4 // ======================================================================
5
6 #ifndef _OS_DIRECTORY_HPP_
7 #define _OS_DIRECTORY_HPP_
8
9 #include <Fw/DataStructures/ExternalArray.hpp>
10 #include <Fw/Deprecate.hpp>
11 #include <Fw/FPrimeBasicTypes.hpp>
12 #include <Fw/Types/String.hpp>
13 #include <Os/Os.hpp>
14
15 namespace Os {
16
17 struct DirectoryHandle {};
18
19 class DirectoryInterface {
20 public:
21 enum Status {
22 OP_OK, //!< Operation was successful
23 DOESNT_EXIST, //!< Directory doesn't exist
24 NO_PERMISSION, //!< No permission to read directory
25 NOT_OPENED, //!< Directory hasn't been opened yet
26 NOT_DIR, //!< Path is not a directory
27 NO_MORE_FILES, //!< Directory stream has no more files
28 FILE_LIMIT, //!< Directory has more files than can be read
29 BAD_DESCRIPTOR, //!< Directory stream descriptor is invalid
30 ALREADY_EXISTS, //!< Directory already exists
31 NOT_SUPPORTED, //!< Operation is not supported by the current implementation
32 OTHER_ERROR, //!< A catch-all for other errors. Have to look in implementation-specific code
33 };
34
35 enum OpenMode {
36 READ, //!< Error if directory doesn't exist
37 CREATE_IF_MISSING, //!< Create directory if it doesn't exist
38 CREATE_EXCLUSIVE, //!< Create directory and error if it already exists
39 MAX_OPEN_MODE //!< Maximum value of OpenMode
40 };
41
42 //! \brief default constructor
43 18 DirectoryInterface() = default;
44
45 //! \brief default virtual destructor
46 36 virtual ~DirectoryInterface() = default;
47
48 //! \brief copy constructor is forbidden
49 DirectoryInterface(const DirectoryInterface& other) = delete;
50
51 //! \brief assignment operator is forbidden
52 DirectoryInterface& operator=(const DirectoryInterface& other) = delete;
53
54 //! \brief return the underlying Directory handle (implementation specific)
55 //! \return internal Directory handle representation
56 virtual DirectoryHandle* getHandle() = 0;
57
58 //! \brief provide a pointer to a Directory delegate object
59 static DirectoryInterface* getDelegate(DirectoryHandleStorage& aligned_new_memory);
60
61 // -----------------------------------------------------------------
62 // Directory operations to be implemented by an OSAL implementation
63 // -----------------------------------------------------------------
64 // These functions are to be overridden in each OS implementation
65 // See an example in in Os/Posix/Directory.hpp
66
67 //! \brief Open or create a directory
68 //!
69 //! Using the path provided, this function will open or create a directory.
70 //! Use OpenMode::READ to open an existing directory and error if the directory is not found
71 //! Use OpenMode::CREATE_IF_MISSING to open a directory, creating the directory if it doesn't exist
72 //! Use OpenMode::CREATE_EXCLUSIVE to open a directory, creating the directory and erroring if it already exists
73 //!
74 //! It is invalid to pass `nullptr` as the path.
75 //! It is invalid to supply `mode` as a non-enumerated value.
76 //!
77 //! \param path: path of directory to open
78 //! \param mode: enum (READ, CREATE_IF_MISSING, CREATE_EXCLUSIVE). See notes above for more information
79 //! \return status of the operation
80 virtual Status open(const char* path, OpenMode mode) = 0;
81
82 //! \brief Rewind directory stream
83 //!
84 //! Each read operation moves the seek position forward. This function resets the seek position to the beginning.
85 //!
86 //! \return status of the operation
87 virtual Status rewind() = 0;
88
89 //! \brief Get next filename from directory stream
90 //!
91 //! Write at most buffSize characters of the file name to fileNameBuffer and guarantee null-termination.
92 //! This function skips the current directory (.) and parent directory (..) entries.
93 //! Returns NO_MORE_FILES if there are no more files to read from the buffer.
94 //!
95 //! It is invalid to pass `nullptr` as fileNameBuffer.
96 //!
97 //! \param fileNameBuffer: buffer to store filename
98 //! \param buffSize: size of fileNameBuffer
99 //! \return status of the operation
100 virtual Status read(char* fileNameBuffer, FwSizeType buffSize) = 0;
101
102 //! \brief Get next filename from directory stream and write it to a Fw::StringBase object
103 //!
104 //! \param filename: Fw::StringBase (or derived) object to store filename in
105 //! \return status of the operation
106 // virtual Status read(Fw::StringBase& filename) = 0;
107
108 //! \brief Close directory
109 virtual void close() = 0;
110 };
111
112 //! \brief Directory class
113 //!
114 //! This class provides a common interface for directory operations, such as reading files in a directory
115 //! and getting the number of files in a directory.
116 class Directory final : public DirectoryInterface {
117 public:
118 //! \brief Constructor
119 Directory();
120
121 //! \brief Destructor
122 //!
123 //! Destructor will close the Directory if it is open
124 ~Directory() final;
125
126 //! \brief return the underlying Directory handle (implementation specific)
127 //! \return internal Directory handle representation
128 DirectoryHandle* getHandle() override;
129
130 // ------------------------------------------------------------
131 // Implementation-specific Directory member functions
132 // ------------------------------------------------------------
133 // These functions are overridden in each OS implementation (e.g. in Os/Posix/Directory.hpp)
134
135 //! \brief Open or create a directory
136 //!
137 //! Using the path provided, this function will open or create a directory.
138 //! Use OpenMode::READ to open an existing directory and error if the directory is not found
139 //! Use OpenMode::CREATE_IF_MISSING to open a directory, creating the directory if it doesn't exist
140 //! Use OpenMode::CREATE_EXCLUSIVE to open a directory, creating the directory and erroring if it already exists
141 //!
142 //! It is invalid to pass `nullptr` as the path.
143 //! It is invalid to supply `mode` as a non-enumerated value.
144 //!
145 //! \param path: path of directory to open
146 //! \param mode: enum (READ, CREATE_IF_MISSING, CREATE_EXCLUSIVE). See notes above for more information
147 //! \return status of the operation
148 Status open(const char* path, OpenMode mode) override;
149
150 //! \brief Check if Directory is open or not
151 //! \return true if Directory is open, false otherwise
152 bool isOpen();
153
154 //! \brief Rewind directory stream
155 //!
156 //! Each read operation moves the seek position forward. This function resets the seek position to the beginning.
157 //!
158 //! \return status of the operation
159 Status rewind() override;
160
161 //! \brief Get next filename from directory stream
162 //!
163 //! Write at most buffSize characters of the file name to fileNameBuffer and guarantee null-termination.
164 //! This function skips the current directory (.) and parent directory (..) entries.
165 //! Returns NO_MORE_FILES if there are no more files to read from the buffer.
166 //!
167 //! It is invalid to pass `nullptr` as fileNameBuffer.
168 //!
169 //! \param fileNameBuffer: buffer to store filename
170 //! \param buffSize: size of fileNameBuffer
171 //! \return status of the operation
172 Status read(char* fileNameBuffer, FwSizeType buffSize) override;
173
174 //! \brief Close directory
175 void close() override;
176
177 // ------------------------------------------------------------
178 // Common functions built on top of OS-specific functions
179 // ------------------------------------------------------------
180
181 //! \brief Get next filename from directory stream and write it to a Fw::StringBase object
182 //!
183 //! \param filename: Fw::StringBase (or derived) object to store filename in
184 //! \return status of the operation
185 Status read(Fw::StringBase& filename);
186
187 //! \brief Read the contents of the directory and store filenames in the supplied array.
188 //!
189 //! Reads at most filenameArray.getSize() filenames.
190 //! The function first rewinds the directory stream to ensure reading starts from the beginning.
191 //! After reading, it rewinds the directory stream again, resetting seek position to beginning.
192 //!
193 //! \param filenameArray: array to store filenames
194 //! \param filenameCount: number of filenames written to filenameArray (output)
195 //! \return status of the operation
196 Status readDirectory(Fw::ExternalArray<Fw::String>& filenameArray, FwSizeType& filenameCount);
197
198 //! \brief Read the contents of the directory and store filenames in filenameArray of size arraySize.
199 //!
200 //! The function first rewinds the directory stream to ensure reading starts from the beginning.
201 //! After reading, it rewinds the directory stream again, resetting seek position to beginning.
202 //!
203 //! \param filenameArray: array to store filenames
204 //! \param arraySize: size of filenameArray
205 //! \param filenameCount: number of filenames written to filenameArray (output)
206 //! \return status of the operation
207 DEPRECATED(Status readDirectory(Fw::String filenameArray[], const FwSizeType arraySize, FwSizeType& filenameCount),
208 "Use readDirectory(Fw::ExternalArray<Fw::String>& filenameArray, FwSizeType& filenameCount) instead");
209
210 //! \brief Get the number of files in the directory.
211 //!
212 //! Counts the number of files in the directory by reading each file entry and writing the count to fileCount.
213 //!
214 //! The function first rewinds the directory stream to ensure counting starts from the beginning.
215 //! After counting, it rewinds the directory stream again, resetting seek position to beginning.
216 //!
217 //! \param fileCount Reference to a variable where the file count will be stored.
218 //! \return Status indicating the result of the operation.
219 Status getFileCount(FwSizeType& fileCount);
220
221 private:
222 bool m_is_open; //!< Flag indicating if the directory has been open
223
224 private:
225 // This section is used to store the implementation-defined Directory handle. To Os::Directory and fprime, this type
226 // is opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
227 // the byte-array here and set `handle` to that address for storage.
228 alignas(FW_HANDLE_ALIGNMENT) DirectoryHandleStorage m_handle_storage; //!< Directory handle storage
229 DirectoryInterface& m_delegate;
230 };
231
232 } // namespace Os
233
234 #endif
235