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/Types/String.hpp>
11 #include <Os/Os.hpp>
12 
13 namespace Os {
14 
15 struct DirectoryHandle {};
16 
18  public:
19  enum Status {
31  };
32 
33  enum OpenMode {
34  READ,
38  };
39 
41  DirectoryInterface() = default;
42 
44  virtual ~DirectoryInterface() = default;
45 
47  DirectoryInterface(const DirectoryInterface& other) = delete;
48 
50  DirectoryInterface& operator=(const DirectoryInterface& other) = delete;
51 
54  virtual DirectoryHandle* getHandle() = 0;
55 
57  static DirectoryInterface* getDelegate(DirectoryHandleStorage& aligned_new_memory);
58 
59  // -----------------------------------------------------------------
60  // Directory operations to be implemented by an OSAL implementation
61  // -----------------------------------------------------------------
62  // These functions are to be overridden in each OS implementation
63  // See an example in in Os/Posix/Directory.hpp
64 
78  virtual Status open(const char* path, OpenMode mode) = 0;
79 
85  virtual Status rewind() = 0;
86 
98  virtual Status read(char* fileNameBuffer, FwSizeType buffSize) = 0;
99 
104  // virtual Status read(Fw::StringBase& filename) = 0;
105 
107  virtual void close() = 0;
108 };
109 
114 class Directory final : public DirectoryInterface {
115  public:
117  Directory();
118 
122  ~Directory() final;
123 
126  DirectoryHandle* getHandle() override;
127 
128  // ------------------------------------------------------------
129  // Implementation-specific Directory member functions
130  // ------------------------------------------------------------
131  // These functions are overridden in each OS implementation (e.g. in Os/Posix/Directory.hpp)
132 
146  Status open(const char* path, OpenMode mode) override;
147 
150  bool isOpen();
151 
157  Status rewind() override;
158 
170  Status read(char* fileNameBuffer, FwSizeType buffSize) override;
171 
173  void close() override;
174 
175  // ------------------------------------------------------------
176  // Common functions built on top of OS-specific functions
177  // ------------------------------------------------------------
178 
183  Status read(Fw::StringBase& filename);
184 
194  Status readDirectory(Fw::String filenameArray[], const FwSizeType arraySize, FwSizeType& filenameCount);
195 
205  Status getFileCount(FwSizeType& fileCount);
206 
207  private:
208  bool m_is_open;
209 
210  private:
211  // This section is used to store the implementation-defined Directory handle. To Os::Directory and fprime, this type
212  // is opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
213  // the byte-array here and set `handle` to that address for storage.
214  alignas(FW_HANDLE_ALIGNMENT) DirectoryHandleStorage m_handle_storage;
215  DirectoryInterface& m_delegate;
216 };
217 
218 } // namespace Os
219 
220 #endif
Status readDirectory(Fw::String filenameArray[], const FwSizeType arraySize, FwSizeType &filenameCount)
Read the contents of the directory and store filenames in filenameArray of size arraySize.
Definition: Directory.cpp:113
Directory has more files than can be read.
Definition: Directory.hpp:26
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:21
DirectoryInterface()=default
default constructor
Status rewind() override
Rewind directory stream.
Definition: Directory.cpp:46
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:322
Directory hasn&#39;t been opened yet.
Definition: Directory.hpp:23
Directory()
Constructor.
Definition: Directory.cpp:10
A catch-all for other errors. Have to look in implementation-specific code.
Definition: Directory.hpp:30
No permission to read directory.
Definition: Directory.hpp:22
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:35
Directory stream has no more files.
Definition: Directory.hpp:25
DirectoryInterface & operator=(const DirectoryInterface &other)=delete
assignment operator is forbidden
Directory class.
Definition: Directory.hpp:114
Create directory and error if it already exists.
Definition: Directory.hpp:36
Maximum value of OpenMode.
Definition: Directory.hpp:37
Directory stream descriptor is invalid.
Definition: Directory.hpp:27
Operation was successful.
Definition: Directory.hpp:20
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:28
virtual Status rewind()=0
Rewind directory stream.
Operation is not supported by the current implementation.
Definition: Directory.hpp:29
~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:34
Path is not a directory.
Definition: Directory.hpp:24