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...
 
enum  Perms {
  NoPerms = 0,
  OwnerRead = 0400,
  OwnerWrite = 0200,
  OwnerExec = 0100,
  OwnerAll = 0700,
  GroupRead = 040,
  GroupWrite = 020,
  GroupExec = 010,
  GroupAll = 070,
  OthersRead = 04,
  OthersWrite = 02,
  OthersExec = 01,
  OthersAll = 07,
  AllPerms = 0777,
  SetUid = 04000,
  SetGid = 02000,
  StickyBit = 01000,
  PermMask = 07777,
  UnknownPerms = 0xFFFF
}
 File permission flags. More...
 
enum  PermOptions {
  PermReplace = 1,
  PermAdd = 2,
  PermRemove = 4,
  PermNoFollow = 8
}
 Options for modifying permissions. 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.
 
Perms permissions () const
 Returns the file permissions.
 

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 Perms permissions (const Path &path)
 Returns the permissions of the file at the path.
 
static void permissions (const Path &path, Perms prms, PermOptions opts=PermReplace)
 Sets the permissions of the file at the path. More...
 
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 createSymlink (const Path &target, const Path &link)
 Creates a symbolic link at link pointing to target.
 
static void createHardlink (const Path &target, const Path &link)
 Creates a hard link at link pointing to target.
 
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

FileInfo::Perms operator| (FileInfo::Perms a, FileInfo::Perms b)
 Bitwise OR for FileInfo permission flags.
 
FileInfo::Perms operator& (FileInfo::Perms a, FileInfo::Perms b)
 Bitwise AND for FileInfo permission flags.
 
FileInfo::Perms operator^ (FileInfo::Perms a, FileInfo::Perms b)
 Bitwise XOR for FileInfo permission flags.
 
FileInfo::Perms operator~ (FileInfo::Perms a)
 Bitwise NOT for FileInfo permission flags.
 
FileInfo::Permsoperator|= (FileInfo::Perms &a, FileInfo::Perms b)
 Bitwise OR assignment for FileInfo permission flags.
 
FileInfo::Permsoperator&= (FileInfo::Perms &a, FileInfo::Perms b)
 Bitwise AND assignment for FileInfo permission flags.
 
FileInfo::PermOptions operator| (FileInfo::PermOptions a, FileInfo::PermOptions b)
 Bitwise OR for FileInfo permission options.
 
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.

◆ Perms

enum Perms
Enumerator
NoPerms 

No permissions.

OwnerRead 

Owner read permission.

OwnerWrite 

Owner write permission.

OwnerExec 

Owner execute permission.

OwnerAll 

All owner permissions.

GroupRead 

Group read permission.

GroupWrite 

Group write permission.

GroupExec 

Group execute permission.

GroupAll 

All group permissions.

OthersRead 

Others read permission.

OthersWrite 

Others write permission.

OthersExec 

Others execute permission.

OthersAll 

All others permissions.

AllPerms 

All basic permissions.

SetUid 

Set-user-ID on execute.

SetGid 

Set-group-ID on execute.

StickyBit 

Sticky bit.

PermMask 

All valid permission bits.

UnknownPerms 

Permissions not known.

◆ PermOptions

Enumerator
PermReplace 

Replace permissions entirely.

PermAdd 

Add permission bits.

PermRemove 

Remove permission bits.

PermNoFollow 

Do not follow symbolic links.

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.

◆ permissions()

static void permissions ( const Path path,
Perms  prms,
PermOptions  opts = PermReplace 
)
static

The opts parameter controls how the permissions are applied. Use PermReplace to set permissions exactly, PermAdd to add bits, or PermRemove to remove bits. Combine with PermNoFollow using bitwise OR to act on the symbolic link itself.

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:177
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