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 {
nullptr} ;
67 bool m_is_dir {
false} ;
68 bool m_is_link {
false} ;
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 ) :
155 m_d = ::opendir( dir.
path().
cstr() ) ;
156 m_error = m_d == nullptr ;
159bool G::DirectoryIteratorImp::error()
const
164bool G::DirectoryIteratorImp::more()
168 m_dp = ::readdir( m_d ) ;
169 m_error = m_dp == nullptr ;
173 static_assert(
sizeof(dirent::d_name) > 2U ,
"" ) ;
174 bool special = m_dp->d_name[0] ==
'.' && (
175 ( m_dp->d_name[1] ==
'\0' ) ||
176 ( m_dp->d_name[1] ==
'.' && m_dp->d_name[2] ==
'\0' ) ) ;
183 #if GCONFIG_HAVE_DIRENT_D_TYPE
184 else if( m_dp->d_type != DT_UNKNOWN )
186 m_is_dir = m_dp->d_type == DT_DIR ;
187 m_is_link = m_dp->d_type == DT_LNK ;
194 auto statbuf =
File::stat( filePath() ,
true ) ;
195 m_error = statbuf.error != 0 ;
196 m_is_dir = !m_error && statbuf.is_dir ;
197 m_is_link = !m_error && statbuf.is_link ;
201 m_is_dir = statbuf.error != 0 && statbuf.is_dir ;
210G::Path G::DirectoryIteratorImp::filePath()
const
212 return m_dir.path() / fileName() ;
215std::string G::DirectoryIteratorImp::fileName()
const
217 return m_dp ==
nullptr ? std::string() :
std::string(m_dp->d_name) ;
220bool G::DirectoryIteratorImp::isDir()
const
225bool G::DirectoryIteratorImp::isLink()
const
230G::DirectoryIteratorImp::~DirectoryIteratorImp()
236std::string G::DirectoryIteratorImp::sizeString()
const
239 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 or a symlink to 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 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 Stat stat(const Path &path, bool symlink_nofollow=false)
Returns a file status structure.
static bool probe(const Path &) noexcept
Creates and deletes a temporary probe file.
A Path object represents a file system path.
const value_type * cstr() const noexcept
Returns the path's c-string.
std::string str() const
Returns the path string.
bool empty() const noexcept
Returns true if the path is empty.
static int errno_(const SignalSafe &=G::SignalSafe()) noexcept
Returns the process's current 'errno' value.