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