in reply to opening files: link checking and race conditions

I hope I am not being dense, but I can't yet see why you can't check for linkness on the file using it's spec but after opening.
my $lflag; open my $fh, ">>foo" or ErrorHandler( $?, $! ); $lflag = ( -l 'foo' ); unless( $lflag } { # process file } close $fh; # it was opened for append but append only occurred for non +-link case

One world, one people

Replies are listed 'Best First'.
Re^2: opening files: link checking and race conditions
by Joost (Canon) on Aug 03, 2005 at 12:48 UTC
      okay but if that is a problem too, doesn't this fix both?
      my $lflag = ( -l 'foo' ); # check before opening $lflag or open my $fh, ">>foo" or ErrorHandler( $?, $! ); $lflag = ( -l 'foo' ); # and again after opening unless( $lflag } { # process file } $lflag or close $fh;

      One world, one people

        Google "atomicity" and "race case" for more information, but here's a brief way how that would die: (P1 is process 1, P2 is process 2, they're not actually in the same program, but the commands from each are interleaved here since that can happen in reality)
        P1: my $lflag = ( -l 'foo' ); # seems good P2: makes foo a link - uh oh! P1: $lflag or open my $fh, ">>foo"; # does it fine, since $lflag was s +et before P2: remove that link, lickity-split! P1: $lflag = ( -l 'foo' ); # whups, everything *seems* ok...
        Make sense? Dave_the_m's code will exactly perform the actions for P2, btw. (Eimi's earlier explanation did exactly what your code does, too - look it over, the -l check happens before and after.)