358377060081184a7a6ffd2abae248c5fdb86e27
[dvdrip] / dvd-sector-copy.c
1 /* -*-c-*-
2 *
3 * Make an unscrambled copy of a DVD.
4 *
5 * (c) 2022 Mark Wooding
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the DVD ripping toolset.
11 *
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.
16 *
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
20 * for more details.
21 *
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/>.
24 */
25
26 /*----- Header files ------------------------------------------------------*/
27
28 #include "lib.h"
29
30 /*----- Program usage summary ---------------------------------------------*/
31
32 static void usage(FILE *fp)
33 {
34 fprintf(fp,
35 "usage: %s [-ci] [-B PARAM=VALUE,...] [-R MAP]\n"
36 "\t[-b OUTMAP] [-r [START]-[END]] DEVICE OUTFILE\n",
37 prog);
38 }
39
40 /*----- Random utilities --------------------------------------------------*/
41
42 #define PRF_HYPHEN 1u
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.
49 *
50 * The acceptable syntax depends on the flags.
51 *
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
55 * left unchanged.
56 *
57 * * The default syntax matches what's written to the bad-sector
58 * output files. It accepts `START END [# COMMENT]'.
59 */
60 {
61 char *q;
62 int err, rc;
63 unsigned long start, end;
64
65 /* Save any existing error code. */
66 err = errno;
67
68 /* Parse the start address. */
69 if (ISDIGIT(*p)) {
70 /* We found a digit: this is a good start. Convert the integer, check
71 * that it's in range, save it.
72 */
73
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. */
79
80 rc = -1; goto end;
81 } else {
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.
85 */
86
87 start = 0;
88 }
89
90 /* Parse the delimiter. */
91 if (f&PRF_HYPHEN) {
92 if (*p != '-') { rc = -1; goto end; }
93 p++;
94 } else {
95 if (!ISSPACE(*p)) { rc = -1; goto end; }
96 do p++; while (ISSPACE(*p));
97 }
98
99 /* Parse the end address. */
100 if (ISDIGIT(*p)) {
101 /* We found a digit. Parse the integer and check that it's strictly
102 * larger than the start address.
103 */
104
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. */
110
111 rc = -1; goto end;
112 }
113
114 /* In the file syntax, we're now allowed whitespace, so skip past that. */
115 if (!(f&PRF_HYPHEN)) while (ISSPACE(*p)) p++;
116
117 /* Check that there's nothing else. The file syntax allows a trailing
118 * comment here, but the command-line syntax doesn't.
119 */
120 if (*p && ((f&PRF_HYPHEN) || *p != '#')) { rc = -1; goto end; }
121
122 /* All done! */
123 rc = 0;
124 end:
125 errno = err;
126 return (rc);
127 }
128
129 /*----- A few words about the overall approach ----------------------------*
130 *
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.
141 *
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.
147 *
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.
155 *
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'.)
172 *
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.
186 */
187
188 /*----- The file and event tables -----------------------------------------*/
189
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
200 * work very well.)
201 */
202
203 struct file {
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.)
208 */
209
210 ident id; /* file name */
211 secaddr start, end; /* start (inclusive) and end
212 * (exclusive) sector numbers */
213 };
214 DEFVEC(file_v, struct file); /* a vector of files */
215 static file_v filetab = VEC_INIT; /* the file table */
216
217 enum {
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.
221 */
222
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 */
227 };
228
229 struct event {
230 /* An event. */
231
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 */
236 };
237 DEFVEC(event_v, struct event); /* a vector of events */
238 static event_v eventq = VEC_INIT; /* the event queue */
239
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.
244 */
245 {
246 const struct event *eva = a, *evb = b;
247
248 /* Primary ordering by position. */
249 if (eva->pos < evb->pos) return (-1);
250 else if (eva->pos > evb->pos) return (+1);
251
252 /* Secondary ordering by event code. */
253 if (eva->ev < evb->ev) return (-1);
254 else if (eva->ev > evb->ev) return (+1);
255
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
258 * relied upon.
259 */
260 if (eva->file < evb->file) return (-1);
261 else if (eva->file > evb->file) return (+1);
262
263 /* These events are equal. */
264 return (0);
265 }
266
267 #ifdef DEBUG
268 static void dump_eventq(const char *what)
269 /* Dump the event queue, labelling the output with WHAT. */
270 {
271 unsigned i;
272 const struct event *ev;
273 char fn[MAXFNSZ];
274
275 printf("\n;; event dump (%s):\n", what);
276 for (i = 0; i < eventq.n; i++) {
277 ev = &eventq.v[i];
278 switch (ev->ev) {
279 case EV_BEGIN:
280 store_filename(fn, filetab.v[ev->file].id);
281 printf(";; %8"PRIuSEC": begin %s\n", ev->pos, fn);
282 break;
283 case EV_END:
284 store_filename(fn, filetab.v[ev->file].id);
285 printf(";; %8"PRIuSEC": end %s\n", ev->pos, fn);
286 break;
287 case EV_WRITE:
288 printf(";; %8"PRIuSEC": write\n", ev->pos);
289 break;
290 case EV_STOP:
291 printf(";; %8"PRIuSEC": stop\n", ev->pos);
292 break;
293 default:
294 printf(";; %8"PRIuSEC": ?%u\n", ev->pos, ev->ev);
295 break;
296 }
297 }
298 }
299 #endif
300
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'
306 * events.
307 */
308
309 static inline int livep(unsigned i)
310 /* Return whether file I is active. */
311 { return (live[i/32]&((bits)1 << (i%32))); }
312
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); }
316
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)); }
320
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.
327 */
328 {
329 unsigned i, n = (filetab.n + 32)/32;
330 bits b;
331
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; }
334 return (-1);
335
336 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.
341 */
342 i *= 32;
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; }
348 assert(b&1);
349
350 /* Done. */
351 return (i);
352 }
353
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.
359 */
360 {
361 struct event *ev;
362
363 VEC_PUSH(ev, &eventq);
364 ev->ev = evtype; ev->file = file; ev->pos = pos;
365 }
366
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.
370 */
371 {
372 struct file *f;
373 size_t i;
374
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);
379 }
380
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.
385 */
386 {
387 ident id = mkident(VOB, title, 0);
388 char fn[MAXFNSZ];
389 secaddr start, len;
390
391 /* Find out where the file is. */
392 store_filename(fn, id);
393 start = UDFFindFile(dvd, fn, &len); if (!start) return;
394
395 #ifdef DEBUG
396 /* Print out what we've discovered. */
397 printf(";; %8"PRIuSEC" .. %-8"PRIuSEC": %s\n",
398 start, start + SECTORS(len), fn);
399 #endif
400
401 /* Register the file and boundary events. */
402 put_file(id, start, start + SECTORS(len));
403 }
404
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.
409 */
410 {
411 char fn[MAXFNSZ];
412 secaddr start[9], len[9];
413 unsigned i, npart;
414
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.
417 */
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;
421 }
422 npart = i; if (!npart) return;
423
424 #ifdef DEBUG
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);
430 }
431 #endif
432
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
436 * number of sectors.
437 */
438 if (npart > 1)
439 for (i = 0; i < npart - 1; i++) {
440 if (len[i]%SECTORSZ)
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])
444 bail
445 ("title %u part %u end = %"PRIuSEC" /= part %u start = %"PRIuSEC"",
446 title, i, start[i] + len[i]/SECTORSZ, i + 1, start[i + 1]);
447 }
448
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]));
452 }
453
454 /*----- Common variables used by the copying machinery --------------------*/
455
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 */
464
465 static secaddr bad_start; /* start of current bad region */
466 static unsigned retry, max_retries = 4; /* retry state */
467
468 /*----- Progress reporting ------------------------------------------------*/
469
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 */
476
477 static const char throbber[] = "|<-<|>->"; /* throbber pattern */
478 static unsigned throbix = 0; /* current throbber index */
479
480 static struct progress_item /* stock progress items */
481 copy_progress, disc_progress,
482 file_progress, badblock_progress;
483
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.
488 */
489 {
490 const char *unit = "";
491
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);
497 }
498
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.
504 */
505 {
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);
509 return (buf);
510 }
511
512 static void render_perfstats(struct progress_render_state *render)
513 /* Add performance statistics to RENDER.
514 *
515 * Specifically: the average transfer rate, and the estimated time to
516 * completion. (See `update_progress' for how the average
517 * computation works.)
518 */
519 {
520 int eta;
521 char timebuf[TIMESTRMAX];
522 double rate;
523 const char *unit;
524
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);
527
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);
532 }
533
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.
538 */
539 {
540 double frac = (double)ndone/nsectors;
541
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);
546
547 progress_showbar(render, frac);
548 }
549
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.
554 */
555 {
556 double frac = (double)last_pos/limit;
557
558 progress_putleft(render, " disc %.1f%% (%"PRIuSEC" of %"PRIuSEC")",
559 100.0*frac, last_pos, limit);
560 progress_showbar(render, frac);
561 }
562
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.
567 */
568 {
569 secaddr off = last_pos - file->start, len = file->end - file->start;
570 char fn[MAXFNSZ];
571 double frac;
572
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);
578 }
579
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
583 * region.
584 */
585 {
586 secaddr n = last_pos - bad_start;
587 int bg;
588
589 if (!n) {
590 progress_putleft(render, " Retrying bad sector %"PRIuSEC"", bad_start);
591 progress_putright(render, "attempt %u/%u ", retry + 1, max_retries);
592 bg = 4;
593 } else {
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);
598 bg = 1;
599 }
600 if (bad_err && bad_err != EIO)
601 progress_putleft(render, " (%s)", strerror(bad_err));
602 progress_shownotice(render, bg, 7);
603 }
604
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.
608 */
609 {
610 struct timeval now;
611 double t, beta_t, rate;
612
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
617 *
618 * v = α x + (1 − α) v_1 .
619 *
620 * Write β = 1 − α; so
621 *
622 * v = α x + β v_1 .
623 *
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
627 *
628 * v = α (x_0 + β x_1 + ⋯ + β^i x_i + ⋯) .
629 *
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
634 * calculated
635 *
636 * v = α (x + β x + ⋯ + β^{t−1} x) + β^t v_1
637 *
638 * 1 − β^t
639 * = α x ------- + β^t v_1
640 * 1 − β
641 *
642 * = x (1 − β^t) + β^t v_1 (since α = 1 − β)
643 *
644 * = x + β^t (v_1 − x) .
645 *
646 * Does this work in general? It's clearly correct in the case t = 1.
647 *
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
652 *
653 * v_1 = x + β^t (v_2 − x)
654 *
655 * and then
656 *
657 * v = x + β^u (v_1 − x)
658 *
659 * = x + β^u (x + β^t (v_2 − x) − x)
660 *
661 * = x + β^{t+u} (v_2 − x) ,
662 *
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.
665 *
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
670 * intervals as
671 *
672 * x_0 + β x_1 + β^2 x_2 + ⋯ + β^{k−1} x_{k−1}
673 * v′ = ------------------------------------------- .
674 * 1 + β + β^2 + ⋯ + β^{k−1}
675 *
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.
679 *
680 * To apply all of this:
681 *
682 * * we maintain the moving average v in `avg';
683 *
684 * * we maintain the total β^k in `corr'; and
685 *
686 * * we compute v′ = v/(1 − β^k) on demand up in `render_perfstats'.
687 */
688
689 /* Find the current time and the delta since the last time we updated.
690 * This will be the length of the current interval.
691 */
692 gettimeofday(&now, 0); t = tvdiff(&last_time, &now);
693
694 /* If no time at all has passed (unlikely!) then skip the rate
695 * calculation. (The moving average wouldn't be affected anyway.)
696 */
697 if (t) {
698 /* Update the moving average and the correction term, and start the next
699 * interval.
700 */
701
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;
705 }
706
707 /* Advance the throbber character. */
708 throbix++; if (!throbber[throbix]) throbix = 0;
709 }
710
711 static void report_progress(secaddr pos)
712 /* Update the progress variables (as `update_progress') and redraw
713 * the progress display.
714 */
715 { update_progress(pos); progress_update(&progress); }
716
717 /*----- Basic disc I/O ----------------------------------------------------*/
718
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.
725 */
726
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
730 * later.
731 */
732 {
733 const struct badblock *ba = a, *bb = b;
734
735 /* Order by start sector. */
736 if (ba->start < bb->start) return (-1);
737 else if (ba->start > bb->start) return (+1);
738
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);
742
743 /* They're equal. This shouldn't be possible either. */
744 return (0);
745 }
746
747 static double bad_block_delay = 0.0, good_block_delay = 0.0;
748 /* delay parameters for performance testing */
749
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
757 * anything at all.
758 *
759 * This function is where the fake bad-blocks list is handled.
760 */
761 {
762 ssize_t n, done;
763 size_t lo, mid, hi;
764 int fakeerr = 0;
765 struct badblock *bad, *best;
766 unsigned char *p = buf;
767
768 /* See whether the requested range intersects a bad-blocks range. */
769 if (badblocks.n) {
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.
776 */
777
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.
783 */
784 best = 0; lo = 0; hi = badblocks.n;
785 #ifdef DEBUG
786 progress_clear(&progress);
787 printf(";; searching badblocks for %"PRIuSEC" .. %"PRIuSEC"\n",
788 pos, pos + want);
789 #endif
790 while (lo < hi) {
791 /* Standard binary-search loop: we continue until the pointers
792 * converge.
793 */
794
795 /* Try the midpoint between the two bounds. */
796 mid = lo + (hi - lo)/2; bad = &badblocks.v[mid];
797 #ifdef DEBUG
798 printf(";; try %zu (%"PRIuSEC" .. %"PRIuSEC")... ",
799 mid, bad->start, bad->end);
800 #endif
801
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).
808 *
809 * If we get a match then we stop immediately and fake a bad block.
810 */
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; }
813 else {
814 D( printf("match!\n"); )
815 errno = EIO; sit(bad_block_delay); return (-1);
816 }
817 }
818
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.
822 */
823 #ifdef DEBUG
824 if (best)
825 printf(";; next is %"PRIuSEC" .. %"PRIuSEC"\n",
826 best->start, best->end);
827 #endif
828 if (best && pos + want > best->start)
829 { want = best->start - pos; fakeerr = EIO; sit(bad_block_delay); }
830 }
831
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.
835 */
836 done = 0; errno = 0;
837 while (want) {
838
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.
844 */
845 if (vob)
846 { errno = 0; n = DVDReadBlocks(vob, pos - file->start, want, p); }
847 else if (file) {
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;
852 } else {
853 memset(p, 0, want*SECTORSZ);
854 n = want;
855 }
856
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
860 * stop.
861 */
862 if (n > 0) { done += n; pos += n; p += n*SECTORSZ; want -= n; }
863 else if (!n) break;
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");
868 break;
869 } else if (errno != EINTR) break;
870 }
871
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.
879 */
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);
883 }
884
885 /*----- Tracking machinery for the bad-sector algorithm -------------------*
886 *
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.
892 *
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.
896 *
897 * * The `short-range' machinery keeps track of a contiguous region of good
898 * data stored in the caller's buffer.
899 *
900 * * The `long-range' machinery keeps track of a contiguous region of good
901 * data that's beyond the range of the buffer.
902 */
903
904 struct recoverybuf {
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.
907 */
908
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 */
917
918 /* Long-range tracking. */
919 secaddr good_lo, good_hi; /* known-good region, as absolute
920 * sector addresses */
921 };
922
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.
928 *
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.
932 */
933 {
934 assert(dest + len <= r->sz); assert(src + len <= r->sz);
935 memmove(r->buf + dest*SECTORSZ, r->buf + src*SECTORSZ, len*SECTORSZ);
936 }
937
938 #ifdef DEBUG
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. */
943 {
944 va_list ap;
945
946 va_start(ap, what);
947 progress_clear(&progress);
948 printf(";; recovery buffer (");
949 vprintf(what, ap);
950 printf("): "
951 "(%"PRIuSEC") ..%"PRIuSEC".. "
952 "[%"PRIuSEC" ..%"PRIuSEC".. %"PRIuSEC"] "
953 "..%"PRIuSEC".. (%"PRIuSEC")\n",
954 r->pos, r->start,
955 r->pos + r->start, r->end - r->start, r->pos + r->end,
956 r->sz - r->end, r->pos + r->sz);
957 va_end(ap);
958 assert(r->start <= r->end);
959 assert(r->end <= r->sz);
960 }
961 #endif
962
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.
969 *
970 * This is a trivial wrapper around `read_sectors' with some
971 * additional range checking, used only by `recovery_read_buffer'
972 * below.
973 */
974 {
975 ssize_t n;
976
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);
980 return (n);
981 }
982
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.
990 */
991 {
992 secaddr diff, pp, nn;
993 ssize_t n;
994
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.)
998 */
999
1000 #ifdef DEBUG
1001 progress_clear(&progress);
1002 show_recovery_buffer_map(r, "begin(%"PRIuSEC", %"PRIuSEC")", pos, want);
1003 #endif
1004
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.
1007 */
1008 if (pos < r->pos) {
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.
1012 */
1013
1014 /* Determine how far up we'll need to shift. */
1015 diff = r->pos - pos;
1016
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
1020 * completely.
1021 */
1022
1023 r->pos = pos; r->start = r->end = 0;
1024 #ifdef DEBUG
1025 show_recovery_buffer_map(r, "cleared; shift up by %"PRIuSEC"", diff);
1026 #endif
1027 } else {
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.
1031 */
1032
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;
1036 #ifdef DEBUG
1037 show_recovery_buffer_map(r, "shifted up by %"PRIuSEC"", diff);
1038 #endif
1039 }
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
1047 * optimizations.
1048 */
1049
1050 r->pos = pos; r->start = r->end = 0;
1051 #ifdef DEBUG
1052 show_recovery_buffer_map(r, "cleared; beyond previous region");
1053 #endif
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.
1058 */
1059
1060 /* Determine how far down we'll need to shift. */
1061 diff = (pos + want) - (r->pos + r->sz);
1062
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
1066 * completely.
1067 */
1068
1069 r->pos = pos; r->start = r->end = 0;
1070 #ifdef DEBUG
1071 show_recovery_buffer_map(r, "cleared; shift down by %"PRIuSEC"", diff);
1072 #endif
1073 } else {
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.
1077 */
1078
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;
1082 #ifdef DEBUG
1083 show_recovery_buffer_map(r, "shifted down by %"PRIuSEC"", diff);
1084 #endif
1085 }
1086 }
1087
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.
1093 */
1094
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.
1098 */
1099
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.
1105 */
1106 pp = pos - r->pos; nn = r->start - pp;
1107
1108 /* Read the data. */
1109 #ifdef DEBUG
1110 printf(";; read low (%"PRIuSEC"@%"PRIuSEC", %"PRIuSEC")", pos, pp, nn);
1111 fflush(stdout);
1112 #endif
1113 n = recovery_read_sectors(r, pos, pp, nn);
1114 #ifdef DEBUG
1115 printf(" -> %zd\n", n);
1116 #endif
1117
1118 /* See whether it worked. */
1119 if (n != nn) {
1120 /* We didn't get everything we wanted. */
1121
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.
1127 */
1128 if (n >= 0 && n > want) n = want;
1129
1130 /* We're done. */
1131 goto end;
1132 }
1133
1134 /* Extend the region to include the new piece. */
1135 r->start = pp;
1136 #ifdef DEBUG
1137 show_recovery_buffer_map(r, "joined new region");
1138 #endif
1139 }
1140
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.
1144 */
1145
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.
1150 */
1151 pp = r->end; nn = (pos + want) - (r->pos + r->end);
1152
1153 /* Read the data. */
1154 #ifdef DEBUG
1155 printf(";; read high (%"PRIuSEC"@%"PRIuSEC", %"PRIuSEC")",
1156 r->pos + pp, pp, nn);
1157 fflush(stdout);
1158 #endif
1159 n = recovery_read_sectors(r, r->pos + pp, pp, nn);
1160 #ifdef DEBUG
1161 printf(" -> %zd\n", n);
1162 #endif
1163
1164 /* See whether it worked. */
1165 if (n > 0) {
1166 /* We read something, so add it onto the existing region. */
1167
1168 r->end += n;
1169 #ifdef DEBUG
1170 show_recovery_buffer_map(r, "joined new region");
1171 #endif
1172 }
1173 }
1174
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
1180 * requested length.
1181 */
1182 n = r->pos + r->end - pos;
1183 if (!n && want) n = -1;
1184 else if (n > want) n = want;
1185
1186 end:
1187 /* Done. */
1188 #ifdef DEBUG
1189 show_recovery_buffer_map(r, "done; return %zd", n);
1190 #endif
1191 return (n);
1192 }
1193
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.
1201 *
1202 * This is only used by `recovery_read' below.
1203 */
1204 {
1205 ssize_t n;
1206 secaddr skip, want0 = want;
1207
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.
1214 */
1215
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.
1219 */
1220
1221 skip = want - r->sz;
1222 n = recovery_read_buffer(r, pos + skip, r->sz);
1223
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
1226 * material.
1227 */
1228 if (n < r->sz) return (skip + (n >= 0 ? n : 0));
1229
1230 /* Cross off a buffer's worth and go around again. */
1231 want -= r->sz;
1232 }
1233
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.
1236 */
1237 n = recovery_read_buffer(r, pos, want);
1238 if (n < 0 || n < want) return (n);
1239
1240 /* It all worked. Return the full original amount requested. */
1241 return (want0);
1242 }
1243
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
1251 * just thrown away.
1252 */
1253 {
1254 secaddr lo = pos, hi = pos + want, span; /* calculate the request bounds */
1255 ssize_t n;
1256
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
1259 * just dealt with.
1260 */
1261
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.
1267 */
1268
1269 n = recovery_read_multiple(r, lo, hi - lo);
1270 if (n > 0) { r->good_lo = lo; r->good_hi = lo + n; }
1271 return (n);
1272 }
1273
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
1277 * bad sectors.
1278 */
1279
1280 span = hi - r->good_hi;
1281 n = recovery_read_multiple(r, r->good_hi, span);
1282
1283 /* If we read anything at all, then extend the known-good region. */
1284 if (n > 0) r->good_hi += n;
1285
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).
1288 */
1289 if (n < 0 || n < span) return (r->good_hi - lo);
1290 }
1291
1292 if (lo < r->good_lo) {
1293 /* The requested region begins earlier than the known-good region. */
1294
1295 span = r->good_lo - lo;
1296 n = recovery_read_multiple(r, lo, span);
1297
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.
1300 */
1301 if (n == span) r->good_lo = lo;
1302 else return (n);
1303 }
1304
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.
1308 */
1309 n = r->good_hi - pos; if (n > want) n = want;
1310 if (!n) { errno = EIO; n = -1; }
1311 return (n);
1312 }
1313
1314 /*----- Skipping past regions of bad sectors ------------------------------*/
1315
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 */
1320
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
1324 * (exclusive).
1325 */
1326 {
1327 char fn[MAXFNSZ];
1328
1329 /* Remove the progress display temporarily. */
1330 progress_clear(&progress);
1331
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);
1336 else {
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);
1343 }
1344
1345 if (mapfile) {
1346 /* The user requested a map of the skipped regions, so write an entry. */
1347
1348 /* Open the file, if it's not open already. */
1349 open_file_on_demand(mapfile, &mapfp, "bad-sector region map");
1350
1351 /* Write the sector range. */
1352 fprintf(mapfp, "%"PRIuSEC" %"PRIuSEC" # %"PRIuSEC" sectors",
1353 bad_lo, bad_hi, bad_hi - bad_lo);
1354
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
1358 * concerned to be.)
1359 */
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);
1364
1365 /* Done. Flush the output to the file so that we don't lose it if we
1366 * crash!
1367 */
1368 fputc('\n', mapfp);
1369 check_write(mapfp, "bad-sector region map");
1370 }
1371
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.)
1375 */
1376 if (lseek(outfd, (off_t)(bad_hi - bad_lo)*SECTORSZ, SEEK_CUR) < 0)
1377 bail_syserr(errno, "failed to seek past bad sectors");
1378
1379 /* Remove our notice now that we're no longer messing about with bad
1380 * sectors, and reinstate the progress display.
1381 */
1382 progress_removeitem(&progress, &badblock_progress);
1383 progress_update(&progress);
1384 }
1385
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).
1392 */
1393 {
1394 secaddr want;
1395
1396 /* Apply the factor to BADLEN to get an initial length. */
1397 want = ceil(clear_factor*badlen);
1398
1399 /* Apply the user-configurable lower bound. */
1400 if (want < clear_min) want = clear_min;
1401
1402 /* Cap this with the end of the region. */
1403 if (want > end - pos) want = end - pos;
1404
1405 /* And apply the user-configurable upper bound. */
1406 if (clear_max && want > clear_max) want = clear_max;
1407
1408 /* We're done. */
1409 return (want);
1410 }
1411
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
1415 * error was ERR.
1416 */
1417 { bad_err = err; report_progress(bad_hi); }
1418
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
1430 * end-of-file.)
1431 *
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.
1434 */
1435 {
1436 secaddr pos = *pos_inout, bad_lo, bad_hi, good, step, want;
1437 struct recoverybuf r;
1438 ssize_t n;
1439
1440 /* Initial setup. Save the initial state and establish the bad-blocks
1441 * progress notice.
1442 */
1443 bad_start = pos; bad_err = errno;
1444 badblock_progress.render = render_badblock_progress;
1445 progress_additem(&progress, &badblock_progress);
1446
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.
1450 */
1451 want = sz; if (want > end - pos) want = end - pos;
1452 for (retry = 0; retry < max_retries; retry++) {
1453
1454 /* Show the progress report. */
1455 report_bad_blocks_progress(pos, errno);
1456
1457 /* Try reading stuff. */
1458 n = read_sectors(pos, buf, want);
1459 #ifdef DEBUG
1460 progress_clear(&progress);
1461 printf(";; [retry] try reading %"PRIuSEC" .. %"PRIuSEC" -> %zd\n",
1462 pos, pos + want, n);
1463 #endif
1464
1465 if (n > 0) {
1466 /* We won! Remove the progress display, and leave a permanent message
1467 * to inform the user what happened.
1468 */
1469 progress_clear(&progress);
1470 moan("sector %"PRIuSEC" read ok after retry", pos);
1471 progress_removeitem(&progress, &badblock_progress);
1472 progress_update(&progress);
1473 return (n);
1474 }
1475 }
1476
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;
1480
1481 /* Set up the region bound. We know the bad area starts at POS, and that
1482 * it covers at least one sector.
1483 */
1484 bad_lo = pos; bad_hi = pos + 1;
1485
1486 /* Second major step: try to find somewhere on the other side of the bad
1487 * region.
1488 */
1489 for (;;) {
1490 #ifdef DEBUG
1491 progress_clear(&progress);
1492 printf(";; bounding bad-block region: "
1493 "%"PRIuSEC" ..%"PRIuSEC".. %"PRIuSEC"\n",
1494 bad_lo, bad_hi - bad_lo, bad_hi);
1495 #endif
1496
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.
1500 */
1501 if (bad_hi >= end) {
1502 progress_clear(&progress);
1503 moan("giving up on this extent");
1504 recovered(bad_lo, end); *pos_inout = end;
1505 return (0);
1506 }
1507
1508 /* Give a progress update. */
1509 report_bad_blocks_progress(bad_hi, errno);
1510
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.
1514 *
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.
1517 */
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;
1523
1524 /* Now we look at the last sector of the new interval we've just marked
1525 * out.
1526 */
1527 pos = bad_lo + step - 1;
1528 want = run_length_wanted(pos, step, end);
1529 n = recovery_read(&r, pos, want);
1530 #ifdef DEBUG
1531 printf(";; [bound] try reading %"PRIuSEC" .. %"PRIuSEC" -> %zd\n",
1532 pos, pos + want, n);
1533 #endif
1534
1535 /* If everything went OK then we're done with this phase. */
1536 if (n == want) break;
1537
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.
1540 */
1541 if (n < 0) n = 0;
1542 bad_hi = pos + n + 1;
1543 }
1544
1545 /* Third major step: identify exactly where the bad region ends. This is
1546 * a binary search.
1547 */
1548 good = pos;
1549 while (good > bad_hi) {
1550 #ifdef DEBUG
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);
1555 #endif
1556
1557 /* Update the progress report. */
1558 report_bad_blocks_progress(bad_hi, errno);
1559
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);
1563
1564 /* Try reading. */
1565 n = recovery_read(&r, pos, want);
1566 #ifdef DEBUG
1567 printf(";; [limit] try reading %"PRIuSEC" .. %"PRIuSEC" -> %zd\n",
1568 pos, pos + want, n);
1569 #endif
1570
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.
1574 */
1575 if (n < 0) n = 0;
1576 if (n == want) good = pos;
1577 else bad_hi = pos + n + 1;
1578 }
1579
1580 /* We're done. It's time to tidy up.
1581 *
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
1587 */
1588 recovered(bad_lo, good); *pos_inout = good;
1589
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.
1594 */
1595
1596 n = 0;
1597 } else {
1598 /* The new position is covered, so shuffle the data to the start of the
1599 * buffer and return as much as we can.
1600 */
1601
1602 n = r.pos + r.end - good;
1603 rearrange_sectors(&r, 0, good - r.pos, n);
1604 }
1605
1606 /* We're done. */
1607 #ifdef DEBUG
1608 show_recovery_buffer_map(&r, "returning %zd good sectors at %"PRIuSEC"",
1609 n, good);
1610 #endif
1611 return (n);
1612 }
1613
1614 /*----- Copying data from a single input file -----------------------------*/
1615
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'.
1620 */
1621 {
1622 #define BUFSECTORS 512 /* this is a megabyte */
1623
1624 int least;
1625 unsigned char buf[BUFSECTORS*SECTORSZ];
1626 secaddr pos;
1627 size_t want;
1628 ssize_t n;
1629 static int first_time = 1;
1630 #ifdef DEBUG
1631 struct file *f;
1632 char fn[MAXFNSZ];
1633 int act = -1;
1634 int i;
1635 #endif
1636
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
1645 * in the wild.
1646 */
1647 least = least_live();
1648 #ifdef DEBUG
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);
1656 }
1657 if (act == -1) printf(";;\t\t#<no live source>\n");
1658 assert(act == least);
1659 #endif
1660
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'.
1663 */
1664 if (least == -1) {
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.
1668 */
1669
1670 file = 0; vob = 0;
1671 } else {
1672 /* There's a (possibly) virtual file. */
1673
1674 file = &filetab.v[least];
1675 switch (id_kind(file->id)) {
1676
1677 case RAW:
1678 /* It's the raw device. Clear `vob' to prompt `read_sectors' to read
1679 * directly from `dvdfd'.
1680 */
1681
1682 vob = 0;
1683 break;
1684
1685 case VOB:
1686 /* It's a `.VOB' file. We read these through `libdvdread', which
1687 * handles CSS unscrambling for us.
1688 */
1689
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.
1694 */
1695 if (first_time) { progress_clear(&progress); first_time = 0; }
1696
1697 /* Open the `.VOB' file. */
1698 vob = DVDOpenFile(dvd, id_title(file->id),
1699 id_part(file->id)
1700 ? DVD_READ_TITLE_VOBS
1701 : DVD_READ_MENU_VOBS);
1702 if (!vob)
1703 bail("failed to open %s %u",
1704 id_part(file->id) ? "title" : "menu",
1705 id_title(file->id));
1706 break;
1707
1708 default:
1709 /* Some other kind of thing; but there shouldn't be anything else in
1710 * the file table, so there's a bug.
1711 */
1712 abort();
1713
1714 }
1715 }
1716
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.
1722 */
1723 if (file && id_kind(file->id) != RAW) {
1724 file_progress.render = render_file_progress;
1725 progress_additem(&progress, &file_progress);
1726 }
1727
1728 /* Put the progress display back, if we took it away, and show the file
1729 * progress bar if we added one.
1730 */
1731 progress_update(&progress);
1732
1733 /* Read the input region and copy it to the disc. */
1734 pos = start;
1735 while (pos < end) {
1736
1737 /* Decide how much we want. Fill the buffer, unless there's not enough
1738 * input left.
1739 */
1740 want = end - pos; if (want > BUFSECTORS) want = BUFSECTORS;
1741
1742 /* Try to read the input. */
1743 n = read_sectors(pos, buf, want);
1744
1745 if (n <= 0) {
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.
1749 */
1750
1751 n = find_good_sector(&pos, end, buf, BUFSECTORS);
1752 }
1753 if (n > 0) {
1754 /* We made some progress. Write the stuff that we read to the output
1755 * file and update the position.
1756 */
1757
1758 carefully_write(outfd, buf, n*SECTORSZ); pos += n;
1759 }
1760
1761 /* Report our new progress. */
1762 report_progress(pos);
1763 }
1764
1765 /* Close the `libdvdread' file, if we opened one. */
1766 if (vob) { DVDCloseFile(vob); vob = 0; }
1767
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);
1771
1772 /* Update the progress display to report our glorious success. */
1773 progress_update(&progress);
1774
1775 #undef BUFSECTORS
1776 }
1777
1778 /*----- Main program ------------------------------------------------------*/
1779
1780 int main(int argc, char *argv[])
1781 {
1782 unsigned f = 0;
1783 const char *p;
1784 off_t volsz;
1785 secaddr pos;
1786 off_t off;
1787 secaddr start, end, last;
1788 const struct event *ev;
1789 const char *device, *outfile;
1790 struct badblock *bad;
1791 int opt, blksz;
1792 size_t i;
1793 FILE *fp;
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;
1800 #ifdef DEBUG
1801 const struct file *file;
1802 char fn[MAXFNSZ];
1803 #endif
1804
1805 #define f_bogus 1u
1806 #define f_continue 2u
1807 #define f_fixup 4u
1808 #define f_stats 8u
1809 #define f_checkid 16u
1810 #define f_retry 32u
1811 #define f_write 256u
1812
1813 set_prog(argv[0]);
1814
1815 /* First up, handle the command-line options. */
1816 for (;;) {
1817 opt = getopt(argc, argv, "hB:E:FR:X:b:cir:s"); if (opt < 0) break;
1818 switch (opt) {
1819
1820 /* `-h': Help. */
1821 case 'h': usage(stderr); exit(0);
1822
1823 /* `-B PARAM=VALUE[,...]': Setting internal parameters. */
1824 case 'B':
1825
1826 /* Set up a cursor into the parameter string. */
1827 p = optarg;
1828
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.
1833 */
1834
1835 for (;;) {
1836
1837 if (SKIP_PREFIX("cf"))
1838 clear_factor = parse_float(&p, PNF_JUNK, 0, DBL_MAX,
1839 "clear factor");
1840
1841 else if (SKIP_PREFIX("cmin"))
1842 clear_min = parse_int(&p, PNF_JUNK, 1, SECLIMIT,
1843 "clear minimum");
1844
1845 else if (SKIP_PREFIX("cmax"))
1846 clear_max = parse_int(&p, PNF_JUNK, 1, SECLIMIT,
1847 "clear maximum");
1848
1849 else if (SKIP_PREFIX("sf"))
1850 step_factor = parse_float(&p, PNF_JUNK, 0, DBL_MAX,
1851 "step factor");
1852
1853 else if (SKIP_PREFIX("smin"))
1854 step_min = parse_int(&p, PNF_JUNK, 1, SECLIMIT - 1,
1855 "step minimum");
1856
1857 else if (SKIP_PREFIX("smax"))
1858 step_max = parse_int(&p, PNF_JUNK, 1, SECLIMIT - 1,
1859 "step maximum");
1860
1861 else if (SKIP_PREFIX("retry"))
1862 max_retries = parse_int(&p, PNF_JUNK, 0, INT_MAX, "retries");
1863
1864 else if (SKIP_PREFIX("alpha"))
1865 alpha = parse_float(&p, PNF_JUNK, 0, 1, "average decay factor");
1866
1867 else if (SKIP_PREFIX("_badwait"))
1868 bad_block_delay = parse_float(&p, PNF_JUNK, 0, DBL_MAX,
1869 "bad-block delay");
1870
1871 else if (SKIP_PREFIX("_blkwait"))
1872 good_block_delay = parse_float(&p, PNF_JUNK, 0, DBL_MAX,
1873 "good block delay");
1874
1875 else
1876 bail("unknown bad blocks parameter `%s'", p);
1877
1878 /* If we're now at the end of the string then we're done. */
1879 if (!*p) break;
1880
1881 /* We're not done yet, so there should now be a comma and another
1882 * parameter setting.
1883 */
1884 if (*p != ',') bail("unexpected junk in parameters");
1885 p++;
1886 }
1887
1888 #undef SKIP_PREFIX
1889 break;
1890
1891 /* `-E FILE' (undocumented): Log the bad sectors we encountered to
1892 * FILE.
1893 */
1894 case 'E': errfile = optarg; break;
1895
1896 /* `-F' (undocumented): Hack for fixing up images that were broken by
1897 * an old early-stop bug.
1898 */
1899 case 'F': f |= f_fixup; break;
1900
1901 /* `-R FILE': Read ranges to retry from FILE. Retry ranges are
1902 * converted into `EV_WRITE' and `EV_STOP' events.
1903 */
1904 case 'R':
1905 fp = fopen(optarg, "r");
1906 if (!fp)
1907 bail_syserr(errno, "failed to open ranges file `%s'", optarg);
1908
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.
1916 */
1917 i = 0; last = -1;
1918 for (;;) {
1919
1920 /* Read a line from the buffer. If there's nothing left then we're
1921 * done.
1922 */
1923 buf_rewind(&buf); if (read_line(fp, &buf)) break;
1924
1925 /* Increment the line counter and establish a cursor. */
1926 i++; p = buf.p;
1927
1928 /* Skip initial whitespace. */
1929 while (ISSPACE(*p)) p++;
1930
1931 /* If this is a comment then ignore it and go round again. */
1932 if (!*p || *p == '#') continue;
1933
1934 /* Parse the range. Check that the ranges are coming out in
1935 * ascending order.
1936 */
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);
1940
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
1944 * pair of events.
1945 */
1946 if (start < end) {
1947 if (start == last)
1948 eventq.v[eventq.n - 1].pos = end;
1949 else {
1950 put_event(EV_WRITE, 0, start);
1951 put_event(EV_STOP, 0, end);
1952 }
1953 last = end;
1954 }
1955 }
1956
1957 /* Check for read errors. */
1958 if (ferror(fp))
1959 bail_syserr(errno, "failed to read ranges file `%s'", optarg);
1960 f |= f_retry;
1961 break;
1962
1963 /* `-X FILE' (undocumented): Read ranges of bad-blocks from FILE to
1964 * establish fake bad blocks: see `read_sectors' above for the details.
1965 *
1966 * This is very similar to the `-R' option above, except that it
1967 * doesn't do the range coalescing thing.
1968 */
1969 case 'X':
1970 fp = fopen(optarg, "r");
1971 if (!fp)
1972 bail_syserr(errno, "failed to open bad-blocks file `%s'", optarg);
1973 i = 0; last = -1;
1974 for (;;) {
1975 buf_rewind(&buf); if (read_line(fp, &buf)) break;
1976 p = buf.p; i++;
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);
1982 if (start < end) {
1983 VEC_PUSH(bad, &badblocks);
1984 bad->start = start; bad->end = end;
1985 }
1986 }
1987 if (ferror(fp))
1988 bail_syserr(errno, "failed to read bad-blocks file `%s'", optarg);
1989 break;
1990
1991 /* Log regions skipped because of bad blocks to a file. */
1992 case 'b':
1993 if (mapfile) bail("can't have multiple map files");
1994 mapfile = optarg;
1995 break;
1996
1997 /* `-c': Continue copying where we left off last time. */
1998 case 'c': f |= f_continue; break;
1999
2000 /* `-i': Check that we're copying from the right disc. */
2001 case 'i': f |= f_checkid; break;
2002
2003 /* `-r [START]-[END]': Manually provide a range of sectors to retry. */
2004 case 'r':
2005 start = 0; end = -1; f |= f_retry;
2006 if (parse_range(optarg, PRF_HYPHEN, &start, &end))
2007 bail("bad range `%s'", optarg);
2008 if (start < end) {
2009 /* Again, ignore empty ranges. */
2010 put_event(EV_WRITE, 0, start);
2011 if (end <= SECLIMIT) put_event(EV_STOP, 0, end);
2012 }
2013 break;
2014
2015 /* `-s': Print statistics at the end. */
2016 case 's': f |= f_stats; break;
2017
2018 /* Anything else is an error. */
2019 default: f |= f_bogus; break;
2020 }
2021 }
2022
2023 /* We expect two arguments. Check this. Complain about bad usage if we
2024 * have bad arguments or options.
2025 */
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];
2029
2030 /* If there are fake bad blocks (the `-X' option) then sort the list
2031 * because `read_sectors' wants to use a binary search.
2032 */
2033 if (badblocks.n) {
2034 qsort(badblocks.v, badblocks.n, sizeof(struct badblock),
2035 compare_badblock);
2036 #ifdef DEBUG
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);
2041 #endif
2042 }
2043
2044 /* Prepare to display progress information. */
2045 setlocale(LC_ALL, "");
2046 progress_init(&progress);
2047
2048 /* Open the input device. (This may pop up a notice if there's nothing in
2049 * the drive.)
2050 */
2051 if (open_dvd(device, O_RDONLY, &dvdfd, &dvd)) exit(2);
2052
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);
2057 if (volsz%SECTORSZ)
2058 bail("device `%s' volume size %"PRIu64" not a multiple of %d",
2059 device, volsz, SECTORSZ);
2060
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.
2064 */
2065 if (f&f_checkid) {
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))
2069 exit(2);
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);
2073 }
2074
2075 /* Open the output file. */
2076 outfd = open(outfile, O_WRONLY | O_CREAT, 0666);
2077 if (outfd < 0)
2078 bail_syserr(errno, "failed to create output file `%s'", outfile);
2079
2080 if (f&f_continue) {
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.
2086 */
2087
2088 off = lseek(outfd, 0, SEEK_END);
2089 if (off < 0)
2090 bail_syserr(errno, "failed to seek to end of output file `%s'",
2091 outfile);
2092 put_event(EV_WRITE, 0, off/SECTORSZ); f |= f_retry;
2093 }
2094
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
2098 * everything.
2099 */
2100
2101 put_event(EV_WRITE, 0, 0);
2102 }
2103
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.
2109 */
2110 put_menu(dvd, 0);
2111 for (i = 1; i < 100; i++) {
2112 put_menu(dvd, i);
2113 put_title(dvd, i);
2114 }
2115
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
2119 * size.
2120 */
2121 put_file(mkident(RAW, 0, 0), 0, volsz/SECTORSZ);
2122 assert(filetab.n <= MAXFILES);
2123
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
2126 * all of our bases.
2127 */
2128 for (i = 0, limit = 0; i < filetab.n; i++)
2129 if (filetab.v[i].end > limit) limit = filetab.v[i].end;
2130 #ifdef DEBUG
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);
2137 }
2138 #endif
2139
2140 /* Sort the event list.
2141 *
2142 * The event-code ordering is important here.
2143 *
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
2148 * wrong.
2149 *
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.
2154 */
2155 qsort(eventq.v, eventq.n, sizeof(struct event), compare_event);
2156
2157 /* Check that the event list is well-formed. We start out at the
2158 * beginning, not writing anything.
2159 */
2160 for (i = 0, f &= ~f_write, start = 0; i < eventq.n; i++) {
2161 ev = &eventq.v[i];
2162 switch (ev->ev) {
2163
2164 case EV_WRITE:
2165 /* Start writing. We shouldn't be writing yet! */
2166
2167 if (f&f_write)
2168 bail("overlapping ranges: range from %"PRIuSEC" "
2169 "still open at %"PRIuSEC"",
2170 start, ev->pos);
2171 f |= f_write; start = ev->pos;
2172 break;
2173
2174 case EV_STOP:
2175 /* Stop writing. Make a note that we've done this. */
2176
2177 f &= ~f_write;
2178 break;
2179 }
2180 }
2181 #ifdef DEBUG
2182 dump_eventq("initial");
2183 #endif
2184
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.
2187 */
2188 for (i = 0, f &= ~f_write, start = last = 0; i < eventq.n; i++) {
2189 ev = &eventq.v[i];
2190
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.
2195 */
2196 if (ev->ev == EV_WRITE) { start = ev->pos; f |= f_write; }
2197
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.)
2202 */
2203 if (ev->pos >= limit) break;
2204
2205 /* If we're supposed to stop writing here, then add the size of the
2206 * most recent range onto our running total.
2207 */
2208 if (ev->ev == EV_STOP) { nsectors += ev->pos - start; f &= ~f_write; }
2209
2210 /* If we're fixing up images affected by the old early-stop bug, then
2211 * remember this position.
2212 */
2213 if (f&f_fixup) last = ev->pos;
2214 }
2215
2216 /* Truncate the event queue at the point we reached the sector limit. */
2217 eventq.n = i;
2218 #ifdef DEBUG
2219 dump_eventq("trimmed");
2220 #endif
2221
2222 /* Finally, the early-stop bug fix.
2223 *
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
2229 * this last event.
2230 */
2231 if (f&f_fixup) {
2232 put_event(EV_WRITE, 0, last);
2233 f |= f_write;
2234 }
2235
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
2238 * count.
2239 */
2240 if (f&f_write) {
2241 nsectors += limit - start;
2242 put_event(EV_STOP, 0, limit);
2243 }
2244 #ifdef DEBUG
2245 dump_eventq("final");
2246 #endif
2247
2248 /* Set up the main progress display.
2249 *
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.
2255 */
2256 copy_progress.render = render_copy_progress;
2257 progress_additem(&progress, &copy_progress);
2258 if (nsectors == limit - start)
2259 { ndone = start; nsectors = limit; }
2260 else {
2261 disc_progress.render = render_disc_progress;
2262 progress_additem(&progress, &disc_progress);
2263 }
2264
2265 /* If we're producing overall statistics then make a note of the current
2266 * time.
2267 */
2268 if (f&f_stats) gettimeofday(&tv0, 0);
2269
2270 /* We're now ready to start our sweep through the disc. */
2271 #ifdef DEBUG
2272 printf("\n;; event sweep:\n");
2273 #endif
2274
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.
2277 */
2278 for (pos = 0, i = 0, f &= ~f_write; i < eventq.n; i++) {
2279
2280 /* Get the next event. */
2281 ev = &eventq.v[i];
2282
2283 /* If there's a nonempty range between here and the previous event then
2284 * we need to process this.
2285 */
2286 if (ev->pos > pos) {
2287
2288 /* If we're writing then copy the interval from the previous event to
2289 * here to the output.
2290 */
2291 if (f&f_write) emit(pos, ev->pos);
2292
2293 /* Advance the current position now that the output is up-to-date. */
2294 pos = ev->pos;
2295
2296 #ifdef DEBUG
2297 progress_clear(&progress);
2298 printf(";;\n");
2299 #endif
2300 }
2301
2302 /* Decide what to action to take in response to the event. */
2303 switch (ev->ev) {
2304
2305 case EV_BEGIN:
2306 /* A file has started. Set the appropriate bit in the active-files
2307 * map.
2308 */
2309 set_live(ev->file);
2310 #ifdef DEBUG
2311 store_filename(fn, filetab.v[ev->file].id);
2312 progress_clear(&progress);
2313 printf(";; %8"PRIuSEC": begin `%s'\n", pos, fn);
2314 #endif
2315 break;
2316
2317 case EV_WRITE:
2318 /* We're supposed to start writing. */
2319
2320 /* Note the current time and position for the progress display. */
2321 gettimeofday(&last_time, 0); last_pos = pos;
2322
2323 /* Seek to the right place in the output file. */
2324 if (lseek(outfd, (off_t)ev->pos*SECTORSZ, SEEK_SET) < 0)
2325 bail_syserr(errno,
2326 "failed to seek to resume position "
2327 "(sector %"PRIuSEC") in output file `%s'",
2328 ev->pos, outfile);
2329
2330 /* Engage the write head. */
2331 f |= f_write;
2332
2333 #ifdef DEBUG
2334 progress_clear(&progress);
2335 printf(";; %8"PRIuSEC": begin write\n", pos);
2336 #endif
2337 break;
2338
2339 case EV_STOP:
2340 /* We're supposed to stop writing. Disengage the write head. */
2341
2342 f &= ~f_write;
2343 #ifdef DEBUG
2344 progress_clear(&progress);
2345 printf(";; %8"PRIuSEC": end write\n", pos);
2346 #endif
2347 break;
2348
2349 case EV_END:
2350 /* We've found the end of a file. Clear its bit in the table. */
2351
2352 clear_live(ev->file);
2353 #ifdef DEBUG
2354 store_filename(fn, filetab.v[ev->file].id);
2355 progress_clear(&progress);
2356 printf(";; %8"PRIuSEC": end `%s'\n", pos, fn);
2357 #endif
2358 break;
2359
2360 /* Something else. Clearly a bug. */
2361 default: abort();
2362 }
2363 }
2364
2365 /* Take down the progress display because we're done. */
2366 progress_clear(&progress);
2367
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);
2371
2372 /* Report overall statistics. */
2373 if (f&f_stats) {
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);
2380 }
2381
2382 /* Close files. */
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);
2389
2390 /* We're done! */
2391 return (0);
2392
2393 #undef f_bogus
2394 #undef f_continue
2395 #undef f_fixup
2396 #undef f_stats
2397 #undef f_write
2398 }
2399
2400 /*----- That's all, folks -------------------------------------------------*/