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>
44 #include <sys/param.h>
52 #include <vfs/hammer/hammer_disk.h>
53 #include <vfs/hammer/hammer_ioctl.h>
56 * Sorted list of transaction ids
58 struct undo_hist_entry
;
59 RB_HEAD(undo_hist_entry_rb_tree
, undo_hist_entry
);
60 RB_PROTOTYPE2(undo_hist_entry_rb_tree
, undo_hist_entry
, rbnode
,
61 undo_hist_entry_compare
, hammer_tid_t
);
63 struct undo_hist_entry
{
64 RB_ENTRY(undo_hist_entry
) rbnode
;
65 struct hammer_ioc_hist_entry tse
;
69 enum undo_type
{ TYPE_FILE
, TYPE_DIFF
, TYPE_RDIFF
, TYPE_HISTORY
};
70 enum undo_cmd
{ CMD_DUMP
, CMD_ITERATEALL
};
72 #define UNDO_FLAG_MULT 0x0001
73 #define UNDO_FLAG_INOCHG 0x0002
74 #define UNDO_FLAG_SETTID1 0x0004
75 #define UNDO_FLAG_SETTID2 0x0008
77 static int undo_hist_entry_compare(struct undo_hist_entry
*he1
,
78 struct undo_hist_entry
*he2
);
79 static void doiterate(const char *filename
, const char *outFileName
,
80 const char *outFilePostfix
, int flags
,
81 struct hammer_ioc_hist_entry ts1
,
82 struct hammer_ioc_hist_entry ts2
,
83 enum undo_cmd cmd
, enum undo_type type
);
84 static void dogenerate(const char *filename
, const char *outFileName
,
85 const char *outFilePostfix
,
86 int flags
, int idx
, enum undo_type type
,
87 struct hammer_ioc_hist_entry ts1
,
88 struct hammer_ioc_hist_entry ts2
);
89 static void collect_history(int fd
, int *error
,
90 struct undo_hist_entry_rb_tree
*tse_tree
);
91 static void collect_dir_history(const char *filename
, int *error
,
92 struct undo_hist_entry_rb_tree
*dir_tree
);
93 static void clean_tree(struct undo_hist_entry_rb_tree
*tree
);
94 static hammer_tid_t
parse_delta_time(const char *timeStr
, int *flags
,
96 static void runcmd(int fd
, const char *cmd
, ...);
97 static char *timestamp(hammer_ioc_hist_entry_t hen
);
98 static void usage(void);
100 static int VerboseOpt
;
102 RB_GENERATE2(undo_hist_entry_rb_tree
, undo_hist_entry
, rbnode
,
103 undo_hist_entry_compare
, hammer_tid_t
, tse
.tid
);
107 main(int ac
, char **av
)
109 const char *outFileName
= NULL
;
110 const char *outFilePostfix
= NULL
;
113 struct hammer_ioc_hist_entry ts1
;
114 struct hammer_ioc_hist_entry ts2
;
119 bzero(&ts1
, sizeof(ts1
));
120 bzero(&ts2
, sizeof(ts2
));
127 while ((c
= getopt(ac
, av
, "adDiuvo:t:")) != -1) {
136 if (type
!= TYPE_FILE
)
139 cmd
= CMD_ITERATEALL
;
142 cmd
= CMD_ITERATEALL
;
145 outFilePostfix
= ".undo";
151 outFileName
= optarg
;
155 * Parse one or two -t options. If two are specified
156 * -d is implied (but may be overridden)
160 ts1
.tid
= parse_delta_time(optarg
, &flags
,
162 } else if (count_t
== 2) {
163 ts2
.tid
= parse_delta_time(optarg
, &flags
,
165 if (type
== TYPE_FILE
)
181 if (outFileName
&& outFilePostfix
) {
182 fprintf(stderr
, "The -o option may not be combined with -u\n");
189 flags
|= UNDO_FLAG_MULT
;
195 * Validate the output template, if specified.
197 if (outFileName
&& (flags
& UNDO_FLAG_MULT
)) {
198 const char *ptr
= outFileName
;
201 while ((ptr
= strchr(ptr
, '%')) != NULL
) {
204 fprintf(stderr
, "Malformed output "
210 } else if (ptr
[1] != '%') {
211 fprintf(stderr
, "Malformed output template\n");
220 doiterate(*av
, outFileName
, outFilePostfix
,
221 flags
, ts1
, ts2
, cmd
, type
);
229 * Iterate through a file's history. If cmd == CMD_DUMP we take the
230 * next-to-last transaction id, unless another given. Otherwise if
231 * cmd == CMD_ITERATEALL we scan all transaction ids.
233 * Also iterate through the directory's history to locate other inodes that
234 * used the particular file name.
238 doiterate(const char *filename
, const char *outFileName
,
239 const char *outFilePostfix
, int flags
,
240 struct hammer_ioc_hist_entry ts1
,
241 struct hammer_ioc_hist_entry ts2
,
242 enum undo_cmd cmd
, enum undo_type type
)
244 struct undo_hist_entry_rb_tree dir_tree
;
245 struct undo_hist_entry_rb_tree tse_tree
;
246 struct undo_hist_entry
*tse1
;
247 struct undo_hist_entry
*tse2
;
248 struct hammer_ioc_hist_entry tid_max
;
258 tid_max
.tid
= HAMMER_MAX_TID
;
262 * Use the directory history to locate all possible versions of
265 collect_dir_history(filename
, &error
, &dir_tree
);
266 RB_FOREACH(tse1
, undo_hist_entry_rb_tree
, &dir_tree
) {
267 asprintf(&path
, "%s@@0x%016jx", filename
, (uintmax_t)tse1
->tse
.tid
);
268 if (stat(path
, &sb
) == 0 && sb
.st_mode
& S_IFIFO
) {
269 fprintf(stderr
, "Warning: fake transaction id 0x%016jx\n", (uintmax_t)tse1
->tse
.tid
);
272 if ((fd
= open(path
, O_RDONLY
)) > 0) {
273 collect_history(fd
, &error
, &tse_tree
);
278 if ((fd
= open(filename
, O_RDONLY
)) > 0) {
279 collect_history(fd
, &error
, &tse_tree
);
282 if (cmd
== CMD_DUMP
) {
284 flags
& (UNDO_FLAG_SETTID1
|UNDO_FLAG_SETTID2
)) &&
285 RB_EMPTY(&tse_tree
)) {
286 if ((fd
= open(filename
, O_RDONLY
)) > 0) {
287 collect_history(fd
, &error
, &tse_tree
);
292 * Find entry if tid set to placeholder index
294 if (flags
& UNDO_FLAG_SETTID1
){
295 tse1
= RB_MAX(undo_hist_entry_rb_tree
, &tse_tree
);
296 while (tse1
&& ts1
.tid
--) {
297 tse1
= RB_PREV(undo_hist_entry_rb_tree
,
305 if (flags
& UNDO_FLAG_SETTID2
){
306 tse2
= RB_MAX(undo_hist_entry_rb_tree
, &tse_tree
);
307 while (tse2
&& ts2
.tid
--) {
308 tse2
= RB_PREV(undo_hist_entry_rb_tree
,
318 * Single entry, most recent prior to current
321 tse2
= RB_MAX(undo_hist_entry_rb_tree
, &tse_tree
);
324 tse1
= RB_PREV(undo_hist_entry_rb_tree
,
331 printf("%s: No UNDO history found\n", filename
);
334 outFileName
, outFilePostfix
,
338 } else if (RB_ROOT(&tse_tree
)) {
340 * Iterate entire history
342 printf("%s: ITERATE ENTIRE HISTORY\n", filename
);
346 RB_FOREACH(tse2
, undo_hist_entry_rb_tree
, &tse_tree
) {
349 outFileName
, outFilePostfix
,
351 tse1
->tse
, tse2
->tse
);
353 if (tse1
&& tse2
->inum
!= tse1
->inum
)
354 flags
|= UNDO_FLAG_INOCHG
;
356 flags
&= ~UNDO_FLAG_INOCHG
;
361 * There is no delta to print for the last pair,
362 * because they are identical.
364 if (type
!= TYPE_DIFF
&& type
!= TYPE_RDIFF
) {
366 outFileName
, outFilePostfix
,
371 printf("%s: ITERATE ENTIRE HISTORY: %s\n",
372 filename
, strerror(error
));
374 clean_tree(&dir_tree
);
375 clean_tree(&tse_tree
);
379 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
384 dogenerate(const char *filename
, const char *outFileName
,
385 const char *outFilePostfix
,
386 int flags
, int idx
, enum undo_type type
,
387 struct hammer_ioc_hist_entry ts1
,
388 struct hammer_ioc_hist_entry ts2
)
406 * Open the input file. If ts1 is 0 try to locate the most recent
407 * version of the file prior to the current version.
410 asprintf(&ipath1
, "%s", filename
);
412 asprintf(&ipath1
, "%s@@0x%016jx", filename
, (uintmax_t)ts1
.tid
);
415 asprintf(&ipath2
, "%s", filename
);
417 asprintf(&ipath2
, "%s@@0x%016jx", filename
, (uintmax_t)ts2
.tid
);
419 if (lstat(ipath1
, &st
) < 0 && lstat(ipath2
, &st
) < 0) {
420 if (idx
== 0 || VerboseOpt
) {
421 fprintf(stderr
, "Unable to access either %s or %s\n",
430 * elm is the last component of the input file name
432 if ((elm
= strrchr(filename
, '/')) != NULL
)
438 * Where do we stuff our output?
441 if (flags
& UNDO_FLAG_MULT
) {
442 asprintf(&path
, outFileName
, elm
);
443 fp
= fopen(path
, "w");
450 fp
= fopen(outFileName
, "w");
456 } else if (outFilePostfix
) {
458 asprintf(&path
, "%s%s.%04d", filename
,
459 outFilePostfix
, idx
);
461 asprintf(&path
, "%s%s", filename
, outFilePostfix
);
463 fp
= fopen(path
, "w");
470 if ((flags
& UNDO_FLAG_MULT
) && type
== TYPE_FILE
) {
472 printf("\n>>> %s %04d 0x%016jx %s\n\n",
473 filename
, idx
, (uintmax_t)ts1
.tid
,
476 printf("\n>>> %s ---- 0x%016jx %s\n\n",
477 filename
, (uintmax_t)ts1
.tid
,
480 } else if (idx
>= 0 && type
== TYPE_FILE
) {
481 printf("\n>>> %s %04d 0x%016jx %s\n\n",
482 filename
, idx
, (uintmax_t)ts1
.tid
,
490 if ((fi
= fopen(ipath1
, "r")) != NULL
) {
491 while ((n
= fread(buf
, 1, 8192, fi
)) > 0)
492 fwrite(buf
, 1, n
, fp
);
497 printf("diff -N -r -u %s %s (to %s)\n",
498 ipath1
, ipath2
, timestamp(&ts2
));
500 runcmd(fileno(fp
), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath1
, ipath2
, NULL
);
503 printf("diff -N -r -u %s %s\n", ipath2
, ipath1
);
505 runcmd(fileno(fp
), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath2
, ipath1
, NULL
);
508 t
= (time_t)ts1
.time32
;
510 strftime(datestr
, sizeof(datestr
), "%d-%b-%Y %H:%M:%S", tp
);
511 printf("\t0x%016jx %s", (uintmax_t)ts1
.tid
, datestr
);
512 if (flags
& UNDO_FLAG_INOCHG
)
513 printf(" inode-change");
514 if (lstat(ipath1
, &st
) < 0)
515 printf(" file-deleted");
528 clean_tree(struct undo_hist_entry_rb_tree
*tree
)
530 struct undo_hist_entry
*tse
;
532 while ((tse
= RB_ROOT(tree
)) != NULL
) {
533 RB_REMOVE(undo_hist_entry_rb_tree
, tree
, tse
);
540 collect_history(int fd
, int *errorp
, struct undo_hist_entry_rb_tree
*tse_tree
)
542 struct hammer_ioc_history hist
;
543 struct undo_hist_entry
*tse
;
551 bzero(&hist
, sizeof(hist
));
552 hist
.beg_tid
= HAMMER_MIN_TID
;
553 hist
.end_tid
= HAMMER_MAX_TID
;
554 hist
.head
.flags
|= HAMMER_IOC_HISTORY_ATKEY
;
556 hist
.nxt_key
= HAMMER_MAX_KEY
;
560 if (tse_tree
== NULL
) {
561 tse_tree
= malloc(sizeof(*tse_tree
));
569 * Save the inode so inode changes can be reported.
575 * Collect a unique set of transaction ids
577 if (ioctl(fd
, HAMMERIOC_GETHISTORY
, &hist
) < 0) {
582 for (i
= 0; i
< hist
.count
; ++i
) {
583 tse
= malloc(sizeof(*tse
));
584 tse
->tse
= hist
.hist_ary
[i
];
585 tse
->inum
= st
.st_ino
;
586 if (RB_INSERT(undo_hist_entry_rb_tree
, tse_tree
, tse
)) {
590 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_EOF
)
592 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_NEXT_KEY
) {
593 hist
.key
= hist
.nxt_key
;
594 hist
.nxt_key
= HAMMER_MAX_KEY
;
596 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_NEXT_TID
)
597 hist
.beg_tid
= hist
.nxt_tid
;
598 if (ioctl(fd
, HAMMERIOC_GETHISTORY
, &hist
) < 0) {
609 clean_tree(tse_tree
);
616 collect_dir_history(const char *filename
, int *errorp
,
617 struct undo_hist_entry_rb_tree
*dir_tree
)
624 if (strrchr(filename
, '/')) {
625 dirname
= strdup(filename
);
626 *strrchr(dirname
, '/') = 0;
628 dirname
= strdup(".");
630 if ((fd
= open(dirname
, O_RDONLY
)) > 0) {
631 collect_history(fd
, &error
, dir_tree
);
638 parse_delta_time(const char *timeStr
, int *flags
, int ind_flag
)
642 tid
= strtoull(timeStr
, NULL
, 0);
643 if (timeStr
[0] == '+')
645 if (timeStr
[0] >= '0' && timeStr
[0] <= '9' && timeStr
[1] != 'x')
651 runcmd(int fd
, const char *cmd
, ...)
660 for (ac
= 0; va_arg(va
, void *) != NULL
; ++ac
)
664 av
= malloc((ac
+ 1) * sizeof(char *));
666 for (i
= 0; i
< ac
; ++i
)
667 av
[i
] = va_arg(va
, char *);
671 if ((pid
= fork()) < 0) {
674 } else if (pid
== 0) {
682 while (waitpid(pid
, NULL
, 0) != pid
)
689 * Convert tid to timestamp.
692 timestamp(hammer_ioc_hist_entry_t hen
)
694 static char timebuf
[64];
695 time_t t
= (time_t)hen
->time32
;
699 strftime(timebuf
, sizeof(timebuf
), "%d-%b-%Y %H:%M:%S", tp
);
705 undo_hist_entry_compare(struct undo_hist_entry
*he1
,
706 struct undo_hist_entry
*he2
)
708 if (he1
->tse
.tid
< he2
->tse
.tid
)
710 if (he1
->tse
.tid
> he2
->tse
.tid
)
718 fprintf(stderr
, "undo [-adDiuv] [-o outfile] "
719 "[-t transaction-id] [-t transaction-id] path...\n"
720 " -a Iterate all historical segments\n"
723 " -i Dump history transaction ids\n"
724 " -u Generate .undo files\n"
726 " -o file Output to the specified file\n"
727 " -t TID Retrieve as of transaction-id, TID\n"
728 " (a second `-t TID' to diff two)\n"
729 " transaction ids must be prefixed with 0x, and\n"
730 " otherwise may specify an index starting at 0\n"
731 " and iterating backwards through the history.\n"