test


Description:

public bool test (string filename, FileTest test)

Returns true if any of the tests in the bitfield test are 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 test will return true for both g_file_test_is_symlink and g_file_test_is_regular.

Note, that for a dangling symbolic link test will return true for g_file_test_is_symlink and false for all other flags.

You should never use 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, see TOCTOU.

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
}

// DO THIS INSTEAD
fd = g_open (filename, O_WRONLY | O_NOFOLLOW | O_CLOEXEC);
if (fd == -1)
{
// check error
if (errno == ELOOP)
// file is a symlink and can be ignored
else
// handle errors as before
}
else
{
// 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.

Example: Test whether a file exists, is executable, is a directory, a symlink or regular:

public static int main (string[] args) {
// Output: ``./GLib.FileUtils.test``
print ("%s\n", args[0]);

// true if the file is a regular file (not a directory) or a symlink to a regular file.
// Output: ``true``
bool tmp = FileUtils.test (args[0], FileTest.IS_REGULAR);
print ("%s\n", tmp.to_string ());

// true if the file is a symlink.
// Output: ``false``
tmp = FileUtils.test (args[0], FileTest.IS_SYMLINK);
print ("%s\n", tmp.to_string ());

// true if the file is a directory.
// Output: ``false``
tmp = FileUtils.test (args[0], FileTest.IS_DIR);
print ("%s\n", tmp.to_string ());

// true if the file is a executable.
// Output: ``true``
tmp = FileUtils.test (args[0], FileTest.IS_EXECUTABLE);
print ("%s\n", tmp.to_string ());

// true if the file exists. It may or may not be a regular file.
// Output: ``true``
tmp = FileUtils.test (args[0], FileTest.EXISTS);
print ("%s\n", tmp.to_string ());

return 0;
}

valac --pkg glib-2.0 GLib.FileUtils.test.vala

Parameters:

filename

a filename to test in the GLib file name encoding

test

bitfield of FileTest flags

Returns:

whether a test was true


Namespace: GLib.FileUtils
Package: glib-2.0