More cleanups to FSNodePOSIX.

This commit is contained in:
Stephen Anthony 2022-10-12 12:58:00 -02:30
parent 93df53e751
commit 38dc5173ec
2 changed files with 33 additions and 53 deletions

View File

@ -55,23 +55,29 @@ FSNodePOSIX::FSNodePOSIX(const string& path, bool verify)
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FSNodePOSIX::setFlags() bool FSNodePOSIX::setFlags()
{ {
struct stat st; struct stat st;
if(stat(_path.c_str(), &st) == 0)
_isValid = stat(_path.c_str(), &st) == 0;
if(_isValid)
{ {
_isDirectory = S_ISDIR(st.st_mode); _isDirectory = S_ISDIR(st.st_mode);
_isFile = S_ISREG(st.st_mode); _isFile = S_ISREG(st.st_mode);
_size = st.st_size; _size = st.st_size;
// Add a trailing slash, if necessary // Add a trailing slash, if necessary
if (_isDirectory && _path.length() > 0 && _path[_path.length()-1] != '/') if (_isDirectory && _path.length() > 0 &&
_path += '/'; _path[_path.length()-1] != FSNode::PATH_SEPARATOR)
_path += FSNode::PATH_SEPARATOR;
return true;
} }
else else
{
_isDirectory = _isFile = false; _isDirectory = _isFile = false;
_size = 0;
return false;
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -84,7 +90,8 @@ string FSNodePOSIX::getShortPath() const
{ {
string path = "~"; string path = "~";
const char* offset = _path.c_str() + home.size(); const char* offset = _path.c_str() + home.size();
if(*offset != '/') path += "/"; if(*offset != FSNode::PATH_SEPARATOR)
path += FSNode::PATH_SEPARATOR;
path += offset; path += offset;
return path; return path;
} }
@ -139,45 +146,31 @@ bool FSNodePOSIX::getChildren(AbstractFSList& myList, ListMode mode) const
continue; continue;
string newPath(_path); string newPath(_path);
if (newPath.length() > 0 && newPath[newPath.length()-1] != '/') if (newPath.length() > 0 && newPath[newPath.length()-1] != FSNode::PATH_SEPARATOR)
newPath += '/'; newPath += FSNode::PATH_SEPARATOR;
newPath += dp->d_name; newPath += dp->d_name;
FSNodePOSIX entry(newPath, false); FSNodePOSIX entry(newPath, false);
bool valid = true;
if (dp->d_type == DT_UNKNOWN) if (dp->d_type == DT_UNKNOWN || dp->d_type == DT_LNK)
{ {
// Fall back to stat() // Fall back to stat()
entry.setFlags(); valid = entry.setFlags();
} }
else else
{ {
if (dp->d_type == DT_LNK) entry._isDirectory = (dp->d_type == DT_DIR);
{ entry._isFile = (dp->d_type == DT_REG);
struct stat st; // entry._size will be calculated next time ::getSize() is called
if (stat(entry._path.c_str(), &st) == 0)
{
entry._isDirectory = S_ISDIR(st.st_mode);
entry._isFile = S_ISREG(st.st_mode);
}
else
entry._isDirectory = entry._isFile = false;
}
else
{
entry._isDirectory = (dp->d_type == DT_DIR);
entry._isFile = (dp->d_type == DT_REG);
}
if (entry._isDirectory) if (entry._isDirectory)
entry._path += "/"; entry._path += FSNode::PATH_SEPARATOR;
entry._isValid = true;
} }
// Skip files that are invalid for some reason (e.g. because we couldn't // Skip files that are invalid for some reason (e.g. because we couldn't
// properly stat them). // properly stat them).
if (!entry._isValid) if (!valid)
continue; continue;
// Honor the chosen mode // Honor the chosen mode
@ -203,16 +196,9 @@ bool FSNodePOSIX::makeDir()
_path = buf.data(); _path = buf.data();
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path);
setFlags(); return setFlags();
// Add a trailing slash, if necessary
if (_path.length() > 0 && _path[_path.length()-1] != '/')
_path += '/';
return true;
} }
else return false;
return false;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -228,16 +214,9 @@ bool FSNodePOSIX::rename(const string& newfile)
_path = buf.data(); _path = buf.data();
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path);
setFlags(); return setFlags();
// Add a trailing slash, if necessary
if (_isDirectory && _path.length() > 0 && _path[_path.length()-1] != '/')
_path += '/';
return true;
} }
else return false;
return false;
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -79,14 +79,15 @@ class FSNodePOSIX : public AbstractFSNode
private: private:
/** /**
* Tests and sets the _isValid and _isDirectory/_isFile flags, * Set the _isDirectory/_isFile/_size flags using stat().
* using the stat() function. *
* @return Success/failure of stat() function
*/ */
void setFlags(); bool setFlags();
private: private:
string _path, _displayName; string _path, _displayName;
bool _isValid{true}, _isFile{false}, _isDirectory{true}; bool _isFile{false}, _isDirectory{true};
mutable size_t _size{0}; mutable size_t _size{0};
static const char* ourHomeDir; static const char* ourHomeDir;