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 /**********************************************************************/
154 if (!isatty(STDIN_FILENO) || ioctl(STDIN_FILENO, TIOCGWINSZ, &wsz))
161 report_header(int eol)
163 printf("%13s %7s %13s %5s %13s %13s %9s",
179 report_hline(const char *how)
183 for (j = 0; j < REPORTWID; j++) {
184 if (how && (j == 4 || j == 29 || j == 54)) {
187 printf("\xe2\x94\x80");
193 static off_t hist[REPORTWID];
194 static off_t last_done = -1;
197 report_histogram(const struct lump *lp)
199 off_t j, bucket, fp, fe, k, now;
203 bucket = tot_size / REPORTWID;
204 if (tot_size > bucket * REPORTWID)
206 if (done_size != last_done) {
207 memset(hist, 0, sizeof hist);
208 TAILQ_FOREACH(lp2, &lumps, list) {
210 fe = lp2->start + lp2->len;
211 for (j = fp / bucket; fp < fe; j++) {
212 k = (j + 1) * bucket;
220 last_done = done_size;
222 now = lp->start / bucket;
223 for (j = 0; j < REPORTWID; j++) {
224 a = round(8 * (double)hist[j] / bucket);
225 assert (a >= 0 && a < 9);
226 if (a == 0 && hist[j])
235 putchar(0x80 + (int)a);
244 report(const struct lump *lp, size_t sz)
252 printf("\x1b[H%s\x1b[K\n", input);
258 printf("%13jd %7zu %13jd %5d %13jd %13jd %9.4f",
264 (intmax_t)(tot_size - done_size),
265 100*(double)done_size/(double)tot_size
271 report_histogram(lp);
272 if (TAILQ_EMPTY(&minute)) {
275 report_hline("\xe2\x94\xac");
277 report_hline("\xe2\x94\xb4");
279 j = ioctl(STDIN_FILENO, TIOCGWINSZ, &wsz);
281 printf("\x1b[%d;1H", wsz.ws_row);
286 /**********************************************************************/
289 new_lump(off_t start, off_t len, int state)
293 lp = malloc(sizeof *lp);
295 err(1, "Malloc failed");
299 TAILQ_INSERT_TAIL(&lumps, lp, list);
302 /**********************************************************************
303 * Save the worklist if -w was given
313 if (fdw >= 0 && fdatasync(fdw))
314 err(1, "Write error, probably disk full");
316 if (wworklist != NULL) {
317 bprintf(buf, "%s.tmp", wworklist);
318 (void)fprintf(stderr, "\nSaving worklist ...");
319 (void)fflush(stderr);
321 file = fopen(buf, "w");
323 err(1, "Error opening file %s", buf);
325 TAILQ_FOREACH(llp, &lumps, list)
326 fprintf(file, "%jd %jd %d\n",
327 (intmax_t)llp->start, (intmax_t)llp->len,
330 if (ferror(file) || fdatasync(fileno(file)) || fclose(file))
331 err(1, "Error writing file %s", buf);
332 if (rename(buf, wworklist))
333 err(1, "Error renaming %s to %s", buf, wworklist);
334 (void)fprintf(stderr, " done.\n");
338 /* Read the worklist if -r was given */
340 read_worklist(off_t t)
346 (void)fprintf(stderr, "Reading worklist ...");
347 (void)fflush(stderr);
348 file = fopen(rworklist, "r");
350 err(1, "Error opening file %s", rworklist);
356 if (3 != fscanf(file, "%jd %jd %d\n", &s, &l, &state)) {
358 err(1, "Error parsing file %s at line %d",
363 new_lump(s, l, state);
367 err(1, "Error closing file %s", rworklist);
368 (void)fprintf(stderr, " done.\n");
370 * Return the number of bytes already read
371 * (at least not in worklist).
376 /**********************************************************************/
379 write_buf(int fd, const void *buf, ssize_t len, off_t where)
383 i = pwrite(fd, buf, len, where);
387 printf("\nWrite error at %jd/%zu\n\t%s\n",
388 where, i, strerror(errno));
390 if (write_errors_are_fatal)
395 fill_buf(char *buf, ssize_t len, const char *pattern)
397 ssize_t sz = strlen(pattern);
400 for (i = 0; i < len; i += sz) {
404 memcpy(buf + i, pattern, j);
408 /**********************************************************************/
413 (void)fprintf(stderr, "usage: recoverdisk [-b bigsize] [-r readlist] "
414 "[-s interval] [-w writelist] source [destination]\n");
420 sighandler(__unused int sig)
427 main(int argc, char * const argv[])
437 u_int n, snapshot = 60;
438 static struct lump *lp;
440 while ((ch = getopt(argc, argv, "b:r:w:s:u:v")) != -1) {
443 bigsize = strtoul(optarg, NULL, 0);
446 rworklist = strdup(optarg);
447 if (rworklist == NULL)
448 err(1, "Cannot allocate enough memory");
451 snapshot = strtoul(optarg, NULL, 0);
454 unreadable_pattern = optarg;
460 wworklist = strdup(optarg);
461 if (wworklist == NULL)
462 err(1, "Cannot allocate enough memory");
472 if (argc < 1 || argc > 2)
476 fdr = open(argv[0], O_RDONLY);
478 err(1, "Cannot open read descriptor %s", argv[0]);
480 error = fstat(fdr, &sb);
482 err(1, "fstat failed");
483 if (S_ISBLK(sb.st_mode) || S_ISCHR(sb.st_mode)) {
484 error = ioctl(fdr, DIOCGSECTORSIZE, §orsize);
486 err(1, "DIOCGSECTORSIZE failed");
488 error = ioctl(fdr, DIOCGSTRIPESIZE, &stripesize);
489 if (error == 0 && stripesize > sectorsize)
490 sectorsize = stripesize;
492 minsize = sectorsize;
493 bigsize = rounddown(bigsize, sectorsize);
495 error = ioctl(fdr, DIOCGMEDIASIZE, &tot_size);
497 err(1, "DIOCGMEDIASIZE failed");
499 tot_size = sb.st_size;
502 if (bigsize < minsize)
505 for (ch = 0; (bigsize >> ch) > minsize; ch++)
507 medsize = bigsize >> (ch / 2);
508 medsize = rounddown(medsize, minsize);
510 fprintf(stderr, "Bigsize = %zu, medsize = %zu, minsize = %zu\n",
511 bigsize, medsize, minsize);
513 buf = malloc(bigsize);
515 err(1, "Cannot allocate %zu bytes buffer", bigsize);
518 fdw = open(argv[1], O_WRONLY | O_CREAT, DEFFILEMODE);
520 err(1, "Cannot open write descriptor %s", argv[1]);
521 if (ftruncate(fdw, tot_size) < 0)
522 err(1, "Cannot truncate output %s to %jd bytes",
523 argv[1], (intmax_t)tot_size);
527 if (rworklist != NULL) {
528 done_size = read_worklist(tot_size);
530 new_lump(0, tot_size, 0);
533 if (wworklist != NULL)
534 signal(SIGINT, sighandler);
544 lp = TAILQ_FIRST(&lumps);
547 while (lp->len > 0) {
550 sz = MIN(lp->len, (off_t)bigsize);
551 else if (lp->state == 1)
552 sz = MIN(lp->len, (off_t)medsize);
554 sz = MIN(lp->len, (off_t)minsize);
558 if (t1 != t2 || lp->len < (off_t)bigsize) {
560 if (++n == snapshot) {
567 j = pread(fdr, buf, sz, lp->start);
569 if (!(random() & 0xf)) {
577 write_buf(fdw, buf, sz, lp->start);
580 if (verbose && lp->state > 2)
581 report_good_read(t2, sz);
586 printf("%jd %zu %d read error (%s)\n",
587 lp->start, sz, lp->state, strerror(error));
590 if (error == EINVAL) {
591 printf("Try with -b 131072 or lower ?\n");
595 if (error == ENXIO) {
596 printf("Input device probably detached...\n");
600 if (fdw >= 0 && strlen(unreadable_pattern)) {
601 fill_buf(buf, sz, unreadable_pattern);
602 write_buf(fdw, buf, sz, lp->start);
604 new_lump(lp->start, sz, lp->state + 1);
610 if (aborting || !TAILQ_NEXT(lp, list))
614 assert(lp->len == 0);
615 TAILQ_REMOVE(&lumps, lp, list);
618 printf("%s", aborting ? "Aborted\n" : "Completed\n");