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
98
99class File
100{
101public:
102
104 // file exists and can be overwritten
106 // file exists but cannot be overwritten
108 // file does not exist and is creatable
110 // file does not exist but cannot be created
112 };
113
114
115 // Construct a File object whose Path is set to the current working
116 // directory.
118
119 // Construct a File object whose Path is set to the given Path.
120 // <group>
121 File (const Path& path);
122 File (const String& path);
123 // </group>
124
125 // Copy constructor (copy semantics).
126 File (const File& that);
127
128 virtual ~File();
129
130 // Assignment (copy semantics).
131 File& operator= (const File& that);
132
133 // Returns the pathname of the file.
134 const Path& path() const;
135
136 // Check if the file is a regular file. If the boolean followSymLink is
137 // False a symbolic link will not be followed.
138 Bool isRegular (Bool followSymLink = True) const;
139
140 // Check if the file is a directory. If the boolean followSymLink is
141 // False a symbolic link will not be followed.
142 Bool isDirectory (Bool followSymLink = True) const;
143
144 // Check if the file is a symbolic link.
146
147 // Check if the file is a pipe.
148 Bool isPipe() const;
149
150 // Check if the file is a character special file.
152
153 // Check if the file is a block special file.
155
156 // Check if the file is a socket.
157 Bool isSocket() const;
158
159 // Check if the file exists.
160 Bool exists() const;
161
162 // Check if the file is readable.
164
165 // Check if the file is writable.
167
168 // Check if the file is executable.
170
171 // Check if a file can be created.
173
174 // Return the userID of the file.
175 long userID() const;
176
177 // Return the groupID of the file.
178 long groupID() const;
179
180 // Return the size of the file. If the file
181 // does not exist, an exception will be thrown.
182 virtual Int64 size() const;
183
184 // Return the permissions as a decimal value.
186
187 // Set permission with perm. Perm is an octal value.
188 void setPermissions (uInt permissions);
189
190 // Update access time and modification time of a file.
192
193 // Update access time and modification time of a file. This function
194 // updates the file with the current time.
195 void touch();
196
197 // Time related fucnctions:
198 // Return the time when the file was last accessed in seconds since
199 // 00:00:00 GMT Jan 1, 1970.
201
202 // Return the time when the file was last accessed
203 // as a 26-characters String of the form:
204 // Thu Feb 3 13:40:11 1994
206
207 // Return the time when the file was last modified in seconds since
208 // 00:00:00 GMT Jan 1, 1970.
210
211 // Return the time when the file was last modified
212 // as a 26-characters String of the form:
213 // Thu Feb 3 13:40:11 1994
215
216 // Return the time when the file status was last changed in seconds since
217 // 00:00:00 GMT Jan 1, 1970.
218 // It is set both by writing and changing the file status information,
219 // such as changes of owner, group, link count, or mode.
221
222 // return the time when the file status was last changed
223 // as a 26-characters String of the form:
224 // Thu Feb 3 13:40:11 1994
226
227 // Create a new unique path name in the specified directory, with
228 // the specified prefix and random trailing characters:
229 // <srcblock>
230 // p.newUniqueName ("./", "temp") --> "./tempAAA00xx32"
231 // p.newUniqueName ("/home/me", "diary") --> "/home/me/diaryAAA00xxb0"
232 // </srcblock>
233 static Path newUniqueName (const String& directory, const String& prefix);
234
235 // Create a new unique filename without a prefix.
236 // As above, but all the characters in the filename are random:
237 // <srcblock>
238 // p.newUniqueName ("./") --> "./AAA00xx32"
239 // p.newUniqueName ("/home/me") --> "/home/me/AAA00xxb0"
240 // </srcblock>
241 static Path newUniqueName (const String& directory);
242
243
244 // get write status of the file.
245 // OVERWRITABLE - file exists and can be overwritten
246 // NOT_OVERWRITABLE - file exists but cannot be overwritten
247 // CREATABLE - File does not exist and can be created
248 // NOT_CREATABLE - file does not exist and cannot be created.
250
251 // Return the filesystem type.
252 // If the file doesn't exsist crawl up the directory tree to
253 // find one that does.
255
256protected:
257 // This function is used by <linkto class=RegularFile>RegularFile</linkto>
258 // and <linkto class=Directory>Directory</linkto> to remove all the links
259 // which, when followed, ultimately resolve to a Directory or a
260 // RegularFile.
261 // For example, A->B, B->C, C->D and D points to a regular file.
262 // When remove() is called for a regular file A,
263 // that function uses removeLinks() to remove A, B, C and D.
265
266 // Check if the new path for a copy or move is valid.
267 // An exception is thrown if:
268 // <br>- the target directory is not writable
269 // <br>- or the target file already exists and overwrite==False
270 // <br>- or the target file already exists and is not writable
271 // <br>When the targetName represents a directory, the basename
272 // of the file is appended to it. This is done to cover the
273 // case where the source is a symlink to a file. In that case
274 // the target will get the basename of the symlink and not the
275 // the basename of the file pointed to. This is not done when
276 // forDirectory==True (which is used by class Directory).
277 void checkTarget (Path& targetName, Bool overwrite,
278 Bool forDirectory = False) const;
279
280private:
281 // Define a function for lstat.
282 // This is necessary since SunOS4.1.x prototypes lstat() with a first
283 // argument of type (char*), while Solaris (and presumably all other
284 // reasonable OS's) prototype it with a first argument of type
285 // (const char*). Since lstat() does not change its first argument,
286 // it is safe to convert our const variable to a non-const one so that
287 // we can call lstat() successfully.
288 // <br>It is also useful to be able to pass the buffer as void*. In that
289 // way the 32-bit or 64-bit file details are only needed in the cc file.
290 int mylstat (const char* path, void* buf) const;
291
292 // Get the lstat of this file.
293 // Throw an exception when it fails.
294 void getstat (void* buf) const;
295
296 // Get the lstat of a file.
297 // Throw an exception when it fails.
298 void getstat (const File& file, void* buf) const;
299
300
301 // Full pathname of the file.
303 // A sequence number to generate unique file names.
304 static std::atomic<uInt> uniqueSeqnr_p;
305};
306
307
308inline const Path& File::path() const
309{
310 return itsPath;
311}
312
313inline void File::getstat (void* buf) const
314{
315 getstat (*this, buf);
316}
317
318
319
320//# The ifdef's below are similar to those in IO/LargeIOFuncDef.h.
321#if !defined(AIPS_NOLARGEFILE)
322# ifdef AIPS_LINUX
323# if !defined(_LARGEFILE64_SOURCE)
324# define _LARGEFILE64_SOURCE
325# endif
326# endif
327#if defined(AIPS_DARWIN) || defined(AIPS_BSD)
328# define fileFSTAT fstat
329# define fileLSTAT lstat
330# define fileSTAT stat
331# define fileSTATFS statfs
332#else
333# define fileFSTAT fstat64
334# define fileLSTAT lstat64
335# define fileSTAT stat64
336# define fileSTATFS statfs64
337#endif
338#else
339# define fileFSTAT fstat
340# define fileLSTAT lstat
341# define fileSTAT stat
342# define fileSTATFS statfs
343#endif
344
345
346
347} //# NAMESPACE CASACORE - END
348
349#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,...
void getstat(void *buf) const
Get the lstat of this file.
Definition File.h:313
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:304
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:107
@ OVERWRITABLE
file exists and can be overwritten
Definition File.h:105
@ NOT_CREATABLE
file does not exist but cannot be created
Definition File.h:111
@ CREATABLE
file does not exist and is creatable
Definition File.h:109
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:308
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:302
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:223
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
TableExprNode time(const TableExprNode &node)
Definition ExprNode.h:1584
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