Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
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 ...
  1. #1
    Just Joined!
    Join Date
    Aug 2009
    Posts
    44

    Smile 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

  2. #2
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    665
    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

  3. #3
    Just Joined!
    Join Date
    Aug 2009
    Posts
    44

    Smile lock the file in linux

    Quote Originally Posted by meton_magis View Post
    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.
    I do not want to use chmod or chattr to immune the file .
    I want to the process to lock the file ...
    Any suggestion in perl ???

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    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.

  5. #5
    Just Joined!
    Join Date
    Aug 2009
    Posts
    44
    Quote Originally Posted by coopstah13 View Post
    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.

    how to call chmod and chattr in perl ??
    Any advice or suggestion ????

  6. #6
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149

  7. #7
    Linux Newbie tetsujin's Avatar
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by coopstah13 View Post
    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.
    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...

  8. #8
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

  9. #9
    Just Joined!
    Join Date
    Aug 2009
    Posts
    44

    lock the file in linux

    Quote Originally Posted by Cabhan View Post
    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?
    Or any suggestion how to make "file busy" by perl script in linux ???
    any idea ???

  10. #10
    Linux Guru Rubberman's Avatar
    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!

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...