2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 * $DragonFly: src/usr.bin/undo/undo.c,v 1.6 2008/07/17 21:34:47 thomas Exp $
37 * UNDO - retrieve an older version of a file.
40 #include <sys/types.h>
51 #include <vfs/hammer/hammer_disk.h>
52 #include <vfs/hammer/hammer_ioctl.h>
55 * Sorted list of transaction ids
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);
62 struct undo_hist_entry {
63 RB_ENTRY(undo_hist_entry) rbnode;
64 struct hammer_ioc_hist_entry tse;
68 enum undo_type { TYPE_FILE, TYPE_DIFF, TYPE_RDIFF, TYPE_HISTORY };
69 enum undo_cmd { CMD_DUMP, CMD_ITERATEALL };
71 #define UNDO_FLAG_MULT 0x0001
72 #define UNDO_FLAG_INOCHG 0x0002
73 #define UNDO_FLAG_TID_INDEX1 0x0004
74 #define UNDO_FLAG_TID_INDEX2 0x0008
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, int flags,
79 struct hammer_ioc_hist_entry ts1,
80 struct hammer_ioc_hist_entry ts2,
81 enum undo_cmd cmd, enum undo_type type);
82 static int doiterate_dump(const char *filename, int flags,
83 struct undo_hist_entry_rb_tree *ptse_tree,
84 struct hammer_ioc_hist_entry ts1,
85 struct hammer_ioc_hist_entry ts2,
87 static int doiterate_iterall(const char *filename, int flags,
88 struct undo_hist_entry_rb_tree *ptse_tree,
90 static void dogenerate(const char *filename, int flags,
91 struct hammer_ioc_hist_entry ts1,
92 struct hammer_ioc_hist_entry ts2,
93 int idx, enum undo_type type);
94 static void __collect_history(int fd, int *error,
95 struct undo_hist_entry_rb_tree *tse_tree);
96 static void collect_history(const char *filename, int *errorp,
97 struct undo_hist_entry_rb_tree *dir_tree);
98 static void collect_dir_history(const char *filename, int *error,
99 struct undo_hist_entry_rb_tree *dir_tree);
100 static void clean_tree(struct undo_hist_entry_rb_tree *tree);
101 static hammer_tid_t parse_delta_time(const char *timeStr, int *flags,
103 static FILE *_fopen(const char *filename, const char *mode);
104 static void runcmd(int fd, const char *cmd, ...);
105 static char *timestamp(hammer_ioc_hist_entry_t hen);
106 static void usage(void);
108 static int VerboseOpt;
109 static const char *OutFileName = NULL;
110 static const char *OutFilePostfix = NULL;
112 RB_GENERATE2(undo_hist_entry_rb_tree, undo_hist_entry, rbnode,
113 undo_hist_entry_compare, hammer_tid_t, tse.tid);
117 main(int ac, char **av)
121 struct hammer_ioc_hist_entry ts1;
122 struct hammer_ioc_hist_entry ts2;
127 bzero(&ts1, sizeof(ts1));
128 bzero(&ts2, sizeof(ts2));
135 while ((c = getopt(ac, av, "adDiuvo:t:")) != -1) {
144 if (type != TYPE_FILE)
147 cmd = CMD_ITERATEALL;
150 cmd = CMD_ITERATEALL;
153 OutFilePostfix = ".undo";
159 OutFileName = optarg;
163 * Parse one or two -t options. If two are specified
164 * -d is implied (but may be overridden)
168 ts1.tid = parse_delta_time(optarg, &flags,
169 UNDO_FLAG_TID_INDEX1);
170 } else if (count_t == 2) {
171 ts2.tid = parse_delta_time(optarg, &flags,
172 UNDO_FLAG_TID_INDEX2);
173 if (type == TYPE_FILE)
189 if (OutFileName && OutFilePostfix) {
190 fprintf(stderr, "The -o option may not be combined with -u\n");
197 flags |= UNDO_FLAG_MULT;
203 * Validate the output template, if specified.
205 if (OutFileName && (flags & UNDO_FLAG_MULT)) {
206 const char *ptr = OutFileName;
209 while ((ptr = strchr(ptr, '%')) != NULL) {
212 fprintf(stderr, "Malformed output "
218 } else if (ptr[1] != '%') {
219 fprintf(stderr, "Malformed output template\n");
228 doiterate(*av, flags, ts1, ts2, cmd, type);
236 * Iterate through a file's history. If cmd == CMD_DUMP we take the
237 * next-to-last transaction id, unless another given. Otherwise if
238 * cmd == CMD_ITERATEALL we scan all transaction ids.
240 * Also iterate through the directory's history to locate other inodes that
241 * used the particular file name.
245 doiterate(const char *filename, int flags,
246 struct hammer_ioc_hist_entry ts1,
247 struct hammer_ioc_hist_entry ts2,
248 enum undo_cmd cmd, enum undo_type type)
250 struct undo_hist_entry_rb_tree dir_tree;
251 struct undo_hist_entry_rb_tree tse_tree;
252 struct undo_hist_entry *tse;
261 * Use the directory history to locate all possible versions of
264 collect_dir_history(filename, &error, &dir_tree);
265 RB_FOREACH(tse, undo_hist_entry_rb_tree, &dir_tree) {
266 asprintf(&path, "%s@@0x%016jx", filename, (uintmax_t)tse->tse.tid);
267 if (stat(path, &sb) == 0 && (sb.st_mode & S_IFIFO)) {
268 fprintf(stderr, "Warning: fake transaction id %s@@0x%016jx\n",
270 (uintmax_t)tse->tse.tid);
274 collect_history(path, &error, &tse_tree);
277 collect_history(filename, &error, &tse_tree);
281 if (doiterate_dump(filename, flags, &tse_tree, ts1, ts2, type) == -1)
282 printf("%s: No UNDO history found\n", filename);
285 if (doiterate_iterall(filename, flags, &tse_tree, type) == -1)
286 printf("%s: No UNDO history found\n", filename);
289 fprintf(stderr, "Invalid command %d\n", cmd);
293 clean_tree(&dir_tree);
294 clean_tree(&tse_tree);
299 doiterate_dump(const char *filename, int flags,
300 struct undo_hist_entry_rb_tree *ptse_tree,
301 struct hammer_ioc_hist_entry ts1,
302 struct hammer_ioc_hist_entry ts2,
305 struct undo_hist_entry *tse1;
306 struct undo_hist_entry *tse2;
309 * Find entry if tid set to placeholder index
311 if (flags & UNDO_FLAG_TID_INDEX1) {
312 tse1 = RB_MAX(undo_hist_entry_rb_tree, ptse_tree);
313 while (tse1 && ts1.tid--) {
314 tse1 = RB_PREV(undo_hist_entry_rb_tree,
322 if (flags & UNDO_FLAG_TID_INDEX2) {
323 tse2 = RB_MAX(undo_hist_entry_rb_tree, ptse_tree);
324 while (tse2 && ts2.tid--) {
325 tse2 = RB_PREV(undo_hist_entry_rb_tree,
335 * Single entry, most recent prior to current
338 tse2 = RB_MAX(undo_hist_entry_rb_tree, ptse_tree);
341 tse1 = RB_PREV(undo_hist_entry_rb_tree,
349 dogenerate(filename, 0, ts1, ts2, 0, type);
357 doiterate_iterall(const char *filename, int flags,
358 struct undo_hist_entry_rb_tree *ptse_tree,
361 struct undo_hist_entry *tse1;
362 struct undo_hist_entry *tse2;
363 struct hammer_ioc_hist_entry tid_max;
366 if (RB_ROOT(ptse_tree) == NULL)
370 * Iterate entire history
372 printf("%s: ITERATE ENTIRE HISTORY\n", filename);
376 RB_FOREACH(tse2, undo_hist_entry_rb_tree, ptse_tree) {
378 dogenerate(filename, flags, tse1->tse, tse2->tse, i, type);
380 if (tse1 && tse2->inum != tse1->inum)
381 flags |= UNDO_FLAG_INOCHG;
383 flags &= ~UNDO_FLAG_INOCHG;
389 * There is no delta to print for the last pair,
390 * because they are identical.
392 if (type != TYPE_DIFF && type != TYPE_RDIFF) {
393 tid_max.tid = HAMMER_MAX_TID;
395 dogenerate(filename, flags, tse1->tse, tid_max, i, type);
401 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
406 dogenerate(const char *filename, int flags,
407 struct hammer_ioc_hist_entry ts1,
408 struct hammer_ioc_hist_entry ts2,
409 int idx, enum undo_type type)
425 * Open the input file. If ts1 is 0 try to locate the most recent
426 * version of the file prior to the current version.
429 asprintf(&ipath1, "%s", filename);
431 asprintf(&ipath1, "%s@@0x%016jx", filename, (uintmax_t)ts1.tid);
434 asprintf(&ipath2, "%s", filename);
436 asprintf(&ipath2, "%s@@0x%016jx", filename, (uintmax_t)ts2.tid);
438 if (lstat(ipath1, &st) < 0 && lstat(ipath2, &st) < 0) {
439 if (idx == 0 || VerboseOpt) {
440 fprintf(stderr, "Unable to access either %s or %s\n",
449 * elm is the last component of the input file name
451 if ((elm = strrchr(filename, '/')) != NULL)
457 * Where do we stuff our output?
460 if (flags & UNDO_FLAG_MULT) {
461 asprintf(&path, OutFileName, elm);
462 fp = _fopen(path, "w");
465 fp = _fopen(OutFileName, "w");
467 } else if (OutFilePostfix) {
469 asprintf(&path, "%s%s.%04d", filename,
470 OutFilePostfix, idx);
472 asprintf(&path, "%s%s", filename, OutFilePostfix);
474 fp = _fopen(path, "w");
477 if ((flags & UNDO_FLAG_MULT) && type == TYPE_FILE) {
479 printf("\n>>> %s %04d 0x%016jx %s\n\n",
480 filename, idx, (uintmax_t)ts1.tid,
483 printf("\n>>> %s ---- 0x%016jx %s\n\n",
484 filename, (uintmax_t)ts1.tid,
487 } else if (idx >= 0 && type == TYPE_FILE) {
488 printf("\n>>> %s %04d 0x%016jx %s\n\n",
489 filename, idx, (uintmax_t)ts1.tid,
502 if ((fi = fopen(ipath1, "r")) != NULL) {
503 while ((n = fread(buf, 1, 8192, fi)) > 0)
504 fwrite(buf, 1, n, fp);
510 printf("diff -N -r -u %s %s (to %s)\n",
511 ipath1, ipath2, timestamp(&ts2));
513 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u",
514 ipath1, ipath2, NULL);
517 printf("diff -N -r -u %s %s\n", ipath2, ipath1);
519 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u",
520 ipath2, ipath1, NULL);
523 t = (time_t)ts1.time32;
525 strftime(datestr, sizeof(datestr), "%d-%b-%Y %H:%M:%S", tp);
526 printf("\t0x%016jx %s", (uintmax_t)ts1.tid, datestr);
527 if (flags & UNDO_FLAG_INOCHG)
528 printf(" inode-change");
529 if (lstat(ipath1, &st) < 0)
530 printf(" file-deleted");
541 clean_tree(struct undo_hist_entry_rb_tree *tree)
543 struct undo_hist_entry *tse;
545 while ((tse = RB_ROOT(tree)) != NULL) {
546 RB_REMOVE(undo_hist_entry_rb_tree, tree, tse);
553 __collect_history(int fd, int *errorp, struct undo_hist_entry_rb_tree *tse_tree)
555 struct hammer_ioc_history hist;
556 struct undo_hist_entry *tse;
563 bzero(&hist, sizeof(hist));
564 hist.beg_tid = HAMMER_MIN_TID;
565 hist.end_tid = HAMMER_MAX_TID;
566 hist.head.flags |= HAMMER_IOC_HISTORY_ATKEY;
568 hist.nxt_key = HAMMER_MAX_KEY;
573 * Save the inode so inode changes can be reported.
579 * Collect a unique set of transaction ids
581 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
586 for (i = 0; i < hist.count; ++i) {
587 tse = malloc(sizeof(*tse));
588 tse->tse = hist.hist_ary[i];
589 tse->inum = st.st_ino;
590 if (RB_INSERT(undo_hist_entry_rb_tree, tse_tree, tse)) {
594 if (hist.head.flags & HAMMER_IOC_HISTORY_EOF)
596 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY) {
597 hist.key = hist.nxt_key;
598 hist.nxt_key = HAMMER_MAX_KEY;
600 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_TID)
601 hist.beg_tid = hist.nxt_tid;
602 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
611 collect_history(const char *filename, int *errorp,
612 struct undo_hist_entry_rb_tree *dir_tree)
616 fd = open(filename, O_RDONLY);
621 __collect_history(fd, errorp, dir_tree);
627 collect_dir_history(const char *filename, int *errorp,
628 struct undo_hist_entry_rb_tree *dir_tree)
632 if (strrchr(filename, '/')) {
633 dirname = strdup(filename);
634 *strrchr(dirname, '/') = 0;
636 dirname = strdup(".");
639 collect_history(dirname, errorp, dir_tree);
645 parse_delta_time(const char *timeStr, int *flags, int ind_flag)
649 tid = strtoull(timeStr, NULL, 0);
650 if (timeStr[0] == '+')
652 if (timeStr[0] >= '0' && timeStr[0] <= '9' && timeStr[1] != 'x')
659 _fopen(const char *filename, const char *mode)
663 fp = fopen(filename, mode);
672 runcmd(int fd, const char *cmd, ...)
681 for (ac = 0; va_arg(va, void *) != NULL; ++ac)
685 av = malloc((ac + 1) * sizeof(char *));
687 for (i = 0; i < ac; ++i)
688 av[i] = va_arg(va, char *);
692 if ((pid = fork()) < 0) {
695 } else if (pid == 0) {
703 while (waitpid(pid, NULL, 0) != pid)
710 * Convert tid to timestamp.
713 timestamp(hammer_ioc_hist_entry_t hen)
715 static char timebuf[64];
716 time_t t = (time_t)hen->time32;
720 strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp);
726 undo_hist_entry_compare(struct undo_hist_entry *he1,
727 struct undo_hist_entry *he2)
729 if (he1->tse.tid < he2->tse.tid)
731 if (he1->tse.tid > he2->tse.tid)
739 fprintf(stderr, "undo [-adDiuv] [-o outfile] "
740 "[-t transaction-id] [-t transaction-id] path...\n"
741 " -a Iterate all historical segments\n"
744 " -i Dump history transaction ids\n"
745 " -u Generate .undo files\n"
747 " -o file Output to the specified file\n"
748 " -t TID Retrieve as of transaction-id, TID\n"
749 " (a second `-t TID' to diff two)\n"
750 " transaction ids must be prefixed with 0x, and\n"
751 " otherwise may specify an index starting at 0\n"
752 " and iterating backwards through the history.\n"