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
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);
95 static int VerboseOpt;
97 RB_GENERATE2(undo_hist_entry_rb_tree, undo_hist_entry, rbnode,
98 undo_hist_entry_compare, hammer_tid_t, tse.tid);
102 main(int ac, char **av)
104 const char *outFileName = NULL;
105 const char *outFilePostfix = NULL;
108 struct hammer_ioc_hist_entry ts1;
109 struct hammer_ioc_hist_entry ts2;
113 bzero(&ts1, sizeof(ts1));
114 bzero(&ts2, sizeof(ts2));
119 while ((c = getopt(ac, av, "adDiuvo:t:")) != -1) {
122 if (type != TYPE_FILE)
127 if (type != TYPE_FILE)
132 if (type != TYPE_FILE)
135 cmd = CMD_ITERATEALL;
138 cmd = CMD_ITERATEALL;
141 outFilePostfix = ".undo";
147 outFileName = optarg;
150 if (ts1.tid && ts2.tid)
152 else if (ts1.tid == 0)
153 ts1.tid = parse_delta_time(optarg);
155 ts2.tid = parse_delta_time(optarg);
167 if (outFileName && outFilePostfix) {
168 fprintf(stderr, "The -o option may not be combined with -u\n");
176 flags |= UNDO_FLAG_MULT;
182 * Validate the output template, if specified.
184 if (outFileName && (flags & UNDO_FLAG_MULT)) {
185 const char *ptr = outFileName;
188 while ((ptr = strchr(ptr, '%')) != NULL) {
191 fprintf(stderr, "Malformed output "
197 } else if (ptr[1] != '%') {
198 fprintf(stderr, "Malformed output template\n");
207 doiterate(*av, outFileName, outFilePostfix,
208 flags, ts1, cmd, type);
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.
220 * Also iterate through the directory's history to locate other inodes that
221 * used the particular file name.
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)
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;
243 tid_max.tid = HAMMER_MAX_TID;
247 * Use the directory history to locate all possible versions of
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);
258 if (cmd == CMD_DUMP) {
260 * Single entry, most recent prior to current
262 if (ts1.tid == 0 && RB_EMPTY(&tse_tree)) {
263 if ((fd = open(filename, O_RDONLY)) > 0) {
264 collect_history(fd, &error, &tse_tree);
269 tse2 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree);
272 tse1 = RB_PREV(undo_hist_entry_rb_tree,
279 printf("%s: No UNDO history found\n", filename);
282 outFileName, outFilePostfix,
286 } else if (RB_ROOT(&tse_tree)) {
288 * Iterate entire history
290 printf("%s: ITERATE ENTIRE HISTORY\n", filename);
294 RB_FOREACH(tse2, undo_hist_entry_rb_tree, &tse_tree) {
297 outFileName, outFilePostfix,
299 tse1->tse, tse2->tse);
301 if (tse1 && tse2->inum != tse1->inum)
302 flags |= UNDO_FLAG_INOCHG;
304 flags &= ~UNDO_FLAG_INOCHG;
309 outFileName, outFilePostfix,
313 printf("%s: ITERATE ENTIRE HISTORY: %s\n",
314 filename, strerror(error));
318 clean_tree(&dir_tree);
319 clean_tree(&tse_tree);
323 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
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)
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.
354 asprintf(&ipath1, "%s", filename);
356 asprintf(&ipath1, "%s@@0x%016llx", filename, ts1.tid);
359 asprintf(&ipath2, "%s", filename);
361 asprintf(&ipath2, "%s@@0x%016llx", filename, ts2.tid);
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",
374 * elm is the last component of the input file name
376 if ((elm = strrchr(filename, '/')) != NULL)
382 * Where do we stuff our output?
385 if (flags & UNDO_FLAG_MULT) {
386 asprintf(&path, outFileName, elm);
387 fp = fopen(path, "w");
394 fp = fopen(outFileName, "w");
400 } else if (outFilePostfix) {
402 asprintf(&path, "%s%s.%04d", filename,
403 outFilePostfix, idx);
405 asprintf(&path, "%s%s", filename, outFilePostfix);
407 fp = fopen(path, "w");
414 if ((flags & UNDO_FLAG_MULT) && type == TYPE_FILE) {
416 printf("\n>>> %s %04d 0x%016llx %s\n\n",
417 filename, idx, ts1.tid, timestamp(&ts1));
419 printf("\n>>> %s ---- 0x%016llx %s\n\n",
420 filename, ts1.tid, timestamp(&ts1));
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));
431 if ((fi = fopen(ipath1, "r")) != NULL) {
432 while ((n = fread(buf, 1, 8192, fi)) > 0)
433 fwrite(buf, 1, n, fp);
438 printf("diff -N -r -u %s %s (to %s)\n",
439 ipath1, ipath2, timestamp(&ts2));
441 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath1, ipath2, NULL);
444 printf("diff -N -r -u %s %s\n", ipath2, ipath1);
446 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath2, ipath1, NULL);
449 t = (time_t)ts1.time32;
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");
469 clean_tree(struct undo_hist_entry_rb_tree *tree)
471 struct undo_hist_entry *tse;
473 while ((tse = RB_ROOT(tree)) != NULL) {
474 RB_REMOVE(undo_hist_entry_rb_tree, tree, tse);
481 collect_history(int fd, int *errorp, struct undo_hist_entry_rb_tree *tse_tree)
483 struct hammer_ioc_history hist;
484 struct undo_hist_entry *tse;
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;
497 hist.nxt_key = HAMMER_MAX_KEY;
501 if (tse_tree == NULL) {
502 tse_tree = malloc(sizeof(*tse_tree));
510 * Save the inode so inode changes can be reported.
516 * Collect a unique set of transaction ids
518 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
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)) {
531 if (hist.head.flags & HAMMER_IOC_HISTORY_EOF)
533 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY) {
534 hist.key = hist.nxt_key;
535 hist.nxt_key = HAMMER_MAX_KEY;
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) {
550 clean_tree(tse_tree);
557 collect_dir_history(const char *filename, int *errorp,
558 struct undo_hist_entry_rb_tree *dir_tree)
565 if (strrchr(filename, '/')) {
566 dirname = strdup(filename);
567 *strrchr(dirname, '/') = 0;
569 dirname = strdup(".");
571 if ((fd = open(dirname, O_RDONLY)) > 0) {
572 collect_history(fd, &error, dir_tree);
579 parse_delta_time(const char *timeStr)
583 tid = strtoull(timeStr, NULL, 0);
588 runcmd(int fd, const char *cmd, ...)
597 for (ac = 0; va_arg(va, void *) != NULL; ++ac)
601 av = malloc((ac + 1) * sizeof(char *));
603 for (i = 0; i < ac; ++i)
604 av[i] = va_arg(va, char *);
608 if ((pid = fork()) < 0) {
611 } else if (pid == 0) {
619 while (waitpid(pid, NULL, 0) != pid)
626 * Convert tid to timestamp.
629 timestamp(hammer_ioc_hist_entry_t hen)
631 static char timebuf[64];
632 time_t t = (time_t)hen->time32;
636 strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp);
642 undo_hist_entry_compare(struct undo_hist_entry *he1,
643 struct undo_hist_entry *he2)
645 if (he1->tse.tid < he2->tse.tid)
647 if (he1->tse.tid > he2->tse.tid)
655 fprintf(stderr, "undo [-adDiuv] [-o outfile] "
656 "[-t transaction-id] [-t transaction-id] path...\n"
657 " -a Iterate all historical segments\n"
660 " -i Dump history transaction ids\n"
661 " -u Generate .undo files\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");