FileInfo Class Reference

#include <Pt/System/FileInfo.h>

Provides information about a node in the file-system. More...

Public Types

enum  Type {
  Invalid = 0,
  Directory = 1,
  File = 2,
  Link = 4
}
 File-node type. More...
 

Public Member Functions

 FileInfo ()
 Default constructor.
 
 FileInfo (const Path &path)
 Constructs from the path.
 
 FileInfo (const Pt::String &path)
 Constructs from the path.
 
 FileInfo (const char *path)
 Constructs from the path.
 
 ~FileInfo ()
 Destructor.
 
void clear ()
 Clears the state.
 
const Pathpath () const
 Returns the full path of node in the file-system. More...
 
Pt::uint64_t size () const
 Returns the file size.
 
Type type () const
 Returns the file type.
 

Static Public Member Functions

static Type type (const Path &path)
 Returns the type of file at the path.
 
static Pt::uint64_t size (const Path &path)
 Returns the size of the file in bytes.
 
static DateTime lastModified (const Path &path)
 Returns the time when last modified.
 
static bool exists (const Path &path)
 Returns true if a file or directory exists at the path.
 
static void createFile (const Path &path)
 Creates a new file.
 
static void createDirectory (const Path &path)
 Creates a new directory.
 
static void createDirectories (const Path &path)
 Creates a new directory.
 
static void resize (const Path &path, Pt::uint64_t n)
 Resizes a file.
 
static void remove (const Path &path)
 Removes a file or directory.
 
static void removeAll (const Pt::System::Path &path)
 Removes a directory and its contents.
 
static void move (const Path &path, const Path &to)
 Moves a file or directory.
 

Related Functions

bool operator< (const FileInfo &a, const FileInfo &b)
 Compare two FileInfo objects.
 
bool operator== (const FileInfo &a, const FileInfo &b)
 Compare two FileInfo objects.
 
bool operator!= (const FileInfo &a, const FileInfo &b)
 Compare two FileInfo objects.
 

Detailed Description

The Pt::System::FileInfo class provides operations to query information about files and directories in the file system and to add, remove and modify them. FileInfo objects can be created with a path, are assignable, comparable and can be used as keys for e.g. std::map. The path needs not to refer to existing items in the file system, when a FileInfo object is constructed. It can be checked whether a file exists and what type of file it is, as shown in the following example:

Pt::System::Path path("/tmp/logout.txt");
if( fi.type() == Pt::System::FileInfo::File )
{
std::cout << fi.path().toLocal() << ": " << fi.size() << " bytes.\n";
}
else
{
std::cout << fi.path().toLocal() << " is invalid.\n";
}

Most operations are available as non-member functions, so it is not neccessary to create temporary FileInfo objects. Only the paths to files or directories are required to perform file system operations. The next example illustrates some of the non-member functions for file operations:

try
{
Pt::System::Path tmp1("/tmp/tmpfile1");
Pt::System::Path tmp2("/tmp/tmpfile2");
// create a temporary file
// move it to a new location
// remove the file
}
catch(const Pt::System::AccessFailed& e)
{
std::cerr << "file error: " << e.resource() << std::endl;
}

The code shown above creates a file, moves it to a new location and finally deletes it. If an operation fails, for example because the file could not be created, an exception of type AccessFailed is thrown. This is also the case for all other operations such as size(), createFile(), createDirectory(), resize() and remove(). The exception reports the name of the resource that could not be accessed.

Member Enumeration Documentation

◆ Type

enum Type
Enumerator
Invalid 

Invalid file type.

Directory 

Directory.

File 

Regular file.

Link 

Symbolic link.

Member Function Documentation

◆ path()

const Path& path ( ) const

This method may return a relative path, or a fully qualified one depending on how this object was constructed.

static void remove(const Path &path)
Removes a file or directory.
Provides information about a node in the file-system.
Definition: FileInfo.h:104
@ File
Regular file.
Definition: FileInfo.h:111
const Path & path() const
Returns the full path of node in the file-system.
Definition: FileInfo.h:144
static void move(const Path &path, const Path &to)
Moves a file or directory.
Represents a path in the file-system.
Definition: Path.h:48
static void createFile(const Path &path)
Creates a new file.
Failed to access a resource.
Definition: IOError.h:63
const std::string & resource() const
Returns the ID of the inaccessible resource.
Definition: IOError.h:80