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" ;
87 if( in.fail() || in.bad() )
91 return "write error" ;
95 if( sizeString(from) != sizeString(to) )
96 return "file size mismatch" ;
98 return std::string() ;
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 ) ;
121 in.clear( in.rdstate() & ~std::ios_base::failbit ) ;
126 if( ! mkdir( dir ,
NoThrow() ) )
127 throw CannotMkdir( dir.
str() ) ;
132 return exists( path ,
false ,
true ) ;
137 return exists( path ,
false ,
false ) ;
142 bool enoent = false ;
143 bool rc = exists( path.
str().c_str() , enoent ) ;
148 else if( !rc && do_throw )
150 throw StatError( path.
str() ) ;
161 return chmodx(path,
false) ;
172 G_DEBUG(
"File::mkdirs: " << path ) ;
173 if( limit == 0 )
return false ;
174 if( exists(path) )
return true ;
175 if( path.
str().empty() )
return true ;
176 if( ! mkdirs( path.
dirname() ,
NoThrow() , limit-1 ) )
return false ;
177 bool ok = mkdir( path ,
NoThrow() ) ;
178 if( ok ) chmodx( path ,
NoThrow() ) ;
184 if( ! mkdirs(path,
NoThrow(),limit) )
185 throw CannotMkdir(path.
str()) ;
190 std::ofstream f( path.
str().c_str() ) ;
193 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.
std::string::size_type size_type
A std::size_t type.
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, link, device etc.) exists.
Path dirname() const
Returns the drive/directory parts of the path.
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.