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
73 #define UNDO_FLAG_SETTID1 0x0004
74 #define UNDO_FLAG_SETTID2 0x0008
76 static int undo_hist_entry_compare(struct undo_hist_entry
*he1
,
77 struct undo_hist_entry
*he2
);
78 static void doiterate(const char *filename
, const char *outFileName
,
79 const char *outFilePostfix
, int flags
,
80 struct hammer_ioc_hist_entry ts1
,
81 struct hammer_ioc_hist_entry ts2
,
82 enum undo_cmd cmd
, enum undo_type type
);
83 static void dogenerate(const char *filename
, const char *outFileName
,
84 const char *outFilePostfix
,
85 int flags
, int idx
, enum undo_type type
,
86 struct hammer_ioc_hist_entry ts1
,
87 struct hammer_ioc_hist_entry ts2
);
88 static void collect_history(int fd
, int *error
,
89 struct undo_hist_entry_rb_tree
*tse_tree
);
90 static void collect_dir_history(const char *filename
, int *error
,
91 struct undo_hist_entry_rb_tree
*dir_tree
);
92 static void clean_tree(struct undo_hist_entry_rb_tree
*tree
);
93 static hammer_tid_t
parse_delta_time(const char *timeStr
, int *flags
,
95 static void runcmd(int fd
, const char *cmd
, ...);
96 static char *timestamp(hammer_ioc_hist_entry_t hen
);
97 static void usage(void);
99 static int VerboseOpt
;
101 RB_GENERATE2(undo_hist_entry_rb_tree
, undo_hist_entry
, rbnode
,
102 undo_hist_entry_compare
, hammer_tid_t
, tse
.tid
);
106 main(int ac
, char **av
)
108 const char *outFileName
= NULL
;
109 const char *outFilePostfix
= NULL
;
112 struct hammer_ioc_hist_entry ts1
;
113 struct hammer_ioc_hist_entry ts2
;
118 bzero(&ts1
, sizeof(ts1
));
119 bzero(&ts2
, sizeof(ts2
));
126 while ((c
= getopt(ac
, av
, "adDiuvo:t:")) != -1) {
135 if (type
!= TYPE_FILE
)
138 cmd
= CMD_ITERATEALL
;
141 cmd
= CMD_ITERATEALL
;
144 outFilePostfix
= ".undo";
150 outFileName
= optarg
;
154 * Parse one or two -t options. If two are specified
155 * -d is implied (but may be overridden)
159 ts1
.tid
= parse_delta_time(optarg
, &flags
,
161 } else if (count_t
== 2) {
162 ts2
.tid
= parse_delta_time(optarg
, &flags
,
164 if (type
== TYPE_FILE
)
180 if (outFileName
&& outFilePostfix
) {
181 fprintf(stderr
, "The -o option may not be combined with -u\n");
188 flags
|= UNDO_FLAG_MULT
;
194 * Validate the output template, if specified.
196 if (outFileName
&& (flags
& UNDO_FLAG_MULT
)) {
197 const char *ptr
= outFileName
;
200 while ((ptr
= strchr(ptr
, '%')) != NULL
) {
203 fprintf(stderr
, "Malformed output "
209 } else if (ptr
[1] != '%') {
210 fprintf(stderr
, "Malformed output template\n");
219 doiterate(*av
, outFileName
, outFilePostfix
,
220 flags
, ts1
, ts2
, cmd
, type
);
228 * Iterate through a file's history. If cmd == CMD_DUMP we take the
229 * next-to-last transaction id, unless another given. Otherwise if
230 * cmd == CMD_ITERATEALL we scan all transaction ids.
232 * Also iterate through the directory's history to locate other inodes that
233 * used the particular file name.
237 doiterate(const char *filename
, const char *outFileName
,
238 const char *outFilePostfix
, int flags
,
239 struct hammer_ioc_hist_entry ts1
,
240 struct hammer_ioc_hist_entry ts2
,
241 enum undo_cmd cmd
, enum undo_type type
)
243 struct undo_hist_entry_rb_tree dir_tree
;
244 struct undo_hist_entry_rb_tree tse_tree
;
245 struct undo_hist_entry
*tse1
;
246 struct undo_hist_entry
*tse2
;
247 struct hammer_ioc_hist_entry tid_max
;
256 tid_max
.tid
= HAMMER_MAX_TID
;
260 * Use the directory history to locate all possible versions of
263 collect_dir_history(filename
, &error
, &dir_tree
);
264 RB_FOREACH(tse1
, undo_hist_entry_rb_tree
, &dir_tree
) {
265 asprintf(&path
, "%s@@0x%016jx", filename
, (uintmax_t)tse1
->tse
.tid
);
266 if ((fd
= open(path
, O_RDONLY
)) > 0) {
267 collect_history(fd
, &error
, &tse_tree
);
271 if ((fd
= open(filename
, O_RDONLY
)) > 0) {
272 collect_history(fd
, &error
, &tse_tree
);
275 if (cmd
== CMD_DUMP
) {
277 flags
& (UNDO_FLAG_SETTID1
|UNDO_FLAG_SETTID2
)) &&
278 RB_EMPTY(&tse_tree
)) {
279 if ((fd
= open(filename
, O_RDONLY
)) > 0) {
280 collect_history(fd
, &error
, &tse_tree
);
285 * Find entry if tid set to placeholder index
287 if (flags
& UNDO_FLAG_SETTID1
){
288 tse1
= RB_MAX(undo_hist_entry_rb_tree
, &tse_tree
);
289 while (tse1
&& ts1
.tid
--) {
290 tse1
= RB_PREV(undo_hist_entry_rb_tree
,
298 if (flags
& UNDO_FLAG_SETTID2
){
299 tse2
= RB_MAX(undo_hist_entry_rb_tree
, &tse_tree
);
300 while (tse2
&& ts2
.tid
--) {
301 tse2
= RB_PREV(undo_hist_entry_rb_tree
,
311 * Single entry, most recent prior to current
314 tse2
= RB_MAX(undo_hist_entry_rb_tree
, &tse_tree
);
317 tse1
= RB_PREV(undo_hist_entry_rb_tree
,
324 printf("%s: No UNDO history found\n", filename
);
327 outFileName
, outFilePostfix
,
331 } else if (RB_ROOT(&tse_tree
)) {
333 * Iterate entire history
335 printf("%s: ITERATE ENTIRE HISTORY\n", filename
);
339 RB_FOREACH(tse2
, undo_hist_entry_rb_tree
, &tse_tree
) {
342 outFileName
, outFilePostfix
,
344 tse1
->tse
, tse2
->tse
);
346 if (tse1
&& tse2
->inum
!= tse1
->inum
)
347 flags
|= UNDO_FLAG_INOCHG
;
349 flags
&= ~UNDO_FLAG_INOCHG
;
354 * There is no delta to print for the last pair,
355 * because they are identical.
357 if (type
!= TYPE_DIFF
&& type
!= TYPE_RDIFF
) {
359 outFileName
, outFilePostfix
,
364 printf("%s: ITERATE ENTIRE HISTORY: %s\n",
365 filename
, strerror(error
));
369 clean_tree(&dir_tree
);
370 clean_tree(&tse_tree
);
374 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
379 dogenerate(const char *filename
, const char *outFileName
,
380 const char *outFilePostfix
,
381 int flags
, int idx
, enum undo_type type
,
382 struct hammer_ioc_hist_entry ts1
,
383 struct hammer_ioc_hist_entry ts2
)
401 * Open the input file. If ts1 is 0 try to locate the most recent
402 * version of the file prior to the current version.
405 asprintf(&ipath1
, "%s", filename
);
407 asprintf(&ipath1
, "%s@@0x%016jx", filename
, (uintmax_t)ts1
.tid
);
410 asprintf(&ipath2
, "%s", filename
);
412 asprintf(&ipath2
, "%s@@0x%016jx", filename
, (uintmax_t)ts2
.tid
);
414 if (lstat(ipath1
, &st
) < 0 && lstat(ipath2
, &st
) < 0) {
415 if (idx
== 0 || VerboseOpt
) {
416 fprintf(stderr
, "Unable to access either %s or %s\n",
425 * elm is the last component of the input file name
427 if ((elm
= strrchr(filename
, '/')) != NULL
)
433 * Where do we stuff our output?
436 if (flags
& UNDO_FLAG_MULT
) {
437 asprintf(&path
, outFileName
, elm
);
438 fp
= fopen(path
, "w");
445 fp
= fopen(outFileName
, "w");
451 } else if (outFilePostfix
) {
453 asprintf(&path
, "%s%s.%04d", filename
,
454 outFilePostfix
, idx
);
456 asprintf(&path
, "%s%s", filename
, outFilePostfix
);
458 fp
= fopen(path
, "w");
465 if ((flags
& UNDO_FLAG_MULT
) && type
== TYPE_FILE
) {
467 printf("\n>>> %s %04d 0x%016jx %s\n\n",
468 filename
, idx
, (uintmax_t)ts1
.tid
,
471 printf("\n>>> %s ---- 0x%016jx %s\n\n",
472 filename
, (uintmax_t)ts1
.tid
,
475 } else if (idx
>= 0 && type
== TYPE_FILE
) {
476 printf("\n>>> %s %04d 0x%016jx %s\n\n",
477 filename
, idx
, (uintmax_t)ts1
.tid
,
485 if ((fi
= fopen(ipath1
, "r")) != NULL
) {
486 while ((n
= fread(buf
, 1, 8192, fi
)) > 0)
487 fwrite(buf
, 1, n
, fp
);
492 printf("diff -N -r -u %s %s (to %s)\n",
493 ipath1
, ipath2
, timestamp(&ts2
));
495 runcmd(fileno(fp
), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath1
, ipath2
, NULL
);
498 printf("diff -N -r -u %s %s\n", ipath2
, ipath1
);
500 runcmd(fileno(fp
), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath2
, ipath1
, NULL
);
503 t
= (time_t)ts1
.time32
;
505 strftime(datestr
, sizeof(datestr
), "%d-%b-%Y %H:%M:%S", tp
);
506 printf("\t0x%016jx %s", (uintmax_t)ts1
.tid
, datestr
);
507 if (flags
& UNDO_FLAG_INOCHG
)
508 printf(" inode-change");
509 if (lstat(ipath1
, &st
) < 0)
510 printf(" file-deleted");
523 clean_tree(struct undo_hist_entry_rb_tree
*tree
)
525 struct undo_hist_entry
*tse
;
527 while ((tse
= RB_ROOT(tree
)) != NULL
) {
528 RB_REMOVE(undo_hist_entry_rb_tree
, tree
, tse
);
535 collect_history(int fd
, int *errorp
, struct undo_hist_entry_rb_tree
*tse_tree
)
537 struct hammer_ioc_history hist
;
538 struct undo_hist_entry
*tse
;
546 bzero(&hist
, sizeof(hist
));
547 hist
.beg_tid
= HAMMER_MIN_TID
;
548 hist
.end_tid
= HAMMER_MAX_TID
;
549 hist
.head
.flags
|= HAMMER_IOC_HISTORY_ATKEY
;
551 hist
.nxt_key
= HAMMER_MAX_KEY
;
555 if (tse_tree
== NULL
) {
556 tse_tree
= malloc(sizeof(*tse_tree
));
564 * Save the inode so inode changes can be reported.
570 * Collect a unique set of transaction ids
572 if (ioctl(fd
, HAMMERIOC_GETHISTORY
, &hist
) < 0) {
577 for (i
= 0; i
< hist
.count
; ++i
) {
578 tse
= malloc(sizeof(*tse
));
579 tse
->tse
= hist
.hist_ary
[i
];
580 tse
->inum
= st
.st_ino
;
581 if (RB_INSERT(undo_hist_entry_rb_tree
, tse_tree
, tse
)) {
585 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_EOF
)
587 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_NEXT_KEY
) {
588 hist
.key
= hist
.nxt_key
;
589 hist
.nxt_key
= HAMMER_MAX_KEY
;
591 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_NEXT_TID
)
592 hist
.beg_tid
= hist
.nxt_tid
;
593 if (ioctl(fd
, HAMMERIOC_GETHISTORY
, &hist
) < 0) {
604 clean_tree(tse_tree
);
611 collect_dir_history(const char *filename
, int *errorp
,
612 struct undo_hist_entry_rb_tree
*dir_tree
)
619 if (strrchr(filename
, '/')) {
620 dirname
= strdup(filename
);
621 *strrchr(dirname
, '/') = 0;
623 dirname
= strdup(".");
625 if ((fd
= open(dirname
, O_RDONLY
)) > 0) {
626 collect_history(fd
, &error
, dir_tree
);
633 parse_delta_time(const char *timeStr
, int *flags
, int ind_flag
)
637 tid
= strtoull(timeStr
, NULL
, 0);
638 if (timeStr
[0] == '+')
640 if (timeStr
[0] >= '0' && timeStr
[0] <= '9' && timeStr
[1] != 'x')
646 runcmd(int fd
, const char *cmd
, ...)
655 for (ac
= 0; va_arg(va
, void *) != NULL
; ++ac
)
659 av
= malloc((ac
+ 1) * sizeof(char *));
661 for (i
= 0; i
< ac
; ++i
)
662 av
[i
] = va_arg(va
, char *);
666 if ((pid
= fork()) < 0) {
669 } else if (pid
== 0) {
677 while (waitpid(pid
, NULL
, 0) != pid
)
684 * Convert tid to timestamp.
687 timestamp(hammer_ioc_hist_entry_t hen
)
689 static char timebuf
[64];
690 time_t t
= (time_t)hen
->time32
;
694 strftime(timebuf
, sizeof(timebuf
), "%d-%b-%Y %H:%M:%S", tp
);
700 undo_hist_entry_compare(struct undo_hist_entry
*he1
,
701 struct undo_hist_entry
*he2
)
703 if (he1
->tse
.tid
< he2
->tse
.tid
)
705 if (he1
->tse
.tid
> he2
->tse
.tid
)
713 fprintf(stderr
, "undo [-adDiuv] [-o outfile] "
714 "[-t transaction-id] [-t transaction-id] path...\n"
715 " -a Iterate all historical segments\n"
718 " -i Dump history transaction ids\n"
719 " -u Generate .undo files\n"
721 " -o file Output to the specified file\n"
722 " -t TID Retrieve as of transaction-id, TID\n"
723 " (a second `-t TID' to diff two)\n"
724 " transaction ids must be prefixed with 0x, and\n"
725 " otherwise may specify an index starting at 0\n"
726 " and iterating backwards through the history.\n"