casacore
RegularFile.h
Go to the documentation of this file.
1 //# RegularFile.h: Manipulate and get information about regular files
2 //# Copyright (C) 1993,1994,1995,1996,1997,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: aips2-request@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 //# $Id$
27 
28 #ifndef CASA_REGULARFILE_H
29 #define CASA_REGULARFILE_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
33 #include <casacore/casa/OS/Path.h>
34 #include <casacore/casa/OS/File.h>
35 #include <casacore/casa/BasicSL/String.h>
36 
37 
38 namespace casacore { //# NAMESPACE CASACORE - BEGIN
39 
40 // <summary>
41 // Manipulate and get information about regular files
42 // </summary>
43 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
44 // </reviewed>
45 
46 // <use visibility=export>
47 
48 // <prerequisite>
49 // <li>Basic knowledge of the UNIX file system
50 // <li><linkto class=File>File</linkto>
51 // </prerequisite>
52 
53 // <etymology>
54 // The class RegularFile provides functions for manipulating and getting
55 // information about regular files. Regular file are files which hold data.
56 // </etymology>
57 
58 // <synopsis>
59 // This class provides functions to manipulate and to get information about
60 // regular files. The functions for getting information (like ownership, dates)
61 // about regular files are inherited from the <linkto class=File>File</linkto>
62 // class.
63 // <br>
64 // The class RegularFile itself provides functions to rename, remove, copy,
65 // and move regular files. The file name can be a symbolic link resolving
66 // (eventually) to a regular file.
67 // </synopsis>
68 
69 // <example>
70 // <srcblock>
71 // // Create the object rFile
72 // RegularFile rFile ("isFile");
73 //
74 // // Create file; if the file exists it will be overwritten
75 // rFile.create (True);
76 // rFile.copy (newPath);
77 //
78 // cout << rFile.size() << endl; // Get the size of the file
79 // cout << rFile.path() << endl; // Show the relative pathname
80 //
81 // rFile.remove(); // remove the file
82 // </srcblock>
83 // </example>
84 
85 // <motivation>
86 // Provide functions for manipulating and getting information
87 // about regular files.
88 // </motivation>
89 
90 
91 class RegularFile: public File
92 {
93 public:
94 
95  // Default constructor sets path to . (working directory).
97 
98  // Create a regular file object for a file with the given path name.
99  // An exception is thrown if the file is illegal, i.e. if it does
100  // not exist as a regular file or symbolic link or if cannot be created.
101  // Note that the file is not created if it does not exist yet.
102  // This can be done using the function create.
103  // <br>
104  // When the given path name is a symbolic link, the symbolic link
105  // is resolved (recursively) and the resulting file name is used
106  // instead.
107  // <group>
109  RegularFile (const String& string);
110  RegularFile (const File& file);
111  // </group>
112 
113  // Copy constructor (copy semantics).
114  RegularFile (const RegularFile& regularFile);
115 
117 
118  // Assignment (copy semantics).
119  RegularFile& operator= (const RegularFile& regularFile);
120 
121  // Create the regular file.
122  // <br>If the file exists and is not a regular file, an
123  // exception is thrown. Otherwise if overwrite is true the regular file
124  // will be overwritten. If overwrite is false then nothing will be done.
125  // If the file does not exist, it is created.
126  void create (Bool overwrite = True);
127 
128  // Remove the file.
129  // If it does not exist, an exception will be thrown.
130  // If a symbolic link is given, the link chain pointing to the file
131  // will also be removed.
132  void remove();
133 
134  // Copy the file to the target path using the system command cp.
135  // If the file is a symbolic link, the regular file pointed to
136  // will be copied.
137  // The target path can be a directory or a file (as in cp).
138  // An exception is thrown if:
139  // <br>- the target directory is not writable
140  // <br>- or the target file already exists and overwrite==False
141  // <br>- or the target file already exists and is not writable
142  // <note role=caution>
143  // When a readonly file is copied, the resulting
144  // file is also readonly. Therefore <src>chmod</src> is used to
145  // set user write permission after the copy.
146  // The flag <src>setUserWritePermission</src> can be set to False
147  // when that should not be done.
148  // </note>
149  // <group>
150  void copy (const Path& target, Bool overwrite = True,
151  Bool setUserWritePermission = True) const;
152  void copy (const String& target, Bool overwrite = True,
153  Bool setUserWritePermission = True) const;
154  // </group>
155 
156  // Copy the file manually in case the cp command cannot be used.
157  // (like on the Cray XT3).
158  static void manualCopy (const String& source, const String& target);
159 
160  // Move the file to the target path using the system command mv.
161  // If the file is a symbolic link, the regular file pointed to
162  // will be moved.
163  // The target path can be a directory or a file (as in mv).
164  // An exception is thrown if:
165  // <br>- the target directory is not writable
166  // <br>- or the target file already exists and overwrite==False
167  // <br>- or the target file already exists and is not writable
168  // <note role=tip> The system command mv is used instead of the
169  // library function rename to be able to move across file systems.
170  // </note>
171  // <group>
172  void move (const Path& target, Bool overwrite = True);
173  void move (const String& target, Bool overwrite = True);
174  // </group>
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 private:
181  // Check if the path of the file is valid.
182  // Also resolve possible symlinks.
183  void checkPath();
184 
185  // This variable is used when a symbolic link points to the file.
187 };
188 
189 
190 inline void RegularFile::copy (const String& target, Bool overwrite,
191  Bool setUserWritePermission) const
192 {
193  copy (Path(target), overwrite, setUserWritePermission);
194 }
195 inline void RegularFile::move (const String& target, Bool overwrite)
196 {
197  move (Path(target), overwrite);
198 }
199 
200 
201 
202 } //# NAMESPACE CASACORE - END
203 
204 #endif
const Path & path() const
Returns the pathname of the file.
Definition: File.h:310
RegularFile(const File &file)
void move(const Path &target, Bool overwrite=True)
Move the file to the target path using the system command mv.
void remove()
Remove the file.
static void manualCopy(const String &source, const String &target)
Copy the file manually in case the cp command cannot be used.
RegularFile & operator=(const RegularFile &regularFile)
Assignment (copy semantics).
RegularFile(const RegularFile &regularFile)
Copy constructor (copy semantics).
RegularFile()
Default constructor sets path to.
void copy(const Path &target, Bool overwrite=True, Bool setUserWritePermission=True) const
Copy the file to the target path using the system command cp.
File itsFile
This variable is used when a symbolic link points to the file.
Definition: RegularFile.h:186
virtual Int64 size() const
Return the size of the file.
RegularFile(const Path &path)
Create a regular file object for a file with the given path name.
RegularFile(const String &string)
void create(Bool overwrite=True)
Create the regular file.
void checkPath()
Check if the path of the file is valid.
String: the storage and methods of handling collections of characters.
Definition: String.h:225
this file contains all the compiler specific defines
Definition: mainpage.dox:28
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size)
Definition: aipsxtype.h:38
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
const Bool True
Definition: aipstype.h:43