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
84
85class Directory: public File
86{
87public:
88
89 // Sets the path on the current working directory
91
92 // Create a directory object for a file with the given path name.
93 // An exception is thrown if the directory is illegal, i.e. if it does
94 // not exist as a directory or symbolic link or if cannot be created.
95 // Note that the directory is not created if it does not exist yet.
96 // This can be done using the function create.
97 // <br>
98 // When the given path name is a symbolic link, the symbolic link
99 // is resolved (recursively) and the resulting directory name is used
100 // instead.
101 // <group>
102 Directory (const Path& name);
103 Directory (const String& name);
104 Directory (const File& name);
105 // </group>
106
107 // Copy constructor (copy semantics).
108 Directory (const Directory& that);
109
111
112 // Assignment (copy semantics).
114
115 // Check if directory is empty.
116 // If the directory does not exist, an exception will be thrown.
117 Bool isEmpty() const;
118
119 // Return the number of entries in the directory (not counting . and ..).
120 // If the directory does not exist, an exception will be thrown.
121 uInt nEntries() const;
122
123 // Get the amount of free space (in bytes) on the file system this
124 // directory is on. When the directory path is a symbolic link, that
125 // link is resolved first.
126 // <group>
128 uInt freeSpaceInMB() const;
129 // </group>
130
131 // Create the directory.
132 // <br>If the directory exists and overwrite=True, it will be removed
133 // (recursively). Otherwise an exception is thrown.
134 void create (Bool overwrite = True);
135
136 // Remove a directory.
137 // An exception is thrown if the directory is not empty.
138 // If a symbolic link is given, the link chain pointing to the directory
139 // will also be removed.
140 void remove();
141
142 // Remove all files in the directory except subdirectories.
143 // The directory itself is not removed.
145
146 // Remove the directory and its contents (recursively in all
147 // subdirectories).
148 // If <src>keepDir==True</src>, the directory itself is kept
149 //(to keep properties like placement on Lustre).
150 void removeRecursive (Bool keepDir = False);
151
152 // Copy the directory and its contents (recursively) to the target
153 // path using the system command cp -r.
154 // If the target already exists (as a file, directory or symlink),
155 // and overwrite=True, it will first be removed.
156 // The target directory is created and the data in the source
157 // directory is copied to the new directory.
158 // <br>An exception is thrown if:
159 // <br>- the target directory is not writable
160 // <br>- or the target already exists and overwrite!=True
161 // <note role=caution>
162 // 1. The behavior of this copy function is different from cp when the
163 // target directory already exists. Cp copies the source to a
164 // subdirectory of the target, while copy recreates the target.
165 // <br>2. When a readonly file is copied, <src>cp</src> the resulting
166 // file is also readonly. Therefore <src>chmod</src> is used to
167 // set user write permission after the copy.
168 // The flag <src>setUserWritePermission</src> can be set to False
169 // when that should not be done.
170 // </note>
171 // <group>
172 void copy (const Path& target, Bool overwrite = True,
173 Bool setUserWritePermission = True) const;
174 void copy (const String& target, Bool overwrite = True,
175 Bool setUserWritePermission = True) const;
176 // </group>
177
178 // Copy a directory recursively in a manual way.
179 // This is used in a copy using the system command is not possible
180 // (like on the Cray XT3).
181 void copyRecursive (const String& target) const;
182
183 // Move the directory to the target path using the system command mv.
184 // If the target already exists (as a file, directory or symlink),
185 // and overwrite=True, it will first be removed.
186 // The source directory is moved (thus renamed) to the target.
187 // <br>An exception is thrown if:
188 // <br>- the target directory is not writable
189 // <br>- or the target already exists and overwrite!=True
190 // <note role=caution>
191 // The behavior of this move function is different from mv when the
192 // target directory already exists. Mv moves the source to a
193 // subdirectory of the target, while move recreates the target.
194 // </note>
195 // <group>
196 void move (const Path& target, Bool overwrite = True);
197 void move (const String& target, Bool overwrite = True);
198 // </group>
199
200 // Find all files which whose names match <src>regex</src>. You
201 // can do this recursively (default) or not. Note that the
202 // matching is a regular expression match, not a shell file-expansion
203 // match. However, a shell file pattern can be converted to a regexp
204 // using the function <linkto class=Regex>Regex::fromPattern</linkto>.
205 // <src>Regex::fromString</src> allows one to convert a file name
206 // to a regexp and to use this function for eact file name matching.
207 // <br>To match the semantics of the unix <src>find</src> command,
208 // symbolic links are not followed by default, but this behavior
209 // can be over-ridden.
210 Vector<String> find (const Regex& regexp, Bool followSymLinks=False,
211 Bool recursive=True) const;
212
213
214 // For each element of <src>files</src>, find all file names matching
215 // it using shell file-expansion rules. Return the list of all matched files
216 // as absolute path + file names. You may optionally drop the path and just return
217 // the file names. Note tha if <src>files(i)</src> contains a path as well as a file
218 // name, no matching is done on the path, just the trailing file name.
219 // Throws an AipsError if the shell pattern is illegal.
220 static Vector<String> shellExpand (const Vector<String>& files, Bool stripPath=False);
221 // Return the total size of everything in the Directory. If the Directory
222 // does not exist, an exception will be thrown.
223 virtual Int64 size() const;
224
225 //Check if a directory is mounted via NFS or not.
227
228private:
229 // Check if the path defines a directory.
230 // Also resolve possible symlinks.
231 void checkPath();
232
233 // This variable is used when a symbolic link is given to be
234 // a directory.
236};
237
238
239
240inline void Directory::copy (const String& target, Bool overwrite,
241 Bool setUserWritePermission) const
242{
243 copy (Path(target), overwrite, setUserWritePermission);
244}
245inline void Directory::move (const String& target, Bool overwrite)
246{
247 move (Path(target), overwrite);
248}
250{
251 return uInt (0.5 + freeSpace() / (1024*1024));
252}
253
254
255
256} //# NAMESPACE CASACORE - END
257
258#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:249
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:235
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.
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
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
double Double
Definition aipstype.h:53