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() ) )
41 throw CannotRemove( path.
str() ) ;
43 G_DEBUG(
"G::File::remove: \"" << path <<
"\"" ) ;
48 bool rc = 0 == std::rename( from.
str().c_str() , to.
str().c_str() ) ;
49 G_DEBUG(
"G::File::rename: \"" << from <<
"\" -> \"" << to <<
"\": success=" << rc ) ;
55 if( 0 != std::rename( from.
str().c_str() , to.
str().c_str() ) )
58 throw CannotRename( std::string() +
"[" + from.
str() +
"] to [" + to.
str() +
"]" ) ;
60 G_DEBUG(
"G::File::rename: \"" << from <<
"\" -> \"" << to <<
"\"" ) ;
65 std::string reason = copy( from , to , 0 ) ;
67 throw CannotCopy( std::string() +
"[" + from.
str() +
"] to [" + to.
str() +
"]: " + reason ) ;
72 return copy(from,to,0).empty() ;
77 std::ifstream in( from.
str().c_str() , std::ios::binary | std::ios::in ) ;
79 return "cannot open input file" ;
81 std::ofstream out( to.
str().c_str() , std::ios::binary | std::ios::out | std::ios::trunc ) ;
83 return "cannot open output file" ;
94 return "write error" ;
96 return std::string() ;
99 void G::File::copy( std::istream & in , std::ostream & out , std::streamsize limit , std::string::size_type block )
101 std::ios_base::iostate in_state = in.rdstate() ;
103 block = block ? block :
static_cast<std::string::size_type
>(limits::file_buffer) ;
104 std::vector<char> buffer ;
105 buffer.reserve( block ) ;
107 const std::streamsize b =
static_cast<std::streamsize
>(block) ;
108 std::streamsize size = 0U ;
109 while( ( limit == 0U || size < limit ) && in.good() && out.good() )
111 std::streamsize request = limit == 0U || (limit-size) > b ? b : (limit-size) ;
112 in.read( &buffer[0] , request ) ;
113 std::streamsize result = in.gcount() ;
116 out.write( &buffer[0] , result ) ;
123 in.clear( (in.rdstate() & ~std::ios_base::failbit) | (in_state & std::ios_base::failbit) ) ;
128 if( ! mkdir( dir ,
NoThrow() ) )
129 throw CannotMkdir( dir.
str() ) ;
134 return exists( path ,
false ,
true ) ;
139 return exists( path ,
false ,
false ) ;
144 bool enoent = false ;
145 bool eaccess = false ;
146 bool rc = exists( path.
str().c_str() , enoent , eaccess ) ;
151 else if( !rc && do_throw )
153 throw StatError( path.
str() , eaccess?
"permission denied":
"" ) ;
164 return chmodx( path ,
false ) ;
169 chmodx( path ,
true ) ;
176 G_DEBUG(
"File::mkdirs: " << path ) ;
182 const bool mkdir_trial = true ;
187 G_DEBUG(
"File::mkdirs: mkdir(" << path <<
") -> ok" ) ;
196 if( path.
str().empty() )
202 G_DEBUG(
"File::mkdirs: mkdir(" << path <<
")" ) ;
203 bool ok = mkdir( path ,
NoThrow() ) ;
207 G_DEBUG(
"File::mkdirs: mkdir(" << path <<
") -> " << (ok?
"ok":
"failed") ) ;
213 if( ! mkdirs(path,
NoThrow(),limit) )
214 throw CannotMkdir(path.
str()) ;
219 std::ofstream f( path.
str().c_str() , std::ios_base::out | std::ios_base::app ) ;
222 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.