42 void open( T & io ,
const char * path , std::ios_base::openmode mode )
44 #if GCONFIG_HAVE_EXTENDED_OPEN
45 io.open( path , mode , _SH_DENYNO ) ;
47 io.open( path , mode ) ;
50 int open(
const char * path ,
int flags ,
int pmode )
noexcept
52 #if GCONFIG_HAVE_SOPEN_S
55 errno_t rc = _sopen_s( &fd , path , flags | _O_NOINHERIT , _SH_DENYNO , pmode ) ;
56 return rc == 0 ? fd : -1 ;
58 #if GCONFIG_HAVE_SOPEN
59 return _sopen( path , flags | _O_NOINHERIT , _SH_DENYNO , pmode ) ;
61 return _open( path , flags | _O_NOINHERIT , pmode ) ;
65 void uninherited( HANDLE h )
68 SetHandleInformation( h , HANDLE_FLAG_INHERIT , 0 ) ;
70 HANDLE handle(
int fd )
72 return fd >= 0 ?
reinterpret_cast<HANDLE
>( _get_osfhandle(fd) ) : HNULL ;
74 int fd( std::FILE * fp )
76 return fp ? _fileno( fp ) : -1 ;
78 std::FILE * fopen(
const char * path ,
const char * mode )
noexcept
80 std::FILE * fp = nullptr ;
81 #if GCONFIG_HAVE_FSOPEN
82 fp = _fsopen( path , mode , _SH_DENYNO ) ;
84 #if GCONFIG_HAVE_FOPEN_S
85 errno_t e = fopen_s( &fp , path , mode ) ;
89 fp = std::fopen( path , mode ) ;
92 uninherited( handle(fd(fp)) ) ;
98void G::File::open( std::ofstream & ofstream ,
const Path & path )
100 FileImp::open( ofstream , path.cstr() , std::ios_base::out | std::ios_base::binary ) ;
103void G::File::open( std::ofstream & ofstream ,
const Path & path , Text )
105 FileImp::open( ofstream , path.cstr() , std::ios_base::out ) ;
108void G::File::open( std::ofstream & ofstream ,
const Path & path , Append )
110 FileImp::open( ofstream , path.cstr() , std::ios_base::app | std::ios_base::binary ) ;
113void G::File::open( std::ifstream & ifstream ,
const Path & path )
115 FileImp::open( ifstream , path.cstr() , std::ios_base::in | std::ios_base::binary ) ;
118void G::File::open( std::ifstream & ifstream ,
const Path & path , Text )
120 FileImp::open( ifstream , path.cstr() , std::ios_base::in ) ;
123std::filebuf *
G::File::open( std::filebuf & fb ,
const Path & path , InOut inout )
126 FileImp::open( fb , path.cstr() , std::ios_base::in | std::ios_base::binary ) :
127 FileImp::open( fb , path.cstr() ,
std::ios_base::out |
std::ios_base::binary ) ;
128 return fb.is_open() ? &fb : nullptr ;
131int G::File::open(
const char * path , InOutAppend mode )
noexcept
133 int pmode = _S_IREAD | _S_IWRITE ;
134 if( mode == InOutAppend::In )
135 return FileImp::open( path , _O_RDONLY|_O_BINARY , pmode ) ;
136 else if( mode == InOutAppend::Out )
137 return FileImp::open( path , _O_WRONLY|_O_CREAT|_O_TRUNC|_O_BINARY , pmode ) ;
139 return FileImp::open( path , _O_WRONLY|_O_CREAT|_O_APPEND|_O_BINARY , pmode ) ;
142int G::File::open(
const char * path , CreateExclusive )
noexcept
144 int pmode = _S_IREAD | _S_IWRITE ;
145 return FileImp::open( path , _O_WRONLY|_O_CREAT|_O_EXCL|_O_BINARY , pmode ) ;
148std::FILE *
G::File::fopen(
const char * path ,
const char * mode )
noexcept
150 G_ASSERT( path && mode ) ;
151 return FileImp::fopen( path , mode ) ;
156 int pmode = _S_IREAD | _S_IWRITE ;
157 int fd = FileImp::open( path , _O_WRONLY|_O_CREAT|_O_EXCL|O_TEMPORARY|_O_BINARY , pmode ) ;
165 int fd = FileImp::open( path.cstr() , _O_RDONLY|_O_CREAT , _S_IREAD|_S_IWRITE ) ;
167 throw CannotCreate( path.str() ) ;
173 bool ok = 0 == std::rename( from.cstr() , to.cstr() ) ;
175 if( !ok && error == EEXIST )
177 std::remove( to.cstr() ) ;
178 ok = 0 == std::rename( from.cstr() , to.cstr() ) ;
183ssize_t
G::File::read(
int fd ,
char * p , std::size_t n )
noexcept
185 constexpr std::size_t limit = std::numeric_limits<unsigned int>::max() ;
186 unsigned int un =
static_cast<unsigned int>(std::min(limit,n)) ;
187 return _read( fd , p , un ) ;
190ssize_t
G::File::write(
int fd ,
const char * p , std::size_t n )
noexcept
192 constexpr std::size_t limit = std::numeric_limits<unsigned int>::max() ;
193 unsigned int un =
static_cast<unsigned int>(std::min(limit,n)) ;
194 return _write( fd , p , un ) ;
202int G::File::mkdirImp(
const Path & dir )
noexcept
204 int rc = _mkdir( dir.cstr() ) ;
212 if( e == 0 ) e = EINVAL ;
217G::File::Stat G::File::statImp(
const char * path ,
bool )
noexcept
220 struct _stat64 statbuf {} ;
221 if( 0 == _stat64( path , &statbuf ) )
226 s.is_dir = (statbuf.st_mode & S_IFDIR) ;
227 s.is_link = !s.is_dir ;
228 s.is_executable = (statbuf.st_mode & _S_IEXEC) ;
229 s.is_empty = statbuf.st_size == 0 ;
230 s.mtime_s =
static_cast<std::time_t
>(statbuf.st_mtime) ;
232 s.mode =
static_cast<unsigned long>( statbuf.st_mode & 07777 ) ;
233 s.size =
static_cast<unsigned long long>( statbuf.st_size ) ;
234 s.blocks =
static_cast<unsigned long long>( statbuf.st_size >> 24 ) ;
239 s.error = error ? error : EINVAL ;
246bool G::File::existsImp(
const char * path ,
bool & enoent ,
bool & eaccess )
noexcept
248 Stat s = statImp( path ) ;
252 eaccess = s.eaccess ;
254 return s.error == 0 ;
270bool G::File::chgrp(
const Path & ,
const std::string & , std::nothrow_t )
292 throw CannotLink( new_link.str() ,
"not supported" ) ;
295bool G::File::link(
const Path & ,
const Path & , std::nothrow_t )
300std::streamoff
G::File::seek(
int fd , std::streamoff offset , Seek origin )
noexcept
302 off_t rc = _lseek( fd ,
static_cast<off_t
>(offset) ,
303 origin == Seek::Start ? SEEK_SET : ( origin == Seek::End ? SEEK_END : SEEK_CUR ) ) ;
304 return static_cast<std::streamoff
>(rc) ;
static std::FILE * fopen(const char *, const char *mode) noexcept
Calls std::fopen().
static bool probe(const char *) noexcept
Creates and deletes a temporary probe file.
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 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 bool hardlink(const Path &src, const Path &dst, std::nothrow_t)
Creates a hard link.
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 int errno_(const SignalSafe &=G::SignalSafe()) noexcept
Returns the process's current 'errno' value.
A portable 'struct stat'.