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