11 static char *dev_name;
12 static char *out_name;
13 static char *stat_name;
14 static uint block_size = 1048576;
15 static uint error_skip;
17 static u64 end_pos = ~(u64)0;
18 static int show_status;
25 static struct stat_rec *status;
27 #define ASORT_PREFIX(x) ss_##x
28 #define ASORT_KEY_TYPE struct stat_rec
29 #define ASORT_LT(x,y) x.pos < y.pos
30 #include <ucw/sorter/array-simple.h>
32 static void stat_read(int fd)
34 u64 len = ucw_seek(fd, 0, SEEK_END);
35 ucw_seek(fd, 0, SEEK_SET);
36 if (len % sizeof(struct stat_rec))
37 die("Malformed status file");
39 die("Status file too long");
41 uns items = len / sizeof(struct stat_rec);
42 GARY_INIT(status, items);
43 if (careful_read(fd, status, len) <= 0)
44 die("Error reading status file");
46 ss_sort(status, items);
49 for (uint i=0; i<items; i++)
50 printf("%jd %jd\n", (intmax_t) status[i].pos, (intmax_t) status[i].len);
53 for (uint i=1; i<items; i++)
54 if (status[i].pos < status[i-1].pos + status[i-1].len)
55 die("Overlapping status file entries: [%jd,%jd] with [%jd,%jd]",
56 (intmax_t) status[i-1].pos,
57 (intmax_t)(status[i-1].pos + status[i-1].len),
58 (intmax_t) status[i].pos,
59 (intmax_t)(status[i].pos + status[i].len));
62 static u64 stat_have(void)
65 for (uint i=0; i<GARY_SIZE(status); i++)
67 u64 x = CLAMP(status[i].pos, start_pos, end_pos);
68 u64 y = CLAMP(status[i].pos + status[i].len, start_pos, end_pos);
74 static void stat_show(void)
77 for (uint i=0; i<GARY_SIZE(status); i++)
79 if (status[i].pos > last)
80 printf("Missing: %ju +%ju\n", (intmax_t) last, (intmax_t)(status[i].pos - last));
81 last = status[i].pos + status[i].len;
83 printf("End at %ju\n", (intmax_t) last);
86 static struct opt_section options = {
88 OPT_HELP("Usage: ddigger [options] block-device output-file status-file"),
89 OPT_HELP(" or: ddigger [options] --status - - status-file"),
93 OPT_STRING(OPT_POSITIONAL(1), NULL, dev_name, OPT_REQUIRED, ""),
94 OPT_STRING(OPT_POSITIONAL(2), NULL, out_name, OPT_REQUIRED, ""),
95 OPT_STRING(OPT_POSITIONAL(3), NULL, stat_name, OPT_REQUIRED, ""),
96 OPT_UINT('b', "block-size", block_size, OPT_REQUIRED_VALUE, "<bytes>\tTransfer block size (default=1M)"),
97 OPT_UINT('e', "error-skip", error_skip, OPT_REQUIRED_VALUE, "<bytes>\tHow far to skip on error (default=block size)"),
98 OPT_U64(0, "start", start_pos, OPT_REQUIRED_VALUE, "<bytes>\tStart position (default=beginning of device)"),
99 OPT_U64(0, "end", end_pos, OPT_REQUIRED_VALUE, "<bytes>\tEnd position (default=end of device)"),
100 OPT_SWITCH(0, "status", show_status, 1, 0, "\tShow contents of status file"),
105 int main(int argc UNUSED, char **argv)
107 opt_parse(&options, argv+1);
109 error_skip = block_size;
111 int stat_fd = ucw_open(stat_name, (show_status ? O_RDONLY : O_RDWR | O_CREAT), 0666);
113 die("Cannot open %s: %m", stat_name);
122 int dev_fd = ucw_open(dev_name, O_RDONLY | O_DIRECT);
124 die("Cannot open block device %s: %m", dev_name);
127 if (ioctl(dev_fd, BLKGETSIZE64, &dev_size) < 0)
128 die("BLKGETSIZE64: %m");
129 start_pos = MIN(dev_size, start_pos);
130 end_pos = MIN(dev_size, end_pos);
132 int out_fd = ucw_open(out_name, O_RDWR | O_CREAT, 0666);
134 die("Cannot open %s: %m", out_name);
136 if (ucw_ftruncate(out_fd, dev_size) < 0)
137 die("Cannot resize %s: %m", out_name);
139 u64 remains = (end_pos - start_pos) + stat_have();
140 ucw_seek(stat_fd, 0, SEEK_END);
141 *GARY_PUSH(status) = (struct stat_rec) { .pos = end_pos, .len = 0 };
143 byte *buf = big_alloc(MAX(block_size, 4096));
149 while (pos < end_pos)
151 while (status[stat_i].pos + status[stat_i].len < pos)
153 if (status[stat_i].pos <= pos)
155 pos = status[stat_i].pos + status[stat_i].len;
160 u64 len = MIN(block_size, status[stat_i].pos - pos);
161 if (pos % block_size)
162 len = MIN(len, block_size - pos % block_size);
164 #define GAUGE(curr,max) (curr), (max), 100*(double)(curr)/(double)((max)?:1)
165 fprintf(stderr, "Pos: %zd / %zd (%.2f%%) Rel: %zd / %zd (%.2f%%) Have: %zd / %zd (%.2f%%) Errors: %u\r",
166 GAUGE(pos, dev_size),
167 GAUGE(pos-start_pos, end_pos-start_pos),
168 GAUGE(have, remains),
172 if (ucw_seek(dev_fd, pos, SEEK_SET) < 0)
174 if (ucw_seek(out_fd, pos, SEEK_SET) < 0)
177 ssize_t done = read(dev_fd, buf, len);
178 if (done < (ssize_t) len)
180 fprintf(stderr, "\n");
181 // msg(L_INFO, "Error: %d bytes read at position %jd", (int) MAX(done, 0), (intmax_t) pos);
187 if (careful_write(out_fd, buf, len) < 0)
188 die("Error writing %s: %m", out_name);
190 struct stat_rec sr = { .pos = pos, .len = len };
191 if (careful_write(stat_fd, &sr, sizeof(sr)) < 0)
192 die("Error writing status file: %m");