31 bool rc = 0 == std::remove( path.
str().c_str() ) ;
32 G_DEBUG(
"G::File::remove: \"" << path <<
"\": success=" << rc ) ;
38 if( 0 != std::remove( path.
str().c_str() ) )
40 throw CannotRemove( path.
str() ) ;
42 G_DEBUG(
"G::File::remove: \"" << path <<
"\"" ) ;
47 bool is_missing = false ;
48 bool ok = rename( from.
str().c_str() , to.
str().c_str() , is_missing ) ;
49 G_DEBUG(
"G::File::rename: \"" << from <<
"\" -> \"" << to <<
"\": success=" << ok ) ;
55 bool is_missing = false ;
56 bool ok = rename( from.
str().c_str() , to.
str().c_str() , is_missing ) ;
57 if( !ok && !(is_missing && ignore_missing) )
59 throw CannotRename( std::string() +
"[" + from.
str() +
"] to [" + to.
str() +
"]" ) ;
61 G_DEBUG(
"G::File::rename: \"" << from <<
"\" -> \"" << to <<
"\": success=" << ok ) ;
64 bool G::File::rename(
const char * from ,
const char * to ,
bool & enoent )
66 bool ok = 0 == std::rename( from , to ) ;
67 int error = G::Process::errno_() ;
68 enoent = !ok && error == ENOENT ;
74 std::string reason = copy( from , to , 0 ) ;
76 throw CannotCopy( std::string() +
"[" + from.
str() +
"] to [" + to.
str() +
"]: " + reason ) ;
81 return copy(from,to,0).empty() ;
86 std::ifstream in( from.
str().c_str() , std::ios::binary | std::ios::in ) ;
88 return "cannot open input file" ;
90 std::ofstream out( to.
str().c_str() , std::ios::binary | std::ios::out | std::ios::trunc ) ;
92 return "cannot open output file" ;
103 return "write error" ;
105 return std::string() ;
108 void G::File::copy( std::istream & in , std::ostream & out , std::streamsize limit , std::string::size_type block )
110 std::ios_base::iostate in_state = in.rdstate() ;
112 block = block ? block :
static_cast<std::string::size_type
>(limits::file_buffer) ;
113 std::vector<char> buffer ;
114 buffer.reserve( block ) ;
116 const std::streamsize b =
static_cast<std::streamsize
>(block) ;
117 std::streamsize size = 0U ;
118 while( ( limit == 0U || size < limit ) && in.good() && out.good() )
120 std::streamsize request = limit == 0U || (limit-size) > b ? b : (limit-size) ;
121 in.read( &buffer[0] , request ) ;
122 std::streamsize result = in.gcount() ;
125 out.write( &buffer[0] , result ) ;
132 in.clear( (in.rdstate() & ~
std::ios_base::failbit) | (in_state & std::ios_base::failbit) ) ;
137 if( ! mkdir( dir ,
NoThrow() ) )
138 throw CannotMkdir( dir.
str() ) ;
143 return exists( path ,
false ,
true ) ;
148 return exists( path ,
false ,
false ) ;
153 bool enoent = false ;
154 bool eaccess = false ;
155 bool rc = exists( path.
str().c_str() , enoent , eaccess ) ;
160 else if( !rc && do_throw )
162 throw StatError( path.
str() , eaccess?
"permission denied":
"" ) ;
173 return chmodx( path ,
false ) ;
178 chmodx( path ,
true ) ;
185 G_DEBUG(
"File::mkdirs: " << path ) ;
191 const bool mkdir_trial = true ;
196 G_DEBUG(
"File::mkdirs: mkdir(" << path <<
") -> ok" ) ;
205 if( path.
str().empty() )
211 G_DEBUG(
"File::mkdirs: mkdir(" << path <<
")" ) ;
212 bool ok = mkdir( path ,
NoThrow() ) ;
216 G_DEBUG(
"File::mkdirs: mkdir(" << path <<
") -> " << (ok?
"ok":
"failed") ) ;
222 if( ! mkdirs(path,
NoThrow(),limit) )
223 throw CannotMkdir(path.
str()) ;
228 std::ofstream f( path.
str().c_str() , std::ios_base::out | std::ios_base::app ) ;
231 throw CannotCreate( path.
str() ) ;
std::string str() const
Returns the path string.
static bool copy(const Path &from, const Path &to, const NoThrow &)
Copies a file. Returns false on error.
static void create(const Path &)
Creates an empty file. Throws on error.
static bool rename(const Path &from, const Path &to, const NoThrow &)
Renames the file. Returns false on error.
static bool mkdirs(const Path &dir, const NoThrow &, int=100)
Creates a directory and all necessary parents.
static bool exists(const Path &file)
Returns true if the file (directory, device etc.) exists.
Path dirname() const
Returns the path without the rightmost part, ignoring "." parts.
static bool mkdir(const Path &dir, const NoThrow &)
Creates a directory. Returns false on error.
static bool remove(const Path &path, const NoThrow &)
Deletes the file or directory. Returns false on error.
An overload discriminator class for File methods.
static void chmodx(const Path &file)
Makes the file executable.
A Path object represents a file system path.