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