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 tid_max;
243 tid_max.tid = HAMMER_MAX_TID - 1;
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 tse1 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree);
271 tse1 = RB_PREV(undo_hist_entry_rb_tree,
278 printf("%s: No UNDO history found\n", filename);
281 outFileName, outFilePostfix,
285 } else if (RB_ROOT(&tse_tree)) {
287 * Iterate entire history
289 printf("%s: ITERATE ENTIRE HISTORY\n", filename);
293 RB_FOREACH(tse2, undo_hist_entry_rb_tree, &tse_tree) {
296 outFileName, outFilePostfix,
298 tse1->tse, tse2->tse);
300 if (tse1 && tse2->inum != tse1->inum)
301 flags |= UNDO_FLAG_INOCHG;
303 flags &= ~UNDO_FLAG_INOCHG;
308 outFileName, outFilePostfix,
312 printf("%s: ITERATE ENTIRE HISTORY: %s\n",
313 filename, strerror(error));
317 clean_tree(&dir_tree);
318 clean_tree(&tse_tree);
322 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
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)
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.
353 asprintf(&ipath1, "%s", filename);
355 asprintf(&ipath1, "%s@@0x%016llx", filename, ts1.tid);
358 asprintf(&ipath2, "%s", filename);
360 asprintf(&ipath2, "%s@@0x%016llx", filename, ts2.tid);
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",
373 * elm is the last component of the input file name
375 if ((elm = strrchr(filename, '/')) != NULL)
381 * Where do we stuff our output?
384 if (flags & UNDO_FLAG_MULT) {
385 asprintf(&path, outFileName, elm);
386 fp = fopen(path, "w");
393 fp = fopen(outFileName, "w");
399 } else if (outFilePostfix) {
401 asprintf(&path, "%s%s.%04d", filename,
402 outFilePostfix, idx);
404 asprintf(&path, "%s%s", filename, outFilePostfix);
406 fp = fopen(path, "w");
413 if ((flags & UNDO_FLAG_MULT) && type == TYPE_FILE) {
415 printf("\n>>> %s %04d 0x%016llx %s\n\n",
416 filename, idx, ts1.tid, timestamp(&ts1));
418 printf("\n>>> %s ---- 0x%016llx %s\n\n",
419 filename, ts1.tid, timestamp(&ts1));
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));
430 if ((fi = fopen(ipath1, "r")) != NULL) {
431 while ((n = fread(buf, 1, 8192, fi)) > 0)
432 fwrite(buf, 1, n, fp);
437 printf("diff -N -r -u %s %s (to %s)\n",
438 ipath1, ipath2, timestamp(&ts2));
440 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath1, ipath2, NULL);
443 printf("diff -N -r -u %s %s\n", ipath2, ipath1);
445 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath2, ipath1, NULL);
448 t = (time_t)ts1.time32;
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");
468 clean_tree(struct undo_hist_entry_rb_tree *tree)
470 struct undo_hist_entry *tse;
472 while ((tse = RB_ROOT(tree)) != NULL) {
473 RB_REMOVE(undo_hist_entry_rb_tree, tree, tse);
480 collect_history(int fd, int *errorp, struct undo_hist_entry_rb_tree *tse_tree)
482 struct hammer_ioc_history hist;
483 struct undo_hist_entry *tse;
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;
496 hist.nxt_key = HAMMER_MAX_KEY;
500 if (tse_tree == NULL) {
501 tse_tree = malloc(sizeof(*tse_tree));
509 * Save the inode so inode changes can be reported.
515 * Collect a unique set of transaction ids
517 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
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)) {
530 if (hist.head.flags & HAMMER_IOC_HISTORY_EOF)
532 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY) {
533 hist.key = hist.nxt_key;
534 hist.nxt_key = HAMMER_MAX_KEY;
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) {
549 clean_tree(tse_tree);
556 collect_dir_history(const char *filename, int *errorp,
557 struct undo_hist_entry_rb_tree *dir_tree)
564 if (strrchr(filename, '/')) {
565 dirname = strdup(filename);
566 *strrchr(dirname, '/') = 0;
568 dirname = strdup(".");
570 if ((fd = open(dirname, O_RDONLY)) > 0) {
571 collect_history(fd, &error, dir_tree);
578 parse_delta_time(const char *timeStr)
582 tid = strtoull(timeStr, NULL, 0);
587 runcmd(int fd, const char *cmd, ...)
596 for (ac = 0; va_arg(va, void *) != NULL; ++ac)
600 av = malloc((ac + 1) * sizeof(char *));
602 for (i = 0; i < ac; ++i)
603 av[i] = va_arg(va, char *);
607 if ((pid = fork()) < 0) {
610 } else if (pid == 0) {
618 while (waitpid(pid, NULL, 0) != pid)
625 * Convert tid to timestamp.
628 timestamp(hammer_ioc_hist_entry_t hen)
630 static char timebuf[64];
631 time_t t = (time_t)hen->time32;
635 strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp);
641 undo_hist_entry_compare(struct undo_hist_entry *he1,
642 struct undo_hist_entry *he2)
644 if (he1->tse.tid < he2->tse.tid)
646 if (he1->tse.tid > he2->tse.tid)
654 fprintf(stderr, "undo [-adDiuv] [-o outfile] "
655 "[-t transaction-id] [-t transaction-id] path...\n"
656 " -a Iterate all historical segments\n"
659 " -i Dump history transaction ids\n"
660 " -u Generate .undo files\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");