glibmm: File Utilities

Various file-related classes and functions. More...

Classes

class  Glib::FileError
 Exception class for file-related errors. More...

 
class  Glib::DirIterator
 The iterator type of Glib::Dir. More...

 
class  Glib::Dir
 Utility class representing an open directory. More...

 

Enumerations

enum  Glib::FileError::Code {

  Glib::FileError::EXISTS,

  Glib::FileError::IS_DIRECTORY,

  Glib::FileError::ACCESS_DENIED,

  Glib::FileError::NAME_TOO_LONG,

  Glib::FileError::NO_SUCH_ENTITY,

  Glib::FileError::NOT_DIRECTORY,

  Glib::FileError::NO_SUCH_DEVICE,

  Glib::FileError::NOT_DEVICE,

  Glib::FileError::READONLY_FILESYSTEM,

  Glib::FileError::TEXT_FILE_BUSY,

  Glib::FileError::FAULTY_ADDRESS,

  Glib::FileError::SYMLINK_LOOP,

  Glib::FileError::NO_SPACE_LEFT,

  Glib::FileError::NOT_ENOUGH_MEMORY,

  Glib::FileError::TOO_MANY_OPEN_FILES,

  Glib::FileError::FILE_TABLE_OVERFLOW,

  Glib::FileError::BAD_FILE_DESCRIPTOR,

  Glib::FileError::INVALID_ARGUMENT,

  Glib::FileError::BROKEN_PIPE,

  Glib::FileError::TRYAGAIN,

  Glib::FileError::INTERRUPTED,

  Glib::FileError::IO_ERROR,

  Glib::FileError::NOT_OWNER,

  Glib::FileError::NOSYS,

  Glib::FileError::FAILED

}
 Values corresponding to errno codes returned from file operations on UNIX. More...

 

Functions

bool Glib::file_test (const std::string& filename, FileTest test)
 Returns true if any of the tests in the bitfield test are true. More...

 
int Glib::mkstemp (std::string& filename_template)
 Opens a temporary file. More...

 
int Glib::file_open_tmp (std::string& name_used, const std::string& prefix)
 Opens a file for writing in the preferred directory for temporary files (as returned by Glib::get_tmp_dir()). More...

 
int Glib::file_open_tmp (std::string& name_used)
 Opens a file for writing in the preferred directory for temporary files (as returned by Glib::get_tmp_dir()). More...

 
std::string Glib::file_get_contents (const std::string& filename)
 Reads an entire file into a string, with good error checking. More...

 

Detailed Description

Various file-related classes and functions.

Enumeration Type Documentation

Values corresponding to errno codes returned from file operations on UNIX.

Values corresponding to errno codes returned from file operations on UNIX.

Unlike errno codes, GFileError values are available on all systems, even Windows. The exact meaning of each code depends on what sort of file operation you were performing; the UNIX documentation gives more details. The following error code descriptions come from the GNU C Library manual, and are under the copyright of that manual.

It's not very portable to make detailed assumptions about exactly which errors will be returned from a given operation. Some errors don't occur on some systems, etc., sometimes there are subtle differences in when a system will report a given error, etc.

Unlike errno codes, FileError::Code values are available on all systems, even Windows. The exact meaning of each code depends on what sort of file operation you were performing; the UNIX documentation gives more details. The following error code descriptions come from the GNU C Library manual, and are under the copyright of that manual.

It's not very portable to make detailed assumptions about exactly which errors will be returned from a given operation. Some errors don't occur on some systems, etc., sometimes there are subtle differences in when a system will report a given error, etc.

Enumerator
EXISTS 

Operation not permitted; only the owner of the file (or other resource) or processes with special privileges can perform the operation.

(EEXIST) Operation not permitted; only the owner of the file (or other resource) or processes with special privileges can perform the operation.





IS_DIRECTORY 

File is a directory; you cannot open a directory for writing, or create or remove hard links to it.

(EISDIR) File is a directory; you cannot open a directory for writing, or create or remove hard links to it.





ACCESS_DENIED 

Permission denied; the file permissions do not allow the attempted operation.

(EACCES) Permission denied; the file permissions do not allow the attempted operation.





NAME_TOO_LONG 

Filename too long.

(ENAMETOOLONG) Filename too long.





NO_SUCH_ENTITY 

No such file or directory.

(ENOENT) No such file or directory.

This is a "file doesn't exist" error for ordinary files that are referenced in contexts where they are expected to already exist.

This is a "file doesn't exist" error for ordinary files that are referenced in contexts where they are expected to already exist.



NOT_DIRECTORY 

A file that isn't a directory was specified when a directory is required.

(ENOTDIR) A file that isn't a directory was specified when a directory is required.





NO_SUCH_DEVICE 

No such device or address.

(ENXIO) No such device or address.

The system tried to use the device represented by a file you specified, and it couldn't find the device. This can mean that the device file was installed incorrectly, or that the physical device is missing or not correctly attached to the computer.

The system tried to use the device represented by a file you specified, and it couldn't find the device. This can mean that the device file was installed incorrectly, or that the physical device is missing or not correctly attached to the computer.



NOT_DEVICE 

The underlying file system of the specified file does not support memory mapping.

(ENODEV) This file is of a type that doesn't support mapping.





READONLY_FILESYSTEM 

The directory containing the new link can't be modified because it's on a read-only file system.

(EROFS) The directory containing the new link can't be modified because it's on a read-only file system.





TEXT_FILE_BUSY 

Text file busy.

(ETXTBSY) Text file busy.





FAULTY_ADDRESS 

You passed in a pointer to bad memory.

(EFAULT) You passed in a pointer to bad memory.

(GLib won't reliably return this, don't pass in pointers to bad memory.).

(Glib won't reliably return this, don't pass in pointers to bad memory.)



SYMLINK_LOOP 

Too many levels of symbolic links were encountered in looking up a file name.

(ELOOP) Too many levels of symbolic links were encountered in looking up a file name.

This often indicates a cycle of symbolic links.

This often indicates a cycle of symbolic links.



NO_SPACE_LEFT 

No space left on device; write operation on a file failed because the disk is full.

(ENOSPC) No space left on device; write operation on a file failed because the disk is full.





NOT_ENOUGH_MEMORY 

No memory available.

(ENOMEM) No memory available.

The system cannot allocate more virtual memory because its capacity is full.

The system cannot allocate more virtual memory because its capacity is full.



TOO_MANY_OPEN_FILES 

The current process has too many files open and can't open any more.

(EMFILE) The current process has too many files open and can't open any more.

Duplicate descriptors do count toward this limit.

Duplicate descriptors do count toward this limit.



FILE_TABLE_OVERFLOW 

There are too many distinct file openings in the entire system.

(ENFILE) There are too many distinct file openings in the entire system.





BAD_FILE_DESCRIPTOR 

Bad file descriptor; for example, I/O on a descriptor that has been closed or reading from a descriptor open only for writing (or vice versa).

(EBADF) Bad file descriptor; for example, I/O on a descriptor that has been closed or reading from a descriptor open only for writing (or vice versa).





INVALID_ARGUMENT 

Invalid argument.

(EINVAL) Invalid argument.

This is used to indicate various kinds of problems with passing the wrong argument to a library function.

This is used to indicate various kinds of problems with passing the wrong argument to a library function.



BROKEN_PIPE 

Broken pipe; there is no process reading from the other end of a pipe.

(EPIPE) Broken pipe; there is no process reading from the other end of a pipe.

Every library function that returns this error code also generates a 'SIGPIPE' signal; this signal terminates the program if not handled or blocked. Thus, your program will never actually see this code unless it has handled or blocked 'SIGPIPE'.

Every library function that returns this error code also generates a SIGPIPE signal; this signal terminates the program if not handled or blocked. Thus, your program will never actually see this code unless it has handled or blocked SIGPIPE.



TRYAGAIN 

Resource temporarily unavailable; the call might work if you try again later.

(EAGAIN) Resource temporarily unavailable; the call might work if you try again later.

We used TRYAGAIN instead of TRY_AGAIN, because that is a defined as a macro by a Unix header.



INTERRUPTED 

Interrupted function call; an asynchronous signal occurred and prevented completion of the call.

(EINTR) Interrupted function call; an asynchronous signal occurred and prevented completion of the call.

When this happens, you should try the call again.

When this happens, you should try the call again.



IO_ERROR 

Input/output error; usually used for physical read or write errors.

(EIO) Input/output error; usually used for physical read or write errors.

i.e. the disk or other physical device hardware is returning errors.

I.e. the disk or other physical device hardware is returning errors.



NOT_OWNER 

Operation not permitted; only the owner of the file (or other resource) or processes with special privileges can perform the operation.

(EPERM) Operation not permitted; only the owner of the file (or other resource) or processes with special privileges can perform the operation.





NOSYS 

Function not implemented; this indicates that the system is missing some functionality.

FAILED 

Does not correspond to a UNIX error code; this is the standard "failed for unspecified reason" error code present in all Error error code enumerations.

Does not correspond to a UNIX error code; this is the standard "failed for unspecified reason" error code present in all Glib::Error error code enumerations.

Returned if no specific code applies.

Function Documentation

std::string Glib::file_get_contents ( const std::string filename)

Reads an entire file into a string, with good error checking.

Parameters
filenameA file to read contents from.
Returns
The file contents.
Exceptions
Glib::FileError
int Glib::file_open_tmp ( std::string name_used,
const std::string prefix 
)

Opens a file for writing in the preferred directory for temporary files (as returned by Glib::get_tmp_dir()).

prefix should a basename template; it'll be suffixed by 6 characters in order to form a unique filename. No directory components are allowed.

The actual name used is returned in name_used.

Parameters
prefixTemplate for file name, basename only.
Return values
name_usedThe actual name used.
Returns
A file handle (as from open()) to the file opened for reading and writing. The file is opened in binary mode on platforms where there is a difference. The file handle should be closed with close().
Exceptions
Glib::FileError
int Glib::file_open_tmp ( std::string name_used)

Opens a file for writing in the preferred directory for temporary files (as returned by Glib::get_tmp_dir()).

This function works like file_open_tmp(std::string&, const std::string&) but uses a default basename prefix.

Return values
name_usedThe actual name used.
Returns
A file handle (as from open()) to the file opened for reading and writing. The file is opened in binary mode on platforms where there is a difference. The file handle should be closed with close().
Exceptions
Glib::FileError
bool Glib::file_test ( const std::string filename,
FileTest  test 
)

Returns true if any of the tests in the bitfield test are true.

For example, (Glib::FILE_TEST_EXISTS | Glib::FILE_TEST_IS_DIR) will return true if the file exists; the check whether it's a directory doesn't matter since the existence test is true. With the current set of available tests, there's no point passing in more than one test at a time.

Apart from Glib::FILE_TEST_IS_SYMLINK all tests follow symbolic links, so for a symbolic link to a regular file file_test() will return true for both Glib::FILE_TEST_IS_SYMLINK and Glib::FILE_TEST_IS_REGULAR.

Note
For a dangling symbolic link file_test() will return true for Glib::FILE_TEST_IS_SYMLINK and false for all other flags.
Parameters
filenameA filename to test.
testBitfield of Glib::FileTest flags.
Returns
Whether a test was true.
int Glib::mkstemp ( std::string filename_template)

Opens a temporary file.

See the mkstemp() documentation on most UNIX-like systems. This is a portability wrapper, which simply calls mkstemp() on systems that have it, and implements it in GLib otherwise.

Parameters
filename_templateA string that should match the rules for mkstemp(), i.e. end in "XXXXXX". The X string will be modified to form the name of a file that didn't exist.
Returns
A file handle (as from open()) to the file opened for reading and writing. The file is opened in binary mode on platforms where there is a difference. The file handle should be closed with close(). In case of errors, -1 is returned.