[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] file locking



Chris Nandor writes...
>Anyway, there is a flock() done on the CSV file.  At first I just did an if
>($^O ne 'MacOS') around the flocks.  But now I am thinking something like
>this might be better:
>
>  if ($^O eq 'MacOS') {
>    chmod(0444, $file);
>    eval("END {chmod 0666, '$file'}");
>  } else {
>    do_flock_stuff;
>  }
>
>I think that should lock the file, and then unlock it open exiting the
>program, even if it exits prematurely (unless MacPerl itself dies).
>
>Any thoughts?
>


The script at the end of this message can be used to explore your technique.

Run it the first time, as is, and the result is:
line 1
line 2
line 3

If you select the "lock_file_test.txt" file and do a "Get Info" the
"Locked" checkbox is unchecked.

If you open the file with a text editor, make changes to it, and save it
when you reopen it you'll see those changes were saved so the file is not
locked from the finder.

---

Commenting out these two lines:
   # chmod(0444, $file);
   # eval("END {chmod 0666, '$file'}");
and run the code a second time and the result is:
line 1
line 2
line 3
line 4
line 5
line 6

---

0r,comment out the following lines:
   # $text1 = "line 1\nline 2\nline 3\n";
   # open(FILEDATA, ">$file" || die $^E );
   # print FILEDATA ($text1);
   # close(FILEDATA);

   # chmod(0444, $file);
   # eval("END {chmod 0666, '$file'}");
and run the program the second time and the result is:
line 1
line 2
line 3
line 4
line 5
line 6

---

Comment out the following line before running the original code:
   # eval("END {chmod 0666, '$file'}");
and the result is:
line 1
line 2
line 3

Select the "lock_file_test.txt" file and do a "Get Info" the "Locked"
checkbox is checked.

---

If I understand what you meant by "that should lock the file, and then
unlock it open exiting the program" then I'd say you're right.

Very clever.

David Seay
http://www.mastercall.com/g-s


--------------------------------

#!perl

$program_folder_path = `pwd`;
chomp($program_folder_path);
$file = $program_folder_path . ":lock_file_test.txt";

$text1 = "line 1\nline 2\nline 3\n";
open(FILEDATA, ">$file" || die $^E );
print FILEDATA ($text1);
close(FILEDATA);

chmod(0444, $file);
eval("END {chmod 0666, '$file'}");

$text2 = "line 4\nline 5\nline 6";
open(FILEDATA, ">>$file" || die $^E );
print FILEDATA ($text2);
close(FILEDATA);

open(FILEDATA, "$file" || die $^E );
@file = <FILEDATA>;
close(FILEDATA);
$lines = join("",@file);
print "\n\nFILE: \'$file\'\n$lines";






***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch