358377060081184a7a6ffd2abae248c5fdb86e27
3 * Make an unscrambled copy of a DVD.
5 * (c) 2022 Mark Wooding
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the DVD ripping toolset.
12 * DVDrip is free software: you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * DVDrip is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with DVDrip. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
30 /*----- Program usage summary ---------------------------------------------*/
32 static void usage(FILE *fp
)
35 "usage: %s [-ci] [-B PARAM=VALUE,...] [-R MAP]\n"
36 "\t[-b OUTMAP] [-r [START]-[END]] DEVICE OUTFILE\n",
40 /*----- Random utilities --------------------------------------------------*/
43 static int parse_range(const char *p
, unsigned f
,
44 secaddr
*start_out
, secaddr
*end_out
)
45 /* Parse a range of sectors from the string P. If successful, store
46 * the specified start sector address in *START_OUT and the end
47 * address in *END_OUT, and return zero. On failure, return -1;
48 * *START_OUT and/or *END_OUT are clobbered.
50 * The acceptable syntax depends on the flags.
52 * * The `PRF_HYPHEN' syntax is intended for use on the
53 * command-line. It accepts `[START]-[END]'; if the start and/or
54 * end addresses are omitted then *START_OUT and/or *END_OUT are
57 * * The default syntax matches what's written to the bad-sector
58 * output files. It accepts `START END [# COMMENT]'.
63 unsigned long start
, end
;
65 /* Save any existing error code. */
68 /* Parse the start address. */
70 /* We found a digit: this is a good start. Convert the integer, check
71 * that it's in range, save it.
74 start
= strtoul(p
, &q
, 0);
75 if (errno
|| start
>= SECLIMIT
) { rc
= -1; goto end
; }
76 *start_out
= start
; p
= q
;
77 } else if (!(f
&PRF_HYPHEN
)) {
78 /* No digit. We're parsing the map-file syntax, so this is an error. */
82 /* We're parsing the command-line syntax, so this is OK. Set our
83 * internal idea of the position for the range check later, but don't
84 * alter the caller's variables.
90 /* Parse the delimiter. */
92 if (*p
!= '-') { rc
= -1; goto end
; }
95 if (!ISSPACE(*p
)) { rc
= -1; goto end
; }
96 do p
++; while (ISSPACE(*p
));
99 /* Parse the end address. */
101 /* We found a digit. Parse the integer and check that it's strictly
102 * larger than the start address.
105 end
= strtoul(p
, &q
, 0);
106 if (errno
|| end
> SECLIMIT
|| end
< start
) { rc
= -1; goto end
; }
107 *end_out
= end
; p
= q
;
108 } else if (!(f
&PRF_HYPHEN
)) {
109 /* No digit. We're parsing the file syntax, so this is an error. */
114 /* In the file syntax, we're now allowed whitespace, so skip past that. */
115 if (!(f
&PRF_HYPHEN
)) while (ISSPACE(*p
)) p
++;
117 /* Check that there's nothing else. The file syntax allows a trailing
118 * comment here, but the command-line syntax doesn't.
120 if (*p
&& ((f
&PRF_HYPHEN
) || *p
!= '#')) { rc
= -1; goto end
; }
129 /*----- A few words about the overall approach ----------------------------*
131 * The objective is to produce a working copy of the input (commercial,
132 * pressed) DVD disc, only with all of the scrambled video data unscrambled
133 * so that it can be read without the need for cracking CSS keys, which, in
134 * the absence of a cooperative drive with access to the key tables in the
135 * disc lead-in data -- which we /don't/ copy -- is often slow and prone to
136 * failure. Producing a sector-by-sector image preserves all of the menus
137 * and special features, and also any other bonus data stored in the
138 * filesystem for use by computers, such as PDF scripts. DVD images are
139 * large because DVD video is inefficiently compressed by modern standards,
140 * but disk space is cheap and the tradeoff seems worthwhile to me.
142 * The approach is, in essence, simple: start at the beginning of the disc,
143 * reading sectors into a buffer and writing them to the output file, and
144 * continue until we reach the end. But we must cope with scrambled video
145 * files. Fortunately, `libdvdread' knows how to deal with these, and will
146 * tell us where they are on the disc.
148 * Given this information, we build a table of `events', with the sector
149 * numbers at which they occur. An `event' might be something like `such-
150 * and-such a video file began' or `such-and-such a file ended'. Chunks of
151 * disc between events can be read using the same strategy -- either reading
152 * unscrambled sectors directly from the block device, or decrypting
153 * scrambled sectors through `libdvdread' -- while at sector boundaries we
154 * might need to change strategy.
156 * Note that files can /overlap/. The DVD spec says that this can't happen,
157 * and that the data for video titles is laid out with higher-numbered
158 * titlesets occupying higher-numbered sectors, but it does anyway. I think
159 * this is intended to frustrate copiers like `dvdbackup' which try to copy
160 * the DVD files into a directory on the filesystem. The result is that they
161 * copy the same sectors into multiple, very large files, and turn an 8 GB
162 * DVD image into a 60 GB directory. (The reused regions often also contain
163 * intentionally bad sectors, so you have to wait for the drive to fail the
164 * same sectors over and over again. This is no fun.) As far as I know,
165 * files are either disjoint or coincident, but more complex arrangements are
166 * possible in principle. Also, I guess it's possible that the same sector
167 * should be decrypted with different keys depending on which titleset we're
168 * considering it being part of, but (a) DVD CSS keys aren't long enough to
169 * do this very well, and (b) I'm not aware of this actually being a thing.
170 * (Indeed, `libdvdcss' indexes keys by start sector, so such a disc probably
171 * wouldn't play back properly through VLC or `mpv'.)
173 * There's an additional consideration. We want to be able to fill in an
174 * ouptut image file incrementally, in several runs. A run can be
175 * interrupted for lots of reasons (e.g., a faster drive might have become
176 * available; it might be beneficial to switch to a more forgiving drive; it
177 * might be necessary to stop and clean the disc; the output filesystem might
178 * have become full; ...). And discs don't always read perfectly: some discs
179 * are damaged and have areas which can't be read; some discs (I'm looking at
180 * you, Sony, Disney, Lionsgate, and E-One) have intentional bad sectors,
181 * presumably specifically to make my life annoying. So we have other events
182 * which say things like `start writing stuff to the output' or `stop writing
183 * things to the output'. And we have a rather elaborate algorithm for
184 * trying to skip past a region of bad blocks, because drives get /really/
185 * slow when reading bad sectors.
188 /*----- The file and event tables -----------------------------------------*/
190 #define MAXFILES (1 + 2*99 + 1)
191 /* How many (interesting) files there can be. This counts the
192 * magical `raw' file which refers to direct disc access, the master
193 * menu file, and 99 possible menu and titleset pairs. (A titleset
194 * can be split into 9 parts in order to keep each file below a
195 * gigabyte in size, but the rules require that the parts together
196 * form a single contiguous chunk on the disc, in the right order, so
197 * we treat them as a single file. We check this in `put_title'
198 * below, just in case some disc somewhere tries to be awkward, but I
199 * don't have a disc like that in my collection, and I doubt it would
204 /* An interesting DVD file. It has a name, encoded as an `ident'
205 * (see `lib.h'), and start and end sectors. (The `end' here, as
206 * everywhere in this code, is /exclusive/, so that the file's length
207 * is simply end - start.)
210 ident id
; /* file name */
211 secaddr start
, end
; /* start (inclusive) and end
212 * (exclusive) sector numbers */
214 DEFVEC(file_v
, struct file
); /* a vector of files */
215 static file_v filetab
= VEC_INIT
; /* the file table */
218 /* Event codes. The ordering of these is important, because we use
219 * them to tie-break comparisons of events happening at the same
220 * sector when we sort the event queue.
223 EV_STOP
, /* stop copying stuff to output */
224 EV_BEGIN
, /* a (maybe scrambled) file begins */
225 EV_END
, /* a file ends */
226 EV_WRITE
/* start copying stuff to output */
232 unsigned char ev
; /* event code (`EV_...') */
233 unsigned char file
; /* the file (`EV_BEGIN', `EV_END');
234 * index into `filetab' */
235 secaddr pos
; /* the sector at which it happens */
237 DEFVEC(event_v
, struct event
); /* a vector of events */
238 static event_v eventq
= VEC_INIT
; /* the event queue */
240 static int compare_event(const void *a
, const void *b
)
241 /* A `qsort' comparison function for events. Event A sorts earlier
242 * than event B iff A's sector number is smaller than B's, or A's
243 * event code is less than B's.
246 const struct event
*eva
= a
, *evb
= b
;
248 /* Primary ordering by position. */
249 if (eva
->pos
< evb
->pos
) return (-1);
250 else if (eva
->pos
> evb
->pos
) return (+1);
252 /* Secondary ordering by event code. */
253 if (eva
->ev
< evb
->ev
) return (-1);
254 else if (eva
->ev
> evb
->ev
) return (+1);
256 /* We currently have a final tie-break on file numbers so that the ordering
257 * is deterministic, but this is an arbitrary choice that shouldn't be
260 if (eva
->file
< evb
->file
) return (-1);
261 else if (eva
->file
> evb
->file
) return (+1);
263 /* These events are equal. */
268 static void dump_eventq(const char *what
)
269 /* Dump the event queue, labelling the output with WHAT. */
272 const struct event
*ev
;
275 printf("\n;; event dump (%s):\n", what
);
276 for (i
= 0; i
< eventq
.n
; i
++) {
280 store_filename(fn
, filetab
.v
[ev
->file
].id
);
281 printf(";; %8"PRIuSEC
": begin %s\n", ev
->pos
, fn
);
284 store_filename(fn
, filetab
.v
[ev
->file
].id
);
285 printf(";; %8"PRIuSEC
": end %s\n", ev
->pos
, fn
);
288 printf(";; %8"PRIuSEC
": write\n", ev
->pos
);
291 printf(";; %8"PRIuSEC
": stop\n", ev
->pos
);
294 printf(";; %8"PRIuSEC
": ?%u\n", ev
->pos
, ev
->ev
);
301 typedef uint_least32_t bits
;
302 static bits live
[(MAXFILES
+ 31)/32];
303 /* A bitmap which keeps track of which files are currently `active',
304 * i.e., that contain the sector we're currently thinking about. We
305 * set and clear these bits as we encounter `EV_BEGIN' and `EV_END'
309 static inline int livep(unsigned i
)
310 /* Return whether file I is active. */
311 { return (live
[i
/32]&((bits
)1 << (i
%32))); }
313 static inline void set_live(unsigned i
)
314 /* Note that we've seen the start of file I. */
315 { live
[i
/32] |= (bits
)1 << (i
%32); }
317 static inline void clear_live(unsigned i
)
318 /* Note that we've seen the end of file I. */
319 { live
[i
/32] &= ~((bits
)1 << (i
%32)); }
321 static inline int least_live(void)
322 /* Return the smallest index for any active file. This is going to
323 * be the file that we ask `libdvdread' to unscramble for us. This
324 * is important: the imaginary `raw' file that represents the entire
325 * block device has the highest index, and we want any actual video
326 * file to be used in preference so that we unscramble the data.
329 unsigned i
, n
= (filetab
.n
+ 32)/32;
332 /* First part: find the first nonzero word in the `live' table. */
333 for (i
= 0; i
< n
; i
++) { b
= live
[i
]; if (b
) goto found
; }
337 /* Second part: identify which bit in this word is nonzero. First, see if
338 * the bottom 16 bits are clear: if so, shift down and add 16 to the
339 * total. Now we know that the first set bit is indeed in the low 16
340 * bits, so see whether the low 8 bits are clear, and so on.
343 if (!(b
&0x0000ffff)) { b
>>= 16; i
+= 16; }
344 if (!(b
&0x000000ff)) { b
>>= 8; i
+= 8; }
345 if (!(b
&0x0000000f)) { b
>>= 4; i
+= 4; }
346 if (!(b
&0x00000003)) { b
>>= 2; i
+= 2; }
347 if (!(b
&0x00000001)) { b
>>= 1; i
+= 1; }
354 static void put_event(unsigned evtype
, unsigned file
, secaddr pos
)
355 /* Add an event to the queue, with type EVTYPE, for the given FILE,
356 * and at sector POS. You can add events in any order because we'll
357 * sort them later. For `EV_WRITE' and `EV_STOP' events, the FILE
358 * doesn't matter: use zero for concreteness.
363 VEC_PUSH(ev
, &eventq
);
364 ev
->ev
= evtype
; ev
->file
= file
; ev
->pos
= pos
;
367 static void put_file(ident id
, secaddr start
, secaddr end
)
368 /* Add a (VOB) file to the file table and event queue, with ident ID,
369 * starting at sector START and ending just before sector END.
375 VEC_PUSH(f
, &filetab
); i
= f
- filetab
.v
;
376 f
->id
= id
; f
->start
= start
; f
->end
= end
;
377 put_event(EV_BEGIN
, i
, start
);
378 put_event(EV_END
, i
, end
);
381 static void put_menu(dvd_reader_t
*dvd
, unsigned title
)
382 /* Add the menu file for the given TITLE number to the file table and
383 * event queue; use the reader DVD to find out which sectors it
384 * occupies, if it even exists.
387 ident id
= mkident(VOB
, title
, 0);
391 /* Find out where the file is. */
392 store_filename(fn
, id
);
393 start
= UDFFindFile(dvd
, fn
, &len
); if (!start
) return;
396 /* Print out what we've discovered. */
397 printf(";; %8"PRIuSEC
" .. %-8"PRIuSEC
": %s\n",
398 start
, start
+ SECTORS(len
), fn
);
401 /* Register the file and boundary events. */
402 put_file(id
, start
, start
+ SECTORS(len
));
405 static void put_title(dvd_reader_t
*dvd
, unsigned title
)
406 /* Add the titleset file for the given TITLE number to the file table
407 * and event queue; use the reader DVD to find out which sectors it
408 * occupies, if it even exists.
412 secaddr start
[9], len
[9];
415 /* First step: find out where all of the parts of the titleset are. I'm
416 * assuming that there aren't gaps in the numbering.
418 for (i
= 0; i
< 9; i
++) {
419 store_filename(fn
, mkident(VOB
, title
, i
+ 1));
420 start
[i
] = UDFFindFile(dvd
, fn
, &len
[i
]); if (!start
[i
]) break;
422 npart
= i
; if (!npart
) return;
425 /* Print out what we've discovered. */
426 for (i
= 0; i
< npart
; i
++) {
427 store_filename(fn
, mkident(VOB
, title
, i
+ 1));
428 printf(";; %8"PRIuSEC
" .. %-8"PRIuSEC
": %s\n",
429 start
[i
], start
[i
] + SECTORS(len
[i
]), fn
);
433 /* Second step: check that the parts all butt up against each other in the
434 * correct order. For this to work, the lengths, which are expressed in
435 * /bytes/ by `UDFFindFile', of all but the last part must be a whole
439 for (i
= 0; i
< npart
- 1; i
++) {
441 bail("title %u part %u length = %"PRIuSEC
" not a multiple of %d",
442 title
, i
, len
[i
], SECTORSZ
);
443 if (start
[i
] + len
[i
]/SECTORSZ
!= start
[i
+ 1])
445 ("title %u part %u end = %"PRIuSEC
" /= part %u start = %"PRIuSEC
"",
446 title
, i
, start
[i
] + len
[i
]/SECTORSZ
, i
+ 1, start
[i
+ 1]);
449 /* All good: register a single file and its boundary events. */
450 put_file(mkident(VOB
, title
, 1),
451 start
[0], start
[npart
- 1] + SECTORS(len
[npart
- 1]));
454 /*----- Common variables used by the copying machinery --------------------*/
456 /* General reading state. */
457 static dvd_reader_t
*dvd
; /* `libdvdread' state for device */
458 static int dvdfd
= -1, outfd
= -1; /* input device and output image */
459 static struct file
*file
; /* currently active file */
460 static dvd_file_t
*vob
; /* current `.VOB' file, or null */
461 static const char *mapfile
; static FILE *mapfp
; /* skipped regions map */
462 static const char *errfile
; static FILE *errfp
; /* bad-sector log */
463 static secaddr limit
; /* upper bound on sectors */
465 static secaddr bad_start
; /* start of current bad region */
466 static unsigned retry
, max_retries
= 4; /* retry state */
468 /*----- Progress reporting ------------------------------------------------*/
470 static secaddr nsectors
, ndone
; /* number of sectors done/to do */
471 static secaddr last_pos
; /* position last time we updated */
472 static struct timeval last_time
; /* time last time we updated */
473 static double alpha
= 0.1; /* weighting factor for average */
474 static double avg
= 0.0, corr
= 1.0; /* exponential moving average */
475 static int bad_err
; /* most recent error code */
477 static const char throbber
[] = "|<-<|>->"; /* throbber pattern */
478 static unsigned throbix
= 0; /* current throbber index */
480 static struct progress_item
/* stock progress items */
481 copy_progress
, disc_progress
,
482 file_progress
, badblock_progress
;
484 static double scale_bytes(double n
, const char **unit_out
)
485 /* Determine a human-readable representation for N bytes. Divide N
486 * by some power of 1024, and store in *UNIT_OUT a string
487 * representing the conventional unit-prefix for that power of 1024.
490 const char *unit
= "";
492 if (n
> 1600) { n
/= 1024; unit
= "k"; }
493 if (n
> 1600) { n
/= 1024; unit
= "M"; }
494 if (n
> 1600) { n
/= 1024; unit
= "G"; }
495 if (n
> 1600) { n
/= 1024; unit
= "T"; }
496 *unit_out
= unit
; return (n
);
499 #define TIMESTRMAX 16 /* maximum length of a duration string */
500 static char *fmttime(unsigned long t
, char *buf
)
501 /* Format a count T of seconds. Write a suitable string to BUF,
502 * which will be no longer than `TIMESTRMAX' bytes including the
503 * terminating zero. Return BUF.
506 if (t
< 60) sprintf(buf
, "%ld s", t
);
507 else if (t
< 3600) sprintf(buf
, "%ld:%02ld", t
/60, t
%60);
508 else sprintf(buf
, "%ld:%02ld:%02ld", t
/3600, (t
/60)%60, t
%60);
512 static void render_perfstats(struct progress_render_state
*render
)
513 /* Add performance statistics to RENDER.
515 * Specifically: the average transfer rate, and the estimated time to
516 * completion. (See `update_progress' for how the average
517 * computation works.)
521 char timebuf
[TIMESTRMAX
];
525 /* If there's no average computed yet, then use some placeholder values. */
526 rate
= avg
/(1 - corr
); eta
= (int)((nsectors
- ndone
)/rate
+ 0.5);
528 /* Write out the statistics. */
529 rate
= scale_bytes(rate
*SECTORSZ
, &unit
);
530 progress_putright(render
, "ETA %s ", avg ?
fmttime(eta
, timebuf
) : "???");
531 progress_putright(render
, "%.1f %sB/s, ", rate
, unit
);
534 static void render_copy_progress(struct progress_item
*item
,
535 struct progress_render_state
*render
)
536 /* Render the progress for the copy, i.e., the number of sectors
537 * copied against the total number to be copied.
540 double frac
= (double)ndone
/nsectors
;
542 progress_putleft(render
, " %c copied %.1f%%",
543 throbber
[throbix
], 100.0*frac
);
544 render_perfstats(render
);
545 progress_putleft(render
, " (%"PRIuSEC
" of %"PRIuSEC
")", ndone
, nsectors
);
547 progress_showbar(render
, frac
);
550 static void render_disc_progress(struct progress_item
*item
,
551 struct progress_render_state
*render
)
552 /* Render the progress for the disc, i.e., the current position
553 * against the total number of sectors on the disc.
556 double frac
= (double)last_pos
/limit
;
558 progress_putleft(render
, " disc %.1f%% (%"PRIuSEC
" of %"PRIuSEC
")",
559 100.0*frac
, last_pos
, limit
);
560 progress_showbar(render
, frac
);
563 static void render_file_progress(struct progress_item
*item
,
564 struct progress_render_state
*render
)
565 /* Render the progress for the current file, i.e., the current
566 * position within the file against the file size.
569 secaddr off
= last_pos
- file
->start
, len
= file
->end
- file
->start
;
573 store_filename(fn
, file
->id
);
574 frac
= (double)off
/len
;
575 progress_putleft(render
, " `%s' %.1f%% (%"PRIuSEC
" of %"PRIuSEC
")",
576 fn
, 100.0*frac
, off
, len
);
577 progress_showbar(render
, frac
);
580 static void render_badblock_progress(struct progress_item
*item
,
581 struct progress_render_state
*render
)
582 /* Render a notice about the progress through the current bad block
586 secaddr n
= last_pos
- bad_start
;
590 progress_putleft(render
, " Retrying bad sector %"PRIuSEC
"", bad_start
);
591 progress_putright(render
, "attempt %u/%u ", retry
+ 1, max_retries
);
594 progress_putleft(render
, " Found %"PRIuSEC
" bad %s",
595 n
, n
== 1 ?
"sector" : "sectors");
596 progress_putright(render
, "%"PRIuSEC
" .. %"PRIuSEC
" ",
597 bad_start
, last_pos
);
600 if (bad_err
&& bad_err
!= EIO
)
601 progress_putleft(render
, " (%s)", strerror(bad_err
));
602 progress_shownotice(render
, bg
, 7);
605 static void update_progress(secaddr pos
)
606 /* Recompute the data displayed by the progress renderer functions
607 * above, based on the new current sector POS.
611 double t
, beta_t
, rate
;
613 /* We're using an exponential moving average with a weighting factor of α
614 * (`alpha', above); larger values are more sensitive to recent changes.
615 * If the old average was v_1, and the measurement in the current interval
616 * is x, then the new average after this interval is
618 * v = α x + (1 − α) v_1 .
620 * Write β = 1 − α; so
622 * v = α x + β v_1 .
624 * Let x_0 = x, let x_1 be the measurement from the previous interval, and,
625 * in general, let x_i be the measurement from i intervals ago. Then
626 * another way to write the above would be
628 * v = α (x_0 + β x_1 + ⋯ + β^i x_i + ⋯) .
630 * Alas, our time intervals are not regular. Suppose that we get our next
631 * measurement after a gap of t intervals, for some integer t. We can
632 * compensate approximately by pretending that all of the missed intervals
633 * -- and our new one -- had the same mean rate. Then we'd have
636 * v = α (x + β x + ⋯ + β^{t−1} x) + β^t v_1
639 * = α x ------- + β^t v_1
642 * = x (1 − β^t) + β^t v_1 (since α = 1 − β)
644 * = x + β^t (v_1 − x) .
646 * Does this work in general? It's clearly correct in the case t = 1.
648 * Suppose the old average was v_2, and that over a period of t intervals
649 * (where t is not necessarily an integer) we measured a mean rate of x,
650 * and then after u intervals we measured a mean rate of x /again/. Then
651 * we'd firstly determine
653 * v_1 = x + β^t (v_2 − x)
657 * v = x + β^u (v_1 − x)
659 * = x + β^u (x + β^t (v_2 − x) − x)
661 * = x + β^{t+u} (v_2 − x) ,
663 * which is exactly what we'd have done if we'd calculated the same mean
664 * rate over the combined span of t + u intervals.
666 * One final wrinkle, in case that wasn't enough. There's a problem with
667 * the initial setup of an exponential moving average. Apparently
668 * (https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average)
669 * explains that we can do this better by calculating the average after k
672 * x_0 + β x_1 + β^2 x_2 + ⋯ + β^{k−1} x_{k−1}
673 * v′ = ------------------------------------------- .
674 * 1 + β + β^2 + ⋯ + β^{k−1}
676 * The numerator is our existing v/α; the denominator is (1 − β^k)/α; the
677 * factors of α cancel, and we find that v′ = v/(1 − β^k). This still
678 * holds in our situation, where k may not be an integer.
680 * To apply all of this:
682 * * we maintain the moving average v in `avg';
684 * * we maintain the total β^k in `corr'; and
686 * * we compute v′ = v/(1 − β^k) on demand up in `render_perfstats'.
689 /* Find the current time and the delta since the last time we updated.
690 * This will be the length of the current interval.
692 gettimeofday(&now
, 0); t
= tvdiff(&last_time
, &now
);
694 /* If no time at all has passed (unlikely!) then skip the rate
695 * calculation. (The moving average wouldn't be affected anyway.)
698 /* Update the moving average and the correction term, and start the next
702 rate
= (pos
- last_pos
)/t
; beta_t
= pow(1 - alpha
, t
);
703 avg
= rate
+ beta_t
*(avg
- rate
); corr
*= beta_t
;
704 ndone
+= pos
- last_pos
; last_time
= now
; last_pos
= pos
;
707 /* Advance the throbber character. */
708 throbix
++; if (!throbber
[throbix
]) throbix
= 0;
711 static void report_progress(secaddr pos
)
712 /* Update the progress variables (as `update_progress') and redraw
713 * the progress display.
715 { update_progress(pos
); progress_update(&progress
); }
717 /*----- Basic disc I/O ----------------------------------------------------*/
719 struct badblock
{ secaddr start
, end
; };
720 DEFVEC(badblock_v
, struct badblock
);
721 static badblock_v badblocks
= VEC_INIT
;
722 /* This is a list of /fake/ bad-block ranges, used to test the
723 * recovery algorithm. It's a rule that the ranges in this table
724 * mustn't overlap -- though it's OK if they abut.
727 static int compare_badblock(const void *a
, const void *b
)
728 /* A `qsort' comparison function for the fake bad-blocks list.
729 * Ranges which start earlier are sorted before rangers which start
733 const struct badblock
*ba
= a
, *bb
= b
;
735 /* Order by start sector. */
736 if (ba
->start
< bb
->start
) return (-1);
737 else if (ba
->start
> bb
->start
) return (+1);
739 /* Order by end sector as a tiebreak. This shouldn't be possible. */
740 if (ba
->end
< bb
->end
) return (-1);
741 else if (ba
->end
> bb
->end
) return (+1);
743 /* They're equal. This shouldn't be possible either. */
747 static double bad_block_delay
= 0.0, good_block_delay
= 0.0;
748 /* delay parameters for performance testing */
750 static ssize_t
read_sectors(secaddr pos
, void *buf
, secaddr want
)
751 /* Try to read WANT sectors from the input, starting with sector POS,
752 * and write the contents to BUF. Return the number of /whole
753 * sectors/ read; this will be 0 at end-of-file (though that
754 * shouldn't happen). The returned length will be smaller than WANT
755 * only if end-of-file or a system error prevents reading further.
756 * Returns -1 on a system error if that prevented us from reading
759 * This function is where the fake bad-blocks list is handled.
765 struct badblock
*bad
, *best
;
766 unsigned char *p
= buf
;
768 /* See whether the requested range intersects a bad-blocks range. */
770 /* Since the list is sorted, we use a binary search. We're looking for
771 * the earliest-starting range which /ends after/ POS. If this starts
772 * /at or before/ POS, then POS itself is a bad sector, and we should
773 * pretend an I/O error; otherwise, if the bad range /starts/ somewhere
774 * in the range we're trying to read then we must pretend a short read;
775 * and otherwise there's nothing to do.
778 /* Throughout, `best' points to the earliest-starting range we've found
779 * which (starts and) finishes after POS. Ranges with indices below LO
780 * end too early to be interesting; similarly, ranges with indices HI or
781 * above start later than POS. If we find a range which actually covers
782 * POS exactly then we'll stop early.
784 best
= 0; lo
= 0; hi
= badblocks
.n
;
786 progress_clear(&progress
);
787 printf(";; searching badblocks for %"PRIuSEC
" .. %"PRIuSEC
"\n",
791 /* Standard binary-search loop: we continue until the pointers
795 /* Try the midpoint between the two bounds. */
796 mid
= lo
+ (hi
- lo
)/2; bad
= &badblocks
.v
[mid
];
798 printf(";; try %zu (%"PRIuSEC
" .. %"PRIuSEC
")... ",
799 mid
, bad
->start
, bad
->end
);
802 /* Follow our invariant. If the range starts strictly after POS, then
803 * it's too late to overlap, so bring down HI to cover it; but it must
804 * be closer than any previous block we've found, so remember it in
805 * `best'. Similarly, if the range ends /at or before/ POS then it
806 * stops too early, so bring up LO to cover it (but otherwise forget
807 * about it because it can't affect what we're doing).
809 * If we get a match then we stop immediately and fake a bad block.
811 if (pos
< bad
->start
) { D( printf("high\n"); ) best
= bad
; hi
= mid
; }
812 else if (pos
>= bad
->end
) { D( printf("low\n"); ) lo
= mid
+ 1; }
814 D( printf("match!\n"); )
815 errno
= EIO
; sit(bad_block_delay
); return (-1);
819 /* We're done. Check to see whether the bad range starts early enough.
820 * If so, remember that we're simulating an error, apply the delay, and
821 * bamboozle the rest of the code into performing a short read.
825 printf(";; next is %"PRIuSEC
" .. %"PRIuSEC
"\n",
826 best
->start
, best
->end
);
828 if (best
&& pos
+ want
> best
->start
)
829 { want
= best
->start
- pos
; fakeerr
= EIO
; sit(bad_block_delay
); }
832 /* Try to read stuff into the buffer until we find a reason why we can't
833 * continue. Obviously we need to keep track of how much stuff we've read
834 * on previous iterations.
839 /* Read from the current file's input source. If that's a scrambled
840 * video file, then use `libdvdread'; if it's the `raw' file, then go to
841 * the block device; if it's nothing at all, then fill with zeros.
842 * Always force a seek to the right place, in case things got messed up
843 * by some previous error.
846 { errno
= 0; n
= DVDReadBlocks(vob
, pos
- file
->start
, want
, p
); }
848 if (lseek(dvdfd
, (off_t
)pos
*SECTORSZ
, SEEK_SET
) < 0)
849 bail_syserr(errno
, "failed to seek to sector %"PRIuSEC
"", pos
);
850 errno
= 0; n
= read(dvdfd
, p
, want
*SECTORSZ
);
851 if (n
>= 0) n
/= SECTORSZ
;
853 memset(p
, 0, want
*SECTORSZ
);
857 /* If we read some stuff then update the buffer pointer and lengths. If
858 * we hit end-of-file then stop. If we hit a bad sector then maybe make
859 * a note of it in the bad-sector log. On any other kind of error, just
862 if (n
> 0) { done
+= n
; pos
+= n
; p
+= n
*SECTORSZ
; want
-= n
; }
864 else if (errno
== EIO
&& errfile
) {
865 open_file_on_demand(errfile
, &errfp
, "bad-sector error log");
866 fprintf(errfp
, "%"PRIuSEC
" %"PRIuSEC
"\n", pos
, pos
+ 1);
867 check_write(errfp
, "bad-sector error log");
869 } else if (errno
!= EINTR
) break;
872 /* We made it. If we saved up a fake error, and there wasn't a real error
873 * (which should obviously take priority) then present the fake error to
874 * the caller. If there wasn't an error, then everything must have been
875 * good so impose the good-block delay -- note that a bad-block delay will
876 * already have been imposed above. Finally, return the accumulated count
877 * of sectors successfully read, or report the end-of-file or error
878 * condition as applicable.
880 if (fakeerr
&& !errno
) errno
= fakeerr
;
881 else if (done
> 0 && good_block_delay
) sit(done
*good_block_delay
);
882 return (!done
&& errno ?
-1 : done
);
885 /*----- Tracking machinery for the bad-sector algorithm -------------------*
887 * While we're probing around trying to find the end of the bad region, we'll
888 * have read some good data. We want to try to keep as much good data as we
889 * can, and avoid re-reading it because (a) it's pointless I/O work, but more
890 * importantly (b) it might not work the second time. The machinery here
891 * is for making this work properly.
893 * There are two parts to this which don't really intersect, but for
894 * convenience the tracking information for them is kept in the same
895 * `recoverybuf' structure.
897 * * The `short-range' machinery keeps track of a contiguous region of good
898 * data stored in the caller's buffer.
900 * * The `long-range' machinery keeps track of a contiguous region of good
901 * data that's beyond the range of the buffer.
905 /* Information used to keep track of where good and bad sectors are
906 * while we're trying to find the end of a region of bad sectors.
909 /* Short-range buffer tracking. */
910 unsigned char *buf
; /* pointer to the actual buffer */
911 secaddr sz
; /* size of the buffer in sectors */
912 secaddr pos
; /* sector address corresponding to
913 * the start of the buffer */
914 secaddr start
, end
; /* bounds of the live region within
915 * the buffer, as offsets in
916 * sectors from the buffer start */
918 /* Long-range tracking. */
919 secaddr good_lo
, good_hi
; /* known-good region, as absolute
920 * sector addresses */
923 static void rearrange_sectors(struct recoverybuf
*r
,
924 secaddr dest
, secaddr src
, secaddr len
)
925 /* Shuffle data about in R's buffer. Specifically, move LEN sectors
926 * starting SRC sectors from the start of the buffer to a new
927 * position DEST sectors from the start.
929 * Unsurprisingly, this is a trivial wrapper around `memmove', with
930 * some range checking thrown in; it's only used by `recovery_read_-
931 * buffer' and `find_good_sector' below.
934 assert(dest
+ len
<= r
->sz
); assert(src
+ len
<= r
->sz
);
935 memmove(r
->buf
+ dest
*SECTORSZ
, r
->buf
+ src
*SECTORSZ
, len
*SECTORSZ
);
939 static PRINTF_LIKE(2, 3)
940 void show_recovery_buffer_map(const struct recoverybuf
*r
,
941 const char *what
, ...)
942 /* Dump a simple visualization of the short-range tracking state. */
947 progress_clear(&progress
);
948 printf(";; recovery buffer (");
951 "(%"PRIuSEC
") ..%"PRIuSEC
".. "
952 "[%"PRIuSEC
" ..%"PRIuSEC
".. %"PRIuSEC
"] "
953 "..%"PRIuSEC
".. (%"PRIuSEC
")\n",
955 r
->pos
+ r
->start
, r
->end
- r
->start
, r
->pos
+ r
->end
,
956 r
->sz
- r
->end
, r
->pos
+ r
->sz
);
958 assert(r
->start
<= r
->end
);
959 assert(r
->end
<= r
->sz
);
963 static ssize_t
recovery_read_sectors(struct recoverybuf
*r
,
964 secaddr pos
, secaddr off
, secaddr want
)
965 /* Try to read WANT sectors starting at sector address POS from the
966 * current file into R's buffer, at offset OFF sectors from the start
967 * of the buffer. Return the number of sectors read, zero if at end
968 * of file, or -1 in the event of a system error.
970 * This is a trivial wrapper around `read_sectors' with some
971 * additional range checking, used only by `recovery_read_buffer'
977 assert(off
<= r
->sz
); assert(want
<= r
->sz
- off
);
978 assert(pos
== r
->pos
+ off
);
979 n
= read_sectors(pos
, r
->buf
+ off
*SECTORSZ
, want
);
983 static ssize_t
recovery_read_buffer(struct recoverybuf
*r
,
984 secaddr pos
, secaddr want
)
985 /* Try to read WANT sectors, starting at sector address POS, from the
986 * current file into the buffer R, returning a count of the number of
987 * sectors read, or 0 if at end of file, or -1 in the case of a
988 * system error, as for `read_sectors'. The data will end up
989 * /somewhere/ in the buffer, but not necessarily at the start.
992 secaddr diff
, pp
, nn
;
995 /* This is the main piece of the short-range tracking machinery. It's
996 * rather complicated, so hold on tight. (It's much simpler -- and less
997 * broken -- than earlier versions were, though.)
1001 progress_clear(&progress
);
1002 show_recovery_buffer_map(r
, "begin(%"PRIuSEC
", %"PRIuSEC
")", pos
, want
);
1005 /* The first order of business is to make space in the buffer for this new
1006 * data. We therefore start with a case analysis.
1009 /* The new position is before the current start of the buffer, so we have
1010 * no choice but to decrease the buffer position, which will involve
1011 * shifting the existing material upwards.
1014 /* Determine how far up we'll need to shift. */
1015 diff
= r
->pos
- pos
;
1017 if (r
->start
+ diff
>= r
->sz
) {
1018 /* The material that's currently in the buffer would be completely
1019 * shifted off the end, so we have no choice but to discard it
1023 r
->pos
= pos
; r
->start
= r
->end
= 0;
1025 show_recovery_buffer_map(r
, "cleared; shift up by %"PRIuSEC
"", diff
);
1028 /* Some of the material in the buffer will still be there. We might
1029 * lose some stuff off the end: start by throwing that away, and then
1030 * whatever's left can be moved easily.
1033 if (r
->end
+ diff
> r
->sz
) r
->end
= r
->sz
- diff
;
1034 rearrange_sectors(r
, r
->start
+ diff
, r
->start
, r
->end
- r
->start
);
1035 r
->pos
-= diff
; r
->start
+= diff
; r
->end
+= diff
;
1037 show_recovery_buffer_map(r
, "shifted up by %"PRIuSEC
"", diff
);
1040 } else if (pos
> r
->pos
+ r
->end
) {
1041 /* The new position is strictly beyond the old region. We /could/ maybe
1042 * keep this material, but it turns out to be better not to. To keep it,
1043 * we'd have to also read the stuff that's in between the end of the old
1044 * region and the start of the new one, and that might contain bad
1045 * sectors which the caller is specifically trying to skip. We just
1046 * discard the entire region here so as not to subvert the caller's
1050 r
->pos
= pos
; r
->start
= r
->end
= 0;
1052 show_recovery_buffer_map(r
, "cleared; beyond previous region");
1054 } else if (pos
+ want
> r
->pos
+ r
->sz
) {
1055 /* The requested range of sectors extends beyond the region currently
1056 * covered by the buffer. We must therefore increase the buffer position
1057 * which will involve shifting the existing material downwards.
1060 /* Determine how far down we'll need to shift. */
1061 diff
= (pos
+ want
) - (r
->pos
+ r
->sz
);
1063 if (r
->end
<= diff
) {
1064 /* The material that's currently in the buffer would be completely
1065 * shifted off the beginning, so we have no choice but to discard it
1069 r
->pos
= pos
; r
->start
= r
->end
= 0;
1071 show_recovery_buffer_map(r
, "cleared; shift down by %"PRIuSEC
"", diff
);
1074 /* Some of the material in the buffer will still be there. We might
1075 * lose some stuff off the beginning: start by throwing that away, and
1076 * then whatever's left can be moved easily.
1079 if (r
->start
< diff
) r
->start
= diff
;
1080 rearrange_sectors(r
, r
->start
- diff
, r
->start
, r
->end
- r
->start
);
1081 r
->pos
+= diff
; r
->start
-= diff
; r
->end
-= diff
;
1083 show_recovery_buffer_map(r
, "shifted down by %"PRIuSEC
"", diff
);
1088 /* We now have space in the buffer in which to put the new material.
1089 * However, the buffer already contains some stuff. We may need to read
1090 * some data from the input file into an area before the existing
1091 * material, or into an area following the existing stuff, or both, or
1092 * (possibly) neither.
1095 if (pos
< r
->pos
+ r
->start
) {
1096 /* The requested position is before the current good material, so we'll
1097 * need to read some stuff there.
1100 /* Determine the place in the buffer where this data will be placed, and
1101 * how long it will need to be. Try to extend it all the way to the
1102 * existing region even if this is more than the caller wants, because it
1103 * will mean that we can join it onto the existing region rather than
1104 * having to decide which of two disconnected parts to throw away.
1106 pp
= pos
- r
->pos
; nn
= r
->start
- pp
;
1108 /* Read the data. */
1110 printf(";; read low (%"PRIuSEC
"@%"PRIuSEC
", %"PRIuSEC
")", pos
, pp
, nn
);
1113 n
= recovery_read_sectors(r
, pos
, pp
, nn
);
1115 printf(" -> %zd\n", n
);
1118 /* See whether it worked. */
1120 /* We didn't get everything we wanted. */
1122 /* If we got more than the caller asked for then technically this is
1123 * good; but there must be some problem lurking up ahead, and the
1124 * caller will want to skip past that. So we don't update the tracking
1125 * information to reflect our new data; even though this /looks/ like a
1126 * success, it isn't really.
1128 if (n
>= 0 && n
> want
) n
= want
;
1134 /* Extend the region to include the new piece. */
1137 show_recovery_buffer_map(r
, "joined new region");
1141 if (pos
+ want
> r
->pos
+ r
->end
) {
1142 /* The requested region extends beyond the current region, so we'll need
1143 * to read some stuff there.
1146 /* Determine the place in the buffer where this data will be placed, and
1147 * how long it will need to be. Note that pos <= r->pos + r->end, so
1148 * there won't be a gap between the old good region and the material
1149 * we're trying to read.
1151 pp
= r
->end
; nn
= (pos
+ want
) - (r
->pos
+ r
->end
);
1153 /* Read the data. */
1155 printf(";; read high (%"PRIuSEC
"@%"PRIuSEC
", %"PRIuSEC
")",
1156 r
->pos
+ pp
, pp
, nn
);
1159 n
= recovery_read_sectors(r
, r
->pos
+ pp
, pp
, nn
);
1161 printf(" -> %zd\n", n
);
1164 /* See whether it worked. */
1166 /* We read something, so add it onto the existing region. */
1170 show_recovery_buffer_map(r
, "joined new region");
1175 /* Work out the return value to pass back to the caller. The newly read
1176 * material has been merged with the existing region (the case where we
1177 * didn't manage to join the two together has been handled already), so we
1178 * can easily work out how much stuff is available by looking at the
1179 * tracking information. It only remains to bound the region size by the
1182 n
= r
->pos
+ r
->end
- pos
;
1183 if (!n
&& want
) n
= -1;
1184 else if (n
> want
) n
= want
;
1189 show_recovery_buffer_map(r
, "done; return %zd", n
);
1194 static ssize_t
recovery_read_multiple(struct recoverybuf
*r
,
1195 secaddr pos
, secaddr want
)
1196 /* Try to read WANT sectors, starting at sector address POS, from the
1197 * current file, returning a count of the number of sectors read, or
1198 * 0 if at end of file, or -1 in the case of a system error, as for
1199 * `read_sectors'. Some data might end up in R's buffer, but if WANT
1200 * is larger than R->sz then a lot will be just thrown away.
1202 * This is only used by `recovery_read' below.
1206 secaddr skip
, want0
= want
;
1208 /* If the request is larger than the buffer, then we start at the /end/ and
1209 * work backwards. If we encounter a bad sector while we're doing this,
1210 * then we report a short read as far as the bad sector: the idea is to
1211 * find the /latest/ bad sector we can. The caller will want to skip past
1212 * the bad sector, so the fact that we implicitly lied about the earlier
1213 * data as being `good' won't matter.
1216 while (want
> r
->sz
) {
1217 /* There's (strictly!) more than a buffer's worth. Fill the buffer with
1218 * stuff and reduce the requested size.
1221 skip
= want
- r
->sz
;
1222 n
= recovery_read_buffer(r
, pos
+ skip
, r
->sz
);
1224 /* If it failed, then we always return a positive result, because we're
1225 * pretending we managed to read all of the (nonempty) preceding
1228 if (n
< r
->sz
) return (skip
+ (n
>= 0 ? n
: 0));
1230 /* Cross off a buffer's worth and go around again. */
1234 /* Read the last piece. If it fails or comes up short, then we don't need
1235 * to mess with the return code this time.
1237 n
= recovery_read_buffer(r
, pos
, want
);
1238 if (n
< 0 || n
< want
) return (n
);
1240 /* It all worked. Return the full original amount requested. */
1244 static ssize_t
recovery_read(struct recoverybuf
*r
,
1245 secaddr pos
, secaddr want
)
1246 /* Try to read WANT sectors, starting at sector address POS, from the
1247 * current file, returning a count of the number of
1248 * sectors read, or 0 if at end of file, or -1 in the case of a
1249 * system error, as for `read_sectors'. Some data might end up in
1250 * R's buffer, but if WANT is larger than R->sz then a lot will be
1254 secaddr lo
= pos
, hi
= pos
+ want
, span
; /* calculate the request bounds */
1257 /* This is the main piece of the long-range tracking machinery.
1258 * Fortunately, it's much simpler than the short-range stuff that we've
1262 if (hi
< r
->good_lo
|| lo
> r
->good_hi
) {
1263 /* The requested region doesn't abut or overlap with the existing good
1264 * region, so it's no good to us. Just read the requested region; if it
1265 * worked at all, then replace the current known-good region with the
1266 * region that was successfully read.
1269 n
= recovery_read_multiple(r
, lo
, hi
- lo
);
1270 if (n
> 0) { r
->good_lo
= lo
; r
->good_hi
= lo
+ n
; }
1274 if (hi
> r
->good_hi
) {
1275 /* The requested region ends later than the current known-good region.
1276 * Read the missing piece. We're doing this first so that we find later
1280 span
= hi
- r
->good_hi
;
1281 n
= recovery_read_multiple(r
, r
->good_hi
, span
);
1283 /* If we read anything at all, then extend the known-good region. */
1284 if (n
> 0) r
->good_hi
+= n
;
1286 /* If we didn't read everything we wanted, then report this as a short
1287 * read (so including some nonempty portion of the known-good region).
1289 if (n
< 0 || n
< span
) return (r
->good_hi
- lo
);
1292 if (lo
< r
->good_lo
) {
1293 /* The requested region begins earlier than the known-good region. */
1295 span
= r
->good_lo
- lo
;
1296 n
= recovery_read_multiple(r
, lo
, span
);
1298 /* If we read everything we wanted, then extend the known-good region.
1299 * Otherwise, we're better off keeping the stuff after the bad block.
1301 if (n
== span
) r
->good_lo
= lo
;
1305 /* Everything read OK, and we've extended the known-good region to cover
1306 * the requested region. So return an appropriate code by consulting the
1307 * new known-good region.
1309 n
= r
->good_hi
- pos
; if (n
> want
) n
= want
;
1310 if (!n
) { errno
= EIO
; n
= -1; }
1314 /*----- Skipping past regions of bad sectors ------------------------------*/
1316 static double clear_factor
= 0.5; /* proportion of clear sectors needed */
1317 static secaddr clear_min
= 1, clear_max
= SECLIMIT
; /* absolute bounds */
1318 static double step_factor
= 2.0; /* factor for how far to look ahead */
1319 static secaddr step_min
= 1, step_max
= 0; /* and absolute bounds */
1321 static void recovered(secaddr bad_lo
, secaddr bad_hi
)
1322 /* Do all of the things that are necessary when a region of bad
1323 * sectors has been found between BAD_LO (inclusive) and BAD_HI
1329 /* Remove the progress display temporarily. */
1330 progress_clear(&progress
);
1332 /* Print a message into the permanent output log. */
1333 if (!file
|| id_kind(file
->id
) == RAW
)
1334 moan("skipping %"PRIuSEC
" bad sectors (%"PRIuSEC
" .. %"PRIuSEC
")",
1335 bad_hi
- bad_lo
, bad_lo
, bad_hi
);
1337 store_filename(fn
, file
->id
);
1338 moan("skipping %"PRIuSEC
" bad sectors (%"PRIuSEC
" .. %"PRIuSEC
"; "
1339 "`%s' %"PRIuSEC
" .. %"PRIuSEC
" of %"PRIuSEC
")",
1340 bad_hi
- bad_lo
, bad_lo
, bad_hi
,
1341 fn
, bad_lo
- file
->start
, bad_hi
- file
->start
,
1342 file
->end
- file
->start
);
1346 /* The user requested a map of the skipped regions, so write an entry. */
1348 /* Open the file, if it's not open already. */
1349 open_file_on_demand(mapfile
, &mapfp
, "bad-sector region map");
1351 /* Write the sector range. */
1352 fprintf(mapfp
, "%"PRIuSEC
" %"PRIuSEC
" # %"PRIuSEC
" sectors",
1353 bad_lo
, bad_hi
, bad_hi
- bad_lo
);
1355 /* If we're currently reading from a file then note down the position in
1356 * the file in the comment. (Intentional bad sectors are frequently at
1357 * the start and end of titles, so this helps a reader to decide how
1360 if (file
&& id_kind(file
->id
) != RAW
)
1361 fprintf(mapfp
, "; `%s' %"PRIuSEC
" .. %"PRIuSEC
" of %"PRIuSEC
"",
1362 fn
, bad_lo
- file
->start
, bad_hi
- file
->start
,
1363 file
->end
- file
->start
);
1365 /* Done. Flush the output to the file so that we don't lose it if we
1369 check_write(mapfp
, "bad-sector region map");
1372 /* Adjust the position in our output file to skip past the bad region.
1373 * (This avoids overwriting anything that was there already, which is
1374 * almost certainly less wrong than anything we could come up with here.)
1376 if (lseek(outfd
, (off_t
)(bad_hi
- bad_lo
)*SECTORSZ
, SEEK_CUR
) < 0)
1377 bail_syserr(errno
, "failed to seek past bad sectors");
1379 /* Remove our notice now that we're no longer messing about with bad
1380 * sectors, and reinstate the progress display.
1382 progress_removeitem(&progress
, &badblock_progress
);
1383 progress_update(&progress
);
1386 static secaddr
run_length_wanted(secaddr pos
, secaddr badlen
, secaddr end
)
1387 /* Return the number of good sectors that we want to see before
1388 * we're happy, given that we're about to try to read sector POS,
1389 * which is BADLEN sectors beyond where we found the first bad
1390 * sector, and the current region ends at sector END (i.e., this is
1391 * where the next event occurs).
1396 /* Apply the factor to BADLEN to get an initial length. */
1397 want
= ceil(clear_factor
*badlen
);
1399 /* Apply the user-configurable lower bound. */
1400 if (want
< clear_min
) want
= clear_min
;
1402 /* Cap this with the end of the region. */
1403 if (want
> end
- pos
) want
= end
- pos
;
1405 /* And apply the user-configurable upper bound. */
1406 if (clear_max
&& want
> clear_max
) want
= clear_max
;
1412 static void report_bad_blocks_progress(secaddr bad_hi
, int err
)
1413 /* Report progress while we're trying to work past a region of bad
1414 * sectors. We're about to investigate BAD_HI, and the most recent
1417 { bad_err
= err
; report_progress(bad_hi
); }
1419 static ssize_t
find_good_sector(secaddr
*pos_inout
, secaddr end
,
1420 unsigned char *buf
, secaddr sz
)
1421 /* Work out a place to resume after finding a bad sector. The
1422 * current position, where we found a problem, is in *POS_INOUT. The
1423 * current input region goes up up sector END (i.e., this is where
1424 * the next event occurs). The caller's buffer is at BUF, and can
1425 * hold SZ sectors. On exit, update *POS_INOUT to be the start of a
1426 * region of /good/ sector that we decided was worth exploring, and
1427 * return the number of sectors we've already read at that position
1428 * and left at the start of the buffer. (This number may be zero,
1429 * depending on how things work out. That doesn't mean that we hit
1432 * Altough the return value is `ssize_t', this is only to fit in with
1433 * other read functions; a negative return is not actually possible.
1436 secaddr pos
= *pos_inout
, bad_lo
, bad_hi
, good
, step
, want
;
1437 struct recoverybuf r
;
1440 /* Initial setup. Save the initial state and establish the bad-blocks
1443 bad_start
= pos
; bad_err
= errno
;
1444 badblock_progress
.render
= render_badblock_progress
;
1445 progress_additem(&progress
, &badblock_progress
);
1447 /* First, retry the `bad' sector a few times. Sometimes, with damaged
1448 * discs, this actually works. We'll try to read a full buffer, but we're
1449 * not expecting much.
1451 want
= sz
; if (want
> end
- pos
) want
= end
- pos
;
1452 for (retry
= 0; retry
< max_retries
; retry
++) {
1454 /* Show the progress report. */
1455 report_bad_blocks_progress(pos
, errno
);
1457 /* Try reading stuff. */
1458 n
= read_sectors(pos
, buf
, want
);
1460 progress_clear(&progress
);
1461 printf(";; [retry] try reading %"PRIuSEC
" .. %"PRIuSEC
" -> %zd\n",
1462 pos
, pos
+ want
, n
);
1466 /* We won! Remove the progress display, and leave a permanent message
1467 * to inform the user what happened.
1469 progress_clear(&progress
);
1470 moan("sector %"PRIuSEC
" read ok after retry", pos
);
1471 progress_removeitem(&progress
, &badblock_progress
);
1472 progress_update(&progress
);
1477 /* We're going to have to be more creative. Set up the tracking state. */
1478 r
.buf
= buf
; r
.sz
= sz
; r
.pos
= r
.start
= r
.end
= 0;
1479 r
.good_lo
= r
.good_hi
= 0;
1481 /* Set up the region bound. We know the bad area starts at POS, and that
1482 * it covers at least one sector.
1484 bad_lo
= pos
; bad_hi
= pos
+ 1;
1486 /* Second major step: try to find somewhere on the other side of the bad
1491 progress_clear(&progress
);
1492 printf(";; bounding bad-block region: "
1493 "%"PRIuSEC
" ..%"PRIuSEC
".. %"PRIuSEC
"\n",
1494 bad_lo
, bad_hi
- bad_lo
, bad_hi
);
1497 /* If our upper bound has reached all the way to the end of the input
1498 * region then there's nowhere to recover to. Set the next position to
1499 * the end of the region and return.
1501 if (bad_hi
>= end
) {
1502 progress_clear(&progress
);
1503 moan("giving up on this extent");
1504 recovered(bad_lo
, end
); *pos_inout
= end
;
1508 /* Give a progress update. */
1509 report_bad_blocks_progress(bad_hi
, errno
);
1511 /* Choose a new place to look. Apply the step factor to the size of the
1512 * current gap between the start and end of the bad region, and then
1513 * bound by the user bounds and the input-region end.
1515 * We make progress because `step' is at least 1: `step_min' is at least
1516 * 1, and bad_hi < end or we'd have already bailed.
1518 step
= (step_factor
- 1)*(bad_hi
- bad_lo
);
1519 if (step
< step_min
) step
= step_min
;
1520 if (step_max
&& step
> step_max
) step
= step_max
;
1521 step
+= bad_hi
- bad_lo
;
1522 if (step
> end
- bad_lo
) step
= end
- bad_lo
;
1524 /* Now we look at the last sector of the new interval we've just marked
1527 pos
= bad_lo
+ step
- 1;
1528 want
= run_length_wanted(pos
, step
, end
);
1529 n
= recovery_read(&r
, pos
, want
);
1531 printf(";; [bound] try reading %"PRIuSEC
" .. %"PRIuSEC
" -> %zd\n",
1532 pos
, pos
+ want
, n
);
1535 /* If everything went OK then we're done with this phase. */
1536 if (n
== want
) break;
1538 /* If it failed then extend the bad region to cover (the end of) the bad
1539 * sector which terminated the run, and go around again.
1542 bad_hi
= pos
+ n
+ 1;
1545 /* Third major step: identify exactly where the bad region ends. This is
1549 while (good
> bad_hi
) {
1551 progress_clear(&progress
);
1552 printf(";; limiting bad-block region: "
1553 "%"PRIuSEC
" ..%"PRIuSEC
".. %"PRIuSEC
" ..%"PRIuSEC
".. %"PRIuSEC
"\n",
1554 bad_lo
, bad_hi
- bad_lo
, bad_hi
, good
- bad_hi
, good
);
1557 /* Update the progress report. */
1558 report_bad_blocks_progress(bad_hi
, errno
);
1560 /* Pick a new place to try. */
1561 pos
= bad_hi
+ (good
- bad_hi
)/2; step
= pos
- bad_lo
;
1562 want
= run_length_wanted(pos
, step
, end
);
1565 n
= recovery_read(&r
, pos
, want
);
1567 printf(";; [limit] try reading %"PRIuSEC
" .. %"PRIuSEC
" -> %zd\n",
1568 pos
, pos
+ want
, n
);
1571 /* If that worked -- i.e., we got all the data we wanted -- then bring
1572 * down the `good' bound. If it failed, then bring up `bad_hi' to cover
1573 * the bad sector which terminated our read attempt.
1576 if (n
== want
) good
= pos
;
1577 else bad_hi
= pos
+ n
+ 1;
1580 /* We're done. It's time to tidy up.
1582 * One subtle point: it's possible that, as a result of retrying previous
1583 * bad blocks, that we ended up with bad_hi > good, so it's important that
1584 * we make a consistent choice between the two. I've gone with `good'
1585 * because (a) this gives us more of the original data from the disc and
1586 * (b) hopefully any marginal sectors are now in our buffer
1588 recovered(bad_lo
, good
); *pos_inout
= good
;
1590 /* Figure out how much data we can return to the caller from our buffer. */
1591 if (good
< r
.pos
+ r
.start
|| r
.pos
+ r
.end
<= good
) {
1592 /* Our new position is outside of the region covered by the short-range
1593 * tracking, so there's nothing to return.
1598 /* The new position is covered, so shuffle the data to the start of the
1599 * buffer and return as much as we can.
1602 n
= r
.pos
+ r
.end
- good
;
1603 rearrange_sectors(&r
, 0, good
- r
.pos
, n
);
1608 show_recovery_buffer_map(&r
, "returning %zd good sectors at %"PRIuSEC
"",
1614 /*----- Copying data from a single input file -----------------------------*/
1616 static void emit(secaddr start
, secaddr end
)
1617 /* Copy sectors with absolute addresses from START (inclusive) to END
1618 * (exclusive) to the output. The entire input region comes from the
1619 * same source, already established as `file'.
1622 #define BUFSECTORS 512 /* this is a megabyte */
1625 unsigned char buf
[BUFSECTORS
*SECTORSZ
];
1629 static int first_time
= 1;
1637 /* Choose an active file through which to read the source contents. We're
1638 * guaranteed that this file will do for the entire input region. We
1639 * choose the active file with the smallest index. The virtual `raw' file
1640 * which represents the underlying block device has the largest index, so
1641 * we'll always use a `.VOB' file if one is available. Looking at the
1642 * protocol suggests that the host and drive identify the per-title CSS key
1643 * by the start sector address of the `.VOB' file, so coincident files must
1644 * all use the same key. I've not encountered properly overlapping files
1647 least
= least_live();
1649 printf(";; %8"PRIuSEC
" .. %"PRIuSEC
"\n", start
, end
);
1650 for (i
= 0; i
< filetab
.n
; i
++) {
1651 if (!livep(i
)) continue;
1652 if (act
== -1) act
= i
;
1653 f
= &filetab
.v
[i
]; store_filename(fn
, f
->id
);
1654 printf(";;\t\t%8"PRIuSEC
" .. %-8"PRIuSEC
" %s\n",
1655 start
- f
->start
, end
- f
->start
, fn
);
1657 if (act
== -1) printf(";;\t\t#<no live source>\n");
1658 assert(act
== least
);
1661 /* Set the global variables up for reading from the file we decided on.
1662 * These will be primarily used by `read_sectors' and `update_progress'.
1665 /* There's nothing at all. This can happen because the kernel reported
1666 * the wrong block-device size for some reason but the filesystem has
1667 * identified files which start beyond the reported size, leaving a gap.
1672 /* There's a (possibly) virtual file. */
1674 file
= &filetab
.v
[least
];
1675 switch (id_kind(file
->id
)) {
1678 /* It's the raw device. Clear `vob' to prompt `read_sectors' to read
1679 * directly from `dvdfd'.
1686 /* It's a `.VOB' file. We read these through `libdvdread', which
1687 * handles CSS unscrambling for us.
1690 /* The first time we open a `.VOB' file, `libdvdread' wants to spray
1691 * a bunch of information about how it's getting on cracking the
1692 * title keys. This will interfere with the progress display, so
1693 * preemptively hide the display.
1695 if (first_time
) { progress_clear(&progress
); first_time
= 0; }
1697 /* Open the `.VOB' file. */
1698 vob
= DVDOpenFile(dvd
, id_title(file
->id
),
1700 ? DVD_READ_TITLE_VOBS
1701 : DVD_READ_MENU_VOBS
);
1703 bail("failed to open %s %u",
1704 id_part(file
->id
) ?
"title" : "menu",
1705 id_title(file
->id
));
1709 /* Some other kind of thing; but there shouldn't be anything else in
1710 * the file table, so there's a bug.
1717 /* If we're not reading from the raw device then add an additional progress
1718 * bar for the current file. This isn't completely pointless: having a
1719 * ready visualization for whereabouts we are in a file is valuable when we
1720 * encounter bad blocks, because regions of intentional bad blocks near the
1721 * starts and and ends of VOBs are common on discs from annoying studios.
1723 if (file
&& id_kind(file
->id
) != RAW
) {
1724 file_progress
.render
= render_file_progress
;
1725 progress_additem(&progress
, &file_progress
);
1728 /* Put the progress display back, if we took it away, and show the file
1729 * progress bar if we added one.
1731 progress_update(&progress
);
1733 /* Read the input region and copy it to the disc. */
1737 /* Decide how much we want. Fill the buffer, unless there's not enough
1740 want
= end
- pos
; if (want
> BUFSECTORS
) want
= BUFSECTORS
;
1742 /* Try to read the input. */
1743 n
= read_sectors(pos
, buf
, want
);
1746 /* It didn't work. Time to deploy the skipping-past-bad-blocks
1747 * machinery we worked so hard on. This will fill the buffer with
1748 * stuff and return a new count of how much it read.
1751 n
= find_good_sector(&pos
, end
, buf
, BUFSECTORS
);
1754 /* We made some progress. Write the stuff that we read to the output
1755 * file and update the position.
1758 carefully_write(outfd
, buf
, n
*SECTORSZ
); pos
+= n
;
1761 /* Report our new progress. */
1762 report_progress(pos
);
1765 /* Close the `libdvdread' file, if we opened one. */
1766 if (vob
) { DVDCloseFile(vob
); vob
= 0; }
1768 /* If we added a per-file progress bar, then take it away again. */
1769 if (file
&& id_kind(file
->id
) != RAW
)
1770 progress_removeitem(&progress
, &file_progress
);
1772 /* Update the progress display to report our glorious success. */
1773 progress_update(&progress
);
1778 /*----- Main program ------------------------------------------------------*/
1780 int main(int argc
, char *argv
[])
1787 secaddr start
, end
, last
;
1788 const struct event
*ev
;
1789 const char *device
, *outfile
;
1790 struct badblock
*bad
;
1794 struct buf buf
= BUF_INIT
;
1795 struct timeval tv0
, tv1
;
1796 double t
, rate
, tot
;
1797 const char *rateunit
, *totunit
;
1798 char timebuf
[TIMESTRMAX
], id_in
[MAXIDSZ
], id_out
[MAXIDSZ
];
1799 dvd_reader_t
*dvd_out
;
1801 const struct file
*file
;
1806 #define f_continue 2u
1809 #define f_checkid 16u
1811 #define f_write 256u
1815 /* First up, handle the command-line options. */
1817 opt
= getopt(argc
, argv
, "hB:E:FR:X:b:cir:s"); if (opt
< 0) break;
1821 case 'h': usage(stderr
); exit(0);
1823 /* `-B PARAM=VALUE[,...]': Setting internal parameters. */
1826 /* Set up a cursor into the parameter string. */
1829 #define SKIP_PREFIX(s) \
1830 (STRNCMP(p, ==, s "=", sizeof(s)) && (p += sizeof(s), 1))
1831 /* If the text at P matches `S=' then advance P past that and
1832 * evaluate nonzero; otherwise evaluate zero.
1837 if (SKIP_PREFIX("cf"))
1838 clear_factor
= parse_float(&p
, PNF_JUNK
, 0, DBL_MAX
,
1841 else if (SKIP_PREFIX("cmin"))
1842 clear_min
= parse_int(&p
, PNF_JUNK
, 1, SECLIMIT
,
1845 else if (SKIP_PREFIX("cmax"))
1846 clear_max
= parse_int(&p
, PNF_JUNK
, 1, SECLIMIT
,
1849 else if (SKIP_PREFIX("sf"))
1850 step_factor
= parse_float(&p
, PNF_JUNK
, 0, DBL_MAX
,
1853 else if (SKIP_PREFIX("smin"))
1854 step_min
= parse_int(&p
, PNF_JUNK
, 1, SECLIMIT
- 1,
1857 else if (SKIP_PREFIX("smax"))
1858 step_max
= parse_int(&p
, PNF_JUNK
, 1, SECLIMIT
- 1,
1861 else if (SKIP_PREFIX("retry"))
1862 max_retries
= parse_int(&p
, PNF_JUNK
, 0, INT_MAX
, "retries");
1864 else if (SKIP_PREFIX("alpha"))
1865 alpha
= parse_float(&p
, PNF_JUNK
, 0, 1, "average decay factor");
1867 else if (SKIP_PREFIX("_badwait"))
1868 bad_block_delay
= parse_float(&p
, PNF_JUNK
, 0, DBL_MAX
,
1871 else if (SKIP_PREFIX("_blkwait"))
1872 good_block_delay
= parse_float(&p
, PNF_JUNK
, 0, DBL_MAX
,
1873 "good block delay");
1876 bail("unknown bad blocks parameter `%s'", p
);
1878 /* If we're now at the end of the string then we're done. */
1881 /* We're not done yet, so there should now be a comma and another
1882 * parameter setting.
1884 if (*p
!= ',') bail("unexpected junk in parameters");
1891 /* `-E FILE' (undocumented): Log the bad sectors we encountered to
1894 case 'E': errfile
= optarg
; break;
1896 /* `-F' (undocumented): Hack for fixing up images that were broken by
1897 * an old early-stop bug.
1899 case 'F': f
|= f_fixup
; break;
1901 /* `-R FILE': Read ranges to retry from FILE. Retry ranges are
1902 * converted into `EV_WRITE' and `EV_STOP' events.
1905 fp
= fopen(optarg
, "r");
1907 bail_syserr(errno
, "failed to open ranges file `%s'", optarg
);
1909 /* We're going to try to coalesce adjacent ranges from the file.
1910 * When we found a region to skip, we'd have stopped at the a file
1911 * boundary, and possibly restarted again immediately afterwards,
1912 * resulting in two adjacent regions in the file. To do that, and
1913 * also to police the restriction that ranges occur in ascending
1914 * order, we keep track of the upper bound for the most recent range
1915 * -- but there isn't one yet, so we use a sentinel value.
1920 /* Read a line from the buffer. If there's nothing left then we're
1923 buf_rewind(&buf
); if (read_line(fp
, &buf
)) break;
1925 /* Increment the line counter and establish a cursor. */
1928 /* Skip initial whitespace. */
1929 while (ISSPACE(*p
)) p
++;
1931 /* If this is a comment then ignore it and go round again. */
1932 if (!*p
|| *p
== '#') continue;
1934 /* Parse the range. Check that the ranges are coming out in
1937 if (parse_range(p
, 0, &start
, &end
) ||
1938 (last
<= SECLIMIT
&& start
< last
))
1939 bail("bad range `%s' at `%s' line %zu", buf
.p
, optarg
, i
);
1941 /* Ignore empty ranges: this is important (see below where we sort
1942 * the event queue). If this abuts the previous range then just
1943 * overwrite the previous end position. Otherwise, write a new
1948 eventq
.v
[eventq
.n
- 1].pos
= end
;
1950 put_event(EV_WRITE
, 0, start
);
1951 put_event(EV_STOP
, 0, end
);
1957 /* Check for read errors. */
1959 bail_syserr(errno
, "failed to read ranges file `%s'", optarg
);
1963 /* `-X FILE' (undocumented): Read ranges of bad-blocks from FILE to
1964 * establish fake bad blocks: see `read_sectors' above for the details.
1966 * This is very similar to the `-R' option above, except that it
1967 * doesn't do the range coalescing thing.
1970 fp
= fopen(optarg
, "r");
1972 bail_syserr(errno
, "failed to open bad-blocks file `%s'", optarg
);
1975 buf_rewind(&buf
); if (read_line(fp
, &buf
)) break;
1977 while (ISSPACE(*p
)) p
++;
1978 if (!*p
|| *p
== '#') continue;
1979 if (parse_range(p
, 0, &start
, &end
) ||
1980 (last
<= SECLIMIT
&& start
< last
))
1981 bail("bad range `%s' at `%s' line %zu", buf
.p
, optarg
, i
);
1983 VEC_PUSH(bad
, &badblocks
);
1984 bad
->start
= start
; bad
->end
= end
;
1988 bail_syserr(errno
, "failed to read bad-blocks file `%s'", optarg
);
1991 /* Log regions skipped because of bad blocks to a file. */
1993 if (mapfile
) bail("can't have multiple map files");
1997 /* `-c': Continue copying where we left off last time. */
1998 case 'c': f
|= f_continue
; break;
2000 /* `-i': Check that we're copying from the right disc. */
2001 case 'i': f
|= f_checkid
; break;
2003 /* `-r [START]-[END]': Manually provide a range of sectors to retry. */
2005 start
= 0; end
= -1; f
|= f_retry
;
2006 if (parse_range(optarg
, PRF_HYPHEN
, &start
, &end
))
2007 bail("bad range `%s'", optarg
);
2009 /* Again, ignore empty ranges. */
2010 put_event(EV_WRITE
, 0, start
);
2011 if (end
<= SECLIMIT
) put_event(EV_STOP
, 0, end
);
2015 /* `-s': Print statistics at the end. */
2016 case 's': f
|= f_stats
; break;
2018 /* Anything else is an error. */
2019 default: f
|= f_bogus
; break;
2023 /* We expect two arguments. Check this. Complain about bad usage if we
2024 * have bad arguments or options.
2026 if (argc
- optind
!= 2) f
|= f_bogus
;
2027 if (f
&f_bogus
) { usage(stderr
); exit(2); }
2028 device
= argv
[optind
]; outfile
= argv
[optind
+ 1];
2030 /* If there are fake bad blocks (the `-X' option) then sort the list
2031 * because `read_sectors' wants to use a binary search.
2034 qsort(badblocks
.v
, badblocks
.n
, sizeof(struct badblock
),
2037 printf(";; fake bad blocks:\n");
2038 for (i
= 0; i
< badblocks
.n
; i
++)
2039 printf(";;\t%8"PRIuSEC
" .. %"PRIuSEC
"\n",
2040 badblocks
.v
[i
].start
, badblocks
.v
[i
].end
);
2044 /* Prepare to display progress information. */
2045 setlocale(LC_ALL
, "");
2046 progress_init(&progress
);
2048 /* Open the input device. (This may pop up a notice if there's nothing in
2051 if (open_dvd(device
, O_RDONLY
, &dvdfd
, &dvd
)) exit(2);
2053 /* Determine the size of the input device and check the sector size. */
2054 blksz
= SECTORSZ
; volsz
= device_size(dvdfd
, device
, &blksz
);
2055 if (blksz
!= SECTORSZ
)
2056 bail("device `%s' block size %d /= %d", device
, blksz
, SECTORSZ
);
2058 bail("device `%s' volume size %"PRIu64
" not a multiple of %d",
2059 device
, volsz
, SECTORSZ
);
2061 /* Maybe check that we're copying from the right disc. This is intended to
2062 * help avoid image corruption by from the wrong disc, but it obviously
2063 * only works if the output file is mostly there.
2066 if (open_dvd(outfile
, O_RDONLY
, 0, &dvd_out
)) exit(2);
2067 if (dvd_id(id_in
, dvd
, DIF_MUSTIFOHASH
, device
) ||
2068 dvd_id(id_out
, dvd_out
, DIF_MUSTIFOHASH
, device
))
2070 if (STRCMP(id_in
, !=, id_out
))
2071 bail("DVD id mismatch: input `%s' is `%s'; output `%s' is `%s'",
2072 device
, id_in
, outfile
, id_out
);
2075 /* Open the output file. */
2076 outfd
= open(outfile
, O_WRONLY
| O_CREAT
, 0666);
2078 bail_syserr(errno
, "failed to create output file `%s'", outfile
);
2081 /* If we're continuing from where we left off, then find out where that
2082 * was and make a note to copy from there to the end of the disc. Note
2083 * that we're not relying on this position: in particular, it might not
2084 * even be sector-aligned (in which case we'll ignore the final partial
2085 * sector). We'll seek to the right place again when we start writing.
2088 off
= lseek(outfd
, 0, SEEK_END
);
2090 bail_syserr(errno
, "failed to seek to end of output file `%s'",
2092 put_event(EV_WRITE
, 0, off
/SECTORSZ
); f
|= f_retry
;
2095 if (!(f
&(f_retry
| f_fixup
))) {
2096 /* If there are no ranges to retry and we're not fixing an ancient early-
2097 * stop bug, then there's no range to retry and we should just copy
2101 put_event(EV_WRITE
, 0, 0);
2104 /* Now it's time to figure out what the input looks like. Work through the
2105 * titlesets in order, mapping out where the video-object files are. We
2106 * could figure out how many there are properly, but it's fast enough just
2107 * to try everything. That's the menu only for the special titleset 0, and
2108 * menu and titles for the remaining titlesets 1 up to 99.
2111 for (i
= 1; i
< 100; i
++) {
2116 /* Make a final virtual file for the raw device. (See `emit', which
2117 * assumes that this is the last entry in the file table.) Check that we
2118 * don't have more files than we expect, because the bitmap table has fixed
2121 put_file(mkident(RAW
, 0, 0), 0, volsz
/SECTORSZ
);
2122 assert(filetab
.n
<= MAXFILES
);
2124 /* Find an upper limit for what we're supposed to copy. Since the `RAW'
2125 * entry covers the reported size of the input device, this ought to cover
2128 for (i
= 0, limit
= 0; i
< filetab
.n
; i
++)
2129 if (filetab
.v
[i
].end
> limit
) limit
= filetab
.v
[i
].end
;
2131 printf("\n;; files:\n");
2132 for (i
= 0; i
< filetab
.n
; i
++) {
2133 file
= &filetab
.v
[i
];
2134 store_filename(fn
, file
->id
);
2135 printf(";;\t%8"PRIuSEC
" .. %-8"PRIuSEC
" %s\n",
2136 file
->start
, file
->end
, fn
);
2140 /* Sort the event list.
2142 * The event-code ordering is important here.
2144 * * `EV_STOP' sorts /before/ `EV_WRITE'. If we have two abutting ranges
2145 * to retry, then we should stop at the end of the first, and then
2146 * immediately start again. If empty ranges were permitted then we'd
2147 * stop writing and /then/ start, continuing forever, which is clearly
2150 * * `EV_BEGIN' sorts before `EV_END'. If we have empty files then we
2151 * should set the bit that indicates that it's started, and then clear
2152 * it, in that order. If we have abutting files, then we'll just both
2153 * bits for an instant, but that's not a problem.
2155 qsort(eventq
.v
, eventq
.n
, sizeof(struct event
), compare_event
);
2157 /* Check that the event list is well-formed. We start out at the
2158 * beginning, not writing anything.
2160 for (i
= 0, f
&= ~f_write
, start
= 0; i
< eventq
.n
; i
++) {
2165 /* Start writing. We shouldn't be writing yet! */
2168 bail("overlapping ranges: range from %"PRIuSEC
" "
2169 "still open at %"PRIuSEC
"",
2171 f
|= f_write
; start
= ev
->pos
;
2175 /* Stop writing. Make a note that we've done this. */
2182 dump_eventq("initial");
2185 /* Now we make a second pass over the event queue to fix it up. Also
2186 * count up how much work we'll be doing so that we can report progress.
2188 for (i
= 0, f
&= ~f_write
, start
= last
= 0; i
< eventq
.n
; i
++) {
2191 /* If we're supposed to start writing then make a note of the start
2192 * position. We'll want this to count up how much work we're doing. The
2193 * start position of the final range is also used by the logic below that
2194 * determines the progress display.
2196 if (ev
->ev
== EV_WRITE
) { start
= ev
->pos
; f
|= f_write
; }
2198 /* If this event position is past our final limit then stop. Nothing
2199 * beyond here can possibly be interesting. (Since `EV_WRITE' sorts
2200 * before other events, we will notice an `EV_WRITE' exactly at the limit
2201 * sector, but not any other kind of event.)
2203 if (ev
->pos
>= limit
) break;
2205 /* If we're supposed to stop writing here, then add the size of the
2206 * most recent range onto our running total.
2208 if (ev
->ev
== EV_STOP
) { nsectors
+= ev
->pos
- start
; f
&= ~f_write
; }
2210 /* If we're fixing up images affected by the old early-stop bug, then
2211 * remember this position.
2213 if (f
&f_fixup
) last
= ev
->pos
;
2216 /* Truncate the event queue at the point we reached the sector limit. */
2219 dump_eventq("trimmed");
2222 /* Finally, the early-stop bug fix.
2224 * The bug was caused by a broken version of the event-queue truncation
2225 * logic: it trimmed the event queue, but didn't add a final event at the
2226 * file limit. The effect was that the interval between the last event --
2227 * likely `EV_END' for a VOB file -- and the overall end of the disc didn't
2228 * get copied. We address this by starting to write at the position of
2232 put_event(EV_WRITE
, 0, last
);
2236 /* If we're still writing then avoid the early-end bug by adding an
2237 * `EV_STOP' event at the limit position. Include this range in the sector
2241 nsectors
+= limit
- start
;
2242 put_event(EV_STOP
, 0, limit
);
2245 dump_eventq("final");
2248 /* Set up the main progress display.
2250 * If we're copying a single region from somewhere to the end of the disc
2251 * then it seems more sensible to use a single progress bar for both. If
2252 * we're reading multiple ranges, maybe because we're retrying bad blocks,
2253 * then it's better to have separate bars for how much actual copying we've
2254 * done, and which part of the disc we're currently working on.
2256 copy_progress
.render
= render_copy_progress
;
2257 progress_additem(&progress
, ©_progress
);
2258 if (nsectors
== limit
- start
)
2259 { ndone
= start
; nsectors
= limit
; }
2261 disc_progress
.render
= render_disc_progress
;
2262 progress_additem(&progress
, &disc_progress
);
2265 /* If we're producing overall statistics then make a note of the current
2268 if (f
&f_stats
) gettimeofday(&tv0
, 0);
2270 /* We're now ready to start our sweep through the disc. */
2272 printf("\n;; event sweep:\n");
2275 /* We start at the beginning of the disc, and the start of the event queue,
2276 * not writing. We'll advance through the events one by one.
2278 for (pos
= 0, i
= 0, f
&= ~f_write
; i
< eventq
.n
; i
++) {
2280 /* Get the next event. */
2283 /* If there's a nonempty range between here and the previous event then
2284 * we need to process this.
2286 if (ev
->pos
> pos
) {
2288 /* If we're writing then copy the interval from the previous event to
2289 * here to the output.
2291 if (f
&f_write
) emit(pos
, ev
->pos
);
2293 /* Advance the current position now that the output is up-to-date. */
2297 progress_clear(&progress
);
2302 /* Decide what to action to take in response to the event. */
2306 /* A file has started. Set the appropriate bit in the active-files
2311 store_filename(fn
, filetab
.v
[ev
->file
].id
);
2312 progress_clear(&progress
);
2313 printf(";; %8"PRIuSEC
": begin `%s'\n", pos
, fn
);
2318 /* We're supposed to start writing. */
2320 /* Note the current time and position for the progress display. */
2321 gettimeofday(&last_time
, 0); last_pos
= pos
;
2323 /* Seek to the right place in the output file. */
2324 if (lseek(outfd
, (off_t
)ev
->pos
*SECTORSZ
, SEEK_SET
) < 0)
2326 "failed to seek to resume position "
2327 "(sector %"PRIuSEC
") in output file `%s'",
2330 /* Engage the write head. */
2334 progress_clear(&progress
);
2335 printf(";; %8"PRIuSEC
": begin write\n", pos
);
2340 /* We're supposed to stop writing. Disengage the write head. */
2344 progress_clear(&progress
);
2345 printf(";; %8"PRIuSEC
": end write\n", pos
);
2350 /* We've found the end of a file. Clear its bit in the table. */
2352 clear_live(ev
->file
);
2354 store_filename(fn
, filetab
.v
[ev
->file
].id
);
2355 progress_clear(&progress
);
2356 printf(";; %8"PRIuSEC
": end `%s'\n", pos
, fn
);
2360 /* Something else. Clearly a bug. */
2365 /* Take down the progress display because we're done. */
2366 progress_clear(&progress
);
2368 /* Set the output file length correctly. */
2369 if (ftruncate(outfd
, (off_t
)limit
*SECTORSZ
) < 0)
2370 bail_syserr(errno
, "failed to set output file `%s' length", outfile
);
2372 /* Report overall statistics. */
2374 gettimeofday(&tv1
, 0); t
= tvdiff(&tv0
, &tv1
);
2375 if (nsectors
== limit
) { ndone
-= start
; nsectors
-= start
; }
2376 tot
= scale_bytes((double)nsectors
*SECTORSZ
, &totunit
);
2377 rate
= scale_bytes((double)nsectors
*SECTORSZ
/t
, &rateunit
);
2378 moan("all done: %.1f %sB in %s -- %.1f %sB/s",
2379 tot
, totunit
, fmttime(t
, timebuf
), rate
, rateunit
);
2383 if (dvd
) DVDClose(dvd
);
2384 if (dvdfd
>= 0) close(dvdfd
);
2385 if (outfd
>= 0) close(outfd
);
2386 carefully_fclose(mapfp
, "bad-sector region map");
2387 carefully_fclose(errfp
, "bad-sector error log");
2388 progress_free(&progress
);
2400 /*----- That's all, folks -------------------------------------------------*/