open (LOCK, "$admin_dir/locks.txt") || &error_message("File Locking Error", "Could not open file $admin_dir/locks.txt. Make sure $admin_dir/locks.txt exists and is world writable.");
@LOCK = <LOCK>;
close (LOCK);
$time = time;
if (($ll) = grep(/\*/, @LOCK)) {
($timest) = split(/,/, $ll);
if ((time - $timest) <= 180) {
$es = $L{'FILELOCKREGEN'};
$t = (180 - (time - $timest));
$es =~ s/\%sec/$t/g;
&error_message("$L{'FILELOCKERROR'}", "$es");
} else {
@LOCK = grep(!/\*/, @LOCK);
}
}
if (!grep(/,$filename\s*$/, @LOCK)) {
open (LOCK, ">>$admin_dir/locks.txt") || &error_message("File Locking Error", "Could not append file $admin_dir/locks.txt. Make sure $admin_dir/locks.txt exists and is world writable.");
print LOCK "$time,$filename\n";
close (LOCK);
return 1;
} else {
$ctr = 0;
($line) = grep(/,$filename\s*$/, @LOCK);
($time_l) = split(/,/, $line);
if ($time > ($time_l + 5)) {
@LOCK = grep(!/,$filename\s*$/, @LOCK);
push (@LOCK, "$time,$filename\n");
open (LOCK, ">$admin_dir/locks.txt") || &error_message("File Locking Error", "Could not write file $admin_dir/locks.txt. Make sure $admin_dir/locks.txt exists and is world writable.");
print LOCK @LOCK;
close (LOCK);
return 1;
} else {
for ($ctr = 0; $ctr <= 100; $ctr++) {
open (LOCK, "$admin_dir/locks.txt");
@LOCK = <LOCK>;
close (LOCK);
$time = time;
if (!grep(/,$filename\s*$/, @LOCK)) {
open (LOCK, ">>$admin_dir/locks.txt") || &error_message("File Locking Error", "Could not append file $admin_dir/locks.txt. Make sure $admin_dir/locks.txt exists and is world writable.");
open (LOCK, "$admin_dir/locks.txt") || &error_message("File Unlocking Error", "Could not open file $admin_dir/locks.txt. Make sure $admin_dir/locks.txt exists and is world writable.");
@LOCK = <LOCK>;
close (LOCK);
@LOCK = grep(!/,$filename\s*$/, @LOCK) if $filename ne "*";
@LOCK = grep(!/\*/, @LOCK) if $filename eq "*";
if (scalar(@LOCK) == 0) {
push (@LOCK, "#\n");
}
open (LOCK, ">$admin_dir/locks.txt") || &error_message("File Unlocking Error", "Could not open file $admin_dir/locks.txt (System returned error: $!). Make sure $admin_dir/locks.txt exists and is world writable.");
open (FILE, ">$filename.NEW") || &error_message("File Save - Write Error", "Could not open $filename.NEW for writing.");
print FILE @file;
close (FILE);
if (-e "$filename.NEW") {
$fsize_desired = scalar(@file);
$fsize = -s "$filename.NEW";
if ($fsize < $fsize_desired) {
undef $!;
unlink ("$filename.NEW");
&error_message("File Save - Write Error", "Error trying to write to $filename -- source file has wrong length (Desired: $fsize_desired; actual: $fsize). Check your disk quota!");
&error_message("File Save - Write Error", "Error trying to write to $filename -- source file has wrong length (Desired: $fsize_desired; actual: $fsize). Check your disk quota!");
}
unlink ("$filename.NEW");
}
chmod (0666, "$filename");
} else {
&error_message("File Save - Write Error", "Could not open $filename.NEW for reading.");