1 /*
2 * libhfs - library for reading and writing Macintosh HFS volumes
3 * Copyright (C) 1996-1998 Robert Leslie
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 *
20 * $Id: file.c,v 1.9 1998/11/02 22:08:59 rob Exp $
21 */
22
23 #include "config.h"
24 #include "libhfs.h"
25 #include "file.h"
26 #include "btree.h"
27 #include "record.h"
28 #include "volume.h"
29
30 /*
31 * NAME: file->init()
32 * DESCRIPTION: initialize file structure
33 */
f_init(hfsfile * file,hfsvol * vol,long cnid,const char * name)34 void f_init(hfsfile *file, hfsvol *vol, long cnid, const char *name)
35 {
36 int i;
37
38 file->vol = vol;
39 file->parid = 0;
40
41 strcpy(file->name, name);
42
43 file->cat.cdrType = cdrFilRec;
44 file->cat.cdrResrv2 = 0;
45
46 file->cat.u.fil.filFlags = 0;
47 file->cat.u.fil.filTyp = 0;
48
49 file->cat.u.fil.filUsrWds.fdType = 0;
50 file->cat.u.fil.filUsrWds.fdCreator = 0;
51 file->cat.u.fil.filUsrWds.fdFlags = 0;
52 file->cat.u.fil.filUsrWds.fdLocation.v = 0;
53 file->cat.u.fil.filUsrWds.fdLocation.h = 0;
54 file->cat.u.fil.filUsrWds.fdFldr = 0;
55
56 file->cat.u.fil.filFlNum = cnid;
57 file->cat.u.fil.filStBlk = 0;
58 file->cat.u.fil.filLgLen = 0;
59 file->cat.u.fil.filPyLen = 0;
60 file->cat.u.fil.filRStBlk = 0;
61 file->cat.u.fil.filRLgLen = 0;
62 file->cat.u.fil.filRPyLen = 0;
63 file->cat.u.fil.filCrDat = 0;
64 file->cat.u.fil.filMdDat = 0;
65 file->cat.u.fil.filBkDat = 0;
66
67 file->cat.u.fil.filFndrInfo.fdIconID = 0;
68 for (i = 0; i < 4; ++i)
69 file->cat.u.fil.filFndrInfo.fdUnused[i] = 0;
70 file->cat.u.fil.filFndrInfo.fdComment = 0;
71 file->cat.u.fil.filFndrInfo.fdPutAway = 0;
72
73 file->cat.u.fil.filClpSize = 0;
74
75 for (i = 0; i < 3; ++i)
76 {
77 file->cat.u.fil.filExtRec[i].xdrStABN = 0;
78 file->cat.u.fil.filExtRec[i].xdrNumABlks = 0;
79
80 file->cat.u.fil.filRExtRec[i].xdrStABN = 0;
81 file->cat.u.fil.filRExtRec[i].xdrNumABlks = 0;
82 }
83
84 file->cat.u.fil.filResrv = 0;
85
86 f_selectfork(file, fkData);
87
88 file->flags = 0;
89
90 file->prev = NULL;
91 file->next = NULL;
92 }
93
94 /*
95 * NAME: file->selectfork()
96 * DESCRIPTION: choose a fork for file operations
97 */
f_selectfork(hfsfile * file,int fork)98 void f_selectfork(hfsfile *file, int fork)
99 {
100 file->fork = fork;
101
102 memcpy(&file->ext, fork == fkData ?
103 &file->cat.u.fil.filExtRec : &file->cat.u.fil.filRExtRec,
104 sizeof(ExtDataRec));
105
106 file->fabn = 0;
107 file->pos = 0;
108 }
109
110 /*
111 * NAME: file->getptrs()
112 * DESCRIPTION: make pointers to the current fork's lengths and extents
113 */
f_getptrs(hfsfile * file,ExtDataRec ** extrec,unsigned long ** lglen,unsigned long ** pylen)114 void f_getptrs(hfsfile *file, ExtDataRec **extrec,
115 unsigned long **lglen, unsigned long **pylen)
116 {
117 if (file->fork == fkData)
118 {
119 if (extrec)
120 *extrec = &file->cat.u.fil.filExtRec;
121 if (lglen)
122 *lglen = &file->cat.u.fil.filLgLen;
123 if (pylen)
124 *pylen = &file->cat.u.fil.filPyLen;
125 }
126 else
127 {
128 if (extrec)
129 *extrec = &file->cat.u.fil.filRExtRec;
130 if (lglen)
131 *lglen = &file->cat.u.fil.filRLgLen;
132 if (pylen)
133 *pylen = &file->cat.u.fil.filRPyLen;
134 }
135 }
136
137 /*
138 * NAME: file->doblock()
139 * DESCRIPTION: read or write a numbered block from a file
140 */
f_doblock(hfsfile * file,unsigned long num,block * bp,int (* func)(hfsvol *,unsigned int,unsigned int,block *))141 int f_doblock(hfsfile *file, unsigned long num, block *bp,
142 int (*func)(hfsvol *, unsigned int, unsigned int, block *))
143 {
144 unsigned int abnum;
145 unsigned int blnum;
146 unsigned int fabn;
147 int i;
148
149 abnum = num / file->vol->lpa;
150 blnum = num % file->vol->lpa;
151
152 /* locate the appropriate extent record */
153
154 fabn = file->fabn;
155
156 if (abnum < fabn)
157 {
158 ExtDataRec *extrec;
159
160 f_getptrs(file, &extrec, NULL, NULL);
161
162 fabn = file->fabn = 0;
163 memcpy(&file->ext, extrec, sizeof(ExtDataRec));
164 }
165 else
166 abnum -= fabn;
167
168 while (1)
169 {
170 unsigned int n;
171
172 for (i = 0; i < 3; ++i)
173 {
174 n = file->ext[i].xdrNumABlks;
175
176 if (abnum < n)
177 return func(file->vol, file->ext[i].xdrStABN + abnum, blnum, bp);
178
179 fabn += n;
180 abnum -= n;
181 }
182
183 if (v_extsearch(file, fabn, &file->ext, NULL) <= 0)
184 goto fail;
185
186 file->fabn = fabn;
187 }
188
189 fail:
190 return -1;
191 }
192