Re: Utility functions from gnome-libs



Darin Adler <darin@eazel.com> writes:

> On 3 Jul 2000, Owen Taylor wrote:
> >>> enum {
> >>> G_FILE_TEST_ISFILE=1<<0,
> >>> G_FILE_TEST_ISLINK=1<<1,
> >>> G_FILE_TEST_ISDIR=1<<2,
> >>> G_FILE_TEST_EXISTS=(1<<0)|(1<<1)|(1<<2) /*any type of file*/
> >>> };
> >>> 
> >>> gboolean g_file_test   (const char *filename, int test);
> >>> gboolean g_file_exists (const char *filename);
> >>> 
> >>> (These might alternatively be called g_file_race() ;-), but
> >>> they are useful for a lot of things, and a note in the
> >>> docs about security implications should be OK.)
> 
> Tim Janik <timj@gtk.org> writes:
> >> no, that's not a good idea. instead, it'll probably not hurt to
> >> simply implement g_file_test() in terms of:
> >> 
> >> typedef enum
> >> {
> >> G_FILE_IS_FILE    = 1 << 0,
> >> G_FILE_IS_LINK    = 1 << 1,
> >> G_FILE_IS_DIR        = 1 << 2,
> >> G_FILE_EXISTS        = G_FILE_IS_FILE | G_FILE_IS_LINK | G_FILE_IS_DIR
> >> } GFileType;
> >>
> >> GFileType g_file_test (const char *filename);
> 
> on 7/5/00 11:27 AM, Owen Taylor at otaylor@redhat.com wrote:
> > Yes, the enumeration should be named, not anonymous. Was there
> > another objection or was that all?
> 
> I believe Tim's point was that all the tests are done atomically instead of
> passing in the test you want to do, and this would somehow minimize the race
> conditions.

Hmm, it doesn't help the race conditions - the gnome-libs way,
you can do:

 g_file_test(filename, G_FILE_IS_LINK | G_FILE_IS_DIR);

The race conditions aren't between the various tests. The
race condition is between the tests and actually doing
something else with the file.

[ For instance, writing:

 if (g_file_exists (file))
   {
     fd = open (file, O_RDONLY);
     read (fd, buf, 10);
   }

Is completely broken. ]

The big argument in my mind for keeping it something like
the way it is in GNOME libs, is that we can add more tests,
which can

 a) be expensive (that is, we can avoid doing them if they
    aren't in the mask.)
 b) be mutually exclusive.

I think a simple cleanup of the current gnome-libs API
would be most appropriate.

Regards,
                                        Owen

typedef enum {
  G_FILE_TEST_IS_FILE    = 1<<0,
  G_FILE_TEST_IS_LINK    = 1<<1,
  G_FILE_TEST_IS_DIR     = 1<<2,
  G_FILE_TEST_EXISTS     = 1<<3,
  /* more tests as appropriate can be added later */
  G_FILE_TEST_WRITEABLE  = 1<<4, /* -r file */
  G_FILE_TEST_OWNED =      1<<5  /* -O file */
  [...]
} GFileTest;

gboolean g_file_test (const char *filename, guint test);

(The cleanups here are the named enum, ISFILE => IS_FILE,
EXISTS as a separate flag)




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]