3
Copyright (C) 1998-2001, 2007, 2009-2012 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>.
27
needs_duplicating (const FatOpContext* ctx, FatFragment frag)
29
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
30
FatCluster cluster = fat_frag_to_cluster (ctx->old_fs, frag);
33
PED_ASSERT (cluster >= 2 && cluster < old_fs_info->cluster_count + 2);
35
flag = fat_get_fragment_flag (ctx->old_fs, frag);
40
case FAT_FLAG_DIRECTORY:
44
return fat_op_context_map_static_fragment (ctx, frag) == -1;
54
search_next_fragment (FatOpContext* ctx)
56
FatSpecific* fs_info = FAT_SPECIFIC (ctx->old_fs);
58
for (; ctx->buffer_offset < fs_info->frag_count; ctx->buffer_offset++) {
59
if (needs_duplicating (ctx, ctx->buffer_offset))
62
return 0; /* all done! */
66
read_marked_fragments (FatOpContext* ctx, FatFragment length)
68
FatSpecific* fs_info = FAT_SPECIFIC (ctx->old_fs);
72
ped_exception_fetch_all ();
73
status = fat_read_fragments (ctx->old_fs, fs_info->buffer,
74
ctx->buffer_offset, length);
75
ped_exception_leave_all ();
79
ped_exception_catch ();
81
/* something bad happened, so read fragments one by one. (The error may
82
have occurred on an unused fragment: who cares) */
83
for (i = 0; i < length; i++) {
84
if (ctx->buffer_map [i]) {
85
if (!fat_read_fragment (ctx->old_fs,
86
fs_info->buffer + i * fs_info->frag_size,
87
ctx->buffer_offset + i))
96
fetch_fragments (FatOpContext* ctx)
98
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
99
FatFragment fetch_length = 0;
102
for (frag = 0; frag < ctx->buffer_frags; frag++)
103
ctx->buffer_map [frag] = -1;
106
frag < ctx->buffer_frags
107
&& ctx->buffer_offset + frag < old_fs_info->frag_count;
109
if (needs_duplicating (ctx, ctx->buffer_offset + frag)) {
110
ctx->buffer_map [frag] = 1;
111
fetch_length = frag + 1;
115
if (!read_marked_fragments (ctx, fetch_length))
121
/*****************************************************************************
122
* here starts the write code. All assumes that ctx->buffer_map [first] and
123
* ctx->buffer_map [last] are occupied by fragments that need to be duplicated.
124
*****************************************************************************/
126
/* finds the first fragment that is not going to get overwritten (that needs to
129
get_first_underlay (const FatOpContext* ctx, int first, int last)
134
PED_ASSERT (first <= last);
136
new = ctx->buffer_map [first];
137
for (old = first + 1; old <= last; old++) {
138
if (ctx->buffer_map [old] == -1)
141
if (ctx->buffer_map [old] != new)
147
/* finds the last fragment that is not going to get overwritten (that needs to
150
get_last_underlay (const FatOpContext* ctx, int first, int last)
155
PED_ASSERT (first <= last);
157
new = ctx->buffer_map [last];
158
for (old = last - 1; old >= first; old--) {
159
if (ctx->buffer_map [old] == -1)
162
if (ctx->buffer_map [old] != new)
168
/* "underlay" refers to the "static" fragments, that remain unchanged.
169
* when writing large chunks at a time, we don't want to clobber these,
170
* so we read them in, and write them back again. MUCH quicker that way.
173
quick_group_write_read_underlay (FatOpContext* ctx, int first, int last)
175
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
176
FatFragment first_underlay;
177
FatFragment last_underlay;
178
FatFragment underlay_length;
180
PED_ASSERT (first <= last);
182
first_underlay = get_first_underlay (ctx, first, last);
183
if (first_underlay == -1)
185
last_underlay = get_last_underlay (ctx, first, last);
187
PED_ASSERT (first_underlay <= last_underlay);
189
underlay_length = last_underlay - first_underlay + 1;
190
if (!fat_read_fragments (ctx->new_fs,
192
+ (first_underlay - ctx->buffer_map [first])
193
* new_fs_info->frag_size,
200
/* quick_group_write() makes no attempt to recover from errors - just
201
* does things fast. If there is an error, slow_group_write() is
203
* Note: we do syncing writes, to make sure there isn't any
204
* error writing out. It's rather difficult recovering from errors
208
quick_group_write (FatOpContext* ctx, int first, int last)
210
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
211
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
216
PED_ASSERT (first <= last);
218
ped_exception_fetch_all ();
219
if (!quick_group_write_read_underlay (ctx, first, last))
222
for (i = first; i <= last; i++) {
223
if (ctx->buffer_map [i] == -1)
226
offset = ctx->buffer_map [i] - ctx->buffer_map [first];
227
memcpy (new_fs_info->buffer + offset * new_fs_info->frag_size,
228
old_fs_info->buffer + i * new_fs_info->frag_size,
229
new_fs_info->frag_size);
232
active_length = ctx->buffer_map [last] - ctx->buffer_map [first] + 1;
233
if (!fat_write_sync_fragments (ctx->new_fs, new_fs_info->buffer,
234
ctx->buffer_map [first], active_length))
237
ped_exception_leave_all ();
241
ped_exception_catch ();
242
ped_exception_leave_all ();
246
/* Writes fragments out, one at a time, avoiding errors on redundant writes
247
* on damaged parts of the disk we already know about. If there's an error
248
* on one of the required fragments, it gets marked as bad, and a replacement
252
slow_group_write (FatOpContext* ctx, int first, int last)
254
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
255
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
258
PED_ASSERT (first <= last);
260
for (i = first; i <= last; i++) {
261
if (ctx->buffer_map [i] == -1)
264
while (!fat_write_sync_fragment (ctx->new_fs,
265
old_fs_info->buffer + i * old_fs_info->frag_size,
266
ctx->buffer_map [i])) {
267
fat_table_set_bad (new_fs_info->fat,
268
ctx->buffer_map [i]);
269
ctx->buffer_map [i] = fat_table_alloc_cluster
271
if (ctx->buffer_map [i] == 0)
279
update_remap (FatOpContext* ctx, int first, int last)
283
PED_ASSERT (first <= last);
285
for (i = first; i <= last; i++) {
286
if (ctx->buffer_map [i] == -1)
288
ctx->remap [ctx->buffer_offset + i] = ctx->buffer_map [i];
295
group_write (FatOpContext* ctx, int first, int last)
297
PED_ASSERT (first <= last);
299
if (!quick_group_write (ctx, first, last)) {
300
if (!slow_group_write (ctx, first, last))
303
if (!update_remap (ctx, first, last))
308
/* assumes fragment size and new_fs's cluster size are equal */
310
write_fragments (FatOpContext* ctx)
312
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
313
FatSpecific* new_fs_info = FAT_SPECIFIC (ctx->new_fs);
315
int group_end = -1; /* shut gcc up! */
316
FatFragment mapped_length;
318
FatCluster new_cluster;
320
PED_ASSERT (ctx->buffer_offset < old_fs_info->frag_count);
323
for (i = 0; i < ctx->buffer_frags; i++) {
324
if (ctx->buffer_map [i] == -1)
329
new_cluster = fat_table_alloc_cluster (new_fs_info->fat);
332
fat_table_set_eof (new_fs_info->fat, new_cluster);
333
ctx->buffer_map [i] = fat_cluster_to_frag (ctx->new_fs,
336
if (group_start == -1)
337
group_start = group_end = i;
339
PED_ASSERT (ctx->buffer_map [i]
340
>= ctx->buffer_map [group_start]);
342
mapped_length = ctx->buffer_map [i]
343
- ctx->buffer_map [group_start] + 1;
344
if (mapped_length <= ctx->buffer_frags) {
347
/* ran out of room in the buffer, so write this group,
348
* and start a new one...
350
if (!group_write (ctx, group_start, group_end))
352
group_start = group_end = i;
356
PED_ASSERT (group_start != -1);
358
if (!group_write (ctx, group_start, group_end))
363
/* default all fragments to unmoved
366
init_remap (FatOpContext* ctx)
368
FatSpecific* old_fs_info = FAT_SPECIFIC (ctx->old_fs);
371
for (i = 0; i < old_fs_info->frag_count; i++)
372
ctx->remap[i] = fat_op_context_map_static_fragment (ctx, i);
376
count_frags_to_dup (FatOpContext* ctx)
378
FatSpecific* fs_info = FAT_SPECIFIC (ctx->old_fs);
384
for (i = 0; i < fs_info->frag_count; i++) {
385
if (needs_duplicating (ctx, i))
392
/* duplicates unreachable file clusters, and all directory clusters
395
fat_duplicate_clusters (FatOpContext* ctx, PedTimer* timer)
397
FatFragment total_frags_to_dup;
400
total_frags_to_dup = count_frags_to_dup (ctx);
402
ped_timer_reset (timer);
403
ped_timer_set_state_name (timer, "moving data");
405
ctx->buffer_offset = 0;
406
ctx->frags_duped = 0;
407
while (search_next_fragment (ctx)) {
409
timer, 1.0 * ctx->frags_duped / total_frags_to_dup);
411
if (!fetch_fragments (ctx))
413
if (!write_fragments (ctx))
415
ctx->buffer_offset += ctx->buffer_frags;
418
ped_timer_update (timer, 1.0);
422
#endif /* !DISCOVER_ONLY */