Some optimizations to FSNodePOSIX.

This commit is contained in:
Stephen Anthony 2023-07-01 22:05:05 -02:30
parent cf3b9eca89
commit 72227788b1
2 changed files with 15 additions and 15 deletions

View File

@ -43,9 +43,8 @@ FSNodePOSIX::FSNodePOSIX(string_view path, bool verify)
// Get absolute path (only used for relative directories) // Get absolute path (only used for relative directories)
else if (_path[0] == '.') else if (_path[0] == '.')
{ {
std::array<char, MAXPATHLEN> buf; if(realpath(_path.c_str(), ourBuf.data()))
if(realpath(_path.c_str(), buf.data())) _path = ourBuf.data();
_path = buf.data();
} }
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path);
@ -66,7 +65,7 @@ bool FSNodePOSIX::setFlags()
// Add a trailing slash, if necessary // Add a trailing slash, if necessary
if (_isDirectory && _path.length() > 0 && if (_isDirectory && _path.length() > 0 &&
_path[_path.length()-1] != FSNode::PATH_SEPARATOR) _path.back() != FSNode::PATH_SEPARATOR)
_path += FSNode::PATH_SEPARATOR; _path += FSNode::PATH_SEPARATOR;
return true; return true;
@ -101,7 +100,7 @@ string FSNodePOSIX::getShortPath() const
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
size_t FSNodePOSIX::getSize() const size_t FSNodePOSIX::getSize() const
{ {
if (_size == 0) if (_size == 0 && _isFile)
{ {
struct stat st; struct stat st;
_size = (stat(_path.c_str(), &st) == 0) ? st.st_size : 0; _size = (stat(_path.c_str(), &st) == 0) ? st.st_size : 0;
@ -143,8 +142,7 @@ bool FSNodePOSIX::getChildren(AbstractFSList& myList, ListMode mode) const
continue; continue;
string newPath(_path); string newPath(_path);
if (newPath.length() > 0 && if (newPath.length() > 0 && newPath.back() != FSNode::PATH_SEPARATOR)
newPath[newPath.length()-1] != FSNode::PATH_SEPARATOR)
newPath += FSNode::PATH_SEPARATOR; newPath += FSNode::PATH_SEPARATOR;
newPath += dp->d_name; newPath += dp->d_name;
@ -189,9 +187,8 @@ bool FSNodePOSIX::makeDir()
if (mkdir(_path.c_str(), 0777) == 0) if (mkdir(_path.c_str(), 0777) == 0)
{ {
// Get absolute path // Get absolute path
std::array<char, MAXPATHLEN> buf; if (realpath(_path.c_str(), ourBuf.data()))
if (realpath(_path.c_str(), buf.data())) _path = ourBuf.data();
_path = buf.data();
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path);
return setFlags(); return setFlags();
@ -207,9 +204,8 @@ bool FSNodePOSIX::rename(string_view newfile)
_path = newfile; _path = newfile;
// Get absolute path // Get absolute path
std::array<char, MAXPATHLEN> buf; if (realpath(_path.c_str(), ourBuf.data()))
if (realpath(_path.c_str(), buf.data())) _path = ourBuf.data();
_path = buf.data();
_displayName = lastPathComponent(_path); _displayName = lastPathComponent(_path);
return setFlags(); return setFlags();
@ -219,3 +215,4 @@ bool FSNodePOSIX::rename(string_view newfile)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const char* const FSNodePOSIX::ourHomeDir = std::getenv("HOME"); // NOLINT (not thread safe) const char* const FSNodePOSIX::ourHomeDir = std::getenv("HOME"); // NOLINT (not thread safe)
std::array<char, MAXPATHLEN> FSNodePOSIX::ourBuf = { 0 };

View File

@ -39,9 +39,11 @@
#endif #endif
/* /*
* Implementation of the Stella file system API based on POSIX (for Linux and macOS) * Implementation of the Stella file system API based on POSIX (for Linux
* and macOS)
* *
* Parts of this class are documented in the base interface class, AbstractFSNode. * Parts of this class are documented in the base interface class,
* AbstractFSNode.
*/ */
class FSNodePOSIX : public AbstractFSNode class FSNodePOSIX : public AbstractFSNode
{ {
@ -91,6 +93,7 @@ class FSNodePOSIX : public AbstractFSNode
mutable size_t _size{0}; mutable size_t _size{0};
static const char* const ourHomeDir; static const char* const ourHomeDir;
static std::array<char, MAXPATHLEN> ourBuf;
}; };
#endif #endif