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