[PLUG] file locking
bain at devslashzero.com
Wed Jul 22 20:21:21 IST 2009
>>> I am looking at a user level approach, not a programmer level approach.
>> For a user level (requires cooperation) approach, assume you have the same
>> app sharing files in the same folder, then this app co-operates with other
>> instances by always calling for exclusive lock, then NOT working on the file
>> until it can gain the lock.
> I did not expect to be misunderstood on such a grand scale; let me be
> more specific.
> If a file (inode) has already been opened, all other calls to 'open'
> the file should fail. As simple as that. Is there a way to do this
> using either the native filesystem, or using samba?
Short answer ... no, its not possible on linux
Long answer: In some form its possible. if you mount file system with
'mand' options, and set sticky guid with execute permissions on a
file then all fcntl locks on that file become mandatory.
It took me a lot of time to understand this so i don't think you should bother.
More problems: it has races by design.... also the code and the api is
hardly ever used so i will be wary of using it in production
environments. And is generally very less understood.
Even more problems: mmap is still not locked out, and anything
changing stuff from any other api is exempted as well.
But in most cases use of advisory locking is sufficient
Just take a flock on the file in you app always and the app should
co-operate with other instances of the same app.
If you can describe the use case may be we can suggest a better solution
More information about the Plug-mail