2 * libhfs - library for reading and writing Macintosh HFS volumes
4 * The iterator shown here iterates over the blocks of a fork.
6 * Copyright (C) 2000 Klaus Halfmann <khalfmann@libra.de>
7 * Original work by 1996-1998 Robert Leslie <rob@mars.org>
8 * other work 2000 from Brad Boyer (flar@pants.nu)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
25 * $Id: blockiter.c,v 1.2 2000/10/17 05:58:46 hasi Exp $
30 #include "blockiter.h"
38 /* Initialize iterator for a given fork */
40 blockiter_init(blockiter* b, volume* vol, hfsp_fork_raw* f,
41 UInt8 forktype, UInt32 fileId)
46 b->max_block = f->total_blocks;
51 b->forktype = forktype;
55 /* get next extent record when needed */
57 blockiter_next_extent(blockiter *b)
59 btree* extents_tree = volume_get_extents_tree(b->vol);
63 if (b->in_extent) // already using extents record ?
65 err = record_next_extent(&b->er);
66 // Hope there is no need to check this ...
67 // if (b->er.key.start_block != b->curr_block)
69 // "Extents record inconistent");
73 err = record_init_file(&b->er, extents_tree, b->forktype,
74 b->fileId, b->curr_block);
75 b->in_extent = -1; // true
81 /* find next block of the fork iterating over */
83 blockiter_next(blockiter *b)
87 if (b->curr_block >= b->max_block)
88 return -1; // end of Blocks, but no error
89 // in current part of extent ?
90 if (b->block >= b->e->block_count)
93 b->block = 0; // reset relative position
95 if (b -> index >= 8) // need to fetch another extent
97 if (blockiter_next_extent(b))
98 HFSP_ERROR(ENOENT, "Extends record not found.");
107 /* skip the indicated number of blocks */
109 blockiter_skip(blockiter *b, UInt32 skip)
113 // Skip to skip or end of current extent
114 UInt32 diff = b->e->block_count - b->block;
122 b->curr_block += diff;
124 if (b->curr_block >= b->max_block)
125 return -1; // end of Blocks, but no error
126 if (b->block >= b->e->block_count)
129 b->block = 0; // reset relative position
131 if (b -> index >= 8) // need to fetch another extent
133 if (blockiter_next_extent(b))
134 HFSP_ERROR(ENOENT, "Extends record not found.");
137 } // we are here when skip was null, thats ok