After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 139971 - Improvements to g_file_test
Improvements to g_file_test
Status: RESOLVED NOTABUG
Product: glib
Classification: Platform
Component: general
2.5.x
Other Linux
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks: 139973 139974
 
 
Reported: 2004-04-13 20:57 UTC by Ray Strode
Modified: 2004-12-22 21:47 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
add G_FILE_TEST_IS_READABLE, G_FILE_TEST_IS_WRITABLE, and g_file_test_full (6.54 KB, patch)
2004-04-13 20:59 UTC, Ray Strode
none Details | Review

Description Ray Strode 2004-04-13 20:57:33 UTC
It would be useful if g_file_test provided additional tests to check if the
filename parameter is readable or writable.
Comment 1 Ray Strode 2004-04-13 20:59:43 UTC
Created attachment 26633 [details] [review]
add G_FILE_TEST_IS_READABLE, G_FILE_TEST_IS_WRITABLE, and g_file_test_full

The above patch adds conditions for testing if a file is readable or writable. 
It also adds a new function g_file_test_full which checks if all conditions are
true, instead of if any conditions are true.
Comment 2 Owen Taylor 2004-04-21 19:07:26 UTC
We generally use _full() to mean *only* a function that takes an additional
g_destroy_notify()

g_file_test_all() or something might work.

The docs shouldn't be in two places, but instead be done as a cross-reference.

Can you provide non-broken use cases for READABLE/WRITABLE; even more than
general uses of g_file_test, these seem like to be present in code
that isn't properly checking for error conditions ... you need to check
for failure when you go ahead and do the operation, so why are you
testing first?
Comment 3 Ray Strode 2004-04-26 10:41:34 UTC
I'm using g_file_full_test() when searching through a list of paths (in order)
looking for a suitable file in one of them (see g_find_file_in_data_dir and
g_find_file_in_configuration_dir in bug 139973).  Those functions search each
directory in the appropriate $XDG_*_DIRS variables looking for some specified
file.  

I guess an example would be something like this:

do {
    char *filename;
    
    filename = g_find_file_in_configuration_dir("someconfigfile",
G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_READABLE);

    if (!filename || too much time has passed) 
      error out
} while (!load_file(filename));

I agree that g_file_test and g_file_test_all make it easy to write broken code,
but I don't see how G_FILE_TEST_IS_READABLE and G_FILE_TEST_IS_WRITABLE are any
different in that respect than the other tests.
Comment 4 Ray Strode 2004-06-14 13:17:27 UTC
The more I think about this the less confident I get that test_all is going to
be useful.  For my sepecific example above, it seems possible that load_file
could want to impose additional constraints on the file selected that find_file
couldn't know about (constraints beyond those imposed by GFileTest).  In that
case find_file would continually return the same file each iteration of the loop
and things would screw up.  I guess adding more test conditions to GFileTest
doesn't really help the problem, because the user can always want some
constraint that isn't part of GFileTest.  

More generally, test_all doesn't make any guarantees that all the test
conditions will be true by the time the function ends (or even by the time any
of the other test conditions are checked) and that's really quite broken to
think about.
Comment 5 Matthias Clasen 2004-07-31 13:22:02 UTC
Hmm, the usecase in the xdg basedir api is to find a file with certain
properties to call some function on it. Maybe a nicer solution is to let the
find_file function take a callback and loop until it finds true.