E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

gfile.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gfile.h
19 ///
20
21 #ifndef G_FILE_H
22 #define G_FILE_H
23
24 #include "gdef.h"
25 #include "gpath.h"
26 #include "gexception.h"
27 #include "gdatetime.h"
28 #include <cstdio> // std::remove()
29
30 namespace G
31 {
32 class File ;
33 class DirectoryIteratorImp ;
34 }
35
36 /// \class G::File
37 /// A simple static class for dealing with files.
38 /// \see G::Path, G::FileSystem, G::Directory
39 ///
40 class G::File
41 {
42 public:
43 G_EXCEPTION( StatError , "cannot access file" ) ;
44 G_EXCEPTION( CannotRemove , "cannot delete file" ) ;
45 G_EXCEPTION( CannotRename , "cannot rename file" ) ;
46 G_EXCEPTION( CannotCopy , "cannot copy file" ) ;
47 G_EXCEPTION( CannotMkdir , "cannot mkdir" ) ;
48 G_EXCEPTION( CannotChmod , "cannot chmod file" ) ;
49 G_EXCEPTION( CannotLink , "cannot create symlink" ) ;
50 G_EXCEPTION( CannotCreate , "cannot create empty file" ) ;
51 G_EXCEPTION( SizeOverflow , "file size overflow" ) ;
52 G_EXCEPTION( TimeError , "cannot get file modification time" ) ;
53 class NoThrow /// An overload discriminator class for File methods.
54 {} ;
55
56 static bool remove( const Path & path , const NoThrow & ) ;
57 ///< Deletes the file or directory. Returns false on error.
58
59 static void remove( const Path & path ) ;
60 ///< Deletes the file or directory. Throws an exception on error.
61
62 static bool rename( const Path & from , const Path & to , const NoThrow & ) ;
63 ///< Renames the file. Returns false on error.
64
65 static void rename( const Path & from , const Path & to , bool ignore_missing = false ) ;
66 ///< Renames the file. Throws on error, but optionally
67 ///< ignores errors caused by a missing 'from' file or
68 ///< missing 'to' directory component.
69
70 static bool copy( const Path & from , const Path & to , const NoThrow & ) ;
71 ///< Copies a file. Returns false on error.
72
73 static void copy( const Path & from , const Path & to ) ;
74 ///< Copies a file.
75
76 static void copy( std::istream & from , std::ostream & to ,
77 std::streamsize limit = 0U , std::string::size_type block = 0U ) ;
78 ///< Copies a stream with an optional size limit.
79
80 static bool mkdirs( const Path & dir , const NoThrow & , int = 100 ) ;
81 ///< Creates a directory and all necessary parents. Returns false on error.
82 ///< Does chmodx() on all created directories.
83
84 static void mkdirs( const Path & dir , int = 100 ) ;
85 ///< Creates a directory and all necessary parents.
86 ///< Does chmodx() on all created directories.
87
88 static bool mkdir( const Path & dir , const NoThrow & ) ;
89 ///< Creates a directory. Returns false on error.
90
91 static void mkdir( const Path & dir ) ;
92 ///< Creates a directory.
93
94 static std::string sizeString( const Path & file ) ;
95 ///< Returns the file's size in string format.
96 ///< Returns the empty string on error.
97
98 static bool exists( const Path & file ) ;
99 ///< Returns true if the file (directory, device etc.)
100 ///< exists. Symlinks are followed. Throws an exception
101 ///< if permission denied or too many symlinks etc.
102
103 static bool exists( const Path & file , const NoThrow & ) ;
104 ///< Returns true if the file (directory, device etc.)
105 ///< exists. Symlinks are followed. Returns false on error.
106
107 static bool isLink( const Path & path ) ;
108 ///< Returns true if the path is an existing symlink.
109
110 static bool isDirectory( const Path & path ) ;
111 ///< Returns true if the path exists() and is a directory.
112 ///< Symlinks are followed.
113
114 static EpochTime time( const Path & file ) ;
115 ///< Returns the file's timestamp.
116
117 static EpochTime time( const Path & file , const NoThrow & ) ;
118 ///< Returns the file's timestamp. Returns EpochTime(0)
119 ///< on error.
120
121 static void chmodx( const Path & file ) ;
122 ///< Makes the file executable.
123
124 static bool chmodx( const Path & file , const NoThrow & ) ;
125 ///< Makes the file executable.
126
127 static void link( const Path & target , const Path & new_link ) ;
128 ///< Creates a symlink.
129
130 static bool link( const Path & target , const Path & new_link , const NoThrow & ) ;
131 ///< Creates a symlink.
132
133 static bool executable( const Path & ) ;
134 ///< Returns true if the path is probably executable.
135 ///< Because of portability and implementation difficulties
136 ///< this does not return a definitive result so it should
137 ///< only used for generating warnings on a false return.
138
139 static void create( const Path & ) ;
140 ///< Creates an empty file. Throws on error.
141
142 private:
143 friend class G::DirectoryIteratorImp ;
144 static std::string copy( const Path & , const Path & , int ) ;
145 static std::string sizeString( g_uint32_t hi , g_uint32_t lo ) ; // win32
146 static bool exists( const Path & , bool , bool ) ;
147 static bool exists( const char * , bool & , bool & ) ; // o/s-specific
148 static bool rename( const char * from , const char * to , bool & enoent ) ;
149 static bool chmodx( const Path & file , bool ) ;
150 static int link( const char * , const char * ) ;
151 static bool linked( const Path & , const Path & ) ;
152 } ;
153
154 #endif
G::EpochTime
A subsecond-resolution timestamp based on a time_t.
Definition: gdatetime.h:39
G::File::copy
static bool copy(const Path &from, const Path &to, const NoThrow &)
Copies a file. Returns false on error.
Definition: gfile.cpp:79
G::File::isDirectory
static bool isDirectory(const Path &path)
Returns true if the path exists() and is a directory.
Definition: gfile_unix.cpp:73
G::File::create
static void create(const Path &)
Creates an empty file. Throws on error.
Definition: gfile.cpp:226
G::File::isLink
static bool isLink(const Path &path)
Returns true if the path is an existing symlink.
Definition: gfile_unix.cpp:67
G::File
A simple static class for dealing with files.
Definition: gfile.h:40
G::File::rename
static bool rename(const Path &from, const Path &to, const NoThrow &)
Renames the file. Returns false on error.
Definition: gfile.cpp:45
G::File::mkdirs
static bool mkdirs(const Path &dir, const NoThrow &, int=100)
Creates a directory and all necessary parents.
Definition: gfile.cpp:181
G::File::executable
static bool executable(const Path &)
Returns true if the path is probably executable.
Definition: gfile_unix.cpp:79
G
Low-level classes.
Definition: garg.h:29
G::File::exists
static bool exists(const Path &file)
Returns true if the file (directory, device etc.) exists.
Definition: gfile.cpp:141
G::File::mkdir
static bool mkdir(const Path &dir, const NoThrow &)
Creates a directory. Returns false on error.
Definition: gfile_unix.cpp:45
G::File::NoThrow
An overload discriminator class for File methods.
Definition: gfile.h:53
gdatetime.h
gexception.h
G::File::chmodx
static void chmodx(const Path &file)
Makes the file executable.
Definition: gfile.cpp:176
gpath.h
G::File::link
static void link(const Path &target, const Path &new_link)
Creates a symlink.
Definition: gfile_unix.cpp:145
G::File::sizeString
static std::string sizeString(const Path &file)
Returns the file's size in string format.
Definition: gfile_unix.cpp:96
G::DirectoryIteratorImp
A pimple-pattern implementation class for DirectoryIterator using opendir()/readdir().
Definition: gdirectory_unix.cpp:44
G::File::time
static EpochTime time(const Path &file)
Returns the file's timestamp.
Definition: gfile_unix.cpp:107
G::Path
A Path object represents a file system path.
Definition: gpath.h:72