casacore
Loading...
Searching...
No Matches
Directory.h
Go to the documentation of this file.
1// # Directory.h: Get information about, and manipulate directories
2// # Copyright (C) 1996,1997,1999
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_DIRECTORY_H
27#define CASA_DIRECTORY_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Arrays/ArrayFwd.h>
32#include <casacore/casa/OS/Path.h>
33#include <casacore/casa/OS/File.h>
34
35namespace casacore { // # NAMESPACE CASACORE - BEGIN
36
37class Regex;
38class String;
39
40// <summary>
41// Get information about, and manipulate directories
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// <synopsis>
54// Directory provides functions to manipulate and to get information about
55// directories. The functions for getting information (like ownership, dates)
56// about directories are inherited from the <linkto class=File>File</linkto>
57// class.
58// Directory itself provides functions to create, copy, move, or remove
59// a directory. The file name can be a symbolic link resolving
60// (eventually) to a directory.
61// <p>
62// A separate class <linkto class=DirectoryIterator>DirectoryIterator</linkto>
63// allows one to traverse a directory to get the file names in it.
64// </synopsis>
65
66// <example>
67// <srcblock>
68// Directory dir("someDir");
69// // Create directory someDir in the working directory.
70// dir.create();
71// cout << dir.nEntries(); // #entries
72// // Assign to another directory.
73// dir = Directory("otherDir");
74// // Remove the directory and its contents.
75// dir.removeRecursive();
76// </srcblock>
77// </example>
78
79// <motivation>
80// Provide functions for manipulating and getting information
81// about directories.
82// </motivation>
83
84class Directory : public File {
85 public:
86 // Sets the path on the current working directory
88
89 // Create a directory object for a file with the given path name.
90 // An exception is thrown if the directory is illegal, i.e. if it does
91 // not exist as a directory or symbolic link or if cannot be created.
92 // Note that the directory is not created if it does not exist yet.
93 // This can be done using the function create.
94 // <br>
95 // When the given path name is a symbolic link, the symbolic link
96 // is resolved (recursively) and the resulting directory name is used
97 // instead.
98 // <group>
102 // </group>
103
104 // Copy constructor (copy semantics).
105 Directory(const Directory& that);
106
108
109 // Assignment (copy semantics).
111
112 // Check if directory is empty.
113 // If the directory does not exist, an exception will be thrown.
114 Bool isEmpty() const;
115
116 // Return the number of entries in the directory (not counting . and ..).
117 // If the directory does not exist, an exception will be thrown.
118 uInt nEntries() const;
119
120 // Get the amount of free space (in bytes) on the file system this
121 // directory is on. When the directory path is a symbolic link, that
122 // link is resolved first.
123 // <group>
125 uInt freeSpaceInMB() const;
126 // </group>
127
128 // Create the directory.
129 // <br>If the directory exists and overwrite=True, it will be removed
130 // (recursively). Otherwise an exception is thrown.
131 void create(Bool overwrite = True);
132
133 // Remove a directory.
134 // An exception is thrown if the directory is not empty.
135 // If a symbolic link is given, the link chain pointing to the directory
136 // will also be removed.
137 void remove();
138
139 // Remove all files in the directory except subdirectories.
140 // The directory itself is not removed.
142
143 // Remove the directory and its contents (recursively in all
144 // subdirectories).
145 // If <src>keepDir==True</src>, the directory itself is kept
146 //(to keep properties like placement on Lustre).
147 void removeRecursive(Bool keepDir = False);
148
149 // Copy the directory and its contents (recursively) to the target
150 // path using the system command cp -r.
151 // If the target already exists (as a file, directory or symlink),
152 // and overwrite=True, it will first be removed.
153 // The target directory is created and the data in the source
154 // directory is copied to the new directory.
155 // <br>An exception is thrown if:
156 // <br>- the target directory is not writable
157 // <br>- or the target already exists and overwrite!=True
158 // <note role=caution>
159 // 1. The behavior of this copy function is different from cp when the
160 // target directory already exists. Cp copies the source to a
161 // subdirectory of the target, while copy recreates the target.
162 // <br>2. When a readonly file is copied, <src>cp</src> the resulting
163 // file is also readonly. Therefore <src>chmod</src> is used to
164 // set user write permission after the copy.
165 // The flag <src>setUserWritePermission</src> can be set to False
166 // when that should not be done.
167 // </note>
168 // <group>
169 void copy(const Path& target, Bool overwrite = True, Bool setUserWritePermission = True) const;
170 void copy(const String& target, Bool overwrite = True, Bool setUserWritePermission = True) const;
171 // </group>
172
173 // Copy a directory recursively in a manual way.
174 // This is used in a copy using the system command is not possible
175 // (like on the Cray XT3).
176 void copyRecursive(const String& target) const;
177
178 // Move the directory to the target path using the system command mv.
179 // If the target already exists (as a file, directory or symlink),
180 // and overwrite=True, it will first be removed.
181 // The source directory is moved (thus renamed) to the target.
182 // <br>An exception is thrown if:
183 // <br>- the target directory is not writable
184 // <br>- or the target already exists and overwrite!=True
185 // <note role=caution>
186 // The behavior of this move function is different from mv when the
187 // target directory already exists. Mv moves the source to a
188 // subdirectory of the target, while move recreates the target.
189 // </note>
190 // <group>
191 void move(const Path& target, Bool overwrite = True);
192 void move(const String& target, Bool overwrite = True);
193 // </group>
194
195 // Find all files which whose names match <src>regex</src>. You
196 // can do this recursively (default) or not. Note that the
197 // matching is a regular expression match, not a shell file-expansion
198 // match. However, a shell file pattern can be converted to a regexp
199 // using the function <linkto class=Regex>Regex::fromPattern</linkto>.
200 // <src>Regex::fromString</src> allows one to convert a file name
201 // to a regexp and to use this function for eact file name matching.
202 // <br>To match the semantics of the unix <src>find</src> command,
203 // symbolic links are not followed by default, but this behavior
204 // can be over-ridden.
205 Vector<String> find(const Regex& regexp, Bool followSymLinks = False,
206 Bool recursive = True) const;
207
208 // For each element of <src>files</src>, find all file names matching
209 // it using shell file-expansion rules. Return the list of all matched files
210 // as absolute path + file names. You may optionally drop the path and just return
211 // the file names. Note tha if <src>files(i)</src> contains a path as well as a file
212 // name, no matching is done on the path, just the trailing file name.
213 // Throws an AipsError if the shell pattern is illegal.
214 static Vector<String> shellExpand(const Vector<String>& files, Bool stripPath = False);
215 // Return the total size of everything in the Directory. If the Directory
216 // does not exist, an exception will be thrown.
217 virtual Int64 size() const;
218
219 // Check if a directory is mounted via NFS or not.
221
222 private:
223 // Check if the path defines a directory.
224 // Also resolve possible symlinks.
225 void checkPath();
226
227 // This variable is used when a symbolic link is given to be
228 // a directory.
230};
231
232inline void Directory::copy(const String& target, Bool overwrite,
233 Bool setUserWritePermission) const {
234 copy(Path(target), overwrite, setUserWritePermission);
235}
236inline void Directory::move(const String& target, Bool overwrite) { move(Path(target), overwrite); }
237inline uInt Directory::freeSpaceInMB() const { return uInt(0.5 + freeSpace() / (1024 * 1024)); }
238
239} // namespace casacore
240
241#endif
void remove()
Remove a directory.
virtual Int64 size() const
Return the total size of everything in the Directory.
Directory(const Path &name)
Create a directory object for a file with the given path name.
Bool isNFSMounted() const
Check if a directory is mounted via NFS or not.
Double freeSpace() const
Get the amount of free space (in bytes) on the file system this directory is on.
Directory(const String &name)
void copy(const Path &target, Bool overwrite=True, Bool setUserWritePermission=True) const
Copy the directory and its contents (recursively) to the target path using the system command cp -r.
void move(const Path &target, Bool overwrite=True)
Move the directory to the target path using the system command mv.
void removeFiles()
Remove all files in the directory except subdirectories.
Directory(const File &name)
void create(Bool overwrite=True)
Create the directory.
void checkPath()
Check if the path defines a directory.
Directory()
Sets the path on the current working directory.
uInt freeSpaceInMB() const
Definition Directory.h:237
Directory & operator=(const Directory &that)
Assignment (copy semantics).
File itsFile
This variable is used when a symbolic link is given to be a directory.
Definition Directory.h:229
Vector< String > find(const Regex &regexp, Bool followSymLinks=False, Bool recursive=True) const
Find all files which whose names match regex.
void copyRecursive(const String &target) const
Copy a directory recursively in a manual way.
void removeRecursive(Bool keepDir=False)
Remove the directory and its contents (recursively in all subdirectories).
Bool isEmpty() const
Check if directory is empty.
Directory(const Directory &that)
Copy constructor (copy semantics).
static Vector< String > shellExpand(const Vector< String > &files, Bool stripPath=False)
For each element of files, find all file names matching it using shell file-expansion rules.
uInt nEntries() const
Return the number of entries in the directory (not counting.
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
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
String name() const
Return the name of the field.
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53