• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Advisory Locks on files
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Advisory Locks on files


  • Subject: Re: Advisory Locks on files
  • From: Bill Bumgarner <email@hidden>
  • Date: Sun, 25 Jun 2006 19:23:38 -0700

On Jun 25, 2006, at 7:00 PM, Sanford Selznick wrote:
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 NSFileHandle API does not lock anything; it just opens a file for read/write or read-only access.


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.

You need to use the actual advisory locking API -- or some other locking API -- if you want locking type exclusivity to be applied. In general, you likely do not want one writer and many readers unless you are going to great lengths to prevent the readers from reading partially written data or ensuring that, when they do, the reader(s) don't explode.


More likely, you want something like shared-reader, exclusive-writer type semantics. Something like SQLite's behavior. I would suggest reading the incredibly detailed commentary in the SQLite source to learn more than you could possibly ever imagine needing to know about unix advisory locks, most of which is applicable to Mac OS X.

Specifically, the os_unix.c source file is the key:

http://www.sqlite.org/cvstrac/fileview?f=sqlite/src/os_unix.c&v=1.107

Except when it isn't. Be prepared to deal with some really nasty problems (or, take the road that Apple did, and live with a massive performance hit) when working with non-local filesystems. Certain NFS servers lie.

b.bum

_______________________________________________
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


References: 
 >Advisory Locks on files (From: Sanford Selznick <email@hidden>)

  • Prev by Date: Unicode strings in ppc and intel
  • Next by Date: Re: Unicode strings in ppc and intel
  • Previous by thread: Advisory Locks on files
  • Next by thread: Unicode strings in ppc and intel
  • Index(es):
    • Date
    • Thread