1 |
/* |
2 |
* libhfs - library for reading and writing Macintosh HFS volumes |
3 |
* |
4 |
* The iterator shown here iterates over the blocks of a fork. |
5 |
* |
6 |
* Copyright (C) 2000 Klaus Halfmann <klaus.halfmann@feri.de> |
7 |
* Original work by 1996-1998 Robert Leslie <rob@mars.org> |
8 |
* other work 2000 from Brad Boyer (flar@pants.nu) |
9 |
* |
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. |
14 |
* |
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. |
19 |
* |
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., 675 Mass Ave, Cambridge, MA 02139, USA. |
23 |
* |
24 |
*/ |
25 |
|
26 |
# ifdef HAVE_CONFIG_H |
27 |
# include "config.h" |
28 |
# endif |
29 |
|
30 |
# include <stdlib.h> |
31 |
# include <stdio.h> |
32 |
# include <string.h> |
33 |
# include <time.h> |
34 |
# include <errno.h> |
35 |
|
36 |
# include "libhfsp.h" |
37 |
# include "blockiter.h" |
38 |
# include "volume.h" |
39 |
# include "record.h" |
40 |
# include "btree.h" |
41 |
# include "os.h" |
42 |
# include "hfstime.h" |
43 |
|
44 |
/* Initialize iterator for a given fork */ |
45 |
void blockiter_init(blockiter* b, volume* vol, hfsp_fork_raw* f, |
46 |
UInt8 forktype, UInt32 fileId) |
47 |
{ |
48 |
b->vol = vol; |
49 |
b->curr_block = 0; |
50 |
b->block = 0; |
51 |
b->max_block = f->total_blocks; |
52 |
b->fileId = fileId; |
53 |
b->index = 0; |
54 |
b->file = f->extents; |
55 |
b->e = f->extents; |
56 |
b->forktype = forktype; |
57 |
b->in_extent = 0; |
58 |
} |
59 |
|
60 |
/* get next extent record when needed */ |
61 |
static int blockiter_next_extent(blockiter *b) |
62 |
{ |
63 |
btree* extents_tree = volume_get_extents_tree(b->vol); |
64 |
int err; |
65 |
|
66 |
b->index = 0; |
67 |
if (b->in_extent) // already using extents record ? |
68 |
{ |
69 |
err = record_next_extent(&b->er); |
70 |
// Hope there is no need to check this ... |
71 |
// if (b->er.key.start_block != b->curr_block) |
72 |
// HFSP_ERROR(ENOENT, |
73 |
// "Extents record inconistent"); |
74 |
} |
75 |
else |
76 |
{ |
77 |
err = record_init_file(&b->er, extents_tree, b->forktype, |
78 |
b->fileId, b->curr_block); |
79 |
b->in_extent = -1; // true |
80 |
} |
81 |
b->e = b->er.extent; |
82 |
return err; |
83 |
} |
84 |
|
85 |
/* find next block of the fork iterating over */ |
86 |
int blockiter_next(blockiter *b) |
87 |
{ |
88 |
b->curr_block ++; |
89 |
b->block ++; |
90 |
if (b->curr_block >= b->max_block) |
91 |
return -1; // end of Blocks, but no error |
92 |
// in current part of extent ? |
93 |
if (b->block >= b->e->block_count) |
94 |
{ |
95 |
b->index++; |
96 |
b->block = 0; // reset relative position |
97 |
b->e++; |
98 |
if (b -> index >= 8) // need to fetch another extent |
99 |
{ |
100 |
if (blockiter_next_extent(b)) |
101 |
HFSP_ERROR(ENOENT, "Extends record not found."); |
102 |
} |
103 |
} |
104 |
return 0; |
105 |
|
106 |
fail: |
107 |
return -1; |
108 |
} |
109 |
|
110 |
/* skip the indicated number of blocks */ |
111 |
int blockiter_skip(blockiter *b, UInt32 skip) |
112 |
{ |
113 |
while (skip > 0) |
114 |
{ |
115 |
// Skip to skip or end of current extent |
116 |
UInt32 diff = b->e->block_count - b->block; |
117 |
if (skip < diff) |
118 |
{ |
119 |
diff = skip; |
120 |
skip = 0; |
121 |
} |
122 |
else |
123 |
skip -= diff; |
124 |
b->curr_block += diff; |
125 |
b->block += diff; |
126 |
if (b->curr_block >= b->max_block) |
127 |
return -1; // end of Blocks, but no error |
128 |
if (b->block >= b->e->block_count) |
129 |
{ |
130 |
b->index++; |
131 |
b->block = 0; // reset relative position |
132 |
b->e++; |
133 |
if (b -> index >= 8) // need to fetch another extent |
134 |
{ |
135 |
if (blockiter_next_extent(b)) |
136 |
HFSP_ERROR(ENOENT, "Extends record not found."); |
137 |
} |
138 |
} |
139 |
} // we are here when skip was null, thats ok |
140 |
return 0; |
141 |
fail: |
142 |
return -1; |
143 |
} |
144 |
|