[comp.sys.mac.programmer] Yikes: Using both resource & data forks of a file

bdugan@teri.bio.uci.edu (Bill Dugan) (04/17/91)

I'm creating a document file with "Create", writing data to the file's
data fork, and closing the file.  Everything's great.  But then I 
want to put some resources in the file.  I am trying "OpenRFPerm" with
write permission enabled, and it doesn't work; calling ResError() reveals
an EOF, which leads me to believe there's no resource fork, despite
IM's claim that Create will set up both forks of the file created.

My pathetic theory is that a newly created file, when closed, will not
get set up with a resource fork if no data has been written to that
fork.  But since the resource fork & data fork can (supposedly) be treated
separately, I don't see why I should have to nest my open & close calls
just to make sure it all gets written out correctly, and I haven't tried
this anyway.  Can anyone point me in a general direction?

Thanks in advance....

bill 

time@ice.com (Tim Endres) (04/17/91)

In article <280B842C.16551@orion.oac.uci.edu>, bdugan@teri.bio.uci.edu (Bill Dugan) writes:
> I'm creating a document file with "Create", writing data to the file's
> data fork, and closing the file.  Everything's great.  But then I 
> want to put some resources in the file.  I am trying "OpenRFPerm" with
> write permission enabled, and it doesn't work; calling ResError() reveals
> an EOF, which leads me to believe there's no resource fork, despite
> IM's claim that Create will set up both forks of the file created.

IM is correct, but misleading. When you use Creat(), you are indeed
creating the *catalog* entries for both forks of the file. In fact,
I have verified this looking at the catalog for a new file. HOWEVER,
since the resource fork is initially empty, there is not even a resource
map for OpenRFPerm() to load, thus the EOF error. You must, therefore,
create the map (and other needed space) in the resource fork with
a CreateResFile() call. This is *certainly* not obvious from IM.

tim.

-------------------------------------------------------------
Tim Endres                |  time@ice.com
ICE Engineering           |  uupsi!ice.com!time
8840 Main Street          |  Voice            FAX
Whitmore Lake MI. 48189   |  (313) 449 8288   (313) 449 9208

dorner@pequod.cso.uiuc.edu (Steve Dorner) (04/17/91)

>map for OpenRFPerm() to load, thus the EOF error. You must, therefore,
>create the map (and other needed space) in the resource fork with
>a CreateResFile() call. This is *certainly* not obvious from IM.

Since I almost always end up using the resource forks of files I create
(window position info, for one), I ALWAYS use CreateResFile,  not Create,
to create files.  I then don't have to do any special monkeying around to
use either fork.
--
Steve Dorner, U of Illinois Computing Services Office
Internet: s-dorner@uiuc.edu  UUCP: uunet!uiucuxc!uiuc.edu!s-dorner

stevec@Apple.COM (Steve Christensen) (04/18/91)

bdugan@teri.bio.uci.edu (Bill Dugan) writes:
>I'm creating a document file with "Create", writing data to the file's
>data fork, and closing the file.  Everything's great.  But then I 
>want to put some resources in the file.  I am trying "OpenRFPerm" with
>write permission enabled, and it doesn't work; calling ResError() reveals
>an EOF, which leads me to believe there's no resource fork, despite
>IM's claim that Create will set up both forks of the file created.
>
>My pathetic theory is that a newly created file, when closed, will not
>get set up with a resource fork if no data has been written to that
>fork.  But since the resource fork & data fork can (supposedly) be treated
>separately, I don't see why I should have to nest my open & close calls
>just to make sure it all gets written out correctly, and I haven't tried
>this anyway.  Can anyone point me in a general direction?

Create will initialize both forks to be zero length.  Adding to the data fork
is done by making Write calls.  To actually create the resource fork, i.e.,
set it up so you can add resources to it, you need to call CreateResFile
before calling OpenResFile/OpenRFPerm.  The reason it's done that way (I
presume), is so that you don't have to carry around a few hundred extra bytes
in the resource fork if all you wanted to do was put something in the data
fork.

Note that there are separate access paths to the resource and data forks of
a file (if both are open), so you can close one fork without affecting the
other...

steve

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Steve Christensen			Never hit a man with glasses.
  stevec@apple.com			Hit him with a baseball bat.

lsr@Apple.COM (Larry Rosenstein) (04/18/91)

In article <1991Apr17.134645.2516@ux1.cso.uiuc.edu> dorner@pequod.cso.uiuc.edu (Steve Dorner) writes:
>
>Since I almost always end up using the resource forks of files I create
>(window position info, for one), I ALWAYS use CreateResFile,  not Create,
>to create files.  I then don't have to do any special monkeying around to

There's a serious problem with this approach, which is described in Tech
Note 101.  Briefly, CreateResFile first checks to see if the resource file
exists, by calling OpenResFile, so it can return an error.  Unfortunately,
OpenResFile will use the Poor Man's Search Path.  So if you try to create a
resource file with the same name as one in the System Folder, it will return
an error.

The solution is to always call Create (which doesn't use the PMSP) to create
the file, and then CreateResFile to create the resource fork. (BTW, an empty
resource fork only contains a hundred bytes or so, but it can occupy quite a
bit of space depending on the allocation block size of your disk.)

-- 
Larry Rosenstein, Apple Computer, Inc.

lsr@apple.com
(or AppleLink: Rosenstein1)