Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: unlink & rename in Win32

by growlf (Pilgrim)
on Dec 01, 2001 at 10:54 UTC ( [id://128839]=note: print w/replies, xml ) Need Help??


in reply to unlink & rename in Win32

Could you post a snippet of the offending code, perhaps?

*G*

Replies are listed 'Best First'.
Re: Re: unlink & rename in Win32
by Flame (Deacon) on Dec 01, 2001 at 20:27 UTC
    Certainly:

    sub changelog { if($GMS->getsetting('OTHER','LOGCLEAR') > param("NOWTIME")){ viewlog("Error: Time-Date mismatch."); exit; } my $filename = $GMS->getsetting('FILE','MEMBERDIRL').'/GMS'; open(logfile,"$filename.log") or $GMS->dienice("Unable to open log +file!"); flock(logfile,2) if($GMS->getsetting('OTHER','FLOCK')); my $length = scalar(@{[<logfile>]})-1; seek(logfile,0,0); my %lines; for(my $i = 1; $i <= $length+1; $i++){ $lines{$i} = 1 if(param("LINE$i")); } open(tempfile,">$filename.tmp") or $GMS->dienice("Unable to open te +mporary file!"); flock(tempfile,2) if($GMS->getsetting('OTHER','FLOCK')); seek(tempfile,0,0); my $count = 0; while(my $line = <logfile>){ $count++; if($lines{$count}){ next; }else{ print STDERR "$count kept\n"; } print tempfile $line; } close(tempfile); close(logfile); rename("$filename.log","$filename.bak") or $GMS->dienice(); rename("$filename.tmp","$filename.log") or $GMS->dienice(); unlink("$filename.bak") or $GMS->dienice(); $GMS->changesetting('OTHER','LOGCLEAR',time); #The GMS object actually does have a filehandle for the 'logfile' i +n the object #however, I have tried manually closing it and there was no differe +nce. $GMS->logaction("Removed elements from log file"); viewlog("Changes sucessful!"); }


    FYI: This is run under 'strict'
    Now, this code actually has one of the filehandles still open, however I had tried manually closing it with no effect...

    This is only a bit of the code... but I didn't think anyone would want me to send over 2000 lines of code.

    Does this clear anything up?


    -----BEGIN GEEK CODE BLOCK-----
    Version: 3.12
    GIT d- s:++ a--- C++++ UL P+++>++++ L+ E- W++>+++ N !o K- w+ O---- M-- V--
    PS PE Y- PGP t++(+++) 5(+++)++++ X R+@ tv+ b+++ DI+ D- G e->+++ h! r-- y-
    ------END GEEK CODE BLOCK------
    Translate

    "Weird things happen, get used to it."

    Flame ~ Lead Programmer: GMS

      I'm not sure but AFAIK on some OSes you should release locks creates with flock before closing filehandle. That is add
      flock(tempfile, 4); flock(logfile, 4);
      before closing these filehandles.

      P.S. You should avoid using numbers for constants as it is hard to read and is not fail proof. Use symbol names. use Fcntl ':flock' defines for you LOCK_SH, LOCK_EX, LOCK_UN and LOCK_NB constants.

      --
      Ilya Martynov (http://martynov.org/)

        While I wholeheartedly agree with your comment regarding constant usage and symbol names with flock, the question of releasing locks has been discussed previously here - The summary of this thread was that it wasn't necessary to unlock a file if you were about to close the file as the lock will be released by the closure.

         

        perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://128839]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 03:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found