2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)block.c 1.3 01/11/01 joerg */
15 * hfsutils - tools for reading and writing Macintosh HFS volumes
16 * Copyright (C) 1996, 1997 Robert Leslie
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
48 * NAME: block->readlb()
49 * DESCRIPTION: read a logical block from a volume
51 int b_readlb(hfsvol *vol, unsigned long num, block *bp)
61 fprintf(stderr,"b_readlb: start block = %d\n", vol->vstart + num);
66 /* Check to see if requested block is in the HFS header or catalog/exents
67 files. If it is, read info from memory copy. If not, then something
68 has gone horribly wrong ... */
70 if (num < hce->hfs_hdr_size)
71 b = (block *)hce->hfs_hdr + num;
72 else if (num < hce->hfs_hdr_size + hce->hfs_ce_size)
73 b = (block *)hce->hfs_ce + num - hce->hfs_hdr_size;
76 ERROR(EIO, "should not happen!");
80 memcpy(bp, b, HFS_BLOCKSZ);
84 if (lseek(vol->fd, (vol->vstart + num) * HFS_BLOCKSZ, SEEK_SET) < 0)
86 ERROR(errno, "error seeking device");
90 bytes = read(vol->fd, bp, HFS_BLOCKSZ);
93 ERROR(errno, "error reading from device");
98 ERROR(EIO, "read EOF on volume");
101 else if (bytes != HFS_BLOCKSZ)
103 ERROR(EIO, "read incomplete block");
106 #endif /* APPLE_HYB */
111 * NAME: block->writelb()
112 * DESCRIPTION: write a logical block to a volume
114 int b_writelb(hfsvol *vol, unsigned long num, block *bp)
124 fprintf(stderr,"b_writelb: start block = %d\n", vol->vstart + num);
129 /* Check to see if requested block is in the HFS header or catalog/exents
130 files. If it is, write info to memory copy. If not, then it's a block
131 for an ordinary file - and as we are writing the files later, then just
132 ignore and return OK */
133 if (num < hce->hfs_hdr_size)
134 b = (block *)hce->hfs_hdr + num;
135 else if (num < hce->hfs_hdr_size + hce->hfs_ce_size)
136 b = (block *)hce->hfs_ce + num - hce->hfs_hdr_size;
140 fprintf(stderr,"b_writelb: ignoring\n");
145 memcpy(b, bp, HFS_BLOCKSZ);
149 if (lseek(vol->fd, (vol->vstart + num) * HFS_BLOCKSZ, SEEK_SET) < 0)
151 ERROR(errno, "error seeking device");
155 bytes = write(vol->fd, bp, HFS_BLOCKSZ);
159 ERROR(errno, "error writing to device");
162 else if (bytes != HFS_BLOCKSZ)
164 ERROR(EIO, "wrote incomplete block");
167 #endif /* APPLE_HYB */
172 * NAME: block->readab()
173 * DESCRIPTION: read a block from an allocation block from a volume
175 int b_readab(hfsvol *vol, unsigned int anum, unsigned int idx, block *bp)
177 /* verify the allocation block exists and is marked as in-use */
179 if (anum >= vol->mdb.drNmAlBlks)
181 ERROR(EIO, "read nonexistent block");
184 else if (vol->vbm && ! BMTST(vol->vbm, anum))
186 ERROR(EIO, "read unallocated block");
190 return b_readlb(vol, vol->mdb.drAlBlSt + anum * vol->lpa + idx, bp);
195 * DESCRIPTION: write a block to an allocation block to a volume
197 int b_writeab(hfsvol *vol, unsigned int anum, unsigned int idx, block *bp)
199 /* verify the allocation block exists and is marked as in-use */
201 if (anum >= vol->mdb.drNmAlBlks)
203 ERROR(EIO, "write nonexistent block");
206 else if (vol->vbm && ! BMTST(vol->vbm, anum))
208 ERROR(EIO, "write unallocated block");
212 vol->mdb.drAtrb &= ~HFS_ATRB_UMOUNTED;
213 vol->mdb.drLsMod = d_tomtime(time(0));
216 vol->flags |= HFS_UPDATE_MDB;
218 return b_writelb(vol, vol->mdb.drAlBlSt + anum * vol->lpa + idx, bp);