30 #include <sys/types.h>
37 class DirectoryIteratorImp ;
48 struct dirent * m_dp ;
58 std::string sizeString()
const ;
59 Path filePath()
const ;
60 std::string fileName()
const ;
73 if( ::stat( m_path.
str().c_str() , &statbuf ) )
77 else if( !(statbuf.st_mode & S_IFDIR) )
83 DIR * p = ::opendir( m_path.
str().c_str() ) ;
90 if( rc && for_creation )
93 if( 0 != ::access( m_path.
str().c_str() , W_OK ) )
101 std::ostringstream ss ;
102 ss <<
"." <<
DateTime::now() <<
"." << Process::Id() <<
".tmp" ;
109 if( tmp_filename.empty() ) tmp_filename = tmp() ;
112 int fd = ::open( test_file.
str().c_str() , O_WRONLY | O_CREAT | O_EXCL , S_IRWXU ) ;
118 bool ok = 0 == ::unlink( test_file.
str().c_str() ) ;
131 return m_imp->error() ;
136 return m_imp->more() ;
141 return m_imp->filePath() ;
146 return m_imp->fileName() ;
151 return m_imp->isDir() ;
156 return m_imp->sizeString() ;
166 G::DirectoryIteratorImp::DirectoryIteratorImp(
const Directory & dir ) :
172 m_d = ::opendir( dir.
path().
str().c_str() ) ;
173 m_error = m_d == nullptr ;
176 bool G::DirectoryIteratorImp::error()
const
181 bool G::DirectoryIteratorImp::more()
185 m_dp = ::readdir( m_d ) ;
186 m_error = m_dp == nullptr ;
187 bool special = !m_error && ( fileName() ==
"." || fileName() ==
".." ) ;
194 G::Path G::DirectoryIteratorImp::filePath()
const
196 return m_dir.path() + fileName() ;
199 std::string G::DirectoryIteratorImp::fileName()
const
201 return m_dp ==
nullptr ? std::string() : std::string(m_dp->d_name) ;
204 bool G::DirectoryIteratorImp::isDir()
const
206 struct stat statbuf ;
207 return ::stat( filePath().str().c_str() , &statbuf ) == 0 && (statbuf.st_mode & S_IFDIR) ;
210 G::DirectoryIteratorImp::~DirectoryIteratorImp()
216 std::string G::DirectoryIteratorImp::sizeString()
const
219 return s.empty() ? std::string(
"0") : s ;
std::string str() const
Returns the path string.
std::string sizeString() const
Returns the file size as a decimal string.
Path filePath() const
Returns the path of the current item.
Path path() const
Returns the directory's path.
bool valid(bool for_creating_files=false) const
Returns true if the object represents a valid directory.
bool writeable(std::string probe_filename=tmp()) const
Tries to create and then delete an empty test file in the directory.
~DirectoryIterator()
Destructor.
static EpochTime now()
Returns the current epoch time.
An encapsulation of a file system directory that allows for iterating through the set of contained fi...
static std::string tmp()
A convenience function for constructing a filename for writeable().
bool more()
Returns true if more and advances by one.
bool isDir() const
Returns true if the current item is a directory.
void pathAppend(const std::string &tail)
Appends a filename or a relative path to this path.
static std::string sizeString(const Path &file)
Returns the file's size in string format.
A pimple-pattern implementation class for DirectoryIterator using opendir()/readdir().
std::string fileName() const
Returns the name of the current item.
bool error() const
Returns true on error. The caller should stop the iteration.
A Path object represents a file system path.
DirectoryIterator(const Directory &dir)
Constructor taking a directory reference.