Add an indexing feature to the -t option.
[dragonfly.git] / usr.bin / undo / undo.c
CommitLineData
95d7e54d
MD
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
f95efeca 34 * $DragonFly: src/usr.bin/undo/undo.c,v 1.6 2008/07/17 21:34:47 thomas Exp $
95d7e54d
MD
35 */
36/*
37 * UNDO - retrieve an older version of a file.
38 */
39
40#include <sys/types.h>
41#include <sys/stat.h>
42#include <sys/wait.h>
bf31f16d 43#include <sys/tree.h>
95d7e54d
MD
44#include <stdio.h>
45#include <stdlib.h>
46#include <stdarg.h>
47#include <string.h>
48#include <unistd.h>
49#include <fcntl.h>
50#include <errno.h>
51#include <vfs/hammer/hammer_disk.h>
52#include <vfs/hammer/hammer_ioctl.h>
53
bf31f16d
MD
54/*
55 * Sorted list of transaction ids
56 */
57struct undo_hist_entry;
58RB_HEAD(undo_hist_entry_rb_tree, undo_hist_entry);
59RB_PROTOTYPE2(undo_hist_entry_rb_tree, undo_hist_entry, rbnode,
60 undo_hist_entry_compare, hammer_tid_t);
61
62struct undo_hist_entry {
63 RB_ENTRY(undo_hist_entry) rbnode;
64 struct hammer_ioc_hist_entry tse;
b9a33d3f 65 ino_t inum;
bf31f16d
MD
66};
67
95d7e54d 68enum undo_type { TYPE_FILE, TYPE_DIFF, TYPE_RDIFF, TYPE_HISTORY };
b9a33d3f
MD
69enum undo_cmd { CMD_DUMP, CMD_ITERATEALL };
70
71#define UNDO_FLAG_MULT 0x0001
72#define UNDO_FLAG_INOCHG 0x0002
965778c8
MD
73#define UNDO_FLAG_SETTID1 0x0004
74#define UNDO_FLAG_SETTID2 0x0008
95d7e54d 75
bf31f16d
MD
76static int undo_hist_entry_compare(struct undo_hist_entry *he1,
77 struct undo_hist_entry *he2);
b9a33d3f
MD
78static void doiterate(const char *filename, const char *outFileName,
79 const char *outFilePostfix, int flags,
80 struct hammer_ioc_hist_entry ts1,
965778c8 81 struct hammer_ioc_hist_entry ts2,
b9a33d3f 82 enum undo_cmd cmd, enum undo_type type);
95d7e54d
MD
83static void dogenerate(const char *filename, const char *outFileName,
84 const char *outFilePostfix,
b9a33d3f 85 int flags, int idx, enum undo_type type,
1d9f6aa1 86 struct hammer_ioc_hist_entry ts1,
b9a33d3f
MD
87 struct hammer_ioc_hist_entry ts2);
88static void collect_history(int fd, int *error,
bf31f16d 89 struct undo_hist_entry_rb_tree *tse_tree);
b9a33d3f
MD
90static void collect_dir_history(const char *filename, int *error,
91 struct undo_hist_entry_rb_tree *dir_tree);
92static void clean_tree(struct undo_hist_entry_rb_tree *tree);
965778c8
MD
93static hammer_tid_t parse_delta_time(const char *timeStr, int *flags,
94 int ind_flag);
95d7e54d 95static void runcmd(int fd, const char *cmd, ...);
1d9f6aa1 96static char *timestamp(hammer_ioc_hist_entry_t hen);
95d7e54d
MD
97static void usage(void);
98
99static int VerboseOpt;
100
bf31f16d
MD
101RB_GENERATE2(undo_hist_entry_rb_tree, undo_hist_entry, rbnode,
102 undo_hist_entry_compare, hammer_tid_t, tse.tid);
103
104
95d7e54d
MD
105int
106main(int ac, char **av)
107{
108 const char *outFileName = NULL;
109 const char *outFilePostfix = NULL;
b9a33d3f 110 enum undo_cmd cmd;
95d7e54d 111 enum undo_type type;
1d9f6aa1
MD
112 struct hammer_ioc_hist_entry ts1;
113 struct hammer_ioc_hist_entry ts2;
95d7e54d 114 int c;
965778c8 115 int count_t;
b9a33d3f 116 int flags;
95d7e54d 117
1d9f6aa1
MD
118 bzero(&ts1, sizeof(ts1));
119 bzero(&ts2, sizeof(ts2));
120
95d7e54d
MD
121 cmd = CMD_DUMP;
122 type = TYPE_FILE;
965778c8
MD
123 count_t = 0;
124 flags = 0;
95d7e54d 125
59f01588 126 while ((c = getopt(ac, av, "adDiuvo:t:")) != -1) {
95d7e54d
MD
127 switch(c) {
128 case 'd':
95d7e54d
MD
129 type = TYPE_DIFF;
130 break;
131 case 'D':
95d7e54d
MD
132 type = TYPE_RDIFF;
133 break;
134 case 'i':
135 if (type != TYPE_FILE)
136 usage();
137 type = TYPE_HISTORY;
b9a33d3f 138 cmd = CMD_ITERATEALL;
95d7e54d 139 break;
59f01588 140 case 'a':
95d7e54d
MD
141 cmd = CMD_ITERATEALL;
142 break;
143 case 'u':
144 outFilePostfix = ".undo";
145 break;
146 case 'v':
147 ++VerboseOpt;
148 break;
149 case 'o':
150 outFileName = optarg;
151 break;
152 case 't':
965778c8
MD
153 /*
154 * Parse one or two -t options. If two are specified
155 * -d is implied (but may be overridden)
156 */
157 ++count_t;
158 if (count_t == 1) {
159 ts1.tid = parse_delta_time(optarg, &flags,
160 UNDO_FLAG_SETTID1);
161 } else if (count_t == 2) {
162 ts2.tid = parse_delta_time(optarg, &flags,
163 UNDO_FLAG_SETTID2);
164 if (type == TYPE_FILE)
165 type = TYPE_DIFF;
166 } else {
95d7e54d 167 usage();
965778c8 168 }
95d7e54d
MD
169 break;
170 default:
171 usage();
172 /* NOT REACHED */
173 break;
174 }
175 }
176
177 /*
178 * Option validation
179 */
180 if (outFileName && outFilePostfix) {
181 fprintf(stderr, "The -o option may not be combined with -u\n");
182 usage();
183 }
184
185 ac -= optind;
186 av += optind;
b9a33d3f
MD
187 if (ac > 1)
188 flags |= UNDO_FLAG_MULT;
95d7e54d
MD
189
190 if (ac == 0)
191 usage();
192
193 /*
194 * Validate the output template, if specified.
195 */
b9a33d3f 196 if (outFileName && (flags & UNDO_FLAG_MULT)) {
95d7e54d
MD
197 const char *ptr = outFileName;
198 int didStr = 0;
199
200 while ((ptr = strchr(ptr, '%')) != NULL) {
201 if (ptr[1] == 's') {
202 if (didStr) {
203 fprintf(stderr, "Malformed output "
204 "template\n");
205 usage();
206 }
207 didStr = 1;
208 ++ptr;
209 } else if (ptr[1] != '%') {
210 fprintf(stderr, "Malformed output template\n");
211 usage();
212 } else {
213 ptr += 2;
214 }
215 }
216 }
217
218 while (ac) {
b9a33d3f 219 doiterate(*av, outFileName, outFilePostfix,
965778c8 220 flags, ts1, ts2, cmd, type);
95d7e54d
MD
221 ++av;
222 --ac;
223 }
224 return(0);
225}
226
227/*
b9a33d3f 228 * Iterate through a file's history. If cmd == CMD_DUMP we take the
965778c8
MD
229 * next-to-last transaction id, unless another given. Otherwise if
230 * cmd == CMD_ITERATEALL we scan all transaction ids.
b9a33d3f
MD
231 *
232 * Also iterate through the directory's history to locate other inodes that
233 * used the particular file name.
95d7e54d
MD
234 */
235static
236void
b9a33d3f
MD
237doiterate(const char *filename, const char *outFileName,
238 const char *outFilePostfix, int flags,
239 struct hammer_ioc_hist_entry ts1,
965778c8 240 struct hammer_ioc_hist_entry ts2,
b9a33d3f 241 enum undo_cmd cmd, enum undo_type type)
95d7e54d 242{
b9a33d3f 243 struct undo_hist_entry_rb_tree dir_tree;
bf31f16d
MD
244 struct undo_hist_entry_rb_tree tse_tree;
245 struct undo_hist_entry *tse1;
246 struct undo_hist_entry *tse2;
965778c8 247 struct hammer_ioc_hist_entry tid_max;
95d7e54d 248 char *path = NULL;
95d7e54d
MD
249 int i;
250 int fd;
b9a33d3f 251 int error;
95d7e54d 252
b9a33d3f 253 RB_INIT(&dir_tree);
bf31f16d
MD
254 RB_INIT(&tse_tree);
255
9380de42 256 tid_max.tid = HAMMER_MAX_TID;
1d9f6aa1
MD
257 tid_max.time32 = 0;
258
b9a33d3f
MD
259 /*
260 * Use the directory history to locate all possible versions of
261 * the file.
262 */
263 collect_dir_history(filename, &error, &dir_tree);
264 RB_FOREACH(tse1, undo_hist_entry_rb_tree, &dir_tree) {
265 asprintf(&path, "%s@@0x%016llx", filename, tse1->tse.tid);
266 if ((fd = open(path, O_RDONLY)) > 0) {
267 collect_history(fd, &error, &tse_tree);
268 close(fd);
95d7e54d
MD
269 }
270 }
b9a33d3f 271 if (cmd == CMD_DUMP) {
965778c8
MD
272 if ((ts1.tid == 0 ||
273 flags & (UNDO_FLAG_SETTID1|UNDO_FLAG_SETTID2)) &&
274 RB_EMPTY(&tse_tree)) {
b9a33d3f
MD
275 if ((fd = open(filename, O_RDONLY)) > 0) {
276 collect_history(fd, &error, &tse_tree);
277 close(fd);
278 }
279 }
965778c8
MD
280 /*
281 * Find entry if tid set to placeholder index
282 */
283 if (flags & UNDO_FLAG_SETTID1){
284 tse1 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree);
285 while (tse1 && ts1.tid--) {
286 tse1 = RB_PREV(undo_hist_entry_rb_tree,
287 &tse_tree, tse1);
288 }
289 if (tse1)
290 ts1 = tse1->tse;
291 else
292 ts1.tid = 0;
293 }
294 if (flags & UNDO_FLAG_SETTID2){
295 tse2 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree);
296 while (tse2 && ts2.tid--) {
297 tse2 = RB_PREV(undo_hist_entry_rb_tree,
298 &tse_tree, tse2);
299 }
300 if (tse2)
301 ts2 = tse2->tse;
302 else
303 ts2.tid = 0;
304 }
305
306 /*
307 * Single entry, most recent prior to current
308 */
b9a33d3f 309 if (ts1.tid == 0) {
89b353f3
SS
310 tse2 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree);
311 if (tse2) {
312 ts2 = tse2->tse;
b9a33d3f 313 tse1 = RB_PREV(undo_hist_entry_rb_tree,
89b353f3 314 &tse_tree, tse2);
b9a33d3f
MD
315 if (tse1)
316 ts1 = tse1->tse;
317 }
318 }
319 if (ts1.tid == 0) {
320 printf("%s: No UNDO history found\n", filename);
321 } else {
322 dogenerate(filename,
323 outFileName, outFilePostfix,
324 0, 0, type,
89b353f3 325 ts1, ts2);
b9a33d3f
MD
326 }
327 } else if (RB_ROOT(&tse_tree)) {
328 /*
329 * Iterate entire history
330 */
331 printf("%s: ITERATE ENTIRE HISTORY\n", filename);
95d7e54d 332
bf31f16d 333 tse1 = NULL;
004f5c6f 334 i = 0;
bf31f16d
MD
335 RB_FOREACH(tse2, undo_hist_entry_rb_tree, &tse_tree) {
336 if (tse1) {
b9a33d3f 337 dogenerate(filename,
bf31f16d 338 outFileName, outFilePostfix,
b9a33d3f
MD
339 flags, i, type,
340 tse1->tse, tse2->tse);
bf31f16d 341 }
b9a33d3f
MD
342 if (tse1 && tse2->inum != tse1->inum)
343 flags |= UNDO_FLAG_INOCHG;
344 else
345 flags &= ~UNDO_FLAG_INOCHG;
bf31f16d
MD
346 tse1 = tse2;
347 ++i;
004f5c6f 348 }
8debe471
SS
349 /*
350 * There is no delta to print for the last pair,
351 * because they are identical.
352 */
353 if (type != TYPE_DIFF && type != TYPE_RDIFF) {
354 dogenerate(filename,
355 outFileName, outFilePostfix,
356 flags, i, type,
357 tse1->tse, tid_max);
358 }
95d7e54d
MD
359 } else {
360 printf("%s: ITERATE ENTIRE HISTORY: %s\n",
b9a33d3f 361 filename, strerror(error));
95d7e54d
MD
362 }
363 if (path)
364 free(path);
b9a33d3f
MD
365 clean_tree(&dir_tree);
366 clean_tree(&tse_tree);
95d7e54d
MD
367}
368
369/*
370 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
371 * through ts2.
372 */
373static
374void
375dogenerate(const char *filename, const char *outFileName,
376 const char *outFilePostfix,
b9a33d3f 377 int flags, int idx, enum undo_type type,
1d9f6aa1 378 struct hammer_ioc_hist_entry ts1,
b9a33d3f 379 struct hammer_ioc_hist_entry ts2)
95d7e54d
MD
380{
381 struct stat st;
382 const char *elm;
383 char *ipath1 = NULL;
384 char *ipath2 = NULL;
385 FILE *fi;
386 FILE *fp;
387 char *buf;
388 char *path;
b9a33d3f
MD
389 time_t t;
390 struct tm *tp;
391 char datestr[64];
95d7e54d
MD
392 int n;
393
394 buf = malloc(8192);
395
396 /*
397 * Open the input file. If ts1 is 0 try to locate the most recent
398 * version of the file prior to the current version.
399 */
1d9f6aa1 400 if (ts1.tid == 0)
59f01588 401 asprintf(&ipath1, "%s", filename);
bf31f16d
MD
402 else
403 asprintf(&ipath1, "%s@@0x%016llx", filename, ts1.tid);
95d7e54d 404
bf31f16d 405 if (ts2.tid == 0)
95d7e54d 406 asprintf(&ipath2, "%s", filename);
bf31f16d 407 else
004f5c6f 408 asprintf(&ipath2, "%s@@0x%016llx", filename, ts2.tid);
bf31f16d
MD
409
410 if (lstat(ipath1, &st) < 0 && lstat(ipath2, &st) < 0) {
b9a33d3f 411 if (idx == 0 || VerboseOpt) {
bf31f16d
MD
412 fprintf(stderr, "Unable to access either %s or %s\n",
413 ipath1, ipath2);
95d7e54d 414 }
bf31f16d
MD
415 free(ipath1);
416 free(ipath2);
417 goto done;
95d7e54d
MD
418 }
419
420 /*
421 * elm is the last component of the input file name
422 */
423 if ((elm = strrchr(filename, '/')) != NULL)
424 ++elm;
425 else
426 elm = filename;
427
428 /*
429 * Where do we stuff our output?
430 */
431 if (outFileName) {
b9a33d3f 432 if (flags & UNDO_FLAG_MULT) {
95d7e54d
MD
433 asprintf(&path, outFileName, elm);
434 fp = fopen(path, "w");
435 if (fp == NULL) {
436 perror(path);
437 exit(1);
438 }
439 free(path);
440 } else {
441 fp = fopen(outFileName, "w");
442 if (fp == NULL) {
443 perror(outFileName);
444 exit(1);
445 }
446 }
447 } else if (outFilePostfix) {
448 if (idx >= 0) {
449 asprintf(&path, "%s%s.%04d", filename,
450 outFilePostfix, idx);
451 } else {
452 asprintf(&path, "%s%s", filename, outFilePostfix);
453 }
454 fp = fopen(path, "w");
455 if (fp == NULL) {
456 perror(path);
457 exit(1);
458 }
459 free(path);
460 } else {
b9a33d3f 461 if ((flags & UNDO_FLAG_MULT) && type == TYPE_FILE) {
95d7e54d
MD
462 if (idx >= 0) {
463 printf("\n>>> %s %04d 0x%016llx %s\n\n",
1d9f6aa1 464 filename, idx, ts1.tid, timestamp(&ts1));
95d7e54d
MD
465 } else {
466 printf("\n>>> %s ---- 0x%016llx %s\n\n",
1d9f6aa1 467 filename, ts1.tid, timestamp(&ts1));
95d7e54d
MD
468 }
469 } else if (idx >= 0 && type == TYPE_FILE) {
470 printf("\n>>> %s %04d 0x%016llx %s\n\n",
1d9f6aa1 471 filename, idx, ts1.tid, timestamp(&ts1));
95d7e54d
MD
472 }
473 fp = stdout;
474 }
475
476 switch(type) {
477 case TYPE_FILE:
478 if ((fi = fopen(ipath1, "r")) != NULL) {
479 while ((n = fread(buf, 1, 8192, fi)) > 0)
480 fwrite(buf, 1, n, fp);
481 fclose(fi);
482 }
483 break;
484 case TYPE_DIFF:
bf31f16d 485 printf("diff -N -r -u %s %s (to %s)\n",
1d9f6aa1 486 ipath1, ipath2, timestamp(&ts2));
95d7e54d 487 fflush(stdout);
bf31f16d 488 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath1, ipath2, NULL);
95d7e54d
MD
489 break;
490 case TYPE_RDIFF:
bf31f16d 491 printf("diff -N -r -u %s %s\n", ipath2, ipath1);
95d7e54d 492 fflush(stdout);
bf31f16d 493 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath2, ipath1, NULL);
95d7e54d
MD
494 break;
495 case TYPE_HISTORY:
b9a33d3f
MD
496 t = (time_t)ts1.time32;
497 tp = localtime(&t);
498 strftime(datestr, sizeof(datestr), "%d-%b-%Y %H:%M:%S", tp);
499 printf("\t0x%016llx %s", ts1.tid, datestr);
500 if (flags & UNDO_FLAG_INOCHG)
501 printf(" inode-change");
502 if (lstat(ipath1, &st) < 0)
503 printf(" file-deleted");
504 printf("\n");
95d7e54d
MD
505 break;
506 }
507
508 if (fp != stdout)
509 fclose(fp);
510done:
511 free(buf);
512}
513
1d9f6aa1 514static
b9a33d3f
MD
515void
516clean_tree(struct undo_hist_entry_rb_tree *tree)
95d7e54d 517{
bf31f16d 518 struct undo_hist_entry *tse;
95d7e54d 519
b9a33d3f
MD
520 while ((tse = RB_ROOT(tree)) != NULL) {
521 RB_REMOVE(undo_hist_entry_rb_tree, tree, tse);
bf31f16d
MD
522 free(tse);
523 }
95d7e54d
MD
524}
525
1d9f6aa1 526static
b9a33d3f 527void
bf31f16d 528collect_history(int fd, int *errorp, struct undo_hist_entry_rb_tree *tse_tree)
95d7e54d
MD
529{
530 struct hammer_ioc_history hist;
bf31f16d 531 struct undo_hist_entry *tse;
b9a33d3f 532 struct stat st;
bf31f16d 533 int istmp;
95d7e54d 534 int i;
95d7e54d 535
bf31f16d
MD
536 /*
537 * Setup
538 */
95d7e54d
MD
539 bzero(&hist, sizeof(hist));
540 hist.beg_tid = HAMMER_MIN_TID;
541 hist.end_tid = HAMMER_MAX_TID;
542 hist.head.flags |= HAMMER_IOC_HISTORY_ATKEY;
543 hist.key = 0;
544 hist.nxt_key = HAMMER_MAX_KEY;
545
bf31f16d
MD
546 *errorp = 0;
547
548 if (tse_tree == NULL) {
549 tse_tree = malloc(sizeof(*tse_tree));
550 RB_INIT(tse_tree);
551 istmp = 1;
552 } else {
553 istmp = 0;
554 }
555
556 /*
b9a33d3f
MD
557 * Save the inode so inode changes can be reported.
558 */
559 st.st_ino = 0;
560 fstat(fd, &st);
561
562 /*
bf31f16d
MD
563 * Collect a unique set of transaction ids
564 */
95d7e54d 565 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
bf31f16d 566 *errorp = errno;
95d7e54d
MD
567 goto done;
568 }
95d7e54d 569 for (;;) {
95d7e54d 570 for (i = 0; i < hist.count; ++i) {
bf31f16d
MD
571 tse = malloc(sizeof(*tse));
572 tse->tse = hist.hist_ary[i];
b9a33d3f 573 tse->inum = st.st_ino;
bf31f16d
MD
574 if (RB_INSERT(undo_hist_entry_rb_tree, tse_tree, tse)) {
575 free(tse);
576 }
95d7e54d
MD
577 }
578 if (hist.head.flags & HAMMER_IOC_HISTORY_EOF)
579 break;
580 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY) {
581 hist.key = hist.nxt_key;
582 hist.nxt_key = HAMMER_MAX_KEY;
583 }
584 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_TID)
585 hist.beg_tid = hist.nxt_tid;
586 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
bf31f16d 587 *errorp = errno;
95d7e54d
MD
588 break;
589 }
590 }
92c7f4e9
MD
591
592 /*
bf31f16d 593 * Cleanup
92c7f4e9 594 */
95d7e54d 595done:
bf31f16d 596 if (istmp) {
b9a33d3f 597 clean_tree(tse_tree);
bf31f16d 598 free(tse_tree);
95d7e54d 599 }
b9a33d3f
MD
600}
601
602static
603void
604collect_dir_history(const char *filename, int *errorp,
605 struct undo_hist_entry_rb_tree *dir_tree)
606{
607 char *dirname;
608 int fd;
609 int error;
610
611 *errorp = 0;
612 if (strrchr(filename, '/')) {
613 dirname = strdup(filename);
614 *strrchr(dirname, '/') = 0;
615 } else {
616 dirname = strdup(".");
617 }
618 if ((fd = open(dirname, O_RDONLY)) > 0) {
619 collect_history(fd, &error, dir_tree);
620 close(fd);
621 }
95d7e54d
MD
622}
623
624static
625hammer_tid_t
965778c8 626parse_delta_time(const char *timeStr, int *flags, int ind_flag)
95d7e54d
MD
627{
628 hammer_tid_t tid;
95d7e54d 629
1d9f6aa1 630 tid = strtoull(timeStr, NULL, 0);
965778c8
MD
631 if (timeStr[0] == '+')
632 ++timeStr;
633 if (timeStr[0] >= '0' && timeStr[0] <= '9' && timeStr[1] != 'x')
634 *flags |= ind_flag;
95d7e54d
MD
635 return(tid);
636}
637
638static void
639runcmd(int fd, const char *cmd, ...)
640{
641 va_list va;
642 pid_t pid;
643 char **av;
644 int ac;
645 int i;
646
647 va_start(va, cmd);
648 for (ac = 0; va_arg(va, void *) != NULL; ++ac)
649 ;
650 va_end(va);
651
652 av = malloc((ac + 1) * sizeof(char *));
653 va_start(va, cmd);
654 for (i = 0; i < ac; ++i)
655 av[i] = va_arg(va, char *);
656 va_end(va);
657 av[i] = NULL;
658
659 if ((pid = fork()) < 0) {
660 perror("fork");
661 exit(1);
662 } else if (pid == 0) {
663 if (fd != 1) {
664 dup2(fd, 1);
665 close(fd);
666 }
667 execv(cmd, av);
668 _exit(1);
669 } else {
670 while (waitpid(pid, NULL, 0) != pid)
671 ;
672 }
673 free(av);
674}
675
676/*
677 * Convert tid to timestamp.
678 */
679static char *
1d9f6aa1 680timestamp(hammer_ioc_hist_entry_t hen)
95d7e54d
MD
681{
682 static char timebuf[64];
1d9f6aa1 683 time_t t = (time_t)hen->time32;
95d7e54d
MD
684 struct tm *tp;
685
686 tp = localtime(&t);
1d9f6aa1 687 strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp);
95d7e54d
MD
688 return(timebuf);
689}
690
bf31f16d
MD
691static
692int
693undo_hist_entry_compare(struct undo_hist_entry *he1,
694 struct undo_hist_entry *he2)
695{
696 if (he1->tse.tid < he2->tse.tid)
697 return(-1);
698 if (he1->tse.tid > he2->tse.tid)
699 return(1);
700 return(0);
701}
702
95d7e54d
MD
703static void
704usage(void)
705{
59f01588 706 fprintf(stderr, "undo [-adDiuv] [-o outfile] "
bf31f16d 707 "[-t transaction-id] [-t transaction-id] path...\n"
59f01588
MD
708 " -a Iterate all historical segments\n"
709 " -d Forward diff\n"
95d7e54d
MD
710 " -D Reverse diff\n"
711 " -i Dump history transaction ids\n"
95d7e54d
MD
712 " -u Generate .undo files\n"
713 " -v Verbose\n"
8360ec08 714 " -o file Output to the specified file\n"
02a9630c 715 " -t TID Retrieve as of transaction-id, TID\n"
965778c8
MD
716 " (a second `-t TID' to diff two)\n"
717 " transaction ids must be prefixed with 0x, and\n"
718 " otherwise may specify an index starting at 0\n"
719 " and iterating backwards through the history.\n"
720 );
95d7e54d
MD
721 exit(1);
722}
723