26 #include <sys/types.h>
37 #if GCONFIG_HAVE_STATBUF_NSEC
38 return G::EpochTime( statbuf.st_mtime , statbuf.st_mtim.tv_nsec/1000U ) ;
48 return 0 ==
::mkdir( dir.
str().c_str() , 0777 ) ;
51 bool G::File::exists(
const char * path ,
bool & enoent ,
bool & eaccess )
54 if( 0 == ::stat( path , &statbuf ) )
60 int error = G::Process::errno_() ;
61 enoent = error == ENOENT || error == ENOTDIR ;
62 eaccess = error == EACCES ;
70 return 0 == ::stat( path.
str().c_str() , &statbuf ) && (statbuf.st_mode & S_IFDIR) ;
76 if( 0 == ::stat( path.
str().c_str() , &statbuf ) )
78 bool x = !!( statbuf.st_mode & S_IXUSR ) ;
80 ( statbuf.st_mode & S_IFMT ) == S_IFREG ||
81 ( statbuf.st_mode & S_IFMT ) == S_IFLNK ;
93 if( 0 != ::stat( path.
str().c_str() , &statbuf ) )
94 return std::string() ;
96 std::ostringstream ss ;
97 ss << statbuf.st_size ;
103 struct stat statbuf ;
104 if( 0 != ::stat( path.
str().c_str() , &statbuf ) )
105 throw TimeError( path.
str() ) ;
106 return mtime( statbuf ) ;
111 struct stat statbuf ;
112 if( ::stat( path.
str().c_str() , &statbuf ) != 0 )
114 return mtime( statbuf ) ;
120 struct stat statbuf ;
121 if( 0 == ::stat( path.
str().c_str() , &statbuf ) )
123 mode = statbuf.st_mode | S_IRUSR | S_IXUSR ;
124 if( mode & S_IRGRP ) mode |= S_IXGRP ;
125 if( mode & S_IROTH ) mode |= S_IXOTH ;
134 mode_t mask = ::umask( ::umask(0) ) ;
137 bool ok = 0 == ::chmod( path.
str().c_str() , mode ) ;
138 if( !ok && do_throw )
139 throw CannotChmod( path.
str() ) ;
145 if( !link(target,new_link,
NoThrow()) )
147 int error = G::Process::errno_() ;
148 std::ostringstream ss ;
149 ss <<
"[" << new_link <<
"] -> [" << target <<
"] " "(" << error <<
")" ;
150 throw CannotLink( ss.str() ) ;
157 std::vector<char> buffer( limits::path ,
'\0' ) ;
158 ssize_t rc = ::readlink( new_link.
str().c_str() , &buffer[0] , buffer.size() ) ;
159 size_t n = rc < 0 ? size_t(0U) : static_cast<size_t>(rc) ;
160 if( rc > 0 && n != buffer.size() )
162 std::string old_target( &buffer[0] , n ) ;
163 if( target.
str() == old_target )
167 if( exists(new_link) )
168 remove( new_link ,
NoThrow() ) ;
170 rc = ::symlink( target.
str().c_str() , new_link.
str().c_str() ) ;
std::string str() const
Returns the path string.
A subsecond-resolution timestamp based on a time_t.
static bool isDirectory(const Path &path)
Returns true if the path exists() and is a directory.
static bool executable(const Path &)
Returns true if the path is probably executable.
static bool exists(const Path &file)
Returns true if the file (directory, device etc.) exists.
static bool mkdir(const Path &dir, const NoThrow &)
Creates a directory. Returns false on error.
An overload discriminator class for File methods.
static void chmodx(const Path &file)
Makes the file executable.
static void link(const Path &target, const Path &new_link)
Creates a symlink.
static std::string sizeString(const Path &file)
Returns the file's size in string format.
static EpochTime time(const Path &file)
Returns the file's timestamp.
A Path object represents a file system path.