GLib.file_test
function file_test(filename: String, test: GLib.FileTest): Boolean {
// Gjs wrapper for g_file_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 GLib.FileTest.is_symlink all tests follow symbolic links, so for a symbolic link to a regular file GLib.file_test will return true for both GLib.FileTest.is_symlink and GLib.FileTest.is_regular.
Note, that for a dangling symbolic link GLib.file_test will return true for GLib.FileTest.is_symlink and false for all other flags.
You should never use GLib.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 GLib.FileTest.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! |[<!-- language="C" --> // 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 GLib.FileTest.exists and GLib.FileTest.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 GLib.FileTest.is_symlink will always return false. Testing for GLib.FileTest.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.
- filename
a filename to test in the GLib file name encoding
- test
bitfield of GLib.FileTest flags
- Returns
whether a test was true