Some minor optimizations to FSNodePOSIX.

This commit is contained in:
Stephen Anthony 2022-10-10 16:40:25 -02:30
parent 44c5d35301
commit 437046bb6b
3 changed files with 32 additions and 36 deletions

View File

@ -417,7 +417,6 @@ size_t FSNode::write(const ByteBuffer& buffer, size_t size) const
out.seekp(0, std::ios::end);
sizeWritten = static_cast<size_t>(out.tellp());
out.seekp(0, std::ios::beg);
}
else
throw runtime_error("File open/write error");
@ -442,7 +441,6 @@ size_t FSNode::write(const stringstream& buffer) const
out.seekp(0, std::ios::end);
sizeWritten = static_cast<size_t>(out.tellp());
out.seekp(0, std::ios::beg);
}
else
throw runtime_error("File open/write error");

View File

@ -15,8 +15,6 @@
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//============================================================================
#include "TimerManager.hxx"
#if defined(RETRON77)
#define ROOT_DIR "/mnt/games/"
#else
@ -39,9 +37,8 @@ FSNodePOSIX::FSNodePOSIX(const string& path, bool verify)
// Expand '~' to the HOME environment variable
if(_path[0] == '~')
{
const char* home = std::getenv("HOME"); // NOLINT (not thread safe)
if (home != nullptr)
_path.replace(0, 1, home);
if (ourHomeDir != nullptr)
_path.replace(0, 1, ourHomeDir);
}
// Get absolute path (only used for relative directories)
else if(_path[0] == '.')
@ -62,7 +59,7 @@ void FSNodePOSIX::setFlags()
{
struct stat st;
_isValid = (0 == stat(_path.c_str(), &st));
_isValid = stat(_path.c_str(), &st) == 0;
if(_isValid)
{
_isDirectory = S_ISDIR(st.st_mode);
@ -80,8 +77,7 @@ void FSNodePOSIX::setFlags()
string FSNodePOSIX::getShortPath() const
{
// If the path starts with the home directory, replace it with '~'
const char* env_home = std::getenv("HOME"); // NOLINT (not thread safe)
const string& home = env_home != nullptr ? env_home : EmptyString;
const string& home = ourHomeDir != nullptr ? ourHomeDir : EmptyString;
if(home != EmptyString && BSPF::startsWithIgnoreCase(_path, home))
{
@ -94,12 +90,31 @@ string FSNodePOSIX::getShortPath() const
return _path;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
size_t FSNodePOSIX::getSize() const
{
struct stat st;
return (stat(_path.c_str(), &st) == 0) ? st.st_size : 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool FSNodePOSIX::hasParent() const
{
return !_path.empty() && _path != ROOT_DIR;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
AbstractFSNodePtr FSNodePOSIX::getParent() const
{
if (_path == ROOT_DIR)
return nullptr;
const char* start = _path.c_str();
const char* end = lastPathComponent(_path);
return make_unique<FSNodePOSIX>(string(start, static_cast<size_t>(end - start)));
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool FSNodePOSIX::getChildren(AbstractFSList& myList, ListMode mode) const
{
@ -172,13 +187,6 @@ bool FSNodePOSIX::getChildren(AbstractFSList& myList, ListMode mode) const
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
size_t FSNodePOSIX::getSize() const
{
struct stat st;
return (stat(_path.c_str(), &st) == 0) ? st.st_size : 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool FSNodePOSIX::makeDir()
{
@ -228,13 +236,4 @@ bool FSNodePOSIX::rename(const string& newfile)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
AbstractFSNodePtr FSNodePOSIX::getParent() const
{
if (_path == ROOT_DIR)
return nullptr;
const char* start = _path.c_str();
const char* end = lastPathComponent(_path);
return make_unique<FSNodePOSIX>(string(start, static_cast<size_t>(end - start)));
}
const char* FSNodePOSIX::ourHomeDir = std::getenv("HOME"); // NOLINT (not thread safe)

View File

@ -65,7 +65,6 @@ class FSNodePOSIX : public AbstractFSNode
void setName(const string& name) override { _displayName = name; }
const string& getPath() const override { return _path; }
string getShortPath() const override;
bool hasParent() const override;
bool isDirectory() const override { return _isDirectory; }
bool isFile() const override { return _isFile; }
bool isReadable() const override { return access(_path.c_str(), R_OK) == 0; }
@ -74,15 +73,9 @@ class FSNodePOSIX : public AbstractFSNode
bool rename(const string& newfile) override;
size_t getSize() const override;
bool getChildren(AbstractFSList& list, ListMode mode) const override;
bool hasParent() const override;
AbstractFSNodePtr getParent() const override;
protected:
string _path;
string _displayName;
bool _isValid{true};
bool _isFile{false};
bool _isDirectory{true};
bool getChildren(AbstractFSList& list, ListMode mode) const override;
private:
/**
@ -90,6 +83,12 @@ class FSNodePOSIX : public AbstractFSNode
* using the stat() function.
*/
void setFlags();
private:
string _path, _displayName;
bool _isValid{true}, _isFile{false}, _isDirectory{true};
static const char* ourHomeDir;
};
#endif