From: REEKES@applelink.apple.com (Jim Reekes)
Newsgroups: comp.sys.mac.programmer
Subject: Re: Is a resource fork already open?
Date: 11 Sep 92 21:39:07 GMT
Followup-To: comp.sys.mac.programmer
Organization: Apple Computer, Inc.

In article <1992Sep8.153520.10687@waikato.ac.nz>, ldo@waikato.ac.nz
(Lawrence D'Oliveiro, Waikato University) wrote:
> 
> In article <39464@imag.imag.fr>, cinquin@imag.fr ( Philippe Cinquin) writes:
> > Sorry if this is a stupid question, but how do you know if the resource fork of
> > a file is already open?
> 
> If a file is already open by somebody else, you'll get an error if you try
> opening it in a mode that's incompatible with the other person's open mode.
> 
> But I suspect you're really asking what happens when you open a resource file
> twice from the same application. The answer is, you will get the same file
> refnum back the second time. So how do you tell that this is happened? It can
> be done, without too much low-level messing about.
> 
> The Resource Manager maintains a chain of currently-open resource files.
> It loads the "resource map" structure from each file into memory, and fills
> in a link field to chain all the resource maps together. There are two
> important low-memory globals that are used for this:
> 
> 	TopMapHndl -- handle to the first resource map in the chain
> 	CurMap -- refnum of the "current" resource file (returned by
> 	    CurResFile, can be changed with UseResFile to point anywhere
> 	    along the chain).
>
> **** some stuff deleted ****

The best approach to take is to avoid the problem in the first place.


* Stay away from low memory globals

* To determine if a file is already open, use PBGetCatInfo.

* To know when it's safe to close a resource file, make sure you're the
only one that opened it.  This can be done by first checking the ioFlAttrib
from PBGetCatInfo *BEFORE* you open the file.  If it's open, then don't
open is again.  Then you'll know that it's safe to close it.

* If you call OpenRFPerm with read-only, then you'll always get a new
FCB and map.  This means it's safe to close it.

* It's dangerous to have more than one path open to a resource file.
If one user has a read/write path, and a second use has another read-only
path, then the first user can change the file and this means the second
reader is screwed.  The file is corrupted to the 2nd reader's point of
view.

* To make things more complicated, and more dangerous, the System file is
always open and always available to everyone.  You'll always find the
System
map in your application's resource chain.  Additionally, any file below
the System will also be in your chain (fonts, suitcases, input methods,
etc.)
These are files you typically never open directly.  Just call GetResource
and assume it's been opened for you.  This is why system resources have
unique numbers and types.

-----------------------------------------------------------------------
Jim Reekes, Polterzeitgeist  |     Macintosh Toolbox Engineering
                             |          Sound Manager Expert
Apple Computer, Inc.         | RAll opinions expressed are mine, and do
20525 Mariani Ave. MS: 81-KS |   not necessarily represent those of my
Cupertino, CA 95014          |       employer, Apple Computer Inc.S
Newsgroups: comp.sys.mac.programmer