1 |
dpavlin |
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., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
|
|
* |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
# ifdef HAVE_CONFIG_H |
22 |
|
|
# include "config.h" |
23 |
|
|
# endif |
24 |
|
|
|
25 |
|
|
# include <string.h> |
26 |
|
|
# include <errno.h> |
27 |
|
|
|
28 |
|
|
# include "libhfs.h" |
29 |
|
|
# include "file.h" |
30 |
|
|
# include "btree.h" |
31 |
|
|
# include "record.h" |
32 |
|
|
# include "volume.h" |
33 |
|
|
|
34 |
|
|
/* |
35 |
|
|
* NAME: file->init() |
36 |
|
|
* DESCRIPTION: initialize file structure |
37 |
|
|
*/ |
38 |
|
|
void f_init(hfsfile *file, hfsvol *vol, long cnid, const char *name) |
39 |
|
|
{ |
40 |
|
|
int i; |
41 |
|
|
|
42 |
|
|
file->vol = vol; |
43 |
|
|
file->parid = 0; |
44 |
|
|
|
45 |
|
|
strcpy(file->name, name); |
46 |
|
|
|
47 |
|
|
file->cat.cdrType = cdrFilRec; |
48 |
|
|
file->cat.cdrResrv2 = 0; |
49 |
|
|
|
50 |
|
|
file->cat.u.fil.filFlags = 0; |
51 |
|
|
file->cat.u.fil.filTyp = 0; |
52 |
|
|
|
53 |
|
|
file->cat.u.fil.filUsrWds.fdType = 0; |
54 |
|
|
file->cat.u.fil.filUsrWds.fdCreator = 0; |
55 |
|
|
file->cat.u.fil.filUsrWds.fdFlags = 0; |
56 |
|
|
file->cat.u.fil.filUsrWds.fdLocation.v = 0; |
57 |
|
|
file->cat.u.fil.filUsrWds.fdLocation.h = 0; |
58 |
|
|
file->cat.u.fil.filUsrWds.fdFldr = 0; |
59 |
|
|
|
60 |
|
|
file->cat.u.fil.filFlNum = cnid; |
61 |
|
|
file->cat.u.fil.filStBlk = 0; |
62 |
|
|
file->cat.u.fil.filLgLen = 0; |
63 |
|
|
file->cat.u.fil.filPyLen = 0; |
64 |
|
|
file->cat.u.fil.filRStBlk = 0; |
65 |
|
|
file->cat.u.fil.filRLgLen = 0; |
66 |
|
|
file->cat.u.fil.filRPyLen = 0; |
67 |
|
|
file->cat.u.fil.filCrDat = 0; |
68 |
|
|
file->cat.u.fil.filMdDat = 0; |
69 |
|
|
file->cat.u.fil.filBkDat = 0; |
70 |
|
|
|
71 |
|
|
file->cat.u.fil.filFndrInfo.fdIconID = 0; |
72 |
|
|
for (i = 0; i < 4; ++i) |
73 |
|
|
file->cat.u.fil.filFndrInfo.fdUnused[i] = 0; |
74 |
|
|
file->cat.u.fil.filFndrInfo.fdComment = 0; |
75 |
|
|
file->cat.u.fil.filFndrInfo.fdPutAway = 0; |
76 |
|
|
|
77 |
|
|
file->cat.u.fil.filClpSize = 0; |
78 |
|
|
|
79 |
|
|
for (i = 0; i < 3; ++i) |
80 |
|
|
{ |
81 |
|
|
file->cat.u.fil.filExtRec[i].xdrStABN = 0; |
82 |
|
|
file->cat.u.fil.filExtRec[i].xdrNumABlks = 0; |
83 |
|
|
|
84 |
|
|
file->cat.u.fil.filRExtRec[i].xdrStABN = 0; |
85 |
|
|
file->cat.u.fil.filRExtRec[i].xdrNumABlks = 0; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
file->cat.u.fil.filResrv = 0; |
89 |
|
|
|
90 |
|
|
f_selectfork(file, fkData); |
91 |
|
|
|
92 |
|
|
file->flags = 0; |
93 |
|
|
|
94 |
|
|
file->prev = 0; |
95 |
|
|
file->next = 0; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
/* |
99 |
|
|
* NAME: file->selectfork() |
100 |
|
|
* DESCRIPTION: choose a fork for file operations |
101 |
|
|
*/ |
102 |
|
|
void f_selectfork(hfsfile *file, int fork) |
103 |
|
|
{ |
104 |
|
|
file->fork = fork; |
105 |
|
|
|
106 |
|
|
memcpy(&file->ext, fork == fkData ? |
107 |
|
|
&file->cat.u.fil.filExtRec : &file->cat.u.fil.filRExtRec, |
108 |
|
|
sizeof(ExtDataRec)); |
109 |
|
|
|
110 |
|
|
file->fabn = 0; |
111 |
|
|
file->pos = 0; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/* |
115 |
|
|
* NAME: file->getptrs() |
116 |
|
|
* DESCRIPTION: make pointers to the current fork's lengths and extents |
117 |
|
|
*/ |
118 |
|
|
void f_getptrs(hfsfile *file, ExtDataRec **extrec, |
119 |
|
|
unsigned long **lglen, unsigned long **pylen) |
120 |
|
|
{ |
121 |
|
|
if (file->fork == fkData) |
122 |
|
|
{ |
123 |
|
|
if (extrec) |
124 |
|
|
*extrec = &file->cat.u.fil.filExtRec; |
125 |
|
|
if (lglen) |
126 |
|
|
*lglen = &file->cat.u.fil.filLgLen; |
127 |
|
|
if (pylen) |
128 |
|
|
*pylen = &file->cat.u.fil.filPyLen; |
129 |
|
|
} |
130 |
|
|
else |
131 |
|
|
{ |
132 |
|
|
if (extrec) |
133 |
|
|
*extrec = &file->cat.u.fil.filRExtRec; |
134 |
|
|
if (lglen) |
135 |
|
|
*lglen = &file->cat.u.fil.filRLgLen; |
136 |
|
|
if (pylen) |
137 |
|
|
*pylen = &file->cat.u.fil.filRPyLen; |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
/* |
142 |
|
|
* NAME: file->doblock() |
143 |
|
|
* DESCRIPTION: read or write a numbered block from a file |
144 |
|
|
*/ |
145 |
|
|
int f_doblock(hfsfile *file, unsigned long num, block *bp, |
146 |
|
|
int (*func)(hfsvol *, unsigned int, unsigned int, block *)) |
147 |
|
|
{ |
148 |
|
|
unsigned int abnum; |
149 |
|
|
unsigned int blnum; |
150 |
|
|
unsigned int fabn; |
151 |
|
|
int i; |
152 |
|
|
|
153 |
|
|
abnum = num / file->vol->lpa; |
154 |
|
|
blnum = num % file->vol->lpa; |
155 |
|
|
|
156 |
|
|
/* locate the appropriate extent record */ |
157 |
|
|
|
158 |
|
|
fabn = file->fabn; |
159 |
|
|
|
160 |
|
|
if (abnum < fabn) |
161 |
|
|
{ |
162 |
|
|
ExtDataRec *extrec; |
163 |
|
|
|
164 |
|
|
f_getptrs(file, &extrec, 0, 0); |
165 |
|
|
|
166 |
|
|
fabn = file->fabn = 0; |
167 |
|
|
memcpy(&file->ext, extrec, sizeof(ExtDataRec)); |
168 |
|
|
} |
169 |
|
|
else |
170 |
|
|
abnum -= fabn; |
171 |
|
|
|
172 |
|
|
while (1) |
173 |
|
|
{ |
174 |
|
|
unsigned int n; |
175 |
|
|
|
176 |
|
|
for (i = 0; i < 3; ++i) |
177 |
|
|
{ |
178 |
|
|
n = file->ext[i].xdrNumABlks; |
179 |
|
|
|
180 |
|
|
if (abnum < n) |
181 |
|
|
return func(file->vol, file->ext[i].xdrStABN + abnum, blnum, bp); |
182 |
|
|
|
183 |
|
|
fabn += n; |
184 |
|
|
abnum -= n; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
if (v_extsearch(file, fabn, &file->ext, 0) <= 0) |
188 |
|
|
goto fail; |
189 |
|
|
|
190 |
|
|
file->fabn = fabn; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
fail: |
194 |
|
|
return -1; |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
/* |
198 |
|
|
* NAME: file->addextent() |
199 |
|
|
* DESCRIPTION: add an extent to a file |
200 |
|
|
*/ |
201 |
|
|
int f_addextent(hfsfile *file, ExtDescriptor *blocks) |
202 |
|
|
{ |
203 |
|
|
hfsvol *vol = file->vol; |
204 |
|
|
ExtDataRec *extrec; |
205 |
|
|
unsigned long *pylen; |
206 |
|
|
unsigned int start, end; |
207 |
|
|
node n; |
208 |
|
|
int i; |
209 |
|
|
|
210 |
|
|
f_getptrs(file, &extrec, 0, &pylen); |
211 |
|
|
|
212 |
|
|
start = file->fabn; |
213 |
|
|
end = *pylen / vol->mdb.drAlBlkSiz; |
214 |
|
|
|
215 |
|
|
n.nnum = 0; |
216 |
|
|
i = -1; |
217 |
|
|
|
218 |
|
|
while (start < end) |
219 |
|
|
{ |
220 |
|
|
for (i = 0; i < 3; ++i) |
221 |
|
|
{ |
222 |
|
|
unsigned int num; |
223 |
|
|
|
224 |
|
|
num = file->ext[i].xdrNumABlks; |
225 |
|
|
start += num; |
226 |
|
|
|
227 |
|
|
if (start == end) |
228 |
|
|
break; |
229 |
|
|
else if (start > end) |
230 |
|
|
ERROR(EIO, "file extents exceed file physical length"); |
231 |
|
|
else if (num == 0) |
232 |
|
|
ERROR(EIO, "empty file extent"); |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
if (start == end) |
236 |
|
|
break; |
237 |
|
|
|
238 |
|
|
if (v_extsearch(file, start, &file->ext, &n) <= 0) |
239 |
|
|
goto fail; |
240 |
|
|
|
241 |
|
|
file->fabn = start; |
242 |
|
|
} |
243 |
|
|
|
244 |
|
|
if (i >= 0 && |
245 |
|
|
file->ext[i].xdrStABN + file->ext[i].xdrNumABlks == blocks->xdrStABN) |
246 |
|
|
file->ext[i].xdrNumABlks += blocks->xdrNumABlks; |
247 |
|
|
else |
248 |
|
|
{ |
249 |
|
|
/* create a new extent descriptor */ |
250 |
|
|
|
251 |
|
|
if (++i < 3) |
252 |
|
|
file->ext[i] = *blocks; |
253 |
|
|
else |
254 |
|
|
{ |
255 |
|
|
ExtKeyRec key; |
256 |
|
|
byte record[HFS_MAX_EXTRECLEN]; |
257 |
|
|
unsigned int reclen; |
258 |
|
|
|
259 |
|
|
/* record is full; create a new one */ |
260 |
|
|
|
261 |
|
|
file->ext[0] = *blocks; |
262 |
|
|
|
263 |
|
|
for (i = 1; i < 3; ++i) |
264 |
|
|
{ |
265 |
|
|
file->ext[i].xdrStABN = 0; |
266 |
|
|
file->ext[i].xdrNumABlks = 0; |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
file->fabn = start; |
270 |
|
|
|
271 |
|
|
r_makeextkey(&key, file->fork, file->cat.u.fil.filFlNum, end); |
272 |
|
|
r_packextrec(&key, &file->ext, record, &reclen); |
273 |
|
|
|
274 |
|
|
if (bt_insert(&vol->ext, record, reclen) == -1) |
275 |
|
|
goto fail; |
276 |
|
|
|
277 |
|
|
i = -1; |
278 |
|
|
} |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
if (i >= 0) |
282 |
|
|
{ |
283 |
|
|
/* store the modified extent record */ |
284 |
|
|
|
285 |
|
|
if (file->fabn) |
286 |
|
|
{ |
287 |
|
|
if ((n.nnum == 0 && |
288 |
|
|
v_extsearch(file, file->fabn, 0, &n) <= 0) || |
289 |
|
|
v_putextrec(&file->ext, &n) == -1) |
290 |
|
|
goto fail; |
291 |
|
|
} |
292 |
|
|
else |
293 |
|
|
memcpy(extrec, &file->ext, sizeof(ExtDataRec)); |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
*pylen += blocks->xdrNumABlks * vol->mdb.drAlBlkSiz; |
297 |
|
|
|
298 |
|
|
file->flags |= HFS_FILE_UPDATE_CATREC; |
299 |
|
|
|
300 |
|
|
return 0; |
301 |
|
|
|
302 |
|
|
fail: |
303 |
|
|
return -1; |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
/* |
307 |
|
|
* NAME: file->alloc() |
308 |
|
|
* DESCRIPTION: reserve allocation blocks for a file |
309 |
|
|
*/ |
310 |
|
|
long f_alloc(hfsfile *file) |
311 |
|
|
{ |
312 |
|
|
hfsvol *vol = file->vol; |
313 |
|
|
unsigned long clumpsz; |
314 |
|
|
ExtDescriptor blocks; |
315 |
|
|
|
316 |
|
|
clumpsz = file->cat.u.fil.filClpSize; |
317 |
|
|
if (clumpsz == 0) |
318 |
|
|
{ |
319 |
|
|
if (file == &vol->ext.f) |
320 |
|
|
clumpsz = vol->mdb.drXTClpSiz; |
321 |
|
|
else if (file == &vol->cat.f) |
322 |
|
|
clumpsz = vol->mdb.drCTClpSiz; |
323 |
|
|
else |
324 |
|
|
clumpsz = vol->mdb.drClpSiz; |
325 |
|
|
} |
326 |
|
|
|
327 |
|
|
blocks.xdrNumABlks = clumpsz / vol->mdb.drAlBlkSiz; |
328 |
|
|
|
329 |
|
|
if (v_allocblocks(vol, &blocks) == -1) |
330 |
|
|
goto fail; |
331 |
|
|
|
332 |
|
|
if (f_addextent(file, &blocks) == -1) |
333 |
|
|
{ |
334 |
|
|
v_freeblocks(vol, &blocks); |
335 |
|
|
goto fail; |
336 |
|
|
} |
337 |
|
|
|
338 |
|
|
return blocks.xdrNumABlks; |
339 |
|
|
|
340 |
|
|
fail: |
341 |
|
|
return -1; |
342 |
|
|
} |
343 |
|
|
|
344 |
|
|
/* |
345 |
|
|
* NAME: file->trunc() |
346 |
|
|
* DESCRIPTION: release allocation blocks unneeded by a file |
347 |
|
|
*/ |
348 |
|
|
int f_trunc(hfsfile *file) |
349 |
|
|
{ |
350 |
|
|
hfsvol *vol = file->vol; |
351 |
|
|
ExtDataRec *extrec; |
352 |
|
|
unsigned long *lglen, *pylen, alblksz, newpylen; |
353 |
|
|
unsigned int dlen, start, end; |
354 |
|
|
node n; |
355 |
|
|
int i; |
356 |
|
|
|
357 |
|
|
if (vol->flags & HFS_VOL_READONLY) |
358 |
|
|
goto done; |
359 |
|
|
|
360 |
|
|
f_getptrs(file, &extrec, &lglen, &pylen); |
361 |
|
|
|
362 |
|
|
alblksz = vol->mdb.drAlBlkSiz; |
363 |
|
|
newpylen = (*lglen / alblksz + (*lglen % alblksz != 0)) * alblksz; |
364 |
|
|
|
365 |
|
|
if (newpylen > *pylen) |
366 |
|
|
ERROR(EIO, "file size exceeds physical length"); |
367 |
|
|
else if (newpylen == *pylen) |
368 |
|
|
goto done; |
369 |
|
|
|
370 |
|
|
dlen = (*pylen - newpylen) / alblksz; |
371 |
|
|
|
372 |
|
|
start = file->fabn; |
373 |
|
|
end = newpylen / alblksz; |
374 |
|
|
|
375 |
|
|
if (start >= end) |
376 |
|
|
{ |
377 |
|
|
start = file->fabn = 0; |
378 |
|
|
memcpy(&file->ext, extrec, sizeof(ExtDataRec)); |
379 |
|
|
} |
380 |
|
|
|
381 |
|
|
n.nnum = 0; |
382 |
|
|
i = -1; |
383 |
|
|
|
384 |
|
|
while (start < end) |
385 |
|
|
{ |
386 |
|
|
for (i = 0; i < 3; ++i) |
387 |
|
|
{ |
388 |
|
|
unsigned int num; |
389 |
|
|
|
390 |
|
|
num = file->ext[i].xdrNumABlks; |
391 |
|
|
start += num; |
392 |
|
|
|
393 |
|
|
if (start >= end) |
394 |
|
|
break; |
395 |
|
|
else if (num == 0) |
396 |
|
|
ERROR(EIO, "empty file extent"); |
397 |
|
|
} |
398 |
|
|
|
399 |
|
|
if (start >= end) |
400 |
|
|
break; |
401 |
|
|
|
402 |
|
|
if (v_extsearch(file, start, &file->ext, &n) <= 0) |
403 |
|
|
goto fail; |
404 |
|
|
|
405 |
|
|
file->fabn = start; |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
if (start > end) |
409 |
|
|
{ |
410 |
|
|
ExtDescriptor blocks; |
411 |
|
|
|
412 |
|
|
file->ext[i].xdrNumABlks -= start - end; |
413 |
|
|
dlen -= start - end; |
414 |
|
|
|
415 |
|
|
blocks.xdrStABN = file->ext[i].xdrStABN + file->ext[i].xdrNumABlks; |
416 |
|
|
blocks.xdrNumABlks = start - end; |
417 |
|
|
|
418 |
|
|
if (v_freeblocks(vol, &blocks) == -1) |
419 |
|
|
goto fail; |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
*pylen = newpylen; |
423 |
|
|
|
424 |
|
|
file->flags |= HFS_FILE_UPDATE_CATREC; |
425 |
|
|
|
426 |
|
|
do |
427 |
|
|
{ |
428 |
|
|
while (dlen && ++i < 3) |
429 |
|
|
{ |
430 |
|
|
unsigned int num; |
431 |
|
|
|
432 |
|
|
num = file->ext[i].xdrNumABlks; |
433 |
|
|
start += num; |
434 |
|
|
|
435 |
|
|
if (num == 0) |
436 |
|
|
ERROR(EIO, "empty file extent"); |
437 |
|
|
else if (num > dlen) |
438 |
|
|
ERROR(EIO, "file extents exceed physical size"); |
439 |
|
|
|
440 |
|
|
dlen -= num; |
441 |
|
|
|
442 |
|
|
if (v_freeblocks(vol, &file->ext[i]) == -1) |
443 |
|
|
goto fail; |
444 |
|
|
|
445 |
|
|
file->ext[i].xdrStABN = 0; |
446 |
|
|
file->ext[i].xdrNumABlks = 0; |
447 |
|
|
} |
448 |
|
|
|
449 |
|
|
if (file->fabn) |
450 |
|
|
{ |
451 |
|
|
if (n.nnum == 0 && |
452 |
|
|
v_extsearch(file, file->fabn, 0, &n) <= 0) |
453 |
|
|
goto fail; |
454 |
|
|
|
455 |
|
|
if (file->ext[0].xdrNumABlks) |
456 |
|
|
{ |
457 |
|
|
if (v_putextrec(&file->ext, &n) == -1) |
458 |
|
|
goto fail; |
459 |
|
|
} |
460 |
|
|
else |
461 |
|
|
{ |
462 |
|
|
if (bt_delete(&vol->ext, HFS_NODEREC(n, n.rnum)) == -1) |
463 |
|
|
goto fail; |
464 |
|
|
|
465 |
|
|
n.nnum = 0; |
466 |
|
|
} |
467 |
|
|
} |
468 |
|
|
else |
469 |
|
|
memcpy(extrec, &file->ext, sizeof(ExtDataRec)); |
470 |
|
|
|
471 |
|
|
if (dlen) |
472 |
|
|
{ |
473 |
|
|
if (v_extsearch(file, start, &file->ext, &n) <= 0) |
474 |
|
|
goto fail; |
475 |
|
|
|
476 |
|
|
file->fabn = start; |
477 |
|
|
i = -1; |
478 |
|
|
} |
479 |
|
|
} |
480 |
|
|
while (dlen); |
481 |
|
|
|
482 |
|
|
done: |
483 |
|
|
return 0; |
484 |
|
|
|
485 |
|
|
fail: |
486 |
|
|
return -1; |
487 |
|
|
} |
488 |
|
|
|
489 |
|
|
/* |
490 |
|
|
* NAME: file->flush() |
491 |
|
|
* DESCRIPTION: flush all pending changes to an open file |
492 |
|
|
*/ |
493 |
|
|
int f_flush(hfsfile *file) |
494 |
|
|
{ |
495 |
|
|
hfsvol *vol = file->vol; |
496 |
|
|
|
497 |
|
|
if (vol->flags & HFS_VOL_READONLY) |
498 |
|
|
goto done; |
499 |
|
|
|
500 |
|
|
if (file->flags & HFS_FILE_UPDATE_CATREC) |
501 |
|
|
{ |
502 |
|
|
node n; |
503 |
|
|
|
504 |
|
|
file->cat.u.fil.filStBlk = file->cat.u.fil.filExtRec[0].xdrStABN; |
505 |
|
|
file->cat.u.fil.filRStBlk = file->cat.u.fil.filRExtRec[0].xdrStABN; |
506 |
|
|
|
507 |
|
|
if (v_catsearch(vol, file->parid, file->name, 0, 0, &n) <= 0 || |
508 |
|
|
v_putcatrec(&file->cat, &n) == -1) |
509 |
|
|
goto fail; |
510 |
|
|
|
511 |
|
|
file->flags &= ~HFS_FILE_UPDATE_CATREC; |
512 |
|
|
} |
513 |
|
|
|
514 |
|
|
done: |
515 |
|
|
return 0; |
516 |
|
|
|
517 |
|
|
fail: |
518 |
|
|
return -1; |
519 |
|
|
} |