31 int rc = std::remove( path.cstr() ) ;
37 int rc = std::remove( path.
cstr() ) ;
41 G_WARNING(
"G::File::remove: cannot delete file [" << path <<
"]: " <<
Process::strerror(e) ) ;
48 bool ok = 0 == std::rename( from.cstr() , to.cstr() ) ;
51 std::remove( to.cstr() ) ;
52 ok = 0 == std::rename( from.cstr() , to.cstr() ) ;
59 bool is_missing = false ;
60 bool ok = rename( from.
cstr() , to.
cstr() , is_missing ) ;
61 if( !ok && !(is_missing && ignore_missing) )
63 throw CannotRename( std::string() +
"[" + from.
str() +
"] to [" + to.
str() +
"]" ) ;
65 G_DEBUG(
"G::File::rename: \"" << from <<
"\" -> \"" << to <<
"\": success=" << ok ) ;
68bool G::File::rename(
const char * from ,
const char * to ,
bool & enoent )
noexcept
70 bool ok = 0 == std::rename( from , to ) ;
72 enoent = !ok && error == ENOENT ;
78 std::string reason = copy( from , to , 0 ) ;
80 throw CannotCopy( std::string() +
"[" + from.
str() +
"] to [" + to.
str() +
"]: " + reason ) ;
85 return copy(from,to,0).empty() ;
91 bool ok = copy(from,to,0).empty() ;
92 if( ok && isExecutable(from,std::nothrow) )
93 ok = chmodx( to , std::nothrow ) ;
99 std::ifstream in ; open( in , from ) ;
101 return "cannot open input file" ;
103 std::ofstream out ; open( out , to ) ;
105 return "cannot open output file" ;
110 return "read error" ;
118 if( out.fail() && !empty )
119 return "write error" ;
121 return std::string() ;
124void G::File::copy( std::istream & in , std::ostream & out , std::streamsize limit , std::string::size_type block )
126 std::ios_base::iostate in_state = in.rdstate() ;
128 block = block ? block :
static_cast<std::string::size_type
>(limits::file_buffer) ;
129 std::vector<char> buffer ;
130 buffer.reserve( block ) ;
132 const auto b =
static_cast<std::streamsize
>(block) ;
133 std::streamsize size = 0U ;
134 while( ( limit == 0U || size < limit ) && in.good() && out.good() )
136 std::streamsize request = limit == 0U || (limit-size) > b ? b : (limit-size) ;
137 in.read( &buffer[0] , request ) ;
138 std::streamsize result = in.gcount() ;
141 out.write( &buffer[0] , result ) ;
148 in.clear( (in.rdstate() & ~std::ios_base::failbit) | (in_state & std::ios_base::failbit) ) ;
153 return path.
empty() ? false : exists( path ,
false ,
true ) ;
158 return path.
empty() ? false : exists( path ,
false ,
false ) ;
163 bool enoent = false ;
164 bool eaccess = false ;
165 bool rc = existsImp( path.
cstr() , enoent , eaccess ) ;
170 else if( !rc && do_throw )
172 throw StatError( path.
str() , eaccess?
"permission denied":
"" ) ;
176 return error_return_value ;
184 return 0 == s.error && s.is_link ;
190 return 0 == s.error && s.is_dir ;
196 return 0 == s.error && s.is_executable ;
202 return 0 == s.error && s.is_empty ;
208 return s.error ? std::string() : std::to_string(s.size) ;
229 return chmodx( path ,
false ) ;
234 chmodx( path ,
true ) ;
239 return 0 == mkdirImp( dir ) ;
244 int e = mkdirImp( dir ) ;
249bool G::File::mkdirsr(
int * ep ,
const Path & path ,
int limit )
259 if( path.
str().empty() )
262 if( !mkdirsr( ep , path.
dirname() , limit-1 ) )
265 int e = mkdirImp( path ) ;
269 chmodx( path , std::nothrow ) ;
277 bool ok = mkdirsr( &e , path , limit ) ;
278 return ok || e == EEXIST ;
284 if( !mkdirsr(&e,path,limit) && e != EEXIST )
290 std::ifstream file_1 ; open( file_1 , path_1 ) ;
291 std::ifstream file_2 ; open( file_2 , path_2 ) ;
292 constexpr int eof = std::char_traits<char>::eof() ;
293 if( !file_1.good() && !file_2.good() )
return -1 ;
294 if( !file_1.good() )
return -1 ;
295 if( !file_2.good() )
return 1 ;
299 auto isspace = [](
int c){
return c ==
' ' || c ==
'\t' || c ==
'\n' || c ==
'\r' ; } ;
302 do { a = file_1.get() ; }
while( ignore_whitespace && isspace(a) ) ;
303 do { b = file_2.get() ; }
while( ignore_whitespace && isspace(b) ) ;
304 if( a == eof && b == eof )
308 result = a < b ? -1 : 1 ;
static bool isExecutable(const Path &, std::nothrow_t)
Returns true if the path is probably executable by the calling process.
static SystemTime time(const Path &file)
Returns the file's timestamp. Throws on error.
static bool isEmpty(const Path &file, std::nothrow_t)
Returns true if the file size is zero.
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 rename(const Path &from, const Path &to, std::nothrow_t) noexcept
Renames the file.
static bool remove(const Path &path, std::nothrow_t) noexcept
Deletes the file or directory. Returns false on error.
static bool exists(const Path &file)
Returns true if the file (directory, device etc.) exists.
static void chmodx(const Path &file)
Makes the file executable. Throws on error.
static bool isLink(const Path &path, std::nothrow_t)
Returns true if the path is an existing symlink.
static bool mkdirs(const Path &dir, std::nothrow_t, int=100)
Creates a directory and all necessary parents.
static bool copy(const Path &from, const Path &to, std::nothrow_t)
Copies a file. Returns false on error.
static bool copyInto(const Path &from, const Path &to_dir, std::nothrow_t)
Copies a file into a directory and does a chmodx() if necessary.
static bool mkdir(const Path &dir, std::nothrow_t)
Creates a directory.
static int compare(const Path &, const Path &, bool ignore_whitespace=false)
Compares the contents of the two files. Returns 0, 1 or -1.
A Path object represents a file system path.
const char * cstr() const noexcept
Returns the path string.
std::string basename() const
Returns the rightmost part of the path, ignoring "." parts.
Path dirname() const
Returns the path without the rightmost part, ignoring "." parts.
std::string str() const
Returns the path string.
bool empty() const noexcept
Returns true if size() is zero.
static std::string strerror(int errno_)
Translates an 'errno' value into a meaningful diagnostic string.
static int errno_(const SignalSafe &=G::SignalSafe()) noexcept
Returns the process's current 'errno' value.
Represents a unix-epoch time with microsecond resolution.
A portable 'struct stat'.