Dermot wrote:
Hi,

Hello,

I have a cgi script that writes to a disk file. It would be safest if I can
get an exclusive lock on the file. I had a look at the opentut and believe I
have followed that the example there. Here's what I have

sysopen my $fh, $file_path, O_WRONLY || die "can't open $file_path: $!\n";

You are using the high precedence '||' operator which means that O_WRONLY is ORed with die() before sysopen() is called and since O_WRONLY is probably always true then die() will *never* be called.

You intend to append to the file so you should probably also include O_APPEND with the third argument.

You want to either use parentheses:

sysopen( my $fh, $file_path, O_WRONLY | O_APPEND ) || die "can't open $file_path: $!\n";

Or use the low precedence 'or' operator:

sysopen my $fh, $file_path, O_WRONLY | O_APPEND or die "can't open $file_path: $!\n";


flock($fh,LOCK_EX) or die "can't lock $file_path: $!\n";
seek($fh, 0, 2);   # Append to file

It looks like you are using the Fcntl.pm module to get the LOCK_EX macro so you should use it to get the SEEK_END macro as well. And since you are using sysopen() to open the file you should use sysseek() instead of seek()

sysseek $fh, 0, SEEK_END or die "can't seek on $file_path: $!\n";

Although you don't really need to do this if you use O_APPEND with sysopen().


print $fh $status."\n";
print STDERR "$0: $! $?\n";

It makes no sense to print the values of $! or $? unless *an* *error* *actually* *occured*. Without an actual error the values in them will be meaningless.


close($fh);

I am getting the error
"Inappropriate ioctl for device 0"

That error message is not listed in perldiag.pod so it is not a Perl error/warning and it doesn't match any of the error messages in your code above so something other than your Perl program is producing it.

perldoc perldiag


The worst part about this problem is that it seems completely erratic.
Sometimes the string is written to the file and sometimes not. Sometimes I
get an error and sometimes not but it never dies and the user will think the
data has been written to file! Does anyone see something wrong with the
snippet above or have any insights on file-clocking they could offer.

perldoc -q lock



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to