casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
casacore::Directory Class Reference

Get information about, and manipulate directories. More...

#include <Directory.h>

Inheritance diagram for casacore::Directory:
casacore::File

Public Member Functions

 Directory ()
 Sets the path on the current working directory. More...
 
 Directory (const Path &name)
 Create a directory object for a file with the given path name. More...
 
 Directory (const String &name)
 
 Directory (const File &name)
 
 Directory (const Directory &that)
 Copy constructor (copy semantics). More...
 
 ~Directory ()
 
Directoryoperator= (const Directory &that)
 Assignment (copy semantics). More...
 
Bool isEmpty () const
 Check if directory is empty. More...
 
uInt nEntries () const
 Return the number of entries in the directory (not counting. More...
 
Double freeSpace () const
 Get the amount of free space (in bytes) on the file system this directory is on. More...
 
uInt freeSpaceInMB () const
 
void create (Bool overwrite=True)
 Create the directory. More...
 
void remove ()
 Remove a directory. More...
 
void removeFiles ()
 Remove all files in the directory except subdirectories. More...
 
void removeRecursive (Bool keepDir=False)
 Remove the directory and its contents (recursively in all subdirectories). More...
 
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. More...
 
void copy (const String &target, Bool overwrite=True, Bool setUserWritePermission=True) const
 
void copyRecursive (const String &target) const
 Copy a directory recursively in a manual way. More...
 
void move (const Path &target, Bool overwrite=True)
 Move the directory to the target path using the system command mv. More...
 
void move (const String &target, Bool overwrite=True)
 
Vector< Stringfind (const Regex &regexp, Bool followSymLinks=False, Bool recursive=True) const
 Find all files which whose names match regex. More...
 
virtual Int64 size () const
 Return the total size of everything in the Directory. More...
 
Bool isNFSMounted () const
 Check if a directory is mounted via NFS or not. More...
 
- Public Member Functions inherited from casacore::File
 File ()
 Construct a File object whose Path is set to the current working directory. More...
 
 File (const Path &path)
 Construct a File object whose Path is set to the given Path. More...
 
 File (const String &path)
 
 File (const File &that)
 Copy constructor (copy semantics). More...
 
virtual ~File ()
 
Fileoperator= (const File &that)
 Assignment (copy semantics). More...
 
const Pathpath () const
 Returns the pathname of the file. More...
 
Bool isRegular (Bool followSymLink=True) const
 Check if the file is a regular file. More...
 
Bool isDirectory (Bool followSymLink=True) const
 Check if the file is a directory. More...
 
Bool isSymLink () const
 Check if the file is a symbolic link. More...
 
Bool isPipe () const
 Check if the file is a pipe. More...
 
Bool isCharacterSpecial () const
 Check if the file is a character special file. More...
 
Bool isBlockSpecial () const
 Check if the file is a block special file. More...
 
Bool isSocket () const
 Check if the file is a socket. More...
 
Bool exists () const
 Check if the file exists. More...
 
Bool isReadable () const
 Check if the file is readable. More...
 
Bool isWritable () const
 Check if the file is writable. More...
 
Bool isExecutable () const
 Check if the file is executable. More...
 
Bool canCreate () const
 Check if a file can be created. More...
 
long userID () const
 Return the userID of the file. More...
 
long groupID () const
 Return the groupID of the file. More...
 
uInt readPermissions () const
 Return the permissions as a decimal value. More...
 
void setPermissions (uInt permissions)
 Set permission with perm. More...
 
void touch (uInt time)
 Update access time and modification time of a file. More...
 
void touch ()
 Update access time and modification time of a file. More...
 
uInt accessTime () const
 Time related fucnctions: Return the time when the file was last accessed in seconds since 00:00:00 GMT Jan 1, 1970. More...
 
String accessTimeString () const
 Return the time when the file was last accessed as a 26-characters String of the form: Thu Feb 3 13:40:11 1994. More...
 
uInt modifyTime () const
 Return the time when the file was last modified in seconds since 00:00:00 GMT Jan 1, 1970. More...
 
String modifyTimeString () const
 Return the time when the file was last modified as a 26-characters String of the form: Thu Feb 3 13:40:11 1994. More...
 
uInt statusChangeTime () const
 Return the time when the file status was last changed in seconds since 00:00:00 GMT Jan 1, 1970. More...
 
String statusChangeTimeString () const
 return the time when the file status was last changed as a 26-characters String of the form: Thu Feb 3 13:40:11 1994 More...
 
FileWriteStatus getWriteStatus () const
 get write status of the file. More...
 
String getFSType () const
 Return the filesystem type. More...
 

Static Public Member Functions

static Vector< StringshellExpand (const Vector< String > &files, Bool stripPath=False)
 For each element of files, find all file names matching it using shell file-expansion rules. More...
 
- Static Public Member Functions inherited from casacore::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 trailing characters: More...
 
static Path newUniqueName (const String &directory)
 Create a new unique filename without a prefix. More...
 

Private Member Functions

void checkPath ()
 Check if the path defines a directory. More...
 

Private Attributes

File itsFile
 This variable is used when a symbolic link is given to be a directory. More...
 

Additional Inherited Members

- Public Types inherited from casacore::File
enum  FileWriteStatus {
  OVERWRITABLE,
  NOT_OVERWRITABLE,
  CREATABLE,
  NOT_CREATABLE
}
 
- Protected Member Functions inherited from casacore::File
void removeSymLinks ()
 This function is used by RegularFile and Directory to remove all the links which, when followed, ultimately resolve to a Directory or a RegularFile. More...
 
void checkTarget (Path &targetName, Bool overwrite, Bool forDirectory=False) const
 Check if the new path for a copy or move is valid. More...
 

Detailed Description

Get information about, and manipulate directories.

Review Status

Reviewed By:
UNKNOWN
Date Reviewed:
before2004/08/25

Intended use:

Public interface

Prerequisite

Synopsis

Directory provides functions to manipulate and to get information about directories. The functions for getting information (like ownership, dates) about directories are inherited from the File class. Directory itself provides functions to create, copy, move, or remove a directory. The file name can be a symbolic link resolving (eventually) to a directory.

A separate class DirectoryIterator allows one to traverse a directory to get the file names in it.

Example

Directory dir("someDir");
// Create directory someDir in the working directory.
dir.create();
cout << dir.nEntries(); // \#entries
// Assign to another directory.
dir = Directory("otherDir");
// Remove the directory and its contents.
dir.removeRecursive();

Motivation

Provide functions for manipulating and getting information about directories.

Definition at line 87 of file Directory.h.

Constructor & Destructor Documentation

casacore::Directory::Directory ( )

Sets the path on the current working directory.

casacore::Directory::Directory ( const Path name)

Create a directory object for a file with the given path name.

An exception is thrown if the directory is illegal, i.e. if it does not exist as a directory or symbolic link or if cannot be created. Note that the directory is not created if it does not exist yet. This can be done using the function create.
When the given path name is a symbolic link, the symbolic link is resolved (recursively) and the resulting directory name is used instead.

casacore::Directory::Directory ( const String name)
casacore::Directory::Directory ( const File name)
casacore::Directory::Directory ( const Directory that)

Copy constructor (copy semantics).

casacore::Directory::~Directory ( )

Member Function Documentation

void casacore::Directory::checkPath ( )
private

Check if the path defines a directory.

Also resolve possible symlinks.

void casacore::Directory::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.

If the target already exists (as a file, directory or symlink), and overwrite=True, it will first be removed. The target directory is created and the data in the source directory is copied to the new directory.
An exception is thrown if:
- the target directory is not writable
- or the target already exists and overwrite!=True
Caution: 1; The behavior of this copy function is different from cp when the target directory already exists; Cp copies the source to a subdirectory of the target, while copy recreates the target;
2; When a readonly file is copied, cp the resulting file is also readonly; Therefore chmod is used to set user write permission after the copy; The flag setUserWritePermission can be set to False when that should not be done;

Referenced by copy().

void casacore::Directory::copy ( const String target,
Bool  overwrite = True,
Bool  setUserWritePermission = True 
) const
inline

Definition at line 242 of file Directory.h.

References copy().

void casacore::Directory::copyRecursive ( const String target) const

Copy a directory recursively in a manual way.

This is used in a copy using the system command is not possible (like on the Cray XT3).

void casacore::Directory::create ( Bool  overwrite = True)

Create the directory.


If the directory exists and overwrite=True, it will be removed (recursively). Otherwise an exception is thrown.

Vector<String> casacore::Directory::find ( const Regex regexp,
Bool  followSymLinks = False,
Bool  recursive = True 
) const

Find all files which whose names match regex.

You can do this recursively (default) or not. Note that the matching is a regular expression match, not a shell file-expansion match. However, a shell file pattern can be converted to a regexp using the function Regex::fromPattern. Regex::fromString allows one to convert a file name to a regexp and to use this function for eact file name matching.
To match the semantics of the unix find command, symbolic links are not followed by default, but this behavior can be over-ridden.

Double casacore::Directory::freeSpace ( ) const

Get the amount of free space (in bytes) on the file system this directory is on.

When the directory path is a symbolic link, that link is resolved first.

Referenced by freeSpaceInMB().

uInt casacore::Directory::freeSpaceInMB ( ) const
inline

Definition at line 251 of file Directory.h.

References freeSpace().

Bool casacore::Directory::isEmpty ( ) const

Check if directory is empty.

If the directory does not exist, an exception will be thrown.

Bool casacore::Directory::isNFSMounted ( ) const

Check if a directory is mounted via NFS or not.

void casacore::Directory::move ( const Path target,
Bool  overwrite = True 
)

Move the directory to the target path using the system command mv.

If the target already exists (as a file, directory or symlink), and overwrite=True, it will first be removed. The source directory is moved (thus renamed) to the target.
An exception is thrown if:
- the target directory is not writable
- or the target already exists and overwrite!=True
Caution: The behavior of this move function is different from mv when the target directory already exists; Mv moves the source to a subdirectory of the target, while move recreates the target;

Referenced by move().

void casacore::Directory::move ( const String target,
Bool  overwrite = True 
)
inline

Definition at line 247 of file Directory.h.

References move().

uInt casacore::Directory::nEntries ( ) const

Return the number of entries in the directory (not counting.

and..). If the directory does not exist, an exception will be thrown.

Directory& casacore::Directory::operator= ( const Directory that)

Assignment (copy semantics).

void casacore::Directory::remove ( )

Remove a directory.

An exception is thrown if the directory is not empty. If a symbolic link is given, the link chain pointing to the directory will also be removed.

void casacore::Directory::removeFiles ( )

Remove all files in the directory except subdirectories.

The directory itself is not removed.

void casacore::Directory::removeRecursive ( Bool  keepDir = False)

Remove the directory and its contents (recursively in all subdirectories).

If keepDir==True, the directory itself is kept (to keep properties like placement on Lustre).

static Vector<String> casacore::Directory::shellExpand ( const Vector< String > &  files,
Bool  stripPath = False 
)
static

For each element of files, find all file names matching it using shell file-expansion rules.

Return the list of all matched files as absolute path + file names. You may optionally drop the path and just return the file names. Note tha if files(i) contains a path as well as a file name, no matching is done on the path, just the trailing file name. Throws an AipsError if the shell pattern is illegal.

virtual Int64 casacore::Directory::size ( ) const
virtual

Return the total size of everything in the Directory.

If the Directory does not exist, an exception will be thrown.

Reimplemented from casacore::File.

Member Data Documentation

File casacore::Directory::itsFile
private

This variable is used when a symbolic link is given to be a directory.

Definition at line 237 of file Directory.h.


The documentation for this class was generated from the following file: