hi, is there (in Haskell) a function like existFile :: FilePath -> IO (Bool) ? Thanks!
Lennart Augustsson <lennart@augustsson.net> writes:
Diego Yanivello wrote:
hi, is there (in Haskell) a function like existFile :: FilePath -> IO (Bool) ? Thanks!
Using such a function is generally a bad idea because of race conditions.
however, real world programs use those tests since you don't need to care *everytime* about race conditions. (of course using this existFile before creating a temporary file is wrong, but existFile has *many* other applications)
Just because many applications use this doesn't make it safe or good. Presumably you are testing for the existence of the file to do something to it (read, create, delete...). It's better to do the operation you want to do and have it fail if the file is missing (or existing if you create). You then just have to handle the exception (that part is already in H98) or proceed with the normal path. I claim the code simpler this way and you avoid any race conditions. -- Lennart Pixel wrote:
Lennart Augustsson <lennart@augustsson.net> writes:
Diego Yanivello wrote:
hi, is there (in Haskell) a function like existFile :: FilePath -> IO (Bool) ? Thanks!
Using such a function is generally a bad idea because of race conditions.
however, real world programs use those tests since you don't need to care *everytime* about race conditions. (of course using this existFile before creating a temporary file is wrong, but existFile has *many* other applications) _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
Lennart Augustsson <lennart@augustsson.net> writes:
Just because many applications use this doesn't make it safe or good. Presumably you are testing for the existence of the file to do something to it (read, create, delete...). It's better to do the operation you want to do and have it fail if the file is missing (or existing if you create). You then just have to handle the exception (that part is already in H98) or proceed with the normal path. I claim the code simpler this way and you avoid any race conditions.
I said real-world pbs ;p WARNING, perl below! - checking if devfs is mounted in /dev: -e "/dev/.devfsd" - checking wether there is cdrom drive with devfs: -e "/dev/cdroms/cdrom0" - looking for a file in a PATH-like: if ($f !~ m|/|) { -e "$_/$f" and $f = "$_/$f", last foreach @icon_paths } - if (-e "$f.gz") { # ... the wanted file is gzip'ed, doing whatever is needed } - -x "/bin/sh" or die "cannot open shell - /bin/sh doesn't exist"; fork and return; # doing things # now, and only now exec'ing exec "/bin/sh" - if (! -e '/usr/sbin/showmount') { my $pkg = 'nfs-utils-clients'; $in->ask_okcancel('', _("The package %s needs to be installed. Do you want to install it?", $pkg), 1) or return; $in->do_pkgs->install($pkg); } If you want some more, just ask ;)
Thank you for making my point. All these are indeed examples of broken code. (Not because it's Perl. :) -- Lennart Pixel wrote:
Lennart Augustsson <lennart@augustsson.net> writes:
Just because many applications use this doesn't make it safe or good. Presumably you are testing for the existence of the file to do something to it (read, create, delete...). It's better to do the operation you want to do and have it fail if the file is missing (or existing if you create). You then just have to handle the exception (that part is already in H98) or proceed with the normal path. I claim the code simpler this way and you avoid any race conditions.
I said real-world pbs ;p
WARNING, perl below!
- checking if devfs is mounted in /dev: -e "/dev/.devfsd" - checking wether there is cdrom drive with devfs: -e "/dev/cdroms/cdrom0" - looking for a file in a PATH-like: if ($f !~ m|/|) { -e "$_/$f" and $f = "$_/$f", last foreach @icon_paths } - if (-e "$f.gz") { # ... the wanted file is gzip'ed, doing whatever is needed }
- -x "/bin/sh" or die "cannot open shell - /bin/sh doesn't exist"; fork and return; # doing things # now, and only now exec'ing exec "/bin/sh"
- if (! -e '/usr/sbin/showmount') { my $pkg = 'nfs-utils-clients'; $in->ask_okcancel('', _("The package %s needs to be installed. Do you want to install it?", $pkg), 1) or return; $in->do_pkgs->install($pkg); }
If you want some more, just ask ;) _______________________________________________ Haskell mailing list Haskell@haskell.org http://www.haskell.org/mailman/listinfo/haskell
--- Pixel <pixel@mandrakesoft.com> wrote:
Lennart Augustsson <lennart@augustsson.net> writes:
Diego Yanivello wrote:
hi, is there (in Haskell) a function like existFile :: FilePath -> IO (Bool) ? Thanks!
Using such a function is generally a bad idea because of race conditions.
however, real world programs use those tests since you don't need to care *everytime* about race conditions. (of course using this existFile before creating a temporary file is wrong, but existFile has *many* other applications)
Could someone post an example of the creation of a temporary file where race conditions are important? Thanks, David J. Sankel __________________________________________________ Do You Yahoo!? Yahoo! Sports - live college hoops coverage http://sports.yahoo.com/
Could someone post an example of the creation of a temporary file where race conditions are important?
/any/ programme that does this on a multi-process system. Between the test for existence and the creation, some other process could have created a file of the same name. Then the create fails because of insufficient permissions, so the programme has to deal with failure anyway, or it succeeds and stomps on the data belonging to the other process. do possible_handle <- try $ openFile "whatever" ReadMode case possible_handle of (Right handle) -> do_things_to handle (Left error) -> do whatever you would have done had the existence test returned false is no more complex than a version using an existence test, but to create a file for writing, surely we need an openNewFile primitive? Otherwise we might open on a file that already exists and hit the "stomp" error mentioned above. Jón -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk 31 Chalmers Road jf@cl.cam.ac.uk Cambridge CB1 3SZ +44 1223 570179 (after 14:00 only, please!)
On Wed, Mar 20, 2002, Jon Fairbairn wrote:
Could someone post an example of the creation of a temporary file where race conditions are important?
/any/ programme that does this on a multi-process system.
Occasionally, the presence or absence of a file (usually empty) of a certain name in a certain directory is used for communication between processes on a multi-process system. David
On Wed, Mar 20, 2002, Jon Fairbairn wrote:
Could someone post an example of the creation of a temporary file where race conditions are important?
/any/ programme that does this on a multi-process system.
Occasionally, the presence or absence of a file (usually empty) of a certain name in a certain directory is used for communication between processes on a multi-process system.
Hence the need for an atomic openNewFile. -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk 31 Chalmers Road jf@cl.cam.ac.uk Cambridge CB1 3SZ +44 1223 570179 (after 14:00 only, please!)
Hi, The standard library module Directory contains doesFileExist :: FilePath -> IO Bool See http://www.haskell.org/onlinelibrary/directory.html Regards, Thomas Hallgren PS If using doesFileExist is a bad idea, then using some of the other functions from the module Directory, e.g. getDirectoryContents, is probablya bad idea too... Lennart Augustsson wrote:
Using such a function is generally a bad idea because of race conditions.
-- Lennart
Diego Yanivello wrote:
hi, is there (in Haskell) a function like existFile :: FilePath -> IO (Bool) ? Thanks!
participants (7)
-
David Feuer -
David Sankel -
Diego Yanivello -
Jon Fairbairn -
Lennart Augustsson -
Pixel -
Thomas Hallgren