gfileutils.h File Reference

__G_FILEUTILS_H__

G_FILE_ERROR

Enum GFileError

Enum GFileTest

g_file_error_quark ( void )

IMPORT_C GQuarkg_file_error_quark(void)

g_file_error_from_errno ( gint )

IMPORT_C GFileErrorg_file_error_from_errno(ginterr_no)

g_file_error_from_errno: : an "errno" value

Gets a GFileError constant based on the passed-in . For example, if you pass in EEXIST this function returns G_FILE_ERROR_EXIST. Unlike values, you can portably assume that all GFileError values will exist.

Normally a GFileError value goes into a GError returned from a function that manipulates files. So you would use g_file_error_from_errno() when constructing a GError.

Return value: GFileError corresponding to the given

g_file_test ( const gchar *, GFileTest )

IMPORT_C gbooleang_file_test(const gchar *filename,
GFileTesttest
)

g_file_test: : a filename to test in the GLib file name encoding Test: bitfield of GFileTest flags

Returns TRUE if any of the tests in the bitfieldTestare TRUE. For example, <literal>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)</literal> 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 g_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 g_file_test() will return TRUE for G_FILE_TEST_IS_SYMLINK and FALSE for all other flags.

You should never use g_file_test() to test whether it is safe to perform an operation, 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 safe to write to a file without being tricked into writing into a different location. It doesn't work! |[ / DON'T DO THIS / if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK)) { fd = g_open (filename, O_WRONLY); / write to fd / } ]|

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.

On Windows, there are no symlinks, so testing for G_FILE_TEST_IS_SYMLINK will always return FALSE. Testing for G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and its name indicates that it is executable, checking for well-known extensions and those listed in the PATHEXT environment variable.

Return value: whether a test was TRUE

g_file_get_contents ( const gchar *, gchar **, gsize *, GError ** )

IMPORT_C gbooleang_file_get_contents(const gchar *filename,
gchar **contents,
gsize *length,
GError **error
)

g_file_get_contents: : name of a file to read contents from, in the GLib file name encoding : location to store an allocated string, use g_free() to free the returned string : location to store length in bytes of the contents, or NULL : return location for a GError, or NULL

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

If the call was successful, it returns TRUE and sets to the file contents and to the length of the file contents in bytes. The string stored in will be nul-terminated, so for text files you can pass NULL for the argument. If the call was not successful, it returns FALSE and sets . The error domain is G_FILE_ERROR. Possible error codes are those in the GFileError enumeration. In the error case, is set to NULL and is set to zero.

Return value: TRUE on success, FALSE if an error occurred

g_file_set_contents ( const gchar *, const gchar *, gssize, GError ** )

IMPORT_C gbooleang_file_set_contents(const gchar *filename,
const gchar *contents,
gssizelength,
GError **error
)

g_file_set_contents: : name of a file to write to, in the GLib file name encoding : string to write to the file : length of , or -1 if is a nul-terminated string : return location for a GError, or NULL

Writes all of to a file named , with good error checking. If a file called already exists it will be overwritten.

This write is atomic in the sense that it is first written to a temporary file which is then renamed to the final name. Notes: <itemizedlist> <listitem> On Unix, if already exists hard links to will break. Also since the file is recreated, existing permissions, access control lists, metadata etc. may be lost. If is a symbolic link, the link itself will be replaced, not the linked file. </listitem> <listitem> On Windows renaming a file will not remove an existing file with the new name, so on Windows there is a race condition between the existing file being removed and the temporary file being renamed. </listitem> <listitem> On Windows there is no way to remove a file that is open to some process, or mapped into memory. Thus, this function will fail if already exists and is open. </listitem> </itemizedlist>

If the call was sucessful, it returns TRUE. If the call was not successful, it returns FALSE and sets . The error domain is G_FILE_ERROR. Possible error codes are those in the GFileError enumeration.

Return value: TRUE on success, FALSE if an error occurred

Since: 2.8

g_file_read_link ( const gchar *, GError ** )

IMPORT_C gchar *g_file_read_link(const gchar *filename,
GError **error
)

g_file_read_link: : the symbolic link : return location for a GError

Reads the contents of the symbolic link like the POSIX readlink() function. The returned string is in the encoding used for filenames. Use g_filename_to_utf8() to convert it to UTF-8.

Returns: A newly-allocated string with the contents of the symbolic link, or NULL if an error occurred.

Since: 2.4

g_mkstemp ( gchar * )

IMPORT_C gintg_mkstemp(gchar *tmpl)

g_mkstemp: : template filename

Opens a temporary file. See the mkstemp() documentation on most UNIX-like systems.

The parameter is a string that should follow the rules for mkstemp() templates, i.e. contain the string "XXXXXX". g_mkstemp() is slightly more flexible than mkstemp() in that the sequence does not have to occur at the very end of the template. The X string will be modified to form the name of a file that didn't exist. The string should be in the GLib file name encoding. Most importantly, on Windows it should be in UTF-8.

Return value: 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.

g_file_open_tmp ( const gchar *, gchar **, GError ** )

IMPORT_C gintg_file_open_tmp(const gchar *tmpl,
gchar **name_used,
GError **error
)

g_file_open_tmp: : Template for file name, as in g_mkstemp(), basename only, or NULL, to a default template : location to store actual name used, or NULL : return location for a GError

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

should be a string in the GLib file name encoding containing a sequence of six 'X' characters, as the parameter to g_mkstemp(). However, unlike these functions, the template should only be a basename, no directory components are allowed. If template is NULL, a default template is used.

Note that in contrast to g_mkstemp() (and mkstemp()) is not modified, and might thus be a read-only literal string.

The actual name used is returned in if non-NULL. This string should be freed with g_free() when not needed any longer. The returned name is in the GLib file name encoding.

Return value: 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 and will be set.

g_format_size_for_display ( goffset )

IMPORT_C char *g_format_size_for_display(goffsetsize)

g_format_size_for_display: : a size in bytes.

Formats a size (for example the size of a file) into a human readable string. Sizes are rounded to the nearest size prefix (KB, MB, GB) and are displayed rounded to the nearest tenth. E.g. the file size 3292528 bytes will be converted into the string "3.1 MB".

The prefix units base is 1024 (i.e. 1 KB is 1024 bytes).

This string should be freed with g_free() when not needed any longer.

Returns: a newly-allocated formatted string containing a human readable file size.

Since: 2.16

g_build_path ( const gchar *, const gchar *, ... )

IMPORT_C gchar *g_build_path(const gchar *separator,
const gchar *first_element,
...
)

g_build_path: : a string used to separator the elements of the path. : the first element in the path : remaining elements in path, terminated by NULL

Creates a path from a series of elements using as the separator between elements. At the boundary between two elements, any trailing occurrences of separator in the first element, or leading occurrences of separator in the second element are removed and exactly one copy of the separator is inserted.

Empty elements are ignored.

The number of leading copies of the separator on the result is the same as the number of leading copies of the separator on the first non-empty element.

The number of trailing copies of the separator on the result is the same as the number of trailing copies of the separator on the last non-empty element. (Determination of the number of trailing copies is done without stripping leading copies, so if the separator is <literal>ABA</literal>, <literal>ABABA</literal> has 1 trailing copy.)

However, if there is only a single non-empty element, and there are no characters in that element not part of the leading or trailing separators, then the result is exactly the original value of that element.

Other than for determination of the number of leading and trailing copies of the separator, elements consisting only of copies of the separator are ignored.

Return value: a newly-allocated string that must be freed with g_free().

g_build_pathv ( const gchar *, gchar ** )

IMPORT_C gchar *g_build_pathv(const gchar *separator,
gchar **args
)

g_build_pathv: : a string used to separator the elements of the path. : NULL-terminated array of strings containing the path elements.

Behaves exactly like g_build_path(), but takes the path elements as a string array, instead of varargs. This function is mainly meant for language bindings.

Return value: a newly-allocated string that must be freed with g_free().

Since: 2.8

g_build_filename ( const gchar *, ... )

IMPORT_C gchar *g_build_filename(const gchar *first_element,
...
)

g_build_filename: : the first element in the path : remaining elements in path, terminated by NULL

Creates a filename from a series of elements using the correct separator for filenames.

On Unix, this function behaves identically to <literal>g_build_path (G_DIR_SEPARATOR_S, first_element, ....)</literal>.

On Windows, it takes into account that either the backslash (<literal></literal> or slash (<literal>/</literal>) can be used as separator in filenames, but otherwise behaves as on Unix. When file pathname separators need to be inserted, the one that last previously occurred in the parameters (reading from left to right) is used.

No attempt is made to force the resulting filename to be an absolute path. If the first element is a relative path, the result will be a relative path.

Return value: a newly-allocated string that must be freed with g_free().

g_build_filenamev ( gchar ** )

IMPORT_C gchar *g_build_filenamev(gchar **args)

g_build_filenamev: : NULL-terminated array of strings containing the path elements.

Behaves exactly like g_build_filename(), but takes the path elements as a string array, instead of varargs. This function is mainly meant for language bindings.

Return value: a newly-allocated string that must be freed with g_free().

Since: 2.8

g_mkdir_with_parents ( const gchar *, int )

IMPORT_C intg_mkdir_with_parents(const gchar *pathname,
intmode
)

g_mkdir_with_parents: : a pathname in the GLib file name encoding : permissions to use for newly created directories

Create a directory if it doesn't already exist. Create intermediate parent directories as needed, too.

Returns: 0 if the directory already exists, or was successfully created. Returns -1 if an error occurred, with errno set.

Since: 2.8