This is an archived low-energy page for bots and other anonmyous visitors.
Please sign up if you are a human and want to interact.
Tomtom has asked for the wisdom of the Perl Monks concerning the following question:
Hello everyone,
I wanted to know if you knew of another way to lock files, other than by using flock.
The problem is that my script writes to files, that can also be written to by other scripts, which aren't mine, and that don't implement flock.
Isn't flock useless if it isn't used by every script writing to a file ?
Are there any modules or else that can check if a file is being written to, without using flock ?
Thanks,
Tomtom
Re: file locking
by sweetblood (Prior) on May 16, 2005 at 09:20 UTC
|
I have had similar issues in the past and have been able to solve them by do a chmod to keep others out while I process.HTH
| [reply] |
|
|
The other scripts are, unfortunately, executed by the same user as my scripts :(
| [reply] |
Re: file locking
by halley (Prior) on May 16, 2005 at 09:34 UTC
|
You're going to have trouble with this scenario. There's no good portable way to check if a file is being modified by someone else, especially if you're dealing with network mounted filesystems of any flavor. (And no, lsof isn't a good portable way, but you might check its man page if it's available anyway.)
The most secure method would be to centralize all read-write responsibilities to one process, such as a daemon. That's what databases are: one process that can manipulate the files in a centralized way.
Barring that, you'll have to erect some systems involving baling wire and twine. Er, I mean chmod, rename and/or creating/unlinking auxiliary files. The only benefit to these operations is that they are, for the most part, atomic on all useful filesystems.
As for flock(), it's not even guaranteed to work. It's a "suggestion" or "advisory" and not a stable effective lock. If it works, great, but don't count on it working in all situations.
-- [ e d @ h a l l e y . c c ]
| [reply] [d/l] [select] |
Re: file locking
by ambrus (Abbot) on May 16, 2005 at 09:48 UTC
|
Mandatory locking can stop other processes reading/writing
the file even if it does not lock it.
However, it is system dependent.
See fcntl(2) and linux/Documentation/mandatory.txt
if you're on linux. (Update: read the section
on Leases in fcntl(2) too, it might be what you want.)
Note that if you want to lock mailbox files, there's
some special convention to lock them through a lockfile,
but I don't know the details.
Also, you might not even need locking.
In some cases,
you can just write the modifications to a different file
and rename/link the new file to the old name,
thus replacing the old file atomically.
Update: good as it sounds,
even mandatory locking or leashes may not be enough
if the programs not controlled by you want to
write the file asynchronously. If they do not do
the writing in an atomic manner, your script
may lock the file in an invalid state, when it is only
paritally changed. This is why co-operative locking
is important.
| [reply] |
Re: file locking
by fauria (Deacon) on May 16, 2005 at 09:55 UTC
|
You can try os specific tools to see if a file is being used, for example fuser:
open FILE, "sample";
system("fuser sample") or print "File not in use\n";
close FILE;
system("fuser sample") or print "File not in use\n";
Maybe Linux::Fuser from CPAN can help you. | [reply] [d/l] |
Re: file locking
by tcf03 (Deacon) on May 16, 2005 at 11:08 UTC
|
| [reply] |
|
|