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_IFLNK) ;
76 return 0 == ::stat( path.
str().c_str() , &statbuf ) && (statbuf.st_mode & S_IFDIR) ;
82 if( 0 == ::stat( path.
str().c_str() , &statbuf ) )
84 bool x = !!( statbuf.st_mode & S_IXUSR ) ;
86 ( statbuf.st_mode & S_IFMT ) == S_IFREG ||
87 ( statbuf.st_mode & S_IFMT ) == S_IFLNK ;
99 if( 0 != ::stat( path.
str().c_str() , &statbuf ) )
100 return std::string() ;
102 std::ostringstream ss ;
103 ss << statbuf.st_size ;
109 struct stat statbuf ;
110 if( 0 != ::stat( path.
str().c_str() , &statbuf ) )
111 throw TimeError( path.
str() ) ;
112 return mtime( statbuf ) ;
117 struct stat statbuf ;
118 if( ::stat( path.
str().c_str() , &statbuf ) != 0 )
120 return mtime( statbuf ) ;
125 struct stat statbuf ;
127 0 == ::stat( path.
str().c_str() , &statbuf ) ?
131 mode |= ( S_IRUSR | S_IXUSR ) ;
132 if( mode & S_IRGRP ) mode |= S_IXGRP ;
133 if( mode & S_IROTH ) mode |= S_IXOTH ;
136 mode_t mask = ::umask( ::umask(0) ) ;
139 bool ok = 0 == ::chmod( path.
str().c_str() , mode ) ;
140 if( !ok && do_throw )
141 throw CannotChmod( path.
str() ) ;
147 if( linked(target,new_link) )
150 if( exists(new_link) )
151 remove( new_link ,
NoThrow() ) ;
153 int error = link( target.
str().c_str() , new_link.
str().c_str() ) ;
157 std::ostringstream ss ;
158 ss <<
"[" << new_link <<
"] -> [" << target <<
"] " "(" << error <<
")" ;
159 throw CannotLink( ss.str() ) ;
165 if( linked(target,new_link) )
168 if( exists(new_link) )
169 remove( new_link ,
NoThrow() ) ;
171 return 0 == link( target.
str().c_str() , new_link.
str().c_str() ) ;
174 int G::File::link(
const char * target ,
const char * new_link )
176 int rc = ::symlink( target , new_link ) ;
177 int error = G::Process::errno_() ;
178 return rc == 0 ? 0 : (error?error:EINVAL) ;
181 bool G::File::linked(
const Path & target ,
const Path & new_link )
184 std::vector<char> buffer( limits::path ,
'\0' ) ;
185 ssize_t rc = ::readlink( new_link.str().c_str() , &buffer[0] , buffer.size() ) ;
186 size_t n = rc < 0 ? size_t(0U) : static_cast<size_t>(rc) ;
187 if( rc > 0 && n != buffer.size() )
189 std::string old_target( &buffer[0] , n ) ;
190 if( target.str() == old_target )
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 isLink(const Path &path)
Returns true if the path is an existing symlink.
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.