Results 1 to 10 of 16
Hi,
I want to lock the file in linux and the file cannot be edit or modify by other .I know in perl, there is function flock , but it ...
- 08-31-2009 #1Just Joined!
- Join Date
- Aug 2009
- Posts
- 44
lock the file in linux
Hi,
I want to lock the file in linux and the file cannot be edit or modify by other .I know in perl, there is function flock , but it is not worked.
The file can be modifed and edit even if it is locked by flock .
Any other way to lock the file and so other cannot edit or modifed it ????
Any similar fucntion in perl ????
thank
- 08-31-2009 #2
You should look into unix / linux file permisions. Googling should provide what you need.
also possibly the imutable bit file attribute can help prevent an accidental overwrite, but it won't alone stop a determined person.New to the internet, technical forums, or the hacker / open source community??
Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html
RHCE for RHEL version 5
RHCT for RHEL version 4
- 08-31-2009 #3Just Joined!
- Join Date
- Aug 2009
- Posts
- 44
- 08-31-2009 #4
Why wouldn't you use the built in functionality of the OS? The only way to lock a file is to set the permissions properly. There should be a way to call chmod or chattr inside perl.
- 08-31-2009 #5Just Joined!
- Join Date
- Aug 2009
- Posts
- 44
- 08-31-2009 #6
- 08-31-2009 #7
That is not the only way, nor is it the right way. Specifically, using permissions doesn't prevent other processes run by the same user from accessing the file - nor does it protect against race conditions, which is one of the reasons for using locks.
Now, to answer the original question: I believe the problem here is that flock() creates what is known as an advisory lock on the file. That is, it's basically a semaphore - applications that respect this advisory lock are safe from race conditions, but the kernel does nothing to enforce control according to the file lock.
"Mandatory" file locking mechanisms do exist - these are the type of file locks which are enforced by the kernel - that is, if a process obtains a lock on a portion of a file such that no other process may write to that region of the file, then attempts by other processes to write to that region of the file will block until the lock is released.
Try man lockf(), see also mandatory.txt. lockf() is part of the POSIX standard - apparently it originated with SYSV...
(EDIT): and in Perl: File::lockf
Note you need a file descriptor for this call - that is, a file obtained with a syscall like open() as opposed to a library call like fopen()...
Finally, I highly recommend the book "Advanced Programming in the Unix Environment" - it's big and fairly expensive, but it's fantastic for this kind of information. In addition to telling you how to do stuff like this, it can also tell you to what extent various features are compatible with various common Unix systems such as BSD, Mac OS X, Linux, etc.
(EDIT again): Fixed the link to the book so it points to the 2nd edition...
- 08-31-2009 #8
tetsujin is 100% correct in that locks obtained not by the method described in mandatory.txt are all purely voluntary, and are not enforced by the kernel.
The question that comes immediately to my mind is "Why do you need a mandatory file lock?". The purpose of the voluntary lock is generally to keep a file in a sane state: if the data in a file is being modified, it can be locked to prevent other programs that read the file from getting garbled data. In this case, the programs know that locking is employed, so they will respect voluntary locks.
What are you trying to prevent with your mandatory lock?DISTRO=Arch
Registered Linux User #388732
- 09-02-2009 #9Just Joined!
- Join Date
- Aug 2009
- Posts
- 44
- 09-02-2009 #10Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Unlike Windows, Linux/Unix have no concept of a "busy" file. File locks are advisory only. To truly "lock" a file, you have to remove all permissions from the file, which can only be done by root or the owner of the file. In such a case, until at least read permissions are restored then no one, not even the owner of the file, can access it, though it can still be removed by anyone with adequate permissions on the containing directory.
So, the real question here is: what exactly are you trying to accomplish?Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
