2 * SPDX-License-Identifier: Beerware
4 * ----------------------------------------------------------------------------
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
9 * ----------------------------------------------------------------------------
13 #include <sys/param.h>
14 #include <sys/queue.h>
32 /* Safe printf into a fixed-size buffer */
33 #define bprintf(buf, fmt, ...) \
36 ibprintf = snprintf(buf, sizeof buf, fmt, __VA_ARGS__); \
37 assert(ibprintf >= 0 && ibprintf < (int)sizeof buf); \
44 TAILQ_ENTRY(lump) list;
52 TAILQ_ENTRY(period) list;
54 TAILQ_HEAD(period_head, period);
56 static volatile sig_atomic_t aborting = 0;
57 static int verbose = 0;
58 static size_t bigsize = 1024 * 1024;
59 static size_t medsize;
60 static size_t minsize = 512;
61 static off_t tot_size;
62 static off_t done_size;
64 static char *wworklist = NULL;
65 static char *rworklist = NULL;
66 static const char *unreadable_pattern = "_UNREAD_";
67 static const int write_errors_are_fatal = 1;
70 static TAILQ_HEAD(, lump) lumps = TAILQ_HEAD_INITIALIZER(lumps);
71 static struct period_head minute = TAILQ_HEAD_INITIALIZER(minute);
72 static struct period_head quarter = TAILQ_HEAD_INITIALIZER(quarter);
73 static struct period_head hour = TAILQ_HEAD_INITIALIZER(quarter);
74 static struct period_head day = TAILQ_HEAD_INITIALIZER(quarter);
76 /**********************************************************************/
79 report_good_read2(time_t now, size_t bytes, struct period_head *ph, time_t dt)
86 if (pp == NULL || pp->t1 < now) {
87 pp = calloc(sizeof *pp, 1L);
89 pp->t0 = (now / dt) * dt;
90 pp->t1 = (now / dt + 1) * dt;
91 assert(localtime_r(&pp->t0, &tm1) != NULL);
96 assert(strftime(pp->str, sizeof pp->str, fmt, &tm1) != 0);
97 TAILQ_INSERT_HEAD(ph, pp, list);
99 pp->bytes_read += bytes;
103 report_good_read(time_t now, size_t bytes)
106 report_good_read2(now, bytes, &minute, 60L);
107 report_good_read2(now, bytes, &quarter, 900L);
108 report_good_read2(now, bytes, &hour, 3600L);
109 report_good_read2(now, bytes, &day, 86400L);
113 report_one_period(const char *period, struct period_head *ph)
119 printf("%s \xe2\x94\x82", period);
120 TAILQ_FOREACH(pp, ph, list) {
122 TAILQ_REMOVE(ph, pp, list);
127 printf(" \xe2\x94\x82");
128 printf(" %s %14jd", pp->str, pp->bytes_read);
131 printf(" \xe2\x94\x82");
132 printf(" %5s %14s", "", "");
140 report_one_period("1m ", &minute);
141 report_one_period("15m", &quarter);
142 report_one_period("1h ", &hour);
143 report_one_period("1d ", &day);
146 /**********************************************************************/
153 if (!isatty(STDIN_FILENO) || ioctl(STDIN_FILENO, TIOCGWINSZ, &wsz))
159 report_header(int eol)
161 printf("%13s %7s %13s %5s %13s %13s %9s",
177 report_hline(const char *how)
181 for (j = 0; j < REPORTWID; j++) {
182 if (how && (j == 4 || j == 29 || j == 54)) {
185 printf("\xe2\x94\x80");
191 static off_t hist[REPORTWID];
192 static off_t last_done = -1;
195 report_histogram(const struct lump *lp)
197 off_t j, bucket, fp, fe, k, now;
201 bucket = tot_size / REPORTWID;
202 if (tot_size > bucket * REPORTWID)
204 if (done_size != last_done) {
205 memset(hist, 0, sizeof hist);
206 TAILQ_FOREACH(lp2, &lumps, list) {
208 fe = lp2->start + lp2->len;
209 for (j = fp / bucket; fp < fe; j++) {
210 k = (j + 1) * bucket;
218 last_done = done_size;
220 now = lp->start / bucket;
221 for (j = 0; j < REPORTWID; j++) {
222 a = round(8 * (double)hist[j] / bucket);
223 assert (a >= 0 && a < 9);
224 if (a == 0 && hist[j])
233 putchar(0x80 + (int)a);
242 report(const struct lump *lp, size_t sz)
250 printf("\x1b[H%s\x1b[K\n", input);
256 printf("%13jd %7zu %13jd %5d %13jd %13jd %9.4f",
262 (intmax_t)(tot_size - done_size),
263 100*(double)done_size/(double)tot_size
269 report_histogram(lp);
270 if (TAILQ_EMPTY(&minute)) {
273 report_hline("\xe2\x94\xac");
275 report_hline("\xe2\x94\xb4");
277 j = ioctl(STDIN_FILENO, TIOCGWINSZ, &wsz);
279 printf("\x1b[%d;1H", wsz.ws_row);
284 /**********************************************************************/
287 new_lump(off_t start, off_t len, int state)
291 lp = malloc(sizeof *lp);
293 err(1, "Malloc failed");
297 TAILQ_INSERT_TAIL(&lumps, lp, list);
300 /**********************************************************************
301 * Save the worklist if -w was given
311 if (fdw >= 0 && fdatasync(fdw))
312 err(1, "Write error, probably disk full");
314 if (wworklist != NULL) {
315 bprintf(buf, "%s.tmp", wworklist);
316 (void)fprintf(stderr, "\nSaving worklist ...");
317 (void)fflush(stderr);
319 file = fopen(buf, "w");
321 err(1, "Error opening file %s", buf);
323 TAILQ_FOREACH(llp, &lumps, list)
324 fprintf(file, "%jd %jd %d\n",
325 (intmax_t)llp->start, (intmax_t)llp->len,
328 if (ferror(file) || fdatasync(fileno(file)) || fclose(file))
329 err(1, "Error writing file %s", buf);
330 if (rename(buf, wworklist))
331 err(1, "Error renaming %s to %s", buf, wworklist);
332 (void)fprintf(stderr, " done.\n");
336 /* Read the worklist if -r was given */
338 read_worklist(off_t t)
344 (void)fprintf(stderr, "Reading worklist ...");
345 (void)fflush(stderr);
346 file = fopen(rworklist, "r");
348 err(1, "Error opening file %s", rworklist);
354 if (3 != fscanf(file, "%jd %jd %d\n", &s, &l, &state)) {
356 err(1, "Error parsing file %s at line %d",
361 new_lump(s, l, state);
365 err(1, "Error closing file %s", rworklist);
366 (void)fprintf(stderr, " done.\n");
368 * Return the number of bytes already read
369 * (at least not in worklist).
374 /**********************************************************************/
377 write_buf(int fd, const void *buf, ssize_t len, off_t where)
381 i = pwrite(fd, buf, len, where);
385 printf("\nWrite error at %jd/%zu\n\t%s\n",
386 where, i, strerror(errno));
388 if (write_errors_are_fatal)
393 fill_buf(char *buf, ssize_t len, const char *pattern)
395 ssize_t sz = strlen(pattern);
398 for (i = 0; i < len; i += sz) {
402 memcpy(buf + i, pattern, j);
406 /**********************************************************************/
411 (void)fprintf(stderr, "usage: recoverdisk [-b bigsize] [-r readlist] "
412 "[-s interval] [-w writelist] source [destination]\n");
418 sighandler(__unused int sig)
425 main(int argc, char * const argv[])
435 u_int n, snapshot = 60;
436 static struct lump *lp;
438 while ((ch = getopt(argc, argv, "b:r:w:s:u:v")) != -1) {
441 bigsize = strtoul(optarg, NULL, 0);
444 rworklist = strdup(optarg);
445 if (rworklist == NULL)
446 err(1, "Cannot allocate enough memory");
449 snapshot = strtoul(optarg, NULL, 0);
452 unreadable_pattern = optarg;
458 wworklist = strdup(optarg);
459 if (wworklist == NULL)
460 err(1, "Cannot allocate enough memory");
470 if (argc < 1 || argc > 2)
474 fdr = open(argv[0], O_RDONLY);
476 err(1, "Cannot open read descriptor %s", argv[0]);
478 error = fstat(fdr, &sb);
480 err(1, "fstat failed");
481 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
482 error = ioctl(fdr, DIOCGSECTORSIZE, §orsize);
484 err(1, "DIOCGSECTORSIZE failed");
486 error = ioctl(fdr, DIOCGSTRIPESIZE, &stripesize);
487 if (error == 0 && stripesize > sectorsize)
488 sectorsize = stripesize;
490 minsize = sectorsize;
491 bigsize = rounddown(bigsize, sectorsize);
493 error = ioctl(fdr, DIOCGMEDIASIZE, &tot_size);
495 err(1, "DIOCGMEDIASIZE failed");
497 tot_size = sb.st_size;
500 if (bigsize < minsize)
503 for (ch = 0; (bigsize >> ch) > minsize; ch++)
505 medsize = bigsize >> (ch / 2);
506 medsize = rounddown(medsize, minsize);
508 fprintf(stderr, "Bigsize = %zu, medsize = %zu, minsize = %zu\n",
509 bigsize, medsize, minsize);
511 buf = malloc(bigsize);
513 err(1, "Cannot allocate %zu bytes buffer", bigsize);
516 fdw = open(argv[1], O_WRONLY | O_CREAT, DEFFILEMODE);
518 err(1, "Cannot open write descriptor %s", argv[1]);
519 if (ftruncate(fdw, tot_size) < 0)
520 err(1, "Cannot truncate output %s to %jd bytes",
521 argv[1], (intmax_t)tot_size);
525 if (rworklist != NULL) {
526 done_size = read_worklist(tot_size);
528 new_lump(0, tot_size, 0);
531 if (wworklist != NULL)
532 signal(SIGINT, sighandler);
542 lp = TAILQ_FIRST(&lumps);
545 while (lp->len > 0) {
548 sz = MIN(lp->len, (off_t)bigsize);
549 else if (lp->state == 1)
550 sz = MIN(lp->len, (off_t)medsize);
552 sz = MIN(lp->len, (off_t)minsize);
556 if (t1 != t2 || lp->len < (off_t)bigsize) {
558 if (++n == snapshot) {
565 j = pread(fdr, buf, sz, lp->start);
567 if (!(random() & 0xf)) {
575 write_buf(fdw, buf, sz, lp->start);
578 if (verbose && lp->state > 2)
579 report_good_read(t2, sz);
584 printf("%jd %zu %d read error (%s)\n",
585 lp->start, sz, lp->state, strerror(error));
588 if (fdw >= 0 && strlen(unreadable_pattern)) {
589 fill_buf(buf, sz, unreadable_pattern);
590 write_buf(fdw, buf, sz, lp->start);
592 new_lump(lp->start, sz, lp->state + 1);
595 if (error == EINVAL) {
596 printf("Try with -b 131072 or lower ?\n");
600 if (error == ENXIO) {
601 printf("Input device probably detached...\n");
608 if (aborting || !TAILQ_NEXT(lp, list))
612 assert(lp->len == 0);
613 TAILQ_REMOVE(&lumps, lp, list);
616 printf("%s", aborting ? "Aborted\n" : "Completed\n");