F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
Directory.cpp
Go to the documentation of this file.
1 #include <FpConfig.hpp>
2 #include <Os/Directory.hpp>
3 #include <Fw/Types/Assert.hpp>
4 
5 #include <dirent.h>
6 #include <cerrno>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 
12 #include <cstring>
13 
14 namespace Os {
15 
16  Directory::Directory() :m_dir(0),m_lastError(0) {
17  }
18 
20  this->close();
21  }
22 
23  Directory::Status Directory::open(const char* dirName) {
24 
25  Status stat = OP_OK;
26 
27  DIR* dir = ::opendir(dirName);
28 
29  if (dir == nullptr) {
30  this->m_lastError = errno;
31  switch (errno) {
32  case ENOENT:
33  stat = DOESNT_EXIST;
34  break;
35  case EACCES:
36  stat = NO_PERMISSION;
37  break;
38  case ENOTDIR:
39  stat = NOT_DIR;
40  break;
41  default:
42  stat = OTHER_ERROR;
43  break;
44  }
45  return stat;
46  }
47 
48  this->m_dir = reinterpret_cast<POINTER_CAST>(dir);
49  return stat;
50  }
51 
53 
54  // make sure it has been opened
55  if (!this->isOpen()) {
56  return NOT_OPENED;
57  }
58 
59  Status stat = OP_OK;
60  DIR* dir = reinterpret_cast<DIR*>(this->m_dir);
61 
62  // no errors defined
63  ::rewinddir(dir);
64 
65  return stat;
66  }
67 
68  Directory::Status Directory::read(char * fileNameBuffer, U32 bufSize) {
69 
70  I64 dummy;
71  return this->read(fileNameBuffer, bufSize, dummy);
72 
73  }
74 
75  Directory::Status Directory::read(char * fileNameBuffer, U32 bufSize, I64& inode) {
76 
77  FW_ASSERT(fileNameBuffer);
78 
79  // make sure it has been opened
80  if (!this->isOpen()) {
81  return NOT_OPENED;
82  }
83 
84  Status stat = OP_OK;
85  DIR* dir = reinterpret_cast<DIR*>(this->m_dir);
86 
87  // Set errno to 0 so we know why we exited readdir
88  errno = 0;
89 
90  struct dirent *direntData = nullptr;
91  while ((direntData = ::readdir(dir)) != nullptr) {
92  // Skip hidden files
93  if (direntData->d_name[0] != '.') {
94  strncpy(fileNameBuffer, direntData->d_name, bufSize);
95  inode = static_cast<I64>(direntData->d_ino);
96  break;
97  }
98  }
99 
100  if (direntData == nullptr) {
101  // loop ended because readdir failed, did it error or did we run out of files?
102  if(errno != 0) {
103  // Only error from readdir is EBADF
104  stat = OTHER_ERROR;
105  this->m_lastError = errno;
106  }
107  else {
108  stat = NO_MORE_FILES;
109  }
110  }
111 
112  return stat;
113  }
114 
116  return this->m_dir > 0;
117  }
118 
120  if (this->isOpen()) {
121  DIR* dir = reinterpret_cast<DIR*>(this->m_dir);
122  (void)::closedir(dir);
123  }
124  this->m_dir = 0;
125  }
126 
128  return this->m_lastError;
129  }
130 
132  return strerror(this->m_lastError);
133  }
134 
135 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformPointerCastType POINTER_CAST
Definition: BasicTypes.h:53
PlatformIntType NATIVE_INT_TYPE
Definition: BasicTypes.h:51
C++-compatible configuration header for fprime configuration.
@ OP_OK
Operation was successful.
Definition: Directory.hpp:14
@ OTHER_ERROR
A catch-all for other errors. Have to look in implementation-specific code.
Definition: Directory.hpp:20
@ NO_PERMISSION
No permission to read directory.
Definition: Directory.hpp:16
@ NO_MORE_FILES
Directory stream has no more files.
Definition: Directory.hpp:19
@ NOT_DIR
Path is not a directory.
Definition: Directory.hpp:18
@ DOESNT_EXIST
Directory doesn't exist.
Definition: Directory.hpp:15
@ NOT_OPENED
Directory hasn't been opened yet.
Definition: Directory.hpp:17
bool isOpen()
check if file descriptor is open or not.
Definition: Directory.cpp:115
void close()
close directory
Definition: Directory.cpp:119
Status rewind()
rewind directory stream to the beginning
Definition: Directory.cpp:52
Status read(char *fileNameBuffer, U32 bufSize)
get next filename from directory
Definition: Directory.cpp:68
const char * getLastErrorString()
get a string of the last error (typically from strerror)
Definition: Directory.cpp:131
Status open(const char *dirName)
open directory. Directory must already exist
Definition: Directory.cpp:23
NATIVE_INT_TYPE getLastError()
read back last error code (typically errno)
Definition: Directory.cpp:127
Directory()
Constructor.
Definition: Directory.cpp:16
virtual ~Directory()
Destructor. Will close directory if still open.
Definition: Directory.cpp:19