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