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