Implement new v3_memalloc romvec interface for allocating aligned memory.
[openbios.git] / fs / hfs / hfs.c
blob0c5fefb479c6eb2541d3f9278e93ac710156d7ad
1 /*
2 * libhfs - library for reading and writing Macintosh HFS volumes
3 * Copyright (C) 1996-1998 Robert Leslie
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.
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.
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.
20 * $Id: hfs.c,v 1.15 1998/11/02 22:09:00 rob Exp $
23 #include "config.h"
24 #include "libhfs.h"
25 #include "data.h"
26 #include "block.h"
27 #include "medium.h"
28 #include "file.h"
29 #include "btree.h"
30 #include "node.h"
31 #include "record.h"
32 #include "volume.h"
34 const char *hfs_error = "no error"; /* static error string */
36 hfsvol *hfs_mounts; /* linked list of mounted volumes */
38 static
39 hfsvol *curvol; /* current volume */
43 * NAME: getvol()
44 * DESCRIPTION: validate a volume reference
46 static
47 int getvol(hfsvol **vol)
49 if (*vol == NULL)
51 if (curvol == NULL)
52 ERROR(EINVAL, "no volume is current");
54 *vol = curvol;
57 return 0;
59 fail:
60 return -1;
63 /* High-Level Volume Routines ============================================== */
66 * NAME: hfs->mount()
67 * DESCRIPTION: open an HFS volume; return volume descriptor or 0 (error)
69 hfsvol *hfs_mount( int os_fd, int pnum)
71 hfsvol *vol, *check;
72 int mode = HFS_MODE_RDONLY;
74 /* see if the volume is already mounted */
75 for (check = hfs_mounts; check; check = check->next)
77 if (check->pnum == pnum && v_same(check, os_fd) == 1)
79 vol = check;
80 goto done;
84 vol = ALLOC(hfsvol, 1);
85 if (vol == NULL)
86 ERROR(ENOMEM, NULL);
88 v_init(vol, mode);
90 vol->flags |= HFS_VOL_READONLY;
91 if( v_open(vol, os_fd) == -1 )
92 goto fail;
94 /* mount the volume */
96 if (v_geometry(vol, pnum) == -1 ||
97 v_mount(vol) == -1)
98 goto fail;
100 /* add to linked list of volumes */
102 vol->prev = NULL;
103 vol->next = hfs_mounts;
105 if (hfs_mounts)
106 hfs_mounts->prev = vol;
108 hfs_mounts = vol;
110 done:
111 ++vol->refs;
112 curvol = vol;
114 return vol;
116 fail:
117 if (vol)
119 v_close(vol);
120 FREE(vol);
123 return NULL;
128 * NAME: hfs->umount()
129 * DESCRIPTION: close an HFS volume
131 int hfs_umount(hfsvol *vol)
133 int result = 0;
135 if (getvol(&vol) == -1)
136 goto fail;
138 if (--vol->refs)
140 goto done;
143 /* close all open files and directories */
145 while (vol->files)
147 if (hfs_close(vol->files) == -1)
148 result = -1;
151 while (vol->dirs)
153 if (hfs_closedir(vol->dirs) == -1)
154 result = -1;
157 /* close medium */
159 if (v_close(vol) == -1)
160 result = -1;
162 /* remove from linked list of volumes */
164 if (vol->prev)
165 vol->prev->next = vol->next;
166 if (vol->next)
167 vol->next->prev = vol->prev;
169 if (vol == hfs_mounts)
170 hfs_mounts = vol->next;
171 if (vol == curvol)
172 curvol = NULL;
174 FREE(vol);
176 done:
177 return result;
179 fail:
180 return -1;
184 * NAME: hfs->umountall()
185 * DESCRIPTION: unmount all mounted volumes
187 void hfs_umountall(void)
189 while (hfs_mounts)
190 hfs_umount(hfs_mounts);
194 * NAME: hfs->getvol()
195 * DESCRIPTION: return a pointer to a mounted volume
197 hfsvol *hfs_getvol(const char *name)
199 hfsvol *vol;
201 if (name == NULL)
202 return curvol;
204 for (vol = hfs_mounts; vol; vol = vol->next)
206 if (d_relstring(name, vol->mdb.drVN) == 0)
207 return vol;
210 return NULL;
214 * NAME: hfs->setvol()
215 * DESCRIPTION: change the current volume
217 void hfs_setvol(hfsvol *vol)
219 curvol = vol;
223 * NAME: hfs->vstat()
224 * DESCRIPTION: return volume statistics
226 int hfs_vstat(hfsvol *vol, hfsvolent *ent)
228 if (getvol(&vol) == -1)
229 goto fail;
231 strcpy(ent->name, vol->mdb.drVN);
233 ent->flags = (vol->flags & HFS_VOL_READONLY) ? HFS_ISLOCKED : 0;
235 ent->totbytes = vol->mdb.drNmAlBlks * vol->mdb.drAlBlkSiz;
236 ent->freebytes = vol->mdb.drFreeBks * vol->mdb.drAlBlkSiz;
238 ent->alblocksz = vol->mdb.drAlBlkSiz;
239 ent->clumpsz = vol->mdb.drClpSiz;
241 ent->numfiles = vol->mdb.drFilCnt;
242 ent->numdirs = vol->mdb.drDirCnt;
244 ent->crdate = d_ltime(vol->mdb.drCrDate);
245 ent->mddate = d_ltime(vol->mdb.drLsMod);
246 ent->bkdate = d_ltime(vol->mdb.drVolBkUp);
248 ent->blessed = vol->mdb.drFndrInfo[0];
250 return 0;
252 fail:
253 return -1;
257 /* High-Level Directory Routines =========================================== */
260 * NAME: hfs->chdir()
261 * DESCRIPTION: change current HFS directory
263 int hfs_chdir(hfsvol *vol, const char *path)
265 CatDataRec data;
267 if (getvol(&vol) == -1 ||
268 v_resolve(&vol, path, &data, NULL, NULL, NULL) <= 0)
269 goto fail;
271 if (data.cdrType != cdrDirRec)
272 ERROR(ENOTDIR, NULL);
274 vol->cwd = data.u.dir.dirDirID;
276 return 0;
278 fail:
279 return -1;
283 * NAME: hfs->getcwd()
284 * DESCRIPTION: return the current working directory ID
286 unsigned long hfs_getcwd(hfsvol *vol)
288 if (getvol(&vol) == -1)
289 return 0;
291 return vol->cwd;
295 * NAME: hfs->setcwd()
296 * DESCRIPTION: set the current working directory ID
298 int hfs_setcwd(hfsvol *vol, unsigned long id)
300 if (getvol(&vol) == -1)
301 goto fail;
303 if (id == vol->cwd)
304 goto done;
306 /* make sure the directory exists */
308 if (v_getdthread(vol, id, NULL, NULL) <= 0)
309 goto fail;
311 vol->cwd = id;
313 done:
314 return 0;
316 fail:
317 return -1;
321 * NAME: hfs->dirinfo()
322 * DESCRIPTION: given a directory ID, return its (name and) parent ID
324 int hfs_dirinfo(hfsvol *vol, unsigned long *id, char *name)
326 CatDataRec thread;
328 if (getvol(&vol) == -1 ||
329 v_getdthread(vol, *id, &thread, NULL) <= 0)
330 goto fail;
332 *id = thread.u.dthd.thdParID;
334 if (name)
335 strcpy(name, thread.u.dthd.thdCName);
337 return 0;
339 fail:
340 return -1;
344 * NAME: hfs->opendir()
345 * DESCRIPTION: prepare to read the contents of a directory
347 hfsdir *hfs_opendir(hfsvol *vol, const char *path)
349 hfsdir *dir = NULL;
350 CatKeyRec key;
351 CatDataRec data;
352 byte pkey[HFS_CATKEYLEN];
354 if (getvol(&vol) == -1)
355 goto fail;
357 dir = ALLOC(hfsdir, 1);
358 if (dir == NULL)
359 ERROR(ENOMEM, NULL);
361 dir->vol = vol;
363 if (*path == 0)
365 /* meta-directory containing root dirs from all mounted volumes */
367 dir->dirid = 0;
368 dir->vptr = hfs_mounts;
370 else
372 if (v_resolve(&vol, path, &data, NULL, NULL, NULL) <= 0)
373 goto fail;
375 if (data.cdrType != cdrDirRec)
376 ERROR(ENOTDIR, NULL);
378 dir->dirid = data.u.dir.dirDirID;
379 dir->vptr = NULL;
381 r_makecatkey(&key, dir->dirid, "");
382 r_packcatkey(&key, pkey, NULL);
384 if (bt_search(&vol->cat, pkey, &dir->n) <= 0)
385 goto fail;
388 dir->prev = NULL;
389 dir->next = vol->dirs;
391 if (vol->dirs)
392 vol->dirs->prev = dir;
394 vol->dirs = dir;
396 return dir;
398 fail:
399 FREE(dir);
400 return NULL;
404 * NAME: hfs->readdir()
405 * DESCRIPTION: return the next entry in the directory
407 int hfs_readdir(hfsdir *dir, hfsdirent *ent)
409 CatKeyRec key;
410 CatDataRec data;
411 const byte *ptr;
413 if (dir->dirid == 0)
415 hfsvol *vol;
416 char cname[HFS_MAX_FLEN + 1];
418 for (vol = hfs_mounts; vol; vol = vol->next)
420 if (vol == dir->vptr)
421 break;
424 if (vol == NULL)
425 ERROR(ENOENT, "no more entries");
427 if (v_getdthread(vol, HFS_CNID_ROOTDIR, &data, NULL) <= 0 ||
428 v_catsearch(vol, HFS_CNID_ROOTPAR, data.u.dthd.thdCName,
429 &data, cname, NULL) <= 0)
430 goto fail;
432 r_unpackdirent(HFS_CNID_ROOTPAR, cname, &data, ent);
434 dir->vptr = vol->next;
436 goto done;
439 if (dir->n.rnum == -1)
440 ERROR(ENOENT, "no more entries");
442 while (1)
444 ++dir->n.rnum;
446 while (dir->n.rnum >= dir->n.nd.ndNRecs)
448 if (dir->n.nd.ndFLink == 0)
450 dir->n.rnum = -1;
451 ERROR(ENOENT, "no more entries");
454 if (bt_getnode(&dir->n, dir->n.bt, dir->n.nd.ndFLink) == -1)
456 dir->n.rnum = -1;
457 goto fail;
460 dir->n.rnum = 0;
463 ptr = HFS_NODEREC(dir->n, dir->n.rnum);
465 r_unpackcatkey(ptr, &key);
467 if (key.ckrParID != dir->dirid)
469 dir->n.rnum = -1;
470 ERROR(ENOENT, "no more entries");
473 r_unpackcatdata(HFS_RECDATA(ptr), &data);
475 switch (data.cdrType)
477 case cdrDirRec:
478 case cdrFilRec:
479 r_unpackdirent(key.ckrParID, key.ckrCName, &data, ent);
480 goto done;
482 case cdrThdRec:
483 case cdrFThdRec:
484 break;
486 default:
487 dir->n.rnum = -1;
488 ERROR(EIO, "unexpected directory entry found");
492 done:
493 return 0;
495 fail:
496 return -1;
500 * NAME: hfs->closedir()
501 * DESCRIPTION: stop reading a directory
503 int hfs_closedir(hfsdir *dir)
505 hfsvol *vol = dir->vol;
507 if (dir->prev)
508 dir->prev->next = dir->next;
509 if (dir->next)
510 dir->next->prev = dir->prev;
511 if (dir == vol->dirs)
512 vol->dirs = dir->next;
514 FREE(dir);
516 return 0;
519 /* High-Level File Routines ================================================ */
522 * NAME: hfs->open()
523 * DESCRIPTION: prepare a file for I/O
525 hfsfile *hfs_open(hfsvol *vol, const char *path)
527 hfsfile *file = NULL;
529 if (getvol(&vol) == -1)
530 goto fail;
532 file = ALLOC(hfsfile, 1);
533 if (file == NULL)
534 ERROR(ENOMEM, NULL);
536 if (v_resolve(&vol, path, &file->cat, &file->parid, file->name, NULL) <= 0)
537 goto fail;
539 if (file->cat.cdrType != cdrFilRec)
540 ERROR(EISDIR, NULL);
542 /* package file handle for user */
544 file->vol = vol;
545 file->flags = 0;
547 f_selectfork(file, fkData);
549 file->prev = NULL;
550 file->next = vol->files;
552 if (vol->files)
553 vol->files->prev = file;
555 vol->files = file;
557 return file;
559 fail:
560 FREE(file);
561 return NULL;
565 * NAME: hfs->setfork()
566 * DESCRIPTION: select file fork for I/O operations
568 int hfs_setfork(hfsfile *file, int fork)
570 int result = 0;
572 f_selectfork(file, fork ? fkRsrc : fkData);
574 return result;
578 * NAME: hfs->getfork()
579 * DESCRIPTION: return the current fork for I/O operations
581 int hfs_getfork(hfsfile *file)
583 return file->fork != fkData;
587 * NAME: hfs->read()
588 * DESCRIPTION: read from an open file
590 unsigned long hfs_read(hfsfile *file, void *buf, unsigned long len)
592 unsigned long *lglen, count;
593 byte *ptr = buf;
595 f_getptrs(file, NULL, &lglen, NULL);
597 if (file->pos + len > *lglen)
598 len = *lglen - file->pos;
600 count = len;
601 while (count)
603 unsigned long bnum, offs, chunk;
605 bnum = file->pos >> HFS_BLOCKSZ_BITS;
606 offs = file->pos & (HFS_BLOCKSZ - 1);
608 chunk = HFS_BLOCKSZ - offs;
609 if (chunk > count)
610 chunk = count;
612 if (offs == 0 && chunk == HFS_BLOCKSZ)
614 if (f_getblock(file, bnum, (block *) ptr) == -1)
615 goto fail;
617 else
619 block b;
621 if (f_getblock(file, bnum, &b) == -1)
622 goto fail;
624 memcpy(ptr, b + offs, chunk);
627 ptr += chunk;
629 file->pos += chunk;
630 count -= chunk;
633 return len;
635 fail:
636 return -1;
640 * NAME: hfs->seek()
641 * DESCRIPTION: change file seek pointer
643 unsigned long hfs_seek(hfsfile *file, long offset, int from)
645 unsigned long *lglen, newpos;
647 f_getptrs(file, NULL, &lglen, NULL);
649 switch (from)
651 case HFS_SEEK_SET:
652 newpos = (offset < 0) ? 0 : offset;
653 break;
655 case HFS_SEEK_CUR:
656 if (offset < 0 && (unsigned long) -offset > file->pos)
657 newpos = 0;
658 else
659 newpos = file->pos + offset;
660 break;
662 case HFS_SEEK_END:
663 if (offset < 0 && (unsigned long) -offset > *lglen)
664 newpos = 0;
665 else
666 newpos = *lglen + offset;
667 break;
669 default:
670 ERROR(EINVAL, NULL);
673 if (newpos > *lglen)
674 newpos = *lglen;
676 file->pos = newpos;
678 return newpos;
680 fail:
681 return -1;
685 * NAME: hfs->close()
686 * DESCRIPTION: close a file
688 int hfs_close(hfsfile *file)
690 hfsvol *vol = file->vol;
691 int result = 0;
693 if (file->prev)
694 file->prev->next = file->next;
695 if (file->next)
696 file->next->prev = file->prev;
697 if (file == vol->files)
698 vol->files = file->next;
700 FREE(file);
702 return result;
705 /* High-Level Catalog Routines ============================================= */
708 * NAME: hfs->stat()
709 * DESCRIPTION: return catalog information for an arbitrary path
711 int hfs_stat(hfsvol *vol, const char *path, hfsdirent *ent)
713 CatDataRec data;
714 unsigned long parid;
715 char name[HFS_MAX_FLEN + 1];
717 if (getvol(&vol) == -1 ||
718 v_resolve(&vol, path, &data, &parid, name, NULL) <= 0)
719 goto fail;
721 r_unpackdirent(parid, name, &data, ent);
723 return 0;
725 fail:
726 return -1;
730 * NAME: hfs->fstat()
731 * DESCRIPTION: return catalog information for an open file
733 int hfs_fstat(hfsfile *file, hfsdirent *ent)
735 r_unpackdirent(file->parid, file->name, &file->cat, ent);
737 return 0;
741 * NAME: hfs->probe()
742 * DESCRIPTION: return whether a HFS filesystem is present at the given offset
744 int hfs_probe(int fd, long long offset)
746 return v_probe(fd, offset);