GFC Logo GFC Title Logo
Reference Manual
Main Page  |  Namespace List  |  Alphabetical List  |  Class List  |  File List

fileutils.hh File Reference

C++ interface for GDir and the various GLib file-related functions. More...

#include <gfc/stackobject.hh>
#include <gfc/utfstring.hh>
#include <glib/gdir.h>
#include <glib/gfileutils.h>
#include <vector>
#include <gfc/glib/inline/fileutils.inl>

Go to the source code of this file.

Namespaces

Classes

File Methods

Typedefs

Enumerations


Detailed Description

C++ interface for GDir and the various GLib file-related functions.


Enumeration Type Documentation

enum FileTest
 

Specifies a test to perform on a file using G::file_test().

Enumeration values:
FILE_TEST_IS_REGULAR  Tests true if the file is a regular file (not a symlink or directory).
FILE_TEST_IS_SYMLINK  Tests true if the file is a symlink.
FILE_TEST_IS_DIR  Tests true if the file is a directory.
FILE_TEST_IS_EXECUTABLE  Tests true if the file is executable.
FILE_TEST_EXISTS  Tests true if the file exists. It may or may not be a regular file.


Function Documentation

GFileError file_error_from_errno int  err_no  ) 
 

Gets a GFileError constant based on the passed-in errno, for example, if you pass in EEXIST this function returns G_FILE_ERROR_EXIST.

Parameters:
err_no An "errno" value
Returns:
A GFileError corresponding to the given errno.

Unlike errno values, you can portably assume that all GFileError values will exist. Normally a GFileError value goes into a G::Error returned from a method that manipulates files. So you would use G::file_error_from_errno() when constructing a G::Error.

bool file_get_contents const std::string &  filename,
std::string &  contents,
G::Error *  error
 

Reads an entire file into allocated memory, with good error checking.

Parameters:
filename A file to read the contents from.
contents The location to store the file contents.
error The return location for a G::Error.
Returns:
true on success, false if error is set.

If error is set, false is returned, and contents is set to an empty string. If true is returned, error will not be set, and contents will be set to the file contents. The error domain is G_FILE_ERROR. Possible error codes are those in the GFileError enumeration.

bool file_test const std::string &  filename,
FileTestField  test
 

Determines whether any of the tests in the bitfield test are true.

Parameters:
filename A filename to test.
test A bitfield of G::FileTest flags.
Returns:
true if a test was true.

For example, (G::FILE_TEST_EXISTS | G::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 G::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 G::FILE_TEST_IS_SYMLINK and G::FILE_TEST_IS_REGULAR. Note, that for a dangling symbolic link file_test() will return true for G::FILE_TEST_IS_SYMLINK and false for all other flags.

You should never use file_test() to test whether it is safe to perform an operaton, because there is always the possibility of the condition changing before you actually perform the operation. For example, you might think you could use G::FILE_TEST_IS_SYMLINK to know whether it is is safe to write to a file without being tricked into writing into a different location. It doesn't work!

Example: DON'T DO THIS.

     if (!file_test(filename, FILE_TEST_IS_SYMLINK))
     {
        TempFile file(filename);
        if (file.is_open())
        // write to file
     }

Another thing to note is that G::FILE_TEST_EXISTS and G::FILE_TEST_IS_EXECUTABLE are implemented using the access() system call. This usually doesn't matter, but if your program is setuid or setgid it means that these tests will give you the answer for the real user ID and group ID , rather than the effective user ID and group ID.


Generated on Tue Aug 24 00:04:56 2004 for GFC-Core by doxygen 1.3.8