Re: Corrupted Data

Jimstone77_at_aol.com
Date: 10/30/03


Date: Thu, 30 Oct 2003 15:37:40 EST
To: beginners@perl.org

In a message dated 10/30/03 12:04:07 PM Eastern Standard Time,
Jenda@Krynicky.cz writes:

> Yes there is. (Apart from the typo on the open(NEW,...) line.)
>
> The problem is that things could happened between you close the old
> file and rename the new one. And even more likely there can be a
> process that already has the old file open. So even if you rename the
> files, the process still has the old one. And as soon as you unlock
> it, the other process starts reading ... the old data.
>
> You HAVE to use a separate log file!
>
> Eg. like this (I'm sure someone will be happy to correct me if I
> screw up)
>
> sysopen(FH, "file.lock", O_WRONLY|O_EXCL|O_CREAT)
> or die "can't open file.lock: $!";
> open(OLD,$old) or die "Can't Open File: $!";
> open(NEW,">$new") or die "Can't Open File: $!";
> while (<OLD>) {
> if ($_ =~ /NO_EMAIL/) {
> $count++;
> } else {
> print NEW $_;
> }
> }
> close(NEW) or die "Can't Close File: $!";
> close(OLD) or die "Can't Close Old: $!";
> unlink $old or die "cannot unlink the old file\n";
> rename($new => $old) or die "cannot rename\n";
> close FH;
> unlink "file.lock";
>
> or
>
> open(LCK, ">file.lock")
> or die "can't open file.lock: $!";
> # so someone else has it opened as well, who cares
> flock(LCK, LOCK_EX)
> or die "can't lock the lock: $!";
> # I am the only one who can have it locked though
> open(OLD,$old) or die "Can't Open File: $!";
> open(NEW,">$new") or die "Can't Open File: $!";
> while (<OLD>) {
> if ($_ =~ /NO_EMAIL/) {
> $count++;
> } else {
> print NEW $_;
> }
> }
> close(NEW) or die "Can't Close File: $!";
> close(OLD) or die "Can't Close Old: $!";
> unlink $old or die "cannot unlink the old file\n";
> rename($new => $old) or die "cannot rename\n";
> close LCK;
>
> The second seems a little safer to me (the system will remove the
> LOCK (not the lock file, just the lock) if the script dies).
>
> Jenda
>

 A Thousand Thanks! As a real "beginner" I appreciate your taking the time to
help me find a solution to this problem, and explaining the possible reasons.
I think I now have a better grasp of what's going on and why. Again, many
Thanks for your patience!



Relevant Pages

  • [tip:perf/core] tracing: Rename lockdep event subsystem into lock
    ... Lockdep events subsystem gathers various locking related events ... such as a request, release, contention or acquisition of a lock. ... Hence this rename. ...
    (Linux-Kernel)
  • Re: Semaphores
    ... I've never used a rw lock. ... not support atomic rename, you must use a read-write lock and control ... same file at the same time, use an atomic rename. ... set LockID [lock $store] ...
    (comp.lang.tcl)
  • A new and very robust method for doing file locking over NFS?
    ... I'd like to do file locking over NFS without using lockd. ... Instead of using lockf(), lock a file by creating a lock ... instead of using a lock file to lock a file, we rename the file to ...
    (comp.unix.programmer)
  • Re: Solution: Windows Vista lockup when idle for a while
    ... Here's what Tae Song wrote on 9/04/09: ... It didn't lock up during scans, so I used that for a while to keep the computer for locking up when I was away from the keyboard. ... Take ownership of DfrgNtfs.exe and then rename it. ... excessive amount of CPU time. ...
    (microsoft.public.windows.vista.general)
  • Re: Is a file open
    ... This method assumes the rename operation ... releases the lock only when done, so if you try to access the file ... Don't process the data file until you see the flag file. ...
    (comp.os.vms)