36 class DirectoryIteratorImp ;
43class G::DirectoryIteratorImp
46 explicit DirectoryIteratorImp(
const Directory & dir ) ;
47 ~DirectoryIteratorImp() ;
52 std::string sizeString()
const ;
53 Path filePath()
const ;
54 std::string fileName()
const ;
57 DirectoryIteratorImp(
const DirectoryIteratorImp & ) = delete ;
58 DirectoryIteratorImp( DirectoryIteratorImp && ) = delete ;
59 DirectoryIteratorImp & operator=(
const DirectoryIteratorImp & ) = delete ;
60 DirectoryIteratorImp & operator=( DirectoryIteratorImp && ) = delete ;
64 struct dirent * m_dp ;
81 std::string path_dot = m_path.
str() + (m_path.
str()==
"/"?
"":
"/") +
"." ;
83 DIR * p = ::opendir( path_dot.c_str() ) ;
88 if( error == 0 && for_creation )
91 int rc = ::access( m_path.
cstr() , W_OK ) ;
99 Path path( m_path , filename.empty() ? tmp() : filename ) ;
106 m_imp(
std::make_unique<DirectoryIteratorImp>(dir))
115 return m_imp->error() ;
120 return m_imp->more() ;
125 return m_imp->filePath() ;
130 return m_imp->fileName() ;
135 return m_imp->isLink() ;
140 return m_imp->isDir() ;
146 return m_imp->sizeString() ;
152G::DirectoryIteratorImp::DirectoryIteratorImp(
const Directory & dir ) :
160 m_d = ::opendir( dir.
path().
cstr() ) ;
161 m_error = m_d == nullptr ;
164bool G::DirectoryIteratorImp::error()
const
169bool G::DirectoryIteratorImp::more()
173 m_dp = ::readdir( m_d ) ;
174 m_error = m_dp == nullptr ;
178 static_assert(
sizeof(dirent::d_name) > 2U ,
"" ) ;
179 bool special = m_dp->d_name[0] ==
'.' && (
180 ( m_dp->d_name[1] ==
'\0' ) ||
181 ( m_dp->d_name[1] ==
'.' && m_dp->d_name[2] ==
'\0' ) ) ;
188 #if GCONFIG_HAVE_DIRENT_D_TYPE
189 else if( m_dp->d_type != DT_UNKNOWN )
191 m_is_dir = m_dp->d_type == DT_DIR ;
192 m_is_link = m_dp->d_type == DT_LNK ;
208G::Path G::DirectoryIteratorImp::filePath()
const
210 return m_dir.path() + fileName() ;
213std::string G::DirectoryIteratorImp::fileName()
const
215 return m_dp ==
nullptr ? std::string() :
std::string(m_dp->d_name) ;
218bool G::DirectoryIteratorImp::isDir()
const
223bool G::DirectoryIteratorImp::isLink()
const
228G::DirectoryIteratorImp::~DirectoryIteratorImp()
234std::string G::DirectoryIteratorImp::sizeString()
const
237 return s.empty() ? std::string(1U,
'0') : s ;
DirectoryIterator(const Directory &dir)
Constructor taking a directory reference.
std::string fileName() const
Returns the name of the current item.
bool error() const
Returns true on error. The caller should stop the iteration.
std::string sizeString() const
Returns the file size as a decimal string.
bool isLink() const
Returns true if the current item is a symlink.
~DirectoryIterator()
Destructor.
bool isDir() const
Returns true if the current item is a directory.
bool more()
Returns true if more and advances by one.
Path filePath() const
Returns the path of the current item.
An encapsulation of a file system directory that works with G::DirectoryIterator.
int usable(bool for_creating_files=false) const
Returns zero if the object represents a valid directory with permissions that dont disallow reading o...
bool writeable(const std::string &probe_filename=tmp()) const
Tries to create and then delete an empty test file in the directory.
Path path() const
Returns the directory's path, as passed in to the ctor.
static bool probe(const char *) noexcept
Creates and deletes a temporary probe file.
static bool isDirectory(const Path &path, std::nothrow_t)
Returns true if the path exists() and is a directory.
static std::string sizeString(const Path &file)
Returns the file's size in string format.
static bool isLink(const Path &path, std::nothrow_t)
Returns true if the path is an existing symlink.
A Path object represents a file system path.
const char * cstr() const noexcept
Returns the path string.
std::string str() const
Returns the path string.
bool empty() const noexcept
Returns true if size() is zero.
static int errno_(const SignalSafe &=G::SignalSafe()) noexcept
Returns the process's current 'errno' value.