29
30
#include <sys/stat.h>
30
31
#include <dirent.h>
33
36
#include <unistd.h>
38
41
* Implementation of the Stella file system API based on POSIX (for Linux and OSX)
43
* Parts of this class are documented in the base interface class, AbstractFilesystemNode.
41
45
class POSIXFilesystemNode : public AbstractFilesystemNode
49
* Creates a POSIXFilesystemNode with the root node as path.
44
51
POSIXFilesystemNode();
45
POSIXFilesystemNode(const string& path);
46
POSIXFilesystemNode(const POSIXFilesystemNode* node);
48
virtual string displayName() const { return _displayName; }
49
virtual bool isValid() const { return _isValid; }
54
* Creates a POSIXFilesystemNode for a given path.
56
* @param path String with the path the new node should point to.
57
* @param verify true if the isValid and isDirectory flags should be verified during the construction.
59
POSIXFilesystemNode(const string& path, bool verify);
61
virtual bool exists() const { return access(_path.c_str(), F_OK) == 0; }
62
virtual string getDisplayName() const { return _displayName; }
63
virtual string getName() const { return _displayName; }
64
virtual string getPath() const { return _path; }
65
virtual string getRelativePath() const;
50
66
virtual bool isDirectory() const { return _isDirectory; }
51
virtual string path() const { return _path; }
67
virtual bool isReadable() const { return access(_path.c_str(), R_OK) == 0; }
68
virtual bool isWritable() const { return access(_path.c_str(), W_OK) == 0; }
53
virtual FSList listDir(ListMode mode = kListDirectoriesOnly) const;
54
virtual AbstractFilesystemNode* parent() const;
70
virtual bool getChildren(AbstractFSList& list, ListMode mode, bool hidden) const;
71
virtual AbstractFilesystemNode* getParent() const;
57
74
string _displayName;
81
* Tests and sets the _isValid and _isDirectory flags, using the stat() function.
83
virtual void setFlags();
87
* Returns the last component of a given path.
90
* /foo/bar.txt would return /bar.txt
91
* /foo/bar/ would return /bar/
93
* @param str String containing the path.
94
* @return Pointer to the first char of the last component inside str.
63
96
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
64
static const char* lastPathComponent(const string& str)
97
const char* lastPathComponent(const string& str)
66
102
const char *start = str.c_str();
67
103
const char *cur = start + str.size() - 2;
69
while (cur > start && *cur != '/')
105
while (cur >= start && *cur != '/')
75
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
76
static string validatePath(const string& p)
79
if(p.size() <= 0 || p[0] != '/')
85
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86
AbstractFilesystemNode* FilesystemNode::getRoot()
88
return new POSIXFilesystemNode();
91
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
92
AbstractFilesystemNode* FilesystemNode::getNodeForPath(const string& path)
94
return new POSIXFilesystemNode(validatePath(path));
111
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112
void POSIXFilesystemNode::setFlags()
116
_isValid = (0 == stat(_path.c_str(), &st));
117
_isDirectory = _isValid ? S_ISDIR(st.st_mode) : false;
97
120
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
98
121
POSIXFilesystemNode::POSIXFilesystemNode()
100
char buf[MAXPATHLEN];
101
getcwd(buf, MAXPATHLEN);
104
_displayName = lastPathComponent(_path);
125
_displayName = _path;
107
127
_isDirectory = true;
110
130
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
111
POSIXFilesystemNode::POSIXFilesystemNode(const string& p)
113
string path = validatePath(p);
115
Int32 len = 0, offset = path.size();
120
// Extract last component from path
121
const char *str = path.c_str();
122
while (offset > 0 && str[offset-1] == '/')
124
while (offset > 0 && str[offset-1] != '/')
129
_displayName = string(str + offset, len);
131
// Check whether it is a directory, and whether the file actually exists
132
_isValid = (0 == stat(_path.c_str(), &st));
133
_isDirectory = S_ISDIR(st.st_mode);
136
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137
POSIXFilesystemNode::POSIXFilesystemNode(const POSIXFilesystemNode* node)
139
_displayName = node->_displayName;
140
_isValid = node->_isValid;
141
_isDirectory = node->_isDirectory;
145
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
146
FSList POSIXFilesystemNode::listDir(ListMode mode) const
131
POSIXFilesystemNode::POSIXFilesystemNode(const string& p, bool verify)
133
// Expand "~/" to the value of the HOME env variable
134
if ( p.length() >= 2 && p[0] == '~' && p[1] == '/')
136
const char *home = getenv("HOME");
137
if (home != NULL && strlen(home) < MAXPATHLEN)
140
// Skip over the tilde. We know that p contains at least
141
// two chars, so this is safe:
142
_path += p.c_str() + 1;
145
// Expand "./" to the current directory
146
else if ( p.length() >= 2 && p[0] == '.' && p[1] == '/')
148
char buf[MAXPATHLEN];
149
char* ret = getcwd(buf, MAXPATHLEN);
153
// Skip over the tilda. We know that p contains at least
154
// two chars, so this is safe:
155
_path += p.c_str() + 1;
161
_displayName = lastPathComponent(_path);
167
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
168
string POSIXFilesystemNode::getRelativePath() const
170
// If the path starts with the home directory, replace it with '~'
171
const char* home = getenv("HOME");
174
int len = strlen(home);
175
if(strncmp(_path.c_str(), home, len) == 0)
178
const char* offset = _path.c_str() + len;
179
if(*offset != '/') path += "/";
187
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
188
bool POSIXFilesystemNode::getChildren(AbstractFSList& myList, ListMode mode,
191
assert(_isDirectory);
148
193
DIR *dirp = opendir(_path.c_str());
151
194
struct dirent *dp;
154
196
if (dirp == NULL)
157
// ... loop over dir entries using readdir
199
// loop over dir entries using readdir
158
200
while ((dp = readdir(dirp)) != NULL)
160
// Skip 'invisible' files
161
if (dp->d_name[0] == '.')
164
POSIXFilesystemNode entry;
165
entry._displayName = dp->d_name;
167
if (entry._path.length() > 0 && entry._path[entry._path.length()-1] != '/')
169
entry._path += dp->d_name;
171
if (stat(entry._path.c_str(), &st))
173
entry._isDirectory = S_ISDIR(st.st_mode);
202
// Skip 'invisible' files if necessary
203
if (dp->d_name[0] == '.' && !hidden)
206
// Skip '.' and '..' to avoid cycles
207
if ((dp->d_name[0] == '.' && dp->d_name[1] == 0) || (dp->d_name[0] == '.' && dp->d_name[1] == '.'))
210
string newPath(_path);
211
if (newPath.length() > 0 && newPath[newPath.length()-1] != '/')
213
newPath += dp->d_name;
215
POSIXFilesystemNode entry(newPath, false);
217
#if defined(SYSTEM_NOT_SUPPORTING_D_TYPE)
218
/* TODO: d_type is not part of POSIX, so it might not be supported
219
* on some of our targets. For those systems where it isn't supported,
220
* add this #elif case, which tries to use stat() instead.
222
* The d_type method is used to avoid costly recurrent stat() calls in big
227
if (dp->d_type == DT_UNKNOWN)
229
// Fall back to stat()
234
entry._isValid = (dp->d_type == DT_DIR) || (dp->d_type == DT_REG) || (dp->d_type == DT_LNK);
235
if (dp->d_type == DT_LNK)
238
if (stat(entry._path.c_str(), &st) == 0)
239
entry._isDirectory = S_ISDIR(st.st_mode);
241
entry._isDirectory = false;
244
entry._isDirectory = (dp->d_type == DT_DIR);
248
// Skip files that are invalid for some reason (e.g. because we couldn't
249
// properly stat them).
175
253
// Honor the chosen mode
176
if ((mode == kListFilesOnly && entry._isDirectory) ||
177
(mode == kListDirectoriesOnly && !entry._isDirectory))
254
if ((mode == FilesystemNode::kListFilesOnly && entry._isDirectory) ||
255
(mode == FilesystemNode::kListDirectoriesOnly && !entry._isDirectory))
180
258
if (entry._isDirectory)
181
259
entry._path += "/";
183
myList.push_back(wrap(new POSIXFilesystemNode(&entry)));
261
myList.push_back(new POSIXFilesystemNode(entry));
190
268
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
191
AbstractFilesystemNode *POSIXFilesystemNode::parent() const
269
AbstractFilesystemNode* POSIXFilesystemNode::getParent() const
193
271
if (_path == "/")
196
POSIXFilesystemNode* p = new POSIXFilesystemNode();
197
274
const char *start = _path.c_str();
198
275
const char *end = lastPathComponent(_path);
200
p->_path = string(start, end - start);
201
p->_displayName = lastPathComponent(p->_path);
204
p->_isDirectory = true;
209
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
210
bool AbstractFilesystemNode::fileExists(const string& path)
213
if(stat(path.c_str(), &st) != 0)
216
return S_ISREG(st.st_mode);
219
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
220
bool AbstractFilesystemNode::dirExists(const string& path)
223
if(stat(path.c_str(), &st) != 0)
226
return S_ISDIR(st.st_mode);
277
return new POSIXFilesystemNode(string(start, end - start), true);
280
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
281
AbstractFilesystemNode* AbstractFilesystemNode::makeRootFileNode()
283
return new POSIXFilesystemNode();
286
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
287
AbstractFilesystemNode* AbstractFilesystemNode::makeCurrentDirectoryFileNode()
289
return new POSIXFilesystemNode("./", true);
292
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
293
AbstractFilesystemNode* AbstractFilesystemNode::makeHomeDirectoryFileNode()
295
return new POSIXFilesystemNode("~/", true);
298
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
299
AbstractFilesystemNode* AbstractFilesystemNode::makeFileNodePath(const string& path)
301
return new POSIXFilesystemNode(path, true);
229
304
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -