F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Directory.hpp
Go to the documentation of this file.
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 
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 
20  public:
21  enum Status {
33  };
34 
35  enum OpenMode {
36  READ,
40  };
41 
43  DirectoryInterface() = default;
44 
46  virtual ~DirectoryInterface() = default;
47 
49  DirectoryInterface(const DirectoryInterface& other) = delete;
50 
52  DirectoryInterface& operator=(const DirectoryInterface& other) = delete;
53 
56  virtual DirectoryHandle* getHandle() = 0;
57 
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 
80  virtual Status open(const char* path, OpenMode mode) = 0;
81 
87  virtual Status rewind() = 0;
88 
100  virtual Status read(char* fileNameBuffer, FwSizeType buffSize) = 0;
101 
106  // virtual Status read(Fw::StringBase& filename) = 0;
107 
109  virtual void close() = 0;
110 };
111 
116 class Directory final : public DirectoryInterface {
117  public:
119  Directory();
120 
124  ~Directory() final;
125 
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 
148  Status open(const char* path, OpenMode mode) override;
149 
152  bool isOpen();
153 
159  Status rewind() override;
160 
172  Status read(char* fileNameBuffer, FwSizeType buffSize) override;
173 
175  void close() override;
176 
177  // ------------------------------------------------------------
178  // Common functions built on top of OS-specific functions
179  // ------------------------------------------------------------
180 
185  Status read(Fw::StringBase& filename);
186 
196  Status readDirectory(Fw::ExternalArray<Fw::String>& filenameArray, FwSizeType& filenameCount);
197 
207  DEPRECATED(Status readDirectory(Fw::String filenameArray[], const FwSizeType arraySize, FwSizeType& filenameCount),
208  "Use readDirectory(Fw::ExternalArray<Fw::String>& filenameArray, FwSizeType& filenameCount) instead");
209 
219  Status getFileCount(FwSizeType& fileCount);
220 
221  private:
222  bool m_is_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;
229  DirectoryInterface& m_delegate;
230 };
231 
232 } // namespace Os
233 
234 #endif
Directory has more files than can be read.
Definition: Directory.hpp:28
virtual DirectoryHandle * getHandle()=0
return the underlying Directory handle (implementation specific)
virtual ~DirectoryInterface()=default
default virtual destructor
U8 DirectoryHandleStorage[FW_DIRECTORY_HANDLE_MAX_SIZE]
Definition: Os.hpp:15
virtual Status read(char *fileNameBuffer, FwSizeType buffSize)=0
Get next filename from directory stream.
PlatformSizeType FwSizeType
Directory doesn&#39;t exist.
Definition: Directory.hpp:23
DirectoryInterface()=default
default constructor
Status rewind() override
Rewind directory stream.
Definition: Directory.cpp:46
Directory hasn&#39;t been opened yet.
Definition: Directory.hpp:25
Directory()
Constructor.
Definition: Directory.cpp:10
A catch-all for other errors. Have to look in implementation-specific code.
Definition: Directory.hpp:32
No permission to read directory.
Definition: Directory.hpp:24
Status getFileCount(FwSizeType &fileCount)
Get the number of files in the directory.
Definition: Directory.cpp:83
void close() override
Close directory.
Definition: Directory.cpp:73
Status read(char *fileNameBuffer, FwSizeType buffSize) override
Get next filename from directory stream.
Definition: Directory.cpp:54
Create directory if it doesn&#39;t exist.
Definition: Directory.hpp:37
DEPRECATED(Status readDirectory(Fw::String filenameArray[], const FwSizeType arraySize, FwSizeType &filenameCount), "Use readDirectory(Fw::ExternalArray<Fw::String>& filenameArray, FwSizeType& filenameCount) instead")
Read the contents of the directory and store filenames in filenameArray of size arraySize.
Directory stream has no more files.
Definition: Directory.hpp:27
DirectoryInterface & operator=(const DirectoryInterface &other)=delete
assignment operator is forbidden
Directory class.
Definition: Directory.hpp:116
Create directory and error if it already exists.
Definition: Directory.hpp:38
Maximum value of OpenMode.
Definition: Directory.hpp:39
Status readDirectory(Fw::ExternalArray< Fw::String > &filenameArray, FwSizeType &filenameCount)
Read the contents of the directory and store filenames in the supplied array.
Definition: Directory.cpp:113
Directory stream descriptor is invalid.
Definition: Directory.hpp:29
Operation was successful.
Definition: Directory.hpp:22
bool isOpen()
Check if Directory is open or not.
Definition: Directory.cpp:42
Status open(const char *path, OpenMode mode) override
Open or create a directory.
Definition: Directory.cpp:31
virtual Status open(const char *path, OpenMode mode)=0
Open or create a directory.
DirectoryHandle * getHandle() override
return the underlying Directory handle (implementation specific)
Definition: Directory.cpp:26
static DirectoryInterface * getDelegate(DirectoryHandleStorage &aligned_new_memory)
provide a pointer to a Directory delegate object
Definition: DefaultFile.cpp:20
Directory already exists.
Definition: Directory.hpp:30
virtual Status rewind()=0
Rewind directory stream.
Operation is not supported by the current implementation.
Definition: Directory.hpp:31
~Directory() final
Destructor.
Definition: Directory.cpp:15
virtual void close()=0
Get next filename from directory stream and write it to a Fw::StringBase object.
Error if directory doesn&#39;t exist.
Definition: Directory.hpp:36
Path is not a directory.
Definition: Directory.hpp:26