casacore
Loading...
Searching...
No Matches
File.h
Go to the documentation of this file.
1// # File.h: Class to get file information and a base for other file classes
2// # Copyright (C) 1993,1994,1995,1996,2000,2003
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_FILE_H
27#define CASA_FILE_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/OS/Path.h>
32#include <casacore/casa/BasicSL/String.h>
33#include <atomic>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37// <summary>
38// Class to get file information and a base for other file classes.
39// </summary>
40// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
41// </reviewed>
42
43// <use visibility=export>
44
45// <prerequisite>
46// <li> Basic knowledge of the UNIX file system
47// <li> <linkto class=Path>Path</linkto>
48// </prerequisite>
49
50// <etymology>
51// 'File' is used in a traditional sense.
52// </etymology>
53
54// <synopsis>
55// The File class provides the primary functions needed by all kinds of
56// files (directories, regular files, symbolic links, named pipes etc.).
57// These shared functions serve mostly to return information about a
58// particular file -- for instance, its type, its ownership, read, write
59// and execute permissions, date of latest access and the path on secundary
60// storage associated with this file. Every file object has, by definition,
61// a <linkto class=Path>Path</linkto> object associated with it which
62// defines the file name.
63// <p>
64// See also the derived classes
65// <linkto class=RegularFile>RegularFile</linkto>,
66// <linkto class=Directory>Directory</linkto>, and
67// <linkto class=SymLink>SymLink</linkto>.
68// <br>
69// This class does not contain virtual functions, because a lot of functions
70// have different parameters, e.g. 'create' for RegularFile has one parameter
71// and 'create' for SymLink has two parameters.
72//
73// It handles large files correctly.
74// </synopsis>
75
76// <example>
77// <srcblock>
78// File myFile("someFileName");
79// if (myFile.exists()) {
80// myFile.setPermissions(0644);
81// if (myFile.isRegular()) {
82// cout << "this file is a regular file" << endl;
83// }
84// }
85// else if (!myFile.exists()) {
86// if (!myFile.canCreate()){
87// cout << "cannot create this file" << endl;
88// }
89// }
90// </srcblock>
91// </example>
92
93// <motivation>
94// File systems operations are a notorious source of porting problems.
95// The file class provides a standard interface for programmers to use.
96// </motivation>
97
98class File {
99 public:
101 // file exists and can be overwritten
103 // file exists but cannot be overwritten
105 // file does not exist and is creatable
107 // file does not exist but cannot be created
109 };
110
111 // Construct a File object whose Path is set to the current working
112 // directory.
114
115 // Construct a File object whose Path is set to the given Path.
116 // <group>
117 File(const Path& path);
118 File(const String& path);
119 // </group>
120
121 // Copy constructor (copy semantics).
122 File(const File& that);
123
124 virtual ~File();
125
126 // Assignment (copy semantics).
127 File& operator=(const File& that);
128
129 // Returns the pathname of the file.
130 const Path& path() const;
131
132 // Check if the file is a regular file. If the boolean followSymLink is
133 // False a symbolic link will not be followed.
134 Bool isRegular(Bool followSymLink = True) const;
135
136 // Check if the file is a directory. If the boolean followSymLink is
137 // False a symbolic link will not be followed.
138 Bool isDirectory(Bool followSymLink = True) const;
139
140 // Check if the file is a symbolic link.
142
143 // Check if the file is a pipe.
144 Bool isPipe() const;
145
146 // Check if the file is a character special file.
148
149 // Check if the file is a block special file.
151
152 // Check if the file is a socket.
153 Bool isSocket() const;
154
155 // Check if the file exists.
156 Bool exists() const;
157
158 // Check if the file is readable.
160
161 // Check if the file is writable.
163
164 // Check if the file is executable.
166
167 // Check if a file can be created.
169
170 // Return the userID of the file.
171 long userID() const;
172
173 // Return the groupID of the file.
174 long groupID() const;
175
176 // Return the size of the file. If the file
177 // does not exist, an exception will be thrown.
178 virtual Int64 size() const;
179
180 // Return the permissions as a decimal value.
182
183 // Set permission with perm. Perm is an octal value.
184 void setPermissions(uInt permissions);
185
186 // Update access time and modification time of a file.
188
189 // Update access time and modification time of a file. This function
190 // updates the file with the current time.
191 void touch();
192
193 // Time related fucnctions:
194 // Return the time when the file was last accessed in seconds since
195 // 00:00:00 GMT Jan 1, 1970.
197
198 // Return the time when the file was last accessed
199 // as a 26-characters String of the form:
200 // Thu Feb 3 13:40:11 1994
202
203 // Return the time when the file was last modified in seconds since
204 // 00:00:00 GMT Jan 1, 1970.
206
207 // Return the time when the file was last modified
208 // as a 26-characters String of the form:
209 // Thu Feb 3 13:40:11 1994
211
212 // Return the time when the file status was last changed in seconds since
213 // 00:00:00 GMT Jan 1, 1970.
214 // It is set both by writing and changing the file status information,
215 // such as changes of owner, group, link count, or mode.
217
218 // return the time when the file status was last changed
219 // as a 26-characters String of the form:
220 // Thu Feb 3 13:40:11 1994
222
223 // Create a new unique path name in the specified directory, with
224 // the specified prefix and random trailing characters:
225 // <srcblock>
226 // p.newUniqueName ("./", "temp") --> "./tempAAA00xx32"
227 // p.newUniqueName ("/home/me", "diary") --> "/home/me/diaryAAA00xxb0"
228 // </srcblock>
229 static Path newUniqueName(const String& directory, const String& prefix);
230
231 // Create a new unique filename without a prefix.
232 // As above, but all the characters in the filename are random:
233 // <srcblock>
234 // p.newUniqueName ("./") --> "./AAA00xx32"
235 // p.newUniqueName ("/home/me") --> "/home/me/AAA00xxb0"
236 // </srcblock>
237 static Path newUniqueName(const String& directory);
238
239 // get write status of the file.
240 // OVERWRITABLE - file exists and can be overwritten
241 // NOT_OVERWRITABLE - file exists but cannot be overwritten
242 // CREATABLE - File does not exist and can be created
243 // NOT_CREATABLE - file does not exist and cannot be created.
245
246 // Return the filesystem type.
247 // If the file doesn't exsist crawl up the directory tree to
248 // find one that does.
250
251 protected:
252 // This function is used by <linkto class=RegularFile>RegularFile</linkto>
253 // and <linkto class=Directory>Directory</linkto> to remove all the links
254 // which, when followed, ultimately resolve to a Directory or a
255 // RegularFile.
256 // For example, A->B, B->C, C->D and D points to a regular file.
257 // When remove() is called for a regular file A,
258 // that function uses removeLinks() to remove A, B, C and D.
260
261 // Check if the new path for a copy or move is valid.
262 // An exception is thrown if:
263 // <br>- the target directory is not writable
264 // <br>- or the target file already exists and overwrite==False
265 // <br>- or the target file already exists and is not writable
266 // <br>When the targetName represents a directory, the basename
267 // of the file is appended to it. This is done to cover the
268 // case where the source is a symlink to a file. In that case
269 // the target will get the basename of the symlink and not the
270 // the basename of the file pointed to. This is not done when
271 // forDirectory==True (which is used by class Directory).
272 void checkTarget(Path& targetName, Bool overwrite, Bool forDirectory = False) const;
273
274 private:
275 // Define a function for lstat.
276 // This is necessary since SunOS4.1.x prototypes lstat() with a first
277 // argument of type (char*), while Solaris (and presumably all other
278 // reasonable OS's) prototype it with a first argument of type
279 // (const char*). Since lstat() does not change its first argument,
280 // it is safe to convert our const variable to a non-const one so that
281 // we can call lstat() successfully.
282 // <br>It is also useful to be able to pass the buffer as void*. In that
283 // way the 32-bit or 64-bit file details are only needed in the cc file.
284 int mylstat(const char* path, void* buf) const;
285
286 // Get the lstat of this file.
287 // Throw an exception when it fails.
288 void getstat(void* buf) const;
289
290 // Get the lstat of a file.
291 // Throw an exception when it fails.
292 void getstat(const File& file, void* buf) const;
293
294 // Full pathname of the file.
296 // A sequence number to generate unique file names.
297 static std::atomic<uInt> uniqueSeqnr_p;
298};
299
300inline const Path& File::path() const { return itsPath; }
301
302inline void File::getstat(void* buf) const { getstat(*this, buf); }
303
304// # The ifdef's below are similar to those in IO/LargeIOFuncDef.h.
305#if !defined(AIPS_NOLARGEFILE)
306#ifdef AIPS_LINUX
307#if !defined(_LARGEFILE64_SOURCE)
308#define _LARGEFILE64_SOURCE
309#endif
310#endif
311#if defined(AIPS_DARWIN) || defined(AIPS_BSD)
312#define fileFSTAT fstat
313#define fileLSTAT lstat
314#define fileSTAT stat
315#define fileSTATFS statfs
316#else
317#define fileFSTAT fstat64
318#define fileLSTAT lstat64
319#define fileSTAT stat64
320#define fileSTATFS statfs64
321#endif
322#else
323#define fileFSTAT fstat
324#define fileLSTAT lstat
325#define fileSTAT stat
326#define fileSTATFS statfs
327#endif
328
329} // namespace casacore
330
331#endif
String getFSType() const
Return the filesystem type.
File(const Path &path)
Construct a File object whose Path is set to the given Path.
Bool canCreate() const
Check if a file can be created.
Bool isPipe() const
Check if the file is a pipe.
uInt statusChangeTime() const
Return the time when the file status was last changed in seconds since 00:00:00 GMT Jan 1,...
File(const String &path)
void removeSymLinks()
This function is used by RegularFile and Directory to remove all the links which, when followed,...
void getstat(void *buf) const
Get the lstat of this file.
Definition File.h:302
uInt accessTime() const
Time related fucnctions: Return the time when the file was last accessed in seconds since 00:00:00 GM...
Bool isBlockSpecial() const
Check if the file is a block special file.
FileWriteStatus getWriteStatus() const
get write status of the file.
Bool isWritable() const
Check if the file is writable.
uInt readPermissions() const
Return the permissions as a decimal value.
Bool isSocket() const
Check if the file is a socket.
Bool isSymLink() const
Check if the file is a symbolic link.
void checkTarget(Path &targetName, Bool overwrite, Bool forDirectory=False) const
Check if the new path for a copy or move is valid.
virtual Int64 size() const
Return the size of the file.
uInt modifyTime() const
Return the time when the file was last modified in seconds since 00:00:00 GMT Jan 1,...
void setPermissions(uInt permissions)
Set permission with perm.
void touch(uInt time)
Update access time and modification time of a file.
Bool isReadable() const
Check if the file is readable.
void touch()
Update access time and modification time of a file.
File(const File &that)
Copy constructor (copy semantics).
Bool isCharacterSpecial() const
Check if the file is a character special file.
String modifyTimeString() const
Return the time when the file was last modified as a 26-characters String of the form: Thu Feb 3 13:4...
static std::atomic< uInt > uniqueSeqnr_p
A sequence number to generate unique file names.
Definition File.h:297
virtual ~File()
long groupID() const
Return the groupID of the file.
void getstat(const File &file, void *buf) const
Get the lstat of a file.
String statusChangeTimeString() const
return the time when the file status was last changed as a 26-characters String of the form: Thu Feb ...
Bool isDirectory(Bool followSymLink=True) const
Check if the file is a directory.
@ NOT_OVERWRITABLE
file exists but cannot be overwritten
Definition File.h:104
@ OVERWRITABLE
file exists and can be overwritten
Definition File.h:102
@ NOT_CREATABLE
file does not exist but cannot be created
Definition File.h:108
@ CREATABLE
file does not exist and is creatable
Definition File.h:106
int mylstat(const char *path, void *buf) const
Define a function for lstat.
const Path & path() const
Returns the pathname of the file.
Definition File.h:300
String accessTimeString() const
Return the time when the file was last accessed as a 26-characters String of the form: Thu Feb 3 13:4...
Bool exists() const
Check if the file exists.
long userID() const
Return the userID of the file.
Bool isRegular(Bool followSymLink=True) const
Check if the file is a regular file.
static Path newUniqueName(const String &directory, const String &prefix)
Create a new unique path name in the specified directory, with the specified prefix and random traili...
Bool isExecutable() const
Check if the file is executable.
Path itsPath
Full pathname of the file.
Definition File.h:295
static Path newUniqueName(const String &directory)
Create a new unique filename without a prefix.
File & operator=(const File &that)
Assignment (copy semantics).
File()
Construct a File object whose Path is set to the current working directory.
String: the storage and methods of handling collections of characters.
Definition String.h:355
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
TableExprNode time(const TableExprNode &node)
Definition ExprNode.h:1368
unsigned int uInt
Definition aipstype.h:49
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41