xref: /dragonfly/usr.bin/undo/undo.c (revision 740ed10d)
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  *
34  * $DragonFly: src/usr.bin/undo/undo.c,v 1.6 2008/07/17 21:34:47 thomas Exp $
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>
43 #include <sys/tree.h>
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 
54 /*
55  * Sorted list of transaction ids
56  */
57 struct undo_hist_entry;
58 RB_HEAD(undo_hist_entry_rb_tree, undo_hist_entry);
59 RB_PROTOTYPE2(undo_hist_entry_rb_tree, undo_hist_entry, rbnode,
60 	undo_hist_entry_compare, hammer_tid_t);
61 
62 struct undo_hist_entry {
63 	RB_ENTRY(undo_hist_entry) rbnode;
64 	struct hammer_ioc_hist_entry tse;
65 	ino_t inum;
66 };
67 
68 enum undo_type { TYPE_FILE, TYPE_DIFF, TYPE_RDIFF, TYPE_HISTORY };
69 enum undo_cmd { CMD_DUMP, CMD_ITERATEALL };
70 
71 #define UNDO_FLAG_MULT		0x0001
72 #define UNDO_FLAG_INOCHG	0x0002
73 #define UNDO_FLAG_TID_INDEX1	0x0004
74 #define UNDO_FLAG_TID_INDEX2	0x0008
75 
76 static int undo_hist_entry_compare(struct undo_hist_entry *he1,
77 		    struct undo_hist_entry *he2);
78 static void doiterate(const char *filename, int flags,
79 		   struct hammer_ioc_hist_entry ts1,
80 		   struct hammer_ioc_hist_entry ts2,
81 		   enum undo_cmd cmd, enum undo_type type);
82 static int doiterate_dump(const char *filename, int flags,
83 		   struct undo_hist_entry_rb_tree *ptse_tree,
84 		   struct hammer_ioc_hist_entry ts1,
85 		   struct hammer_ioc_hist_entry ts2,
86 		   enum undo_type type);
87 static int doiterate_iterall(const char *filename, int flags,
88 		   struct undo_hist_entry_rb_tree *ptse_tree,
89 		   enum undo_type type);
90 static void dogenerate(const char *filename, int flags,
91 		   struct hammer_ioc_hist_entry ts1,
92 		   struct hammer_ioc_hist_entry ts2,
93 		   int idx, enum undo_type type);
94 static void collect_history(int fd, int *error,
95 		   struct undo_hist_entry_rb_tree *tse_tree);
96 static void collect_dir_history(const char *filename, int *error,
97 		   struct undo_hist_entry_rb_tree *dir_tree);
98 static void clean_tree(struct undo_hist_entry_rb_tree *tree);
99 static hammer_tid_t parse_delta_time(const char *timeStr, int *flags,
100 		   int ind_flag);
101 static void runcmd(int fd, const char *cmd, ...);
102 static char *timestamp(hammer_ioc_hist_entry_t hen);
103 static void usage(void);
104 
105 static int VerboseOpt;
106 static const char *OutFileName = NULL;
107 static const char *OutFilePostfix = NULL;
108 
109 RB_GENERATE2(undo_hist_entry_rb_tree, undo_hist_entry, rbnode,
110 	undo_hist_entry_compare, hammer_tid_t, tse.tid);
111 
112 
113 int
114 main(int ac, char **av)
115 {
116 	enum undo_cmd cmd;
117 	enum undo_type type;
118 	struct hammer_ioc_hist_entry ts1;
119 	struct hammer_ioc_hist_entry ts2;
120 	int c;
121 	int count_t;
122 	int flags;
123 
124 	bzero(&ts1, sizeof(ts1));
125 	bzero(&ts2, sizeof(ts2));
126 
127 	cmd = CMD_DUMP;
128 	type = TYPE_FILE;
129 	count_t = 0;
130 	flags = 0;
131 
132 	while ((c = getopt(ac, av, "adDiuvo:t:")) != -1) {
133 		switch(c) {
134 		case 'd':
135 			type = TYPE_DIFF;
136 			break;
137 		case 'D':
138 			type = TYPE_RDIFF;
139 			break;
140 		case 'i':
141 			if (type != TYPE_FILE)
142 				usage();
143 			type = TYPE_HISTORY;
144 			cmd = CMD_ITERATEALL;
145 			break;
146 		case 'a':
147 			cmd = CMD_ITERATEALL;
148 			break;
149 		case 'u':
150 			OutFilePostfix = ".undo";
151 			break;
152 		case 'v':
153 			++VerboseOpt;
154 			break;
155 		case 'o':
156 			OutFileName = optarg;
157 			break;
158 		case 't':
159 			/*
160 			 * Parse one or two -t options.  If two are specified
161 			 * -d is implied (but may be overridden)
162 			 */
163 			++count_t;
164 			if (count_t == 1) {
165 				ts1.tid = parse_delta_time(optarg, &flags,
166 							UNDO_FLAG_TID_INDEX1);
167 			} else if (count_t == 2) {
168 				ts2.tid = parse_delta_time(optarg, &flags,
169 							UNDO_FLAG_TID_INDEX2);
170 				if (type == TYPE_FILE)
171 					type = TYPE_DIFF;
172 			} else {
173 				usage();
174 			}
175 			break;
176 		default:
177 			usage();
178 			/* NOT REACHED */
179 			break;
180 		}
181 	}
182 
183 	/*
184 	 * Option validation
185 	 */
186 	if (OutFileName && OutFilePostfix) {
187 		fprintf(stderr, "The -o option may not be combined with -u\n");
188 		usage();
189 	}
190 
191 	ac -= optind;
192 	av += optind;
193 	if (ac > 1)
194 		flags |= UNDO_FLAG_MULT;
195 
196 	if (ac == 0)
197 		usage();
198 
199 	/*
200 	 * Validate the output template, if specified.
201 	 */
202 	if (OutFileName && (flags & UNDO_FLAG_MULT)) {
203 		const char *ptr = OutFileName;
204 		int didStr = 0;
205 
206 		while ((ptr = strchr(ptr, '%')) != NULL) {
207 			if (ptr[1] == 's') {
208 				if (didStr) {
209 					fprintf(stderr, "Malformed output "
210 							"template\n");
211 					usage();
212 				}
213 				didStr = 1;
214 				++ptr;
215 			} else if (ptr[1] != '%') {
216 				fprintf(stderr, "Malformed output template\n");
217 				usage();
218 			} else {
219 				ptr += 2;
220 			}
221 		}
222 	}
223 
224 	while (ac) {
225 		doiterate(*av, flags, ts1, ts2, cmd, type);
226 		++av;
227 		--ac;
228 	}
229 	return(0);
230 }
231 
232 /*
233  * Iterate through a file's history.  If cmd == CMD_DUMP we take the
234  * next-to-last transaction id, unless another given.  Otherwise if
235  * cmd == CMD_ITERATEALL we scan all transaction ids.
236  *
237  * Also iterate through the directory's history to locate other inodes that
238  * used the particular file name.
239  */
240 static
241 void
242 doiterate(const char *filename, int flags,
243 	  struct hammer_ioc_hist_entry ts1,
244 	  struct hammer_ioc_hist_entry ts2,
245 	  enum undo_cmd cmd, enum undo_type type)
246 {
247 	struct undo_hist_entry_rb_tree dir_tree;
248 	struct undo_hist_entry_rb_tree tse_tree;
249 	struct undo_hist_entry *tse;
250 	struct stat sb;
251 	char *path = NULL;
252 	int fd;
253 	int error;
254 
255 	RB_INIT(&dir_tree);
256 	RB_INIT(&tse_tree);
257 
258 	/*
259 	 * Use the directory history to locate all possible versions of
260 	 * the file.
261 	 */
262 	collect_dir_history(filename, &error, &dir_tree);
263 	RB_FOREACH(tse, undo_hist_entry_rb_tree, &dir_tree) {
264 		asprintf(&path, "%s@@0x%016jx", filename, (uintmax_t)tse->tse.tid);
265 		if (stat(path, &sb) == 0 && (sb.st_mode & S_IFIFO)) {
266 			fprintf(stderr, "Warning: fake transaction id %s@@0x%016jx\n",
267 				filename,
268 				(uintmax_t)tse->tse.tid);
269 			free(path);
270 			continue;
271 		}
272 		if ((fd = open(path, O_RDONLY)) > 0) {
273 			collect_history(fd, &error, &tse_tree);
274 			close(fd);
275 		}
276 		free(path);
277 	}
278 	if ((fd = open(filename, O_RDONLY)) > 0) {
279 		collect_history(fd, &error, &tse_tree);
280 		close(fd);
281 	}
282 
283 	switch (cmd) {
284 	case CMD_DUMP:
285 		if (doiterate_dump(filename, flags, &tse_tree, ts1, ts2, type) == -1)
286 			printf("%s: No UNDO history found\n", filename);
287 		break;
288 	case CMD_ITERATEALL:
289 		if (doiterate_iterall(filename, flags, &tse_tree, type) == -1)
290 			printf("%s: ITERATE ENTIRE HISTORY: %s\n",
291 			       filename, strerror(error));
292 		break;
293 	default:
294 		fprintf(stderr, "Invalid command %d\n", cmd);
295 		break;
296 	}
297 
298 	clean_tree(&dir_tree);
299 	clean_tree(&tse_tree);
300 }
301 
302 static
303 int
304 doiterate_dump(const char *filename, int flags,
305 		struct undo_hist_entry_rb_tree *ptse_tree,
306 		struct hammer_ioc_hist_entry ts1,
307 		struct hammer_ioc_hist_entry ts2,
308 		enum undo_type type)
309 {
310 	struct undo_hist_entry *tse1;
311 	struct undo_hist_entry *tse2;
312 
313 	/*
314 	 * Find entry if tid set to placeholder index
315 	 */
316 	if (flags & UNDO_FLAG_TID_INDEX1) {
317 		tse1 = RB_MAX(undo_hist_entry_rb_tree, ptse_tree);
318 		while (tse1 && ts1.tid--) {
319 			tse1 = RB_PREV(undo_hist_entry_rb_tree,
320 				       ptse_tree, tse1);
321 		}
322 		if (tse1)
323 			ts1 = tse1->tse;
324 		else
325 			ts1.tid = 0;
326 	}
327 	if (flags & UNDO_FLAG_TID_INDEX2) {
328 		tse2 = RB_MAX(undo_hist_entry_rb_tree, ptse_tree);
329 		while (tse2 && ts2.tid--) {
330 			tse2 = RB_PREV(undo_hist_entry_rb_tree,
331 				       ptse_tree, tse2);
332 		}
333 		if (tse2)
334 			ts2 = tse2->tse;
335 		else
336 			ts2.tid = 0;
337 	}
338 
339 	/*
340 	 * Single entry, most recent prior to current
341 	 */
342 	if (ts1.tid == 0) {
343 		tse2 = RB_MAX(undo_hist_entry_rb_tree, ptse_tree);
344 		if (tse2) {
345 			ts2 = tse2->tse;
346 			tse1 = RB_PREV(undo_hist_entry_rb_tree,
347 				       ptse_tree, tse2);
348 			if (tse1)
349 				ts1 = tse1->tse;
350 		}
351 	}
352 
353 	if (ts1.tid) {
354 		dogenerate(filename, 0, ts1, ts2, 0, type);
355 		return(0);
356 	}
357 	return(-1);
358 }
359 
360 static
361 int
362 doiterate_iterall(const char *filename, int flags,
363 		struct undo_hist_entry_rb_tree *ptse_tree,
364 		enum undo_type type)
365 {
366 	struct undo_hist_entry *tse1;
367 	struct undo_hist_entry *tse2;
368 	struct hammer_ioc_hist_entry tid_max;
369 	int i;
370 
371 	if (RB_ROOT(ptse_tree) == NULL)
372 		return(-1);
373 
374 	/*
375 	 * Iterate entire history
376 	 */
377 	printf("%s: ITERATE ENTIRE HISTORY\n", filename);
378 
379 	tse1 = NULL;
380 	i = 0;
381 	RB_FOREACH(tse2, undo_hist_entry_rb_tree, ptse_tree) {
382 		if (tse1) {
383 			dogenerate(filename, flags, tse1->tse, tse2->tse, i, type);
384 		}
385 		if (tse1 && tse2->inum != tse1->inum)
386 			flags |= UNDO_FLAG_INOCHG;
387 		else
388 			flags &= ~UNDO_FLAG_INOCHG;
389 		tse1 = tse2;
390 		++i;
391 	}
392 
393 	/*
394 	 * There is no delta to print for the last pair,
395 	 * because they are identical.
396 	 */
397 	if (type != TYPE_DIFF && type != TYPE_RDIFF) {
398 		tid_max.tid = HAMMER_MAX_TID;
399 		tid_max.time32 = 0;
400 		dogenerate(filename, flags, tse1->tse, tid_max, i, type);
401 	}
402 	return(0);
403 }
404 
405 /*
406  * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
407  * through ts2.
408  */
409 static
410 void
411 dogenerate(const char *filename, int flags,
412 	   struct hammer_ioc_hist_entry ts1,
413 	   struct hammer_ioc_hist_entry ts2,
414 	   int idx, enum undo_type type)
415 {
416 	struct stat st;
417 	const char *elm;
418 	char *ipath1 = NULL;
419 	char *ipath2 = NULL;
420 	FILE *fi;
421 	FILE *fp;
422 	char *buf;
423 	char *path;
424 	time_t t;
425 	struct tm *tp;
426 	char datestr[64];
427 	int n;
428 
429 	/*
430 	 * Open the input file.  If ts1 is 0 try to locate the most recent
431 	 * version of the file prior to the current version.
432 	 */
433 	if (ts1.tid == 0)
434 		asprintf(&ipath1, "%s", filename);
435 	else
436 		asprintf(&ipath1, "%s@@0x%016jx", filename, (uintmax_t)ts1.tid);
437 
438 	if (ts2.tid == 0)
439 		asprintf(&ipath2, "%s", filename);
440 	else
441 		asprintf(&ipath2, "%s@@0x%016jx", filename, (uintmax_t)ts2.tid);
442 
443 	if (lstat(ipath1, &st) < 0 && lstat(ipath2, &st) < 0) {
444 		if (idx == 0 || VerboseOpt) {
445 			fprintf(stderr, "Unable to access either %s or %s\n",
446 				ipath1, ipath2);
447 		}
448 		free(ipath1);
449 		free(ipath2);
450 		return;
451 	}
452 
453 	/*
454 	 * elm is the last component of the input file name
455 	 */
456 	if ((elm = strrchr(filename, '/')) != NULL)
457 		++elm;
458 	else
459 		elm = filename;
460 
461 	/*
462 	 * Where do we stuff our output?
463 	 */
464 	if (OutFileName) {
465 		if (flags & UNDO_FLAG_MULT) {
466 			asprintf(&path, OutFileName, elm);
467 			fp = fopen(path, "w");
468 			if (fp == NULL) {
469 				perror(path);
470 				exit(1);
471 			}
472 			free(path);
473 		} else {
474 			fp = fopen(OutFileName, "w");
475 			if (fp == NULL) {
476 				perror(OutFileName);
477 				exit(1);
478 			}
479 		}
480 	} else if (OutFilePostfix) {
481 		if (idx >= 0) {
482 			asprintf(&path, "%s%s.%04d", filename,
483 				 OutFilePostfix, idx);
484 		} else {
485 			asprintf(&path, "%s%s", filename, OutFilePostfix);
486 		}
487 		fp = fopen(path, "w");
488 		if (fp == NULL) {
489 			perror(path);
490 			exit(1);
491 		}
492 		free(path);
493 	} else {
494 		if ((flags & UNDO_FLAG_MULT) && type == TYPE_FILE) {
495 			if (idx >= 0) {
496 				printf("\n>>> %s %04d 0x%016jx %s\n\n",
497 				       filename, idx, (uintmax_t)ts1.tid,
498 				       timestamp(&ts1));
499 			} else {
500 				printf("\n>>> %s ---- 0x%016jx %s\n\n",
501 				       filename, (uintmax_t)ts1.tid,
502 				       timestamp(&ts1));
503 			}
504 		} else if (idx >= 0 && type == TYPE_FILE) {
505 			printf("\n>>> %s %04d 0x%016jx %s\n\n",
506 			       filename, idx, (uintmax_t)ts1.tid,
507 			       timestamp(&ts1));
508 		}
509 		fp = stdout;
510 	}
511 
512 	switch(type) {
513 	case TYPE_FILE:
514 		buf = malloc(8192);
515 		if (buf == NULL) {
516 			perror("malloc");
517 			exit(1);
518 		}
519 		if ((fi = fopen(ipath1, "r")) != NULL) {
520 			while ((n = fread(buf, 1, 8192, fi)) > 0)
521 				fwrite(buf, 1, n, fp);
522 			fclose(fi);
523 		}
524 		free(buf);
525 		break;
526 	case TYPE_DIFF:
527 		printf("diff -N -r -u %s %s (to %s)\n",
528 		       ipath1, ipath2, timestamp(&ts2));
529 		fflush(stdout);
530 		runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u",
531 			ipath1, ipath2, NULL);
532 		break;
533 	case TYPE_RDIFF:
534 		printf("diff -N -r -u %s %s\n", ipath2, ipath1);
535 		fflush(stdout);
536 		runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u",
537 			ipath2, ipath1, NULL);
538 		break;
539 	case TYPE_HISTORY:
540 		t = (time_t)ts1.time32;
541 		tp = localtime(&t);
542 		strftime(datestr, sizeof(datestr), "%d-%b-%Y %H:%M:%S", tp);
543 		printf("\t0x%016jx %s", (uintmax_t)ts1.tid, datestr);
544 		if (flags & UNDO_FLAG_INOCHG)
545 			printf(" inode-change");
546 		if (lstat(ipath1, &st) < 0)
547 			printf(" file-deleted");
548 		printf("\n");
549 		break;
550 	}
551 
552 	if (fp != stdout)
553 		fclose(fp);
554 }
555 
556 static
557 void
558 clean_tree(struct undo_hist_entry_rb_tree *tree)
559 {
560 	struct undo_hist_entry *tse;
561 
562 	while ((tse = RB_ROOT(tree)) != NULL) {
563 		RB_REMOVE(undo_hist_entry_rb_tree, tree, tse);
564 		free(tse);
565 	}
566 }
567 
568 static
569 void
570 collect_history(int fd, int *errorp, struct undo_hist_entry_rb_tree *tse_tree)
571 {
572 	struct hammer_ioc_history hist;
573 	struct undo_hist_entry *tse;
574 	struct stat st;
575 	int i;
576 
577 	/*
578 	 * Setup
579 	 */
580 	bzero(&hist, sizeof(hist));
581 	hist.beg_tid = HAMMER_MIN_TID;
582 	hist.end_tid = HAMMER_MAX_TID;
583 	hist.head.flags |= HAMMER_IOC_HISTORY_ATKEY;
584 	hist.key = 0;
585 	hist.nxt_key = HAMMER_MAX_KEY;
586 
587 	*errorp = 0;
588 
589 	/*
590 	 * Save the inode so inode changes can be reported.
591 	 */
592 	st.st_ino = 0;
593 	fstat(fd, &st);
594 
595 	/*
596 	 * Collect a unique set of transaction ids
597 	 */
598 	if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
599 		*errorp = errno;
600 		return;
601 	}
602 	for (;;) {
603 		for (i = 0; i < hist.count; ++i) {
604 			tse = malloc(sizeof(*tse));
605 			tse->tse = hist.hist_ary[i];
606 			tse->inum = st.st_ino;
607 			if (RB_INSERT(undo_hist_entry_rb_tree, tse_tree, tse)) {
608 				free(tse);
609 			}
610 		}
611 		if (hist.head.flags & HAMMER_IOC_HISTORY_EOF)
612 			break;
613 		if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY) {
614 			hist.key = hist.nxt_key;
615 			hist.nxt_key = HAMMER_MAX_KEY;
616 		}
617 		if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_TID)
618 			hist.beg_tid = hist.nxt_tid;
619 		if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
620 			*errorp = errno;
621 			break;
622 		}
623 	}
624 }
625 
626 static
627 void
628 collect_dir_history(const char *filename, int *errorp,
629 		    struct undo_hist_entry_rb_tree *dir_tree)
630 {
631 	char *dirname;
632 	int fd;
633 	int error;
634 
635 	*errorp = 0;
636 	if (strrchr(filename, '/')) {
637 		dirname = strdup(filename);
638 		*strrchr(dirname, '/') = 0;
639 	} else {
640 		dirname = strdup(".");
641 	}
642 	if ((fd = open(dirname, O_RDONLY)) > 0) {
643 		collect_history(fd, &error, dir_tree);
644 		close(fd);
645 	}
646 	free(dirname);
647 }
648 
649 static
650 hammer_tid_t
651 parse_delta_time(const char *timeStr, int *flags, int ind_flag)
652 {
653 	hammer_tid_t tid;
654 
655 	tid = strtoull(timeStr, NULL, 0);
656 	if (timeStr[0] == '+')
657 		++timeStr;
658 	if (timeStr[0] >= '0' && timeStr[0] <= '9' && timeStr[1] != 'x')
659 		*flags |= ind_flag;
660 	return(tid);
661 }
662 
663 static void
664 runcmd(int fd, const char *cmd, ...)
665 {
666 	va_list va;
667 	pid_t pid;
668 	char **av;
669 	int ac;
670 	int i;
671 
672 	va_start(va, cmd);
673 	for (ac = 0; va_arg(va, void *) != NULL; ++ac)
674 		;
675 	va_end(va);
676 
677 	av = malloc((ac + 1) * sizeof(char *));
678 	va_start(va, cmd);
679 	for (i = 0; i < ac; ++i)
680 		av[i] = va_arg(va, char *);
681 	va_end(va);
682 	av[i] = NULL;
683 
684 	if ((pid = fork()) < 0) {
685 		perror("fork");
686 		exit(1);
687 	} else if (pid == 0) {
688 		if (fd != 1) {
689 			dup2(fd, 1);
690 			close(fd);
691 		}
692 		execv(cmd, av);
693 		_exit(1);
694 	} else {
695 		while (waitpid(pid, NULL, 0) != pid)
696 			;
697 	}
698 	free(av);
699 }
700 
701 /*
702  * Convert tid to timestamp.
703  */
704 static char *
705 timestamp(hammer_ioc_hist_entry_t hen)
706 {
707 	static char timebuf[64];
708 	time_t t = (time_t)hen->time32;
709 	struct tm *tp;
710 
711 	tp = localtime(&t);
712 	strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp);
713 	return(timebuf);
714 }
715 
716 static
717 int
718 undo_hist_entry_compare(struct undo_hist_entry *he1,
719 			struct undo_hist_entry *he2)
720 {
721 	if (he1->tse.tid < he2->tse.tid)
722 		return(-1);
723 	if (he1->tse.tid > he2->tse.tid)
724 		return(1);
725 	return(0);
726 }
727 
728 static void
729 usage(void)
730 {
731 	fprintf(stderr, "undo [-adDiuv] [-o outfile] "
732 			"[-t transaction-id] [-t transaction-id] path...\n"
733 			"    -a       Iterate all historical segments\n"
734 			"    -d       Forward diff\n"
735 			"    -D       Reverse diff\n"
736 			"    -i       Dump history transaction ids\n"
737 			"    -u       Generate .undo files\n"
738 			"    -v       Verbose\n"
739 			"    -o file  Output to the specified file\n"
740 			"    -t TID   Retrieve as of transaction-id, TID\n"
741 			"             (a second `-t TID' to diff two)\n"
742 			"    transaction ids must be prefixed with 0x, and\n"
743 			"    otherwise may specify an index starting at 0\n"
744 			"    and iterating backwards through the history.\n"
745 	);
746 	exit(1);
747 }
748 
749