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