jrbush82 has asked for the wisdom of the Perl Monks concerning the following question:

I am appending information to a file with the following:
# 0 to go directly to the web, or 1 to authorize first $auth = 0; if ($auth == 0) { LINE: for ($i=0; defined($i); $i++){ unless (-e "./data/$month.dat") { open (FILE, "+>>./data/$month.dat"); flock(FILE,2) or next LINE; print FILE "$event\|$month $day $year\|$place\|$addr\|$desc\|$contact\ +|$phone\|$county\|"; close FILE; last LINE; } } # Tried to add this to get access to file, but didn't work # chmod 0666, "./data/$month.dat"; }elsif ($auth == 1){ LINE: for ($i=0; defined($i); $i++){ unless (-e "./tempevents/$i.dat") { open (FILE, ">./tempevents/$i.dat"); flock(FILE,2) or next LINE; print FILE "$event\|$month $day $year\|$place\|$addr\|$desc\|$contact\ +|$phone\|$county\|"; close FILE; last LINE; } } } # End
It works when auth is set to 1 because it just creates a file for each post. When it is set to 0, I am appending information to a file. During the first post, it works fine, after that, I can't post to it or nothing, it just hangs. I have tried the script with >>, +<, >+, and +>>. Any ideas on why this doesn't work after the first post? I have also tried running chmod as it is commented out above. Thanks, Jason Bush

Replies are listed 'Best First'.
Re: File not responding after first append
by dws (Chancellor) on Nov 27, 2001 at 23:59 UTC
    Any ideas on why this doesn't work after the first post?

    You're liable to get further on this if you check errors, particularly the result of open(). By not checking, you're discarding potentially valuable clues. Try:

    open(FILE, ">./tempevents/$i.dat") or die "./tempevents/$i.dat: $!";
    I suspect you don't have permission to write into tempevents/

    Note also that you have a potentially file-corrupting race condition when you're appending a record (in the $auth==0 case). Between the time you open() the file and the time you flock() it, there's a window were a separate process can add a record, which you're positioned to overwrite. The solution is to seek to EOF once you've obtained the lock.

    And are you sure you don't want a "\n" on the end of each record?

Re: File not responding after first append
by Dogma (Pilgrim) on Nov 28, 2001 at 00:20 UTC
    First Read This then for the LOVE OF GOD read perlstyle.

    There a couple of things you could try here. Trapping file open and closes would be the frist thing. You might also want to take a look at using sysopen().

Re: File not responding after first append
by impossiblerobot (Deacon) on Nov 28, 2001 at 01:58 UTC
    Maybe I'm just not paying enough attention, but it looks like you're only trying to append when the file doesn't exist; otherwise you're just looping.

    LINE: for ($i=0; defined($i); $i++){ # Loop eternally unless (-e "./data/$month.dat") { # If file does not exist, do open (FILE, "+>>./data/$month.dat"); . . . close FILE; last LINE; # Loop will only exit if file did not exist } }


    Impossible Robot
Re: File not responding after first append
by Gerryjun (Scribe) on Nov 28, 2001 at 00:05 UTC
    try using cmod 777, and look in your error_log it would really help on errors!

      Ack! If you use 'chmod 777', please only do it temporarily to see if permissions are part of your problem. This opens up a whole can of worms and security problems for your application if any other process owned by any other user can just go in and make changes to files and directories on a whim.

      If doing this fixes your problem, please try to find out why this fixed your problem and adjust your permissions or methods so that things continue to be secure. You should never have files or directories with permissions set read-write-execute across the board.