Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Read/Update File

by rpike (Scribe)
on Aug 24, 2010 at 20:32 UTC ( #857020=perlquestion: print w/ replies, xml ) Need Help??
rpike has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to update a file - so read it, insert data into appropriate places within the contents retrieved, and then write the file back. I also need to make sure no other user can open the file and read and/or write to it while the process is taking place for another user running it. What are the exact steps needed to take in order to accomplish this? For simplicity sake let's say I'm reading a file of persons' names and I want to enter a name and insert it in the proper alphabetic location within the file (a name per line for instance). I don't need to know what code is needed to insert the name, jsut what steps can I take to read and write back the contents with a lock from read up to write (if possible). Thanks for any help in advance. AggressiveFish

Comment on Read/Update File
Re: Read/Update File
by JavaFan (Canon) on Aug 24, 2010 at 21:10 UTC
    I also need to make sure no other user can open the file and read and/or write to it while the process is taking place for another user running it. What are the exact steps needed to take in order to accomplish this?
    That depends on your OS/filesystem. If you have mandatory locking, just require an exclusive lock. Otherwise, you may need do drastic measures like dropping the run level so the OS runs in single user mode. Or by hacking the kernel sources, recompiling and installing the changed kernel. Of course, if you own the file, removing write access for everyone else easily accomplishes the task.
Re: Read/Update File
by ww (Bishop) on Aug 25, 2010 at 00:12 UTC
    You've used the requisite search terms -- file and lock.

    So why didn't you search for a howto?

    And, not just BTW, most of your "simplicity sake" (sic) discussion simply adds verbosity and concepts that are irrelevant to "jsut (sic) what steps" are required to accomplish file locking during a specific set of operations.

    OTOH, you don't tell us what OS (which is relevant, as JavaFan points out) nor do you show us any code or pseudocode illustrating what you've tried and where you're stuck.

Re: Read/Update File
by rpike (Scribe) on Aug 25, 2010 at 13:36 UTC
    I appreciate the help but I didn't think I was entering into a spelling bee. All I want to know is what is the best (most efficient, cross OS, generic) way of locking a file (to read in and modify the contents and then write them back to the same file using, if possible, the same filehandle). I don't want to have to read in a file with a lock, modify the contents, close that handle, then open the file again with a lock to write back. This leaves a small window between locks where the file could get read in before another user has his changes saved (the latter user would then overwrite the first user's changes). The application should be able to accomplish this on as many platforms as possible (Windows would come first, Unix, Linux). I'd appreciate any HELPFUL input on this, not posts that simply are used to criticize or weak attempts at trying to impress with their vocab with no helpful insight.
        Thank you for the post. Most helpful. :-) I've been trying to use +> to open the file for read/write purposes but cannot read the contents in. I'm trying to read the contents into an array such as (simpler version) but it isn't working :
        open (FH, ">+path"); my @lineArray = <FH>; print join("<br>", @lineArray); # nothing prints although the file do +es have content
        Is there something I have to do with buffering? Reading from the handle in this way is no good when opening for read/write? Any help on this would be most appreciated as well. Thanks.
      best (most efficient, cross OS, generic) way of locking a file
      There is no generic solution!! It will be OS and file system dependent. The best you could do is write (or find) a module that accounts for all of these variances. Though, as JavaFan indicated, even that could be challenge since on some systems you will have to isolate the file.

      Elda Taluta; Sarks Sark; Ark Arks

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://857020]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-10-23 02:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls