Advisory Locks on files
Advisory Locks on files
- Subject: Advisory Locks on files
- From: Sanford Selznick <email@hidden>
- Date: Sun, 25 Jun 2006 19:00:33 -0700
Hello,
I have a document based application. I'd like the first person
that opens a document to get r/w access and all others to get r-only
access.
According to technote TN2037:
"Mac OS X will enforce exclusive file access, i.e. one writer and
many readers of a file, through it's application frameworks, Carbon,
Cocoa, and Java, by enforcing BSD advisory locks as though they are
exclusive. ... By accessing files through the application frameworks
(Carbon, Cocoa, Java), in versions of the OS supporting the advisory
locks feature in frameworks, this will be provided automatically if
you use the framework's file access methods."
In my Cocoa app, I open the file as follows:
{
fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
if (nil == fileHandle) {
// open read only
fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
...
}
[fileHandle retain];
}
The problem is, I can open filePath from multiple processes
simultaneously and they all get "updating" access! I know this is
the "unix" way, but it's contrary to the statement from TN2037.
What's the right way to open a file with one writer and multiple readers?
The technote mentions using open() with O_EXLOCK but this appears
to be one r/w and nobody else, not even readers-only.
Thank you,
Sanford
PS - I've read the archives and I'm not really looking to start a
war. I just want an answer to my questions above. :-)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden