Re: Only allowing a file to be opened by one app
Re: Only allowing a file to be opened by one app
- Subject: Re: Only allowing a file to be opened by one app
- From: Andy Bettis <email@hidden>
- Date: Thu, 2 Jul 2009 00:36:11 +0100
On 2 Jul 2009, at 00:02, Michael Ash wrote:
On Wed, Jul 1, 2009 at 11:47 AM, Andy Bettis<email@hidden>
wrote:
Hi,
I'd like to set up my app so that if one user has a file open with it
another user cannot open the file. This only needs to limit access
by copies
of my app, no need to block out any other process.
In the past I'd just leave a write access path open when the file
was opened
from within my app - this would prevent anyone else from getting
write
access (and therefore opening the file) and has the bonus that if
the app
was closed down unexpectedly (these things happen) then the 'lock'
was
removed. This doesn't happen with default NSDocument based apps
(like mine)
- is there a way of doing it? Or a better way of setting exclusive
access?
Any help gratefully appreciated.
How about calling open() with the O_EXLOCK flag, or using the flock()
call on your document file?
I was hoping to integrate things gracefully within the AppKit
framework. It seems like the default NSDocument behaviour is to open a
file, load the data into an NSData structure, then close the file
before (or after?) passing the data on to readFromData:ofType:error:
to process. If I add a locking call I need to be sure it will be
unlocked before saving the file or the program will fall over itself
(if you see what I mean).
I thought I might need to supply a delegate to handle NSApplication's
application:openFile: method to do any locking and checking for
locking but there doesn't seem to be a corresponding closeFile call to
tidy things up.
Reading further it looks like I could achieve my locking by overriding
readFromURL:ofType:error: to leave the file opened with a lock (or to
give an error message if the file is locked) and remove the lock when
the file's window is closed. I'd also need to override
writeToURL:ofType:error: to allow for the file already being open.
Does this sound like a good plan?
I feel like this is a bit messy and there should be a more elegant way
of doing things - am I missing something?
Cheers,
Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden