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.2 2008/06/24 17:40:24 dillon Exp $
37 * UNDO - retrieve an older version of a file.
40 #include <sys/types.h>
50 #include <vfs/hammer/hammer_disk.h>
51 #include <vfs/hammer/hammer_ioctl.h>
53 enum undo_type { TYPE_FILE, TYPE_DIFF, TYPE_RDIFF, TYPE_HISTORY };
55 static void doiterate(const char *orig_filename, const char *outFileName,
56 const char *outFilePostfix, int mult, enum undo_type type);
57 static void dogenerate(const char *filename, const char *outFileName,
58 const char *outFilePostfix,
59 int mult, int idx, enum undo_type type,
60 struct hammer_ioc_hist_entry ts1,
61 struct hammer_ioc_hist_entry ts2);
62 static struct hammer_ioc_hist_entry
63 find_recent(const char *filename);
64 static struct hammer_ioc_hist_entry
65 output_history(const char *filename, int fd, FILE *fp,
66 struct hammer_ioc_hist_entry **hist_ary, int *tid_num);
67 static hammer_tid_t parse_delta_time(const char *timeStr);
68 static void runcmd(int fd, const char *cmd, ...);
69 static char *timestamp(hammer_ioc_hist_entry_t hen);
70 static void usage(void);
72 static int VerboseOpt;
75 main(int ac, char **av)
77 const char *outFileName = NULL;
78 const char *outFilePostfix = NULL;
79 enum { CMD_DUMP, CMD_ITERATEALL } cmd;
81 struct hammer_ioc_hist_entry ts1;
82 struct hammer_ioc_hist_entry ts2;
86 bzero(&ts1, sizeof(ts1));
87 bzero(&ts2, sizeof(ts2));
92 while ((c = getopt(ac, av, "dDhiuvo:t:")) != -1) {
95 if (type != TYPE_FILE)
100 if (type != TYPE_FILE)
105 if (type != TYPE_FILE)
110 cmd = CMD_ITERATEALL;
113 outFilePostfix = ".undo";
119 outFileName = optarg;
122 if (ts1.tid && ts2.tid)
124 else if (ts1.tid == 0)
125 ts1.tid = parse_delta_time(optarg);
127 ts2.tid = parse_delta_time(optarg);
139 if (outFileName && outFilePostfix) {
140 fprintf(stderr, "The -o option may not be combined with -u\n");
152 * Validate the output template, if specified.
154 if (outFileName && mult) {
155 const char *ptr = outFileName;
158 while ((ptr = strchr(ptr, '%')) != NULL) {
161 fprintf(stderr, "Malformed output "
167 } else if (ptr[1] != '%') {
168 fprintf(stderr, "Malformed output template\n");
179 dogenerate(*av, outFileName, outFilePostfix,
180 mult, -1, type, ts1, ts2);
183 doiterate(*av, outFileName, outFilePostfix,
194 * Iterate through a file's history
198 doiterate(const char *orig_filename, const char *outFileName,
199 const char *outFilePostfix, int mult, enum undo_type type)
201 hammer_ioc_hist_entry_t tid_ary = NULL;
202 struct hammer_ioc_hist_entry tid_max;
203 struct hammer_ioc_hist_entry ts1;
204 const char *use_filename;
210 tid_max.tid = HAMMER_MAX_TID;
213 use_filename = orig_filename;
214 if ((fd = open(orig_filename, O_RDONLY)) < 0) {
215 ts1 = find_recent(orig_filename);
217 asprintf(&path, "%s@@0x%016llx",
218 orig_filename, ts1.tid);
223 if ((fd = open(use_filename, O_RDONLY)) >= 0) {
224 printf("%s: ITERATE ENTIRE HISTORY\n", orig_filename);
225 output_history(NULL, fd, NULL, &tid_ary, &tid_num);
228 for (i = 0; i < tid_num; ++i) {
229 if (i && tid_ary[i].tid == tid_ary[i-1].tid)
232 if (i == tid_num - 1) {
233 dogenerate(orig_filename,
234 outFileName, outFilePostfix,
236 tid_ary[i], tid_max);
238 dogenerate(orig_filename,
239 outFileName, outFilePostfix,
241 tid_ary[i], tid_ary[i+1]);
246 printf("%s: ITERATE ENTIRE HISTORY: %s\n",
247 orig_filename, strerror(errno));
254 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
259 dogenerate(const char *filename, const char *outFileName,
260 const char *outFilePostfix,
261 int mult, int idx, enum undo_type type,
262 struct hammer_ioc_hist_entry ts1,
263 struct hammer_ioc_hist_entry ts2)
278 * Open the input file. If ts1 is 0 try to locate the most recent
279 * version of the file prior to the current version.
282 ts1 = find_recent(filename);
283 asprintf(&ipath1, "%s@@0x%016llx", filename, ts1.tid);
284 if (lstat(ipath1, &st) < 0) {
286 fprintf(stderr, "Cannot locate src/historical "
294 asprintf(&ipath2, "%s", filename);
296 asprintf(&ipath2, "%s@@0x%015llx", filename, ts2.tid);
298 if (lstat(ipath2, &st) < 0) {
301 fprintf(stderr, "Cannot locate tgt/historical "
304 } else if (VerboseOpt > 1) {
305 fprintf(stderr, "Cannot locate %s\n", filename);
308 ipath2 = strdup("/dev/null");
312 * elm is the last component of the input file name
314 if ((elm = strrchr(filename, '/')) != NULL)
320 * Where do we stuff our output?
324 asprintf(&path, outFileName, elm);
325 fp = fopen(path, "w");
332 fp = fopen(outFileName, "w");
338 } else if (outFilePostfix) {
340 asprintf(&path, "%s%s.%04d", filename,
341 outFilePostfix, idx);
343 asprintf(&path, "%s%s", filename, outFilePostfix);
345 fp = fopen(path, "w");
352 if (mult && type == TYPE_FILE) {
354 printf("\n>>> %s %04d 0x%016llx %s\n\n",
355 filename, idx, ts1.tid, timestamp(&ts1));
357 printf("\n>>> %s ---- 0x%016llx %s\n\n",
358 filename, ts1.tid, timestamp(&ts1));
360 } else if (idx >= 0 && type == TYPE_FILE) {
361 printf("\n>>> %s %04d 0x%016llx %s\n\n",
362 filename, idx, ts1.tid, timestamp(&ts1));
369 if ((fi = fopen(ipath1, "r")) != NULL) {
370 while ((n = fread(buf, 1, 8192, fi)) > 0)
371 fwrite(buf, 1, n, fp);
376 printf("diff -u %s %s (to %s)\n",
377 ipath1, ipath2, timestamp(&ts2));
379 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-u", ipath1, ipath2, NULL);
382 printf("diff -u %s %s\n", ipath2, ipath1);
384 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-u", ipath2, ipath1, NULL);
387 if ((fi = fopen(ipath1, "r")) != NULL) {
388 output_history(filename, fileno(fi), fp, NULL, NULL);
401 * Try to find a recent version of the file.
403 * XXX if file cannot be found
406 struct hammer_ioc_hist_entry
407 find_recent(const char *filename)
409 hammer_ioc_hist_entry_t tid_ary = NULL;
411 struct hammer_ioc_hist_entry hen;
417 if ((fd = open(filename, O_RDONLY)) >= 0) {
418 hen = output_history(NULL, fd, NULL, NULL, NULL);
424 * If the object does not exist acquire the history of its
425 * directory and then try accessing the object at each TID.
427 if (strrchr(filename, '/')) {
428 dirname = strdup(filename);
429 *strrchr(dirname, '/') = 0;
431 dirname = strdup(".");
436 if ((fd = open(dirname, O_RDONLY)) >= 0) {
437 output_history(NULL, fd, NULL, &tid_ary, &tid_num);
441 for (i = tid_num - 1; i >= 0; --i) {
442 asprintf(&path, "%s@@0x%016llx", filename, tid_ary[i].tid);
443 if ((fd = open(path, O_RDONLY)) >= 0) {
444 hen = output_history(NULL, fd, NULL, NULL, NULL);
456 * Collect all the transaction ids representing changes made to the
457 * file, sort, and output (weeding out duplicates). If fp is NULL
458 * we do not output anything and simply return the most recent TID we
462 tid_cmp(const void *arg1, const void *arg2)
464 const struct hammer_ioc_hist_entry *tid1 = arg1;
465 const struct hammer_ioc_hist_entry *tid2 = arg2;
467 if (tid1->tid < tid2->tid)
469 if (tid1->tid > tid2->tid)
475 struct hammer_ioc_hist_entry
476 output_history(const char *filename, int fd, FILE *fp,
477 struct hammer_ioc_hist_entry **hist_aryp, int *tid_nump)
479 struct hammer_ioc_hist_entry hen;
480 struct hammer_ioc_history hist;
487 hammer_ioc_hist_entry_t hist_ary = malloc(tid_max * sizeof(*hist_ary));
489 bzero(&hist, sizeof(hist));
490 hist.beg_tid = HAMMER_MIN_TID;
491 hist.end_tid = HAMMER_MAX_TID;
492 hist.head.flags |= HAMMER_IOC_HISTORY_ATKEY;
494 hist.nxt_key = HAMMER_MAX_KEY;
499 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
501 printf("%s: %s\n", filename, strerror(errno));
505 printf("%s: objid=0x%016llx\n", filename, hist.obj_id);
507 if (tid_num + hist.count >= tid_max) {
508 tid_max = (tid_max * 3 / 2) + hist.count;
509 hist_ary = realloc(hist_ary, tid_max * sizeof(*hist_ary));
511 for (i = 0; i < hist.count; ++i) {
512 hist_ary[tid_num++] = hist.hist_ary[i];
514 if (hist.head.flags & HAMMER_IOC_HISTORY_EOF)
516 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY) {
517 hist.key = hist.nxt_key;
518 hist.nxt_key = HAMMER_MAX_KEY;
520 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_TID)
521 hist.beg_tid = hist.nxt_tid;
522 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
524 printf("%s: %s\n", filename, strerror(errno));
528 qsort(hist_ary, tid_num, sizeof(*hist_ary), tid_cmp);
531 for (i = 0; fp && i < tid_num; ++i) {
532 if (i && hist_ary[i].tid == hist_ary[i-1].tid)
534 t = (time_t)hist_ary[i].time32;
536 strftime(datestr, sizeof(datestr), "%d-%b-%Y %H:%M:%S", tp);
537 printf("\t0x%016llx %s\n", hist_ary[i].tid, datestr);
540 hen = hist_ary[tid_num-2];
543 *hist_aryp = hist_ary;
553 parse_delta_time(const char *timeStr)
557 tid = strtoull(timeStr, NULL, 0);
562 runcmd(int fd, const char *cmd, ...)
571 for (ac = 0; va_arg(va, void *) != NULL; ++ac)
575 av = malloc((ac + 1) * sizeof(char *));
577 for (i = 0; i < ac; ++i)
578 av[i] = va_arg(va, char *);
582 if ((pid = fork()) < 0) {
585 } else if (pid == 0) {
593 while (waitpid(pid, NULL, 0) != pid)
600 * Convert tid to timestamp.
603 timestamp(hammer_ioc_hist_entry_t hen)
605 static char timebuf[64];
606 time_t t = (time_t)hen->time32;
610 strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp);
617 fprintf(stderr, "undo [-dDhiuv] [-t n{s,m,h,d}] [-t n...] "
619 fprintf(stderr, " -d Forward diff\n"
621 " -i Dump history transaction ids\n"
622 " -h Iterate all historical segments\n"
623 " -u Generate .undo files\n"
625 " -t spec Retrieve as of n secs,mins,etc ago\n"
626 " (a second one to diff two versions)\n"
627 " (a 0x TID may also be specified)\n");