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;
67 enum undo_type { TYPE_FILE, TYPE_DIFF, TYPE_RDIFF, TYPE_HISTORY };
69 static int undo_hist_entry_compare(struct undo_hist_entry *he1,
70 struct undo_hist_entry *he2);
71 static void doiterate(const char *orig_filename, const char *outFileName,
72 const char *outFilePostfix, int mult, enum undo_type type);
73 static void dogenerate(const char *filename, const char *outFileName,
74 const char *outFilePostfix,
75 int mult, int idx, enum undo_type type,
76 struct hammer_ioc_hist_entry ts1,
77 struct hammer_ioc_hist_entry ts2,
79 static struct hammer_ioc_hist_entry
80 find_recent(const char *filename);
81 static struct hammer_ioc_hist_entry
82 output_history(const char *filename, int fd, FILE *fp,
83 struct undo_hist_entry_rb_tree *tse_tree);
84 static struct hammer_ioc_hist_entry
85 collect_history(int fd, int *error,
86 struct undo_hist_entry_rb_tree *tse_tree);
87 static hammer_tid_t parse_delta_time(const char *timeStr);
88 static void runcmd(int fd, const char *cmd, ...);
89 static char *timestamp(hammer_ioc_hist_entry_t hen);
90 static void usage(void);
92 static int VerboseOpt;
94 RB_GENERATE2(undo_hist_entry_rb_tree, undo_hist_entry, rbnode,
95 undo_hist_entry_compare, hammer_tid_t, tse.tid);
99 main(int ac, char **av)
101 const char *outFileName = NULL;
102 const char *outFilePostfix = NULL;
103 enum { CMD_DUMP, CMD_ITERATEALL } cmd;
105 struct hammer_ioc_hist_entry ts1;
106 struct hammer_ioc_hist_entry ts2;
110 bzero(&ts1, sizeof(ts1));
111 bzero(&ts2, sizeof(ts2));
116 while ((c = getopt(ac, av, "adDiuvo:t:")) != -1) {
119 if (type != TYPE_FILE)
124 if (type != TYPE_FILE)
129 if (type != TYPE_FILE)
134 cmd = CMD_ITERATEALL;
137 outFilePostfix = ".undo";
143 outFileName = optarg;
146 if (ts1.tid && ts2.tid)
148 else if (ts1.tid == 0)
149 ts1.tid = parse_delta_time(optarg);
151 ts2.tid = parse_delta_time(optarg);
163 if (outFileName && outFilePostfix) {
164 fprintf(stderr, "The -o option may not be combined with -u\n");
176 * Validate the output template, if specified.
178 if (outFileName && mult) {
179 const char *ptr = outFileName;
182 while ((ptr = strchr(ptr, '%')) != NULL) {
185 fprintf(stderr, "Malformed output "
191 } else if (ptr[1] != '%') {
192 fprintf(stderr, "Malformed output template\n");
203 dogenerate(*av, outFileName, outFilePostfix,
204 mult, -1, type, ts1, ts2, 1);
207 doiterate(*av, outFileName, outFilePostfix,
218 * Iterate through a file's history
222 doiterate(const char *orig_filename, const char *outFileName,
223 const char *outFilePostfix, int mult, enum undo_type type)
225 struct undo_hist_entry_rb_tree tse_tree;
226 struct undo_hist_entry *tse1;
227 struct undo_hist_entry *tse2;
228 struct hammer_ioc_hist_entry tid_max;
229 struct hammer_ioc_hist_entry ts1;
230 const char *use_filename;
237 tid_max.tid = HAMMER_MAX_TID;
240 use_filename = orig_filename;
241 if ((fd = open(orig_filename, O_RDONLY)) < 0) {
242 ts1 = find_recent(orig_filename);
244 asprintf(&path, "%s@@0x%016llx",
245 orig_filename, ts1.tid);
250 if ((fd = open(use_filename, O_RDONLY)) >= 0) {
251 printf("%s: ITERATE ENTIRE HISTORY\n", orig_filename);
252 output_history(NULL, fd, NULL, &tse_tree);
257 RB_FOREACH(tse2, undo_hist_entry_rb_tree, &tse_tree) {
259 dogenerate(orig_filename,
260 outFileName, outFilePostfix,
262 tse1->tse, tse2->tse, 0);
267 if (!RB_EMPTY(&tse_tree)) {
268 dogenerate(orig_filename,
269 outFileName, outFilePostfix,
271 tse1->tse, tid_max, 0);
274 printf("%s: ITERATE ENTIRE HISTORY: %s\n",
275 orig_filename, strerror(errno));
282 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
287 dogenerate(const char *filename, const char *outFileName,
288 const char *outFilePostfix,
289 int mult, int idx, enum undo_type type,
290 struct hammer_ioc_hist_entry ts1,
291 struct hammer_ioc_hist_entry ts2,
307 * Open the input file. If ts1 is 0 try to locate the most recent
308 * version of the file prior to the current version.
311 ts1 = find_recent(filename);
313 asprintf(&ipath1, "%s", filename);
315 asprintf(&ipath1, "%s@@0x%016llx", filename, ts1.tid);
318 asprintf(&ipath2, "%s", filename);
320 asprintf(&ipath2, "%s@@0x%016llx", filename, ts2.tid);
322 if (lstat(ipath1, &st) < 0 && lstat(ipath2, &st) < 0) {
323 if (force == 0 || VerboseOpt) {
324 fprintf(stderr, "Unable to access either %s or %s\n",
333 * elm is the last component of the input file name
335 if ((elm = strrchr(filename, '/')) != NULL)
341 * Where do we stuff our output?
345 asprintf(&path, outFileName, elm);
346 fp = fopen(path, "w");
353 fp = fopen(outFileName, "w");
359 } else if (outFilePostfix) {
361 asprintf(&path, "%s%s.%04d", filename,
362 outFilePostfix, idx);
364 asprintf(&path, "%s%s", filename, outFilePostfix);
366 fp = fopen(path, "w");
373 if (mult && type == TYPE_FILE) {
375 printf("\n>>> %s %04d 0x%016llx %s\n\n",
376 filename, idx, ts1.tid, timestamp(&ts1));
378 printf("\n>>> %s ---- 0x%016llx %s\n\n",
379 filename, ts1.tid, timestamp(&ts1));
381 } else if (idx >= 0 && type == TYPE_FILE) {
382 printf("\n>>> %s %04d 0x%016llx %s\n\n",
383 filename, idx, ts1.tid, timestamp(&ts1));
390 if ((fi = fopen(ipath1, "r")) != NULL) {
391 while ((n = fread(buf, 1, 8192, fi)) > 0)
392 fwrite(buf, 1, n, fp);
397 printf("diff -N -r -u %s %s (to %s)\n",
398 ipath1, ipath2, timestamp(&ts2));
400 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath1, ipath2, NULL);
403 printf("diff -N -r -u %s %s\n", ipath2, ipath1);
405 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath2, ipath1, NULL);
408 if ((fi = fopen(ipath1, "r")) != NULL) {
409 output_history(filename, fileno(fi), fp, NULL);
422 * Try to find a recent version of the file.
424 * XXX if file cannot be found
427 struct hammer_ioc_hist_entry
428 find_recent(const char *filename)
430 struct undo_hist_entry_rb_tree tse_tree;
431 struct undo_hist_entry *tse;
432 struct hammer_ioc_hist_entry hen;
439 if ((fd = open(filename, O_RDONLY)) >= 0) {
440 hen = output_history(NULL, fd, NULL, NULL);
446 * If the object does not exist acquire the history of its
447 * directory and then try accessing the object at each TID.
449 if (strrchr(filename, '/')) {
450 dirname = strdup(filename);
451 *strrchr(dirname, '/') = 0;
453 dirname = strdup(".");
458 if ((fd = open(dirname, O_RDONLY)) >= 0) {
459 output_history(NULL, fd, NULL, &tse_tree);
463 tse = RB_MAX(undo_hist_entry_rb_tree, &tse_tree);
465 asprintf(&path, "%s@@0x%016llx",
466 filename, tse->tse.tid);
467 if ((fd = open(path, O_RDONLY)) >= 0) {
468 hen = output_history(NULL, fd, NULL, NULL);
474 tse = RB_PREV(undo_hist_entry_rb_tree, &tse_tree, tse);
477 while ((tse = RB_ROOT(&tse_tree)) != NULL) {
478 RB_REMOVE(undo_hist_entry_rb_tree, &tse_tree, tse);
485 struct hammer_ioc_hist_entry
486 output_history(const char *filename, int fd, FILE *fp,
487 struct undo_hist_entry_rb_tree *tse_tree)
489 struct hammer_ioc_hist_entry hen;
490 struct undo_hist_entry *tse;
500 if (tse_tree == NULL) {
501 tse_tree = malloc(sizeof(*tse_tree));
509 * Collect a unique set of transaction ids
511 hen = collect_history(fd, &error, tse_tree);
513 if (error && filename)
514 printf("%s: %s\n", filename, strerror(errno));
516 printf("%s:\n", filename);
519 * If fp != NULL dump the history to stdout.
522 RB_FOREACH(tse, undo_hist_entry_rb_tree, tse_tree) {
523 t = (time_t)tse->tse.time32;
525 strftime(datestr, sizeof(datestr),
526 "%d-%b-%Y %H:%M:%S", tp);
527 printf("\t0x%016llx %s\n", tse->tse.tid, datestr);
532 * Caller did not want a tree returned
535 while ((tse = RB_ROOT(tse_tree)) != NULL) {
536 RB_REMOVE(undo_hist_entry_rb_tree, tse_tree, tse);
545 struct hammer_ioc_hist_entry
546 collect_history(int fd, int *errorp, struct undo_hist_entry_rb_tree *tse_tree)
548 struct hammer_ioc_hist_entry hen;
549 struct hammer_ioc_history hist;
550 struct undo_hist_entry *tse;
557 bzero(&hist, sizeof(hist));
558 hist.beg_tid = HAMMER_MIN_TID;
559 hist.end_tid = HAMMER_MAX_TID;
560 hist.head.flags |= HAMMER_IOC_HISTORY_ATKEY;
562 hist.nxt_key = HAMMER_MAX_KEY;
569 if (tse_tree == NULL) {
570 tse_tree = malloc(sizeof(*tse_tree));
578 * Collect a unique set of transaction ids
580 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
585 for (i = 0; i < hist.count; ++i) {
586 tse = malloc(sizeof(*tse));
587 tse->tse = hist.hist_ary[i];
588 if (RB_INSERT(undo_hist_entry_rb_tree, tse_tree, tse)) {
592 if (hist.head.flags & HAMMER_IOC_HISTORY_EOF)
594 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY) {
595 hist.key = hist.nxt_key;
596 hist.nxt_key = HAMMER_MAX_KEY;
598 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_TID)
599 hist.beg_tid = hist.nxt_tid;
600 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
607 * Locate next-to-last entry
609 tse = RB_MAX(undo_hist_entry_rb_tree, tse_tree);
611 tse = RB_PREV(undo_hist_entry_rb_tree, tse_tree, tse);
620 while ((tse = RB_ROOT(tse_tree)) != NULL) {
621 RB_REMOVE(undo_hist_entry_rb_tree, tse_tree, tse);
631 parse_delta_time(const char *timeStr)
635 tid = strtoull(timeStr, NULL, 0);
640 runcmd(int fd, const char *cmd, ...)
649 for (ac = 0; va_arg(va, void *) != NULL; ++ac)
653 av = malloc((ac + 1) * sizeof(char *));
655 for (i = 0; i < ac; ++i)
656 av[i] = va_arg(va, char *);
660 if ((pid = fork()) < 0) {
663 } else if (pid == 0) {
671 while (waitpid(pid, NULL, 0) != pid)
678 * Convert tid to timestamp.
681 timestamp(hammer_ioc_hist_entry_t hen)
683 static char timebuf[64];
684 time_t t = (time_t)hen->time32;
688 strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp);
694 undo_hist_entry_compare(struct undo_hist_entry *he1,
695 struct undo_hist_entry *he2)
697 if (he1->tse.tid < he2->tse.tid)
699 if (he1->tse.tid > he2->tse.tid)
707 fprintf(stderr, "undo [-adDiuv] [-o outfile] "
708 "[-t transaction-id] [-t transaction-id] path...\n"
709 " -a Iterate all historical segments\n"
712 " -i Dump history transaction ids\n"
713 " -u Generate .undo files\n"
715 " -o file Output to the specified file\n"
716 " -t TID Retrieve as of transaction-id, TID\n"
717 " (a second `-t TID' to diff two versions)\n");