45 void open( T & io ,
const Path & path , std::ios_base::openmode mode )
47 nowide::open( io , path , mode ) ;
49 int open(
const Path & path ,
int flags ,
int pmode ,
bool inherit )
51 return nowide::open( path , flags , pmode , inherit ) ;
53 void uninherited( HANDLE h )
56 SetHandleInformation( h , HANDLE_FLAG_INHERIT , 0 ) ;
58 HANDLE handle(
int fd )
60 return fd >= 0 ?
reinterpret_cast<HANDLE
>( _get_osfhandle(fd) ) : HNULL ;
62 int fd( std::FILE * fp )
64 return fp ? _fileno( fp ) : -1 ;
66 std::FILE * fopen(
const Path & path ,
const char * mode )
68 std::FILE * fp = nowide::fopen( path , mode ) ;
69 uninherited( handle(fd(fp)) ) ;
75void G::File::open( std::ofstream & ofstream ,
const Path & path )
77 FileImp::open( ofstream , path , std::ios_base::out | std::ios_base::binary ) ;
80void G::File::open( std::ofstream & ofstream ,
const Path & path , Text )
82 FileImp::open( ofstream , path , std::ios_base::out ) ;
85void G::File::open( std::ofstream & ofstream ,
const Path & path , Append )
87 FileImp::open( ofstream , path , std::ios_base::app | std::ios_base::binary ) ;
90void G::File::open( std::ifstream & ifstream ,
const Path & path )
92 FileImp::open( ifstream , path , std::ios_base::in | std::ios_base::binary ) ;
95void G::File::open( std::ifstream & ifstream ,
const Path & path , Text )
97 FileImp::open( ifstream , path , std::ios_base::in ) ;
100std::filebuf *
G::File::open( std::filebuf & fb ,
const Path & path , InOut inout )
103 FileImp::open( fb , path , std::ios_base::in | std::ios_base::binary ) :
104 FileImp::open( fb , path ,
std::ios_base::out |
std::ios_base::binary ) ;
105 return fb.is_open() ? &fb : nullptr ;
108int G::File::open(
const Path & path , InOutAppend mode ,
bool inherit )
noexcept
112 const int pmode = _S_IREAD | _S_IWRITE ;
113 if( mode == InOutAppend::In )
114 return FileImp::open( path , _O_RDONLY|_O_BINARY , pmode , inherit ) ;
115 else if( mode == InOutAppend::Out )
116 return FileImp::open( path , _O_WRONLY|_O_CREAT|_O_TRUNC|_O_BINARY , pmode , inherit ) ;
117 else if( mode == InOutAppend::OutNoCreate )
118 return FileImp::open( path , _O_WRONLY|_O_BINARY , pmode , inherit ) ;
120 return FileImp::open( path , _O_WRONLY|_O_CREAT|_O_APPEND|_O_BINARY , pmode , inherit ) ;
128int G::File::open(
const Path & path , CreateExclusive )
noexcept
132 const int pmode = _S_IREAD | _S_IWRITE ;
133 const bool inherit = false ;
134 return FileImp::open( path , _O_WRONLY|_O_CREAT|_O_EXCL|_O_BINARY , pmode , inherit ) ;
142std::FILE *
G::File::fopen(
const Path & path ,
const char * mode )
noexcept
147 return FileImp::fopen( path , mode ) ;
159 const int pmode = _S_IREAD | _S_IWRITE ;
160 const bool inherit = false ;
161 int fd = FileImp::open( path , _O_WRONLY|_O_CREAT|_O_EXCL|O_TEMPORARY|_O_BINARY , pmode , inherit ) ;
174 const int pmode = _S_IREAD | _S_IWRITE ;
175 const bool inherit = false ;
176 int fd = FileImp::open( path , _O_RDONLY|_O_CREAT , pmode , inherit ) ;
178 throw CannotCreate( path.str() ) ;
186 bool ok = nowide::rename( from , to ) ;
188 if( !ok && error == EEXIST )
191 ok = nowide::rename( from , to ) ;
201ssize_t
G::File::read(
int fd ,
char * p , std::size_t n )
noexcept
203 constexpr std::size_t limit = std::numeric_limits<unsigned int>::max() ;
204 unsigned int un =
static_cast<unsigned int>(std::min(limit,n)) ;
205 return _read( fd , p , un ) ;
208ssize_t
G::File::write(
int fd ,
const char * p , std::size_t n )
noexcept
210 constexpr std::size_t limit = std::numeric_limits<unsigned int>::max() ;
211 unsigned int un =
static_cast<unsigned int>(std::min(limit,n)) ;
212 return _write( fd , p , un ) ;
222 bool ok = nowide::remove( path ) ;
227 ok = nowide::rmdir( path ) ;
234 bool ok = remove( path , std::nothrow ) ;
238 G_WARNING(
"G::File::remove: cannot remove [" << path <<
"]: " <<
Process::strerror(e) ) ;
245 return nowide::remove( Path(arg.str()) ) ;
248int G::File::mkdirImp(
const Path & dir )
noexcept
252 int rc = nowide::mkdir( dir ) ;
260 if( e == 0 ) e = EINVAL ;
270G::File::Stat G::File::statImp(
const char * path ,
bool )
noexcept
275 nowide::statbuf_type statbuf {} ;
276 if( 0 == nowide::stat( path , &statbuf ) )
281 s.is_dir = (statbuf.st_mode & S_IFDIR) ;
282 s.is_link = !s.is_dir ;
283 s.is_executable = (statbuf.st_mode & _S_IEXEC) ;
284 s.is_empty = statbuf.st_size == 0 ;
285 s.mtime_s =
static_cast<std::time_t
>(statbuf.st_mtime) ;
287 s.mode =
static_cast<unsigned long>( statbuf.st_mode & 07777 ) ;
288 s.size =
static_cast<unsigned long long>( statbuf.st_size ) ;
289 s.blocks =
static_cast<unsigned long long>( statbuf.st_size >> 24 ) ;
294 s.error = error ? error : EINVAL ;
310bool G::File::existsImp(
const char * path ,
bool & enoent ,
bool & eaccess )
noexcept
314 Stat s = statImp( path ) ;
318 eaccess = s.eaccess ;
320 return s.error == 0 ;
341bool G::File::chgrp(
const Path & ,
const std::string & , std::nothrow_t )
363 throw CannotLink( new_link.str() ,
"not supported" ) ;
366bool G::File::link(
const Path & ,
const Path & , std::nothrow_t )
371std::streamoff
G::File::seek(
int fd , std::streamoff offset , Seek origin )
noexcept
373 off_t rc = _lseek( fd ,
static_cast<off_t
>(offset) ,
374 origin == Seek::Start ? SEEK_SET : ( origin == Seek::End ? SEEK_END : SEEK_CUR ) ) ;
375 return static_cast<std::streamoff
>(rc) ;
static void open(std::ofstream &, const Path &)
Calls open() on the given output file stream.
static void close(int fd) noexcept
Calls ::close() or equivalent.
static void link(const Path &target, const Path &new_link)
Creates a symlink.
static std::streamoff seek(int fd, std::streamoff offset, Seek) noexcept
Does ::lseek() or equivalent.
static ssize_t write(int fd, const char *, std::size_t) noexcept
Calls ::write() or equivalent.
static void chmod(const Path &file, const std::string &spec)
Sets the file permissions.
static bool remove(const Path &path, std::nothrow_t) noexcept
Deletes the file or directory. Returns false on error.
static void chmodx(const Path &file)
Makes the file executable. Throws on error.
static ssize_t read(int fd, char *, std::size_t) noexcept
Calls read() or equivalent.
static bool renameOnto(const Path &from, const Path &to, std::nothrow_t) noexcept
Renames the file, deleting 'to' first if necessary.
static void create(const Path &)
Creates the file if it does not exist.
static G::Path readlink(const Path &link)
Reads a symlink. Throws on error.
static std::FILE * fopen(const Path &, const char *mode) noexcept
Calls std::fopen().
static bool hardlink(const Path &src, const Path &dst, std::nothrow_t)
Creates a hard link.
static bool probe(const Path &) noexcept
Creates and deletes a temporary probe file.
static bool cleanup(const Cleanup::Arg &path_arg) noexcept
Deletes the file.
static void chgrp(const Path &file, const std::string &group)
Sets the file group ownership. Throws on error.
A Path object represents a file system path.
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.
Contains inline functions that convert to and from UTF-8 strings in order to call wide-character "W()...
A portable 'struct stat'.