RegularFile.h

Classes

RegularFile -- Manipulate and get information about regular files (full description)

class RegularFile: public File

Interface

Public Members
RegularFile()
RegularFile (const Path& path)
RegularFile (const String& string)
RegularFile (const File& file)
RegularFile (const RegularFile& regularFile)
~RegularFile()
RegularFile& operator= (const RegularFile& regularFile)
void create (Bool overwrite = True)
void remove()
void copy (const Path& target, Bool overwrite = True, Bool setUserWritePermission = True) const
void copy (const String& target, Bool overwrite = True, Bool setUserWritePermission = True) const
void move (const Path& target, Bool overwrite = True)
void move (const String& target, Bool overwrite = True)
virtual Int64 size() const
Private Members
void checkPath()

Description

Prerequisite

Etymology

The class RegularFile provides functions for manipulating and getting information about regular files. Regular file are files which hold data.

Synopsis

This class provides functions to manipulate and to get information about regular files. The functions for getting information (like ownership, dates) about regular files are inherited from the File class.
The class RegularFile itself provides functions to rename, remove, copy, and move regular files. The file name can be a symbolic link resolving (eventually) to a regular file.

Example

    // Create the object rFile
    RegularFile rFile ("isFile");

    // Create file; if the file exists it will be overwritten
    rFile.create (True);
    rFile.copy (newPath);
  
    cout << rFile.size() << endl;     // Get the size of the file
    cout << rFile.path() << endl;     // Show the relative pathname

    rFile.remove();                   // remove the file

Motivation

Provide functions for manipulating and getting information about regular files.

Member Description

RegularFile()

Default constructor sets path to . (working directory).

RegularFile (const Path& path)
RegularFile (const String& string)
RegularFile (const File& file)

Create a regular file object for a file with the given path name. An exception is thrown if the file is illegal, i.e. if it does not exist as a regular file or symbolic link or if cannot be created. Note that the file 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 file name is used instead.

RegularFile (const RegularFile& regularFile)

Copy constructor (copy semantics).

~RegularFile()

RegularFile& operator= (const RegularFile& regularFile)

Assignment (copy semantics).

void create (Bool overwrite = True)

Create the regular file.
If the file exists and is not a regular file, an exception is thrown. Otherwise if overwrite is true the regular file will be overwritten. If overwrite is false then nothing will be done. If the file does not exist, it is created.

void remove()

Remove the file. If it does not exist, an exception will be thrown. If a symbolic link is given, the link chain pointing to the file will also be removed.

void copy (const Path& target, Bool overwrite = True, Bool setUserWritePermission = True) const
void copy (const String& target, Bool overwrite = True, Bool setUserWritePermission = True) const

Copy the file to the target path using the system command cp. If the file is a symbolic link, the regular file pointed to will be copied. The target path can be a directory or a file (as in cp). An exception is thrown if:
- the target directory is not writable
- or the target file already exists and overwrite==False
- or the target file already exists and is not writable

Caution When a readonly file is copied, 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.

void move (const Path& target, Bool overwrite = True)
void move (const String& target, Bool overwrite = True)

Move the file to the target path using the system command mv. If the file is a symbolic link, the regular file pointed to will be moved. The target path can be a directory or a file (as in mv). An exception is thrown if:
- the target directory is not writable
- or the target file already exists and overwrite==False
- or the target file already exists and is not writable

Tip The system command mv is used instead of the library function rename to be able to move across file systems.

virtual Int64 size() const

Return the size of the file. If the file does not exist, an exception will be thrown.

void checkPath()

Check if the path of the file is valid. Also resolve possible symlinks.