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.5 2008/07/16 21:38:10 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,
63 static struct hammer_ioc_hist_entry
64 find_recent(const char *filename);
65 static struct hammer_ioc_hist_entry
66 output_history(const char *filename, int fd, FILE *fp,
67 struct hammer_ioc_hist_entry **hist_ary, int *tid_num);
68 static hammer_tid_t parse_delta_time(const char *timeStr);
69 static void runcmd(int fd, const char *cmd, ...);
70 static char *timestamp(hammer_ioc_hist_entry_t hen);
71 static void usage(void);
73 static int VerboseOpt;
76 main(int ac, char **av)
78 const char *outFileName = NULL;
79 const char *outFilePostfix = NULL;
80 enum { CMD_DUMP, CMD_ITERATEALL } cmd;
82 struct hammer_ioc_hist_entry ts1;
83 struct hammer_ioc_hist_entry ts2;
87 bzero(&ts1, sizeof(ts1));
88 bzero(&ts2, sizeof(ts2));
93 while ((c = getopt(ac, av, "adDiuvo:t:")) != -1) {
96 if (type != TYPE_FILE)
101 if (type != TYPE_FILE)
106 if (type != TYPE_FILE)
111 cmd = CMD_ITERATEALL;
114 outFilePostfix = ".undo";
120 outFileName = optarg;
123 if (ts1.tid && ts2.tid)
125 else if (ts1.tid == 0)
126 ts1.tid = parse_delta_time(optarg);
128 ts2.tid = parse_delta_time(optarg);
140 if (outFileName && outFilePostfix) {
141 fprintf(stderr, "The -o option may not be combined with -u\n");
153 * Validate the output template, if specified.
155 if (outFileName && mult) {
156 const char *ptr = outFileName;
159 while ((ptr = strchr(ptr, '%')) != NULL) {
162 fprintf(stderr, "Malformed output "
168 } else if (ptr[1] != '%') {
169 fprintf(stderr, "Malformed output template\n");
180 dogenerate(*av, outFileName, outFilePostfix,
181 mult, -1, type, ts1, ts2, 1);
184 doiterate(*av, outFileName, outFilePostfix,
195 * Iterate through a file's history
199 doiterate(const char *orig_filename, const char *outFileName,
200 const char *outFilePostfix, int mult, enum undo_type type)
202 hammer_ioc_hist_entry_t tid_ary = NULL;
203 struct hammer_ioc_hist_entry tid_max;
204 struct hammer_ioc_hist_entry ts1;
205 const char *use_filename;
211 tid_max.tid = HAMMER_MAX_TID;
214 use_filename = orig_filename;
215 if ((fd = open(orig_filename, O_RDONLY)) < 0) {
216 ts1 = find_recent(orig_filename);
218 asprintf(&path, "%s@@0x%016llx",
219 orig_filename, ts1.tid);
224 if ((fd = open(use_filename, O_RDONLY)) >= 0) {
225 printf("%s: ITERATE ENTIRE HISTORY\n", orig_filename);
226 output_history(NULL, fd, NULL, &tid_ary, &tid_num);
229 for (i = 0; i < tid_num; ++i) {
230 if (i && tid_ary[i].tid == tid_ary[i-1].tid)
233 if (i == tid_num - 1) {
234 dogenerate(orig_filename,
235 outFileName, outFilePostfix,
237 tid_ary[i], tid_max, 0);
239 dogenerate(orig_filename,
240 outFileName, outFilePostfix,
242 tid_ary[i], tid_ary[i+1], 0);
247 printf("%s: ITERATE ENTIRE HISTORY: %s\n",
248 orig_filename, strerror(errno));
255 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
260 dogenerate(const char *filename, const char *outFileName,
261 const char *outFilePostfix,
262 int mult, int idx, enum undo_type type,
263 struct hammer_ioc_hist_entry ts1,
264 struct hammer_ioc_hist_entry ts2,
280 * Open the input file. If ts1 is 0 try to locate the most recent
281 * version of the file prior to the current version.
284 ts1 = find_recent(filename);
285 asprintf(&ipath1, "%s@@0x%016llx", filename, ts1.tid);
286 if (lstat(ipath1, &st) < 0) {
288 asprintf(&ipath1, "%s", filename);
289 if (force == 0 || lstat(ipath1, &st) < 0) {
290 fprintf(stderr, "Cannot locate src/historical "
291 "idx=%d %s@@0x%016llx, the file"
292 "may have been renamed in the past.\n",
293 idx, filename, ts1.tid);
297 "WARNING: %s was renamed at some point in the past,\n"
298 "attempting to continue with current version\n",
303 asprintf(&ipath2, "%s", filename);
305 asprintf(&ipath2, "%s@@0x%015llx", filename, ts2.tid);
307 if (lstat(ipath2, &st) < 0) {
310 fprintf(stderr, "Cannot locate tgt/historical "
313 } else if (VerboseOpt > 1) {
314 fprintf(stderr, "Cannot locate %s\n", filename);
317 ipath2 = strdup("/dev/null");
321 * elm is the last component of the input file name
323 if ((elm = strrchr(filename, '/')) != NULL)
329 * Where do we stuff our output?
333 asprintf(&path, outFileName, elm);
334 fp = fopen(path, "w");
341 fp = fopen(outFileName, "w");
347 } else if (outFilePostfix) {
349 asprintf(&path, "%s%s.%04d", filename,
350 outFilePostfix, idx);
352 asprintf(&path, "%s%s", filename, outFilePostfix);
354 fp = fopen(path, "w");
361 if (mult && type == TYPE_FILE) {
363 printf("\n>>> %s %04d 0x%016llx %s\n\n",
364 filename, idx, ts1.tid, timestamp(&ts1));
366 printf("\n>>> %s ---- 0x%016llx %s\n\n",
367 filename, ts1.tid, timestamp(&ts1));
369 } else if (idx >= 0 && type == TYPE_FILE) {
370 printf("\n>>> %s %04d 0x%016llx %s\n\n",
371 filename, idx, ts1.tid, timestamp(&ts1));
378 if ((fi = fopen(ipath1, "r")) != NULL) {
379 while ((n = fread(buf, 1, 8192, fi)) > 0)
380 fwrite(buf, 1, n, fp);
385 printf("diff -u %s %s (to %s)\n",
386 ipath1, ipath2, timestamp(&ts2));
388 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-u", ipath1, ipath2, NULL);
391 printf("diff -u %s %s\n", ipath2, ipath1);
393 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-u", ipath2, ipath1, NULL);
396 if ((fi = fopen(ipath1, "r")) != NULL) {
397 output_history(filename, fileno(fi), fp, NULL, NULL);
410 * Try to find a recent version of the file.
412 * XXX if file cannot be found
415 struct hammer_ioc_hist_entry
416 find_recent(const char *filename)
418 hammer_ioc_hist_entry_t tid_ary = NULL;
420 struct hammer_ioc_hist_entry hen;
426 if ((fd = open(filename, O_RDONLY)) >= 0) {
427 hen = output_history(NULL, fd, NULL, NULL, NULL);
433 * If the object does not exist acquire the history of its
434 * directory and then try accessing the object at each TID.
436 if (strrchr(filename, '/')) {
437 dirname = strdup(filename);
438 *strrchr(dirname, '/') = 0;
440 dirname = strdup(".");
445 if ((fd = open(dirname, O_RDONLY)) >= 0) {
446 output_history(NULL, fd, NULL, &tid_ary, &tid_num);
450 for (i = tid_num - 1; i >= 0; --i) {
451 asprintf(&path, "%s@@0x%016llx", filename, tid_ary[i].tid);
452 if ((fd = open(path, O_RDONLY)) >= 0) {
453 hen = output_history(NULL, fd, NULL, NULL, NULL);
465 * Collect all the transaction ids representing changes made to the
466 * file, sort, and output (weeding out duplicates). If fp is NULL
467 * we do not output anything and simply return the most recent TID we
471 tid_cmp(const void *arg1, const void *arg2)
473 const struct hammer_ioc_hist_entry *tid1 = arg1;
474 const struct hammer_ioc_hist_entry *tid2 = arg2;
476 if (tid1->tid < tid2->tid)
478 if (tid1->tid > tid2->tid)
484 struct hammer_ioc_hist_entry
485 output_history(const char *filename, int fd, FILE *fp,
486 struct hammer_ioc_hist_entry **hist_aryp, int *tid_nump)
488 struct hammer_ioc_hist_entry hen;
489 struct hammer_ioc_history hist;
496 hammer_ioc_hist_entry_t hist_ary = malloc(tid_max * sizeof(*hist_ary));
498 bzero(&hist, sizeof(hist));
499 hist.beg_tid = HAMMER_MIN_TID;
500 hist.end_tid = HAMMER_MAX_TID;
501 hist.head.flags |= HAMMER_IOC_HISTORY_ATKEY;
503 hist.nxt_key = HAMMER_MAX_KEY;
508 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
510 printf("%s: %s\n", filename, strerror(errno));
514 printf("%s: objid=0x%016llx\n", filename, hist.obj_id);
516 if (tid_num + hist.count >= tid_max) {
517 tid_max = (tid_max * 3 / 2) + hist.count;
518 hist_ary = realloc(hist_ary, tid_max * sizeof(*hist_ary));
520 for (i = 0; i < hist.count; ++i) {
521 hist_ary[tid_num++] = hist.hist_ary[i];
523 if (hist.head.flags & HAMMER_IOC_HISTORY_EOF)
525 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY) {
526 hist.key = hist.nxt_key;
527 hist.nxt_key = HAMMER_MAX_KEY;
529 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_TID)
530 hist.beg_tid = hist.nxt_tid;
531 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
533 printf("%s: %s\n", filename, strerror(errno));
537 qsort(hist_ary, tid_num, sizeof(*hist_ary), tid_cmp);
540 for (i = 0; fp && i < tid_num; ++i) {
541 if (i && hist_ary[i].tid == hist_ary[i-1].tid)
543 t = (time_t)hist_ary[i].time32;
545 strftime(datestr, sizeof(datestr), "%d-%b-%Y %H:%M:%S", tp);
546 printf("\t0x%016llx %s\n", hist_ary[i].tid, datestr);
549 hen = hist_ary[tid_num-2];
552 *hist_aryp = hist_ary;
562 parse_delta_time(const char *timeStr)
566 tid = strtoull(timeStr, NULL, 0);
571 runcmd(int fd, const char *cmd, ...)
580 for (ac = 0; va_arg(va, void *) != NULL; ++ac)
584 av = malloc((ac + 1) * sizeof(char *));
586 for (i = 0; i < ac; ++i)
587 av[i] = va_arg(va, char *);
591 if ((pid = fork()) < 0) {
594 } else if (pid == 0) {
602 while (waitpid(pid, NULL, 0) != pid)
609 * Convert tid to timestamp.
612 timestamp(hammer_ioc_hist_entry_t hen)
614 static char timebuf[64];
615 time_t t = (time_t)hen->time32;
619 strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp);
626 fprintf(stderr, "undo [-adDiuv] [-o outfile] "
627 "[-t transaction-id] [-t transaction-id] file...\n"
628 " -a Iterate all historical segments\n"
631 " -i Dump history transaction ids\n"
632 " -u Generate .undo files\n"
634 " -o file Output to the specified file\n"
635 " -t TID Retrieve as of transaction-id, TID\n"
636 " (a second `-t TID' to diff two versions)\n");