Commit 518da86d authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen
Browse files

Directory: Lazy load directory stats & content

parent 6ff4d50f
......@@ -36,9 +36,9 @@ namespace fs
// Returns the absolute path to this directory
virtual const std::string& path() const = 0;
/// Returns a list of absolute files path
virtual const std::vector<std::string>& files() const = 0;
virtual const std::vector<std::string>& files() = 0;
/// Returns a list of absolute path to this folder subdirectories
virtual const std::vector<std::string>& dirs() const = 0;
virtual const std::vector<std::string>& dirs() = 0;
virtual unsigned int lastModificationDate() const = 0;
virtual bool isRemovable() const = 0;
};
......
......@@ -35,13 +35,8 @@ namespace fs
Directory::Directory( const std::string& path )
: m_path( toAbsolute( path ) )
, m_lastModificationDate( 0 )
{
struct stat s;
lstat( path.c_str(), &s );
if ( S_ISDIR( s.st_mode ) == false )
throw std::runtime_error( "The provided path isn't a directory" );
m_lastModificationDate = s.st_mtim.tv_sec;
read();
}
const std::string&Directory::path() const
......@@ -49,18 +44,30 @@ const std::string&Directory::path() const
return m_path;
}
const std::vector<std::string>& Directory::files() const
const std::vector<std::string>& Directory::files()
{
if ( m_dirs.size() == 0 && m_files.size() == 0 )
read();
return m_files;
}
const std::vector<std::string>&Directory::dirs() const
const std::vector<std::string>&Directory::dirs()
{
if ( m_dirs.size() == 0 && m_files.size() == 0 )
read();
return m_dirs;
}
unsigned int Directory::lastModificationDate() const
{
if ( m_lastModificationDate == 0 )
{
struct stat s;
lstat( m_path.c_str(), &s );
if ( S_ISDIR( s.st_mode ) == false )
throw std::runtime_error( "The provided path isn't a directory" );
m_lastModificationDate = s.st_mtim.tv_sec;
}
return m_lastModificationDate;
}
......
......@@ -33,8 +33,8 @@ class Directory : public IDirectory
public:
explicit Directory( const std::string& path );
virtual const std::string& path() const override;
virtual const std::vector<std::string>& files() const override;
virtual const std::vector<std::string>& dirs() const override;
virtual const std::vector<std::string>& files() override;
virtual const std::vector<std::string>& dirs() override;
virtual unsigned int lastModificationDate() const override;
virtual bool isRemovable() const override;
......@@ -48,7 +48,7 @@ private:
const std::string m_path;
std::vector<std::string> m_files;
std::vector<std::string> m_dirs;
unsigned int m_lastModificationDate;
mutable unsigned int m_lastModificationDate;
};
}
......@@ -96,17 +96,17 @@ public:
{
}
virtual const std::string& path() const
virtual const std::string& path() const override
{
return m_path;
}
virtual const std::vector<std::string>& files() const
virtual const std::vector<std::string>& files() override
{
return m_files;
}
virtual const std::vector<std::string>& dirs() const
virtual const std::vector<std::string>& dirs() override
{
return m_dirs;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment