[Pythonmac-SIG] Method to lock/unlock files

Jack Jansen Jack.Jansen@oratrix.com
Thu, 25 Apr 2002 22:39:52 +0200


On donderdag, april 25, 2002, at 04:50 , Mike Nardell wrote:

> I would like to know if there is a method in Python to 
> unlock/lock files (so
> they can be protected) on a Mac running on OS 8.6 or 9. Ideally 
> I would like
> a cross platform solution. It seems that the calls to do things 
> like change
> mode are not implemented in MacPython.

chmod() isn't implemented on MacOS 9 or earlier, only on OSX 
(which is unix-based). And, in general, a platform-independent 
locking solution isn't really possible, there's few areas where 
platforms diverge as much as in locking. Unix already has the 
two wildly incompatible lockf() and flock() methods (and, as 
some people say, one of them doesn't work in the interesting 
cases, and the other one never works:-). On OS9 and earlier (and 
on OSX if all participating parties use the Carbon API) file 
locking is done automatically on the file level (unless you take 
specific measures to disable it), i.e. if one process opens a 
file for writing noone else can access it and if one process 
opens a file for reading noone can open it for writing.

But I wouldn't be surprised if BSD-API based processes are 
completely unaware of this, and will happily break right through 
this (not sure though, just guessing).

All this is assuming that by "lock" you mean "locking against 
concurrent access". If you mean "protect from being written at 
all" (which, seeing your chmod reference may well be the case) 
that's a completely different ballpark. You want to look at the 
macfs module, in particular the FSSpec and FInfo objects.

> I have looked at some of the modules particular to the Mac. It 
> looks like
> the Carbon.Res would do what I want, I just can't seem to find any
> documentation on all of the Carbon stuff. I have looked around 
> at some other
> mac modules, and not been successful.

For the Carbon modules you can just refer to the Apple 
documentation. And you may want to look at the __doc__ string in 
a particular function to see how the C arguments are mapped to 
Python arguments. Aside from the Apple docs there's also a 
pretty good book called "Macintosh C" available at the MacTech 
site (for free!) that is in some respects better than Apple's 
docs.
--
- Jack Jansen        <Jack.Jansen@oratrix.com>        
http://www.cwi.nl/~jack -
- If I can't dance I don't want to be part of your revolution -- 
Emma Goldman -