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/ioctl.h>
53 #include <vfs/hammer/hammer_disk.h>
54 #include <vfs/hammer/hammer_ioctl.h>
57 * Sorted list of transaction ids
59 struct undo_hist_entry
;
60 RB_HEAD(undo_hist_entry_rb_tree
, undo_hist_entry
);
61 RB_PROTOTYPE2(undo_hist_entry_rb_tree
, undo_hist_entry
, rbnode
,
62 undo_hist_entry_compare
, hammer_tid_t
);
64 struct undo_hist_entry
{
65 RB_ENTRY(undo_hist_entry
) rbnode
;
66 struct hammer_ioc_hist_entry tse
;
70 enum undo_type
{ TYPE_FILE
, TYPE_DIFF
, TYPE_RDIFF
, TYPE_HISTORY
};
71 enum undo_cmd
{ CMD_DUMP
, CMD_ITERATEALL
};
73 #define UNDO_FLAG_MULT 0x0001
74 #define UNDO_FLAG_INOCHG 0x0002
75 #define UNDO_FLAG_TID_INDEX1 0x0004
76 #define UNDO_FLAG_TID_INDEX2 0x0008
78 static int undo_hist_entry_compare(struct undo_hist_entry
*he1
,
79 struct undo_hist_entry
*he2
);
80 static void doiterate(const char *filename
, 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 int doiterate_dump(const char *filename
, int flags
,
85 struct undo_hist_entry_rb_tree
*ptse_tree
,
86 struct hammer_ioc_hist_entry ts1
,
87 struct hammer_ioc_hist_entry ts2
,
89 static int doiterate_iterall(const char *filename
, int flags
,
90 struct undo_hist_entry_rb_tree
*ptse_tree
,
92 static void dogenerate(const char *filename
, int flags
,
93 struct hammer_ioc_hist_entry ts1
,
94 struct hammer_ioc_hist_entry ts2
,
95 int idx
, enum undo_type type
);
96 static void __collect_history(int fd
, int *error
,
97 struct undo_hist_entry_rb_tree
*tse_tree
);
98 static void collect_history(const char *filename
, int *errorp
,
99 struct undo_hist_entry_rb_tree
*dir_tree
);
100 static void collect_dir_history(const char *filename
, int *error
,
101 struct undo_hist_entry_rb_tree
*dir_tree
);
102 static void clean_tree(struct undo_hist_entry_rb_tree
*tree
);
103 static hammer_tid_t
parse_delta_time(const char *timeStr
, int *flags
,
105 static FILE *_fopen(const char *filename
, const char *mode
);
106 static void runcmd(int fd
, const char *cmd
, ...);
107 static char *timestamp(struct hammer_ioc_hist_entry
*hen
);
108 static void usage(void);
110 static int VerboseOpt
;
111 static const char *OutFileName
= NULL
;
112 static const char *OutFilePostfix
= NULL
;
114 RB_GENERATE2(undo_hist_entry_rb_tree
, undo_hist_entry
, rbnode
,
115 undo_hist_entry_compare
, hammer_tid_t
, tse
.tid
);
119 main(int ac
, char **av
)
123 struct hammer_ioc_hist_entry ts1
;
124 struct hammer_ioc_hist_entry ts2
;
129 bzero(&ts1
, sizeof(ts1
));
130 bzero(&ts2
, sizeof(ts2
));
137 while ((c
= getopt(ac
, av
, "adDiuvo:t:")) != -1) {
146 if (type
!= TYPE_FILE
)
149 cmd
= CMD_ITERATEALL
;
152 cmd
= CMD_ITERATEALL
;
155 OutFilePostfix
= ".undo";
161 OutFileName
= optarg
;
165 * Parse one or two -t options. If two are specified
166 * -d is implied (but may be overridden)
170 ts1
.tid
= parse_delta_time(optarg
, &flags
,
171 UNDO_FLAG_TID_INDEX1
);
172 } else if (count_t
== 2) {
173 ts2
.tid
= parse_delta_time(optarg
, &flags
,
174 UNDO_FLAG_TID_INDEX2
);
175 if (type
== TYPE_FILE
)
191 if (OutFileName
&& OutFilePostfix
) {
192 fprintf(stderr
, "The -o option may not be combined with -u\n");
199 flags
|= UNDO_FLAG_MULT
;
205 * Validate the output template, if specified.
207 if (OutFileName
&& (flags
& UNDO_FLAG_MULT
)) {
208 const char *ptr
= OutFileName
;
211 while ((ptr
= strchr(ptr
, '%')) != NULL
) {
214 fprintf(stderr
, "Malformed output "
220 } else if (ptr
[1] != '%') {
221 fprintf(stderr
, "Malformed output template\n");
230 doiterate(*av
, flags
, ts1
, ts2
, cmd
, type
);
238 * Iterate through a file's history. If cmd == CMD_DUMP we take the
239 * next-to-last transaction id, unless another given. Otherwise if
240 * cmd == CMD_ITERATEALL we scan all transaction ids.
242 * Also iterate through the directory's history to locate other inodes that
243 * used the particular file name.
247 doiterate(const char *filename
, int flags
,
248 struct hammer_ioc_hist_entry ts1
,
249 struct hammer_ioc_hist_entry ts2
,
250 enum undo_cmd cmd
, enum undo_type type
)
252 struct undo_hist_entry_rb_tree dir_tree
;
253 struct undo_hist_entry_rb_tree tse_tree
;
254 struct undo_hist_entry
*tse
;
263 * Use the directory history to locate all possible versions of
266 collect_dir_history(filename
, &error
, &dir_tree
);
267 RB_FOREACH(tse
, undo_hist_entry_rb_tree
, &dir_tree
) {
268 asprintf(&path
, "%s@@0x%016jx", filename
, (uintmax_t)tse
->tse
.tid
);
269 if (stat(path
, &sb
) == 0 && (sb
.st_mode
& S_IFIFO
)) {
270 fprintf(stderr
, "Warning: fake transaction id %s@@0x%016jx\n",
272 (uintmax_t)tse
->tse
.tid
);
276 collect_history(path
, &error
, &tse_tree
);
279 collect_history(filename
, &error
, &tse_tree
);
283 if (doiterate_dump(filename
, flags
, &tse_tree
, ts1
, ts2
, type
) == -1)
284 printf("%s: No UNDO history found\n", filename
);
287 if (doiterate_iterall(filename
, flags
, &tse_tree
, type
) == -1)
288 printf("%s: No UNDO history found\n", filename
);
291 fprintf(stderr
, "Invalid command %d\n", cmd
);
295 clean_tree(&dir_tree
);
296 clean_tree(&tse_tree
);
301 doiterate_dump(const char *filename
, int flags
,
302 struct undo_hist_entry_rb_tree
*ptse_tree
,
303 struct hammer_ioc_hist_entry ts1
,
304 struct hammer_ioc_hist_entry ts2
,
307 struct undo_hist_entry
*tse1
;
308 struct undo_hist_entry
*tse2
;
311 * Find entry if tid set to placeholder index
313 if (flags
& UNDO_FLAG_TID_INDEX1
) {
314 tse1
= RB_MAX(undo_hist_entry_rb_tree
, ptse_tree
);
315 while (tse1
&& ts1
.tid
--) {
316 tse1
= RB_PREV(undo_hist_entry_rb_tree
,
324 if (flags
& UNDO_FLAG_TID_INDEX2
) {
325 tse2
= RB_MAX(undo_hist_entry_rb_tree
, ptse_tree
);
326 while (tse2
&& ts2
.tid
--) {
327 tse2
= RB_PREV(undo_hist_entry_rb_tree
,
337 * Single entry, most recent prior to current
340 tse2
= RB_MAX(undo_hist_entry_rb_tree
, ptse_tree
);
343 tse1
= RB_PREV(undo_hist_entry_rb_tree
,
351 dogenerate(filename
, 0, ts1
, ts2
, 0, type
);
359 doiterate_iterall(const char *filename
, int flags
,
360 struct undo_hist_entry_rb_tree
*ptse_tree
,
363 struct undo_hist_entry
*tse1
;
364 struct undo_hist_entry
*tse2
;
365 struct hammer_ioc_hist_entry tid_max
;
368 if (RB_ROOT(ptse_tree
) == NULL
)
372 * Iterate entire history
374 printf("%s: ITERATE ENTIRE HISTORY\n", filename
);
378 RB_FOREACH(tse2
, undo_hist_entry_rb_tree
, ptse_tree
) {
380 dogenerate(filename
, flags
, tse1
->tse
, tse2
->tse
, i
, type
);
382 if (tse1
&& tse2
->inum
!= tse1
->inum
)
383 flags
|= UNDO_FLAG_INOCHG
;
385 flags
&= ~UNDO_FLAG_INOCHG
;
391 * There is no delta to print for the last pair,
392 * because they are identical.
394 if (type
!= TYPE_DIFF
&& type
!= TYPE_RDIFF
) {
395 tid_max
.tid
= HAMMER_MAX_TID
;
397 dogenerate(filename
, flags
, tse1
->tse
, tid_max
, i
, type
);
403 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
408 dogenerate(const char *filename
, int flags
,
409 struct hammer_ioc_hist_entry ts1
,
410 struct hammer_ioc_hist_entry ts2
,
411 int idx
, enum undo_type type
)
427 * Open the input file. If ts1 is 0 try to locate the most recent
428 * version of the file prior to the current version.
431 asprintf(&ipath1
, "%s", filename
);
433 asprintf(&ipath1
, "%s@@0x%016jx", filename
, (uintmax_t)ts1
.tid
);
436 asprintf(&ipath2
, "%s", filename
);
438 asprintf(&ipath2
, "%s@@0x%016jx", filename
, (uintmax_t)ts2
.tid
);
440 if (lstat(ipath1
, &st
) < 0 && lstat(ipath2
, &st
) < 0) {
441 if (idx
== 0 || VerboseOpt
) {
442 fprintf(stderr
, "Unable to access either %s or %s\n",
451 * elm is the last component of the input file name
453 if ((elm
= strrchr(filename
, '/')) != NULL
)
459 * Where do we stuff our output?
462 if (flags
& UNDO_FLAG_MULT
) {
463 asprintf(&path
, OutFileName
, elm
);
464 fp
= _fopen(path
, "w");
467 fp
= _fopen(OutFileName
, "w");
469 } else if (OutFilePostfix
) {
471 asprintf(&path
, "%s%s.%04d", filename
,
472 OutFilePostfix
, idx
);
474 asprintf(&path
, "%s%s", filename
, OutFilePostfix
);
476 fp
= _fopen(path
, "w");
479 if ((flags
& UNDO_FLAG_MULT
) && type
== TYPE_FILE
) {
481 printf("\n>>> %s %04d 0x%016jx %s\n\n",
482 filename
, idx
, (uintmax_t)ts1
.tid
,
485 printf("\n>>> %s ---- 0x%016jx %s\n\n",
486 filename
, (uintmax_t)ts1
.tid
,
489 } else if (idx
>= 0 && type
== TYPE_FILE
) {
490 printf("\n>>> %s %04d 0x%016jx %s\n\n",
491 filename
, idx
, (uintmax_t)ts1
.tid
,
502 if ((fi
= fopen(ipath1
, "r")) != NULL
) {
503 while ((n
= fread(buf
, 1, 8192, fi
)) > 0)
504 fwrite(buf
, 1, n
, fp
);
510 printf("diff -N -r -u %s %s (to %s)\n",
511 ipath1
, ipath2
, timestamp(&ts2
));
513 runcmd(fileno(fp
), "/usr/bin/diff", "diff", "-N", "-r", "-u",
514 ipath1
, ipath2
, NULL
);
517 printf("diff -N -r -u %s %s\n", ipath2
, ipath1
);
519 runcmd(fileno(fp
), "/usr/bin/diff", "diff", "-N", "-r", "-u",
520 ipath2
, ipath1
, NULL
);
523 t
= (time_t)ts1
.time32
;
525 strftime(datestr
, sizeof(datestr
), "%d-%b-%Y %H:%M:%S", tp
);
526 printf("\t0x%016jx %s", (uintmax_t)ts1
.tid
, datestr
);
527 if (flags
& UNDO_FLAG_INOCHG
)
528 printf(" inode-change");
529 if (lstat(ipath1
, &st
) < 0)
530 printf(" file-deleted");
541 clean_tree(struct undo_hist_entry_rb_tree
*tree
)
543 struct undo_hist_entry
*tse
;
545 while ((tse
= RB_ROOT(tree
)) != NULL
) {
546 RB_REMOVE(undo_hist_entry_rb_tree
, tree
, tse
);
553 __collect_history(int fd
, int *errorp
, struct undo_hist_entry_rb_tree
*tse_tree
)
555 struct hammer_ioc_history hist
;
556 struct undo_hist_entry
*tse
;
563 bzero(&hist
, sizeof(hist
));
564 hist
.beg_tid
= HAMMER_MIN_TID
;
565 hist
.end_tid
= HAMMER_MAX_TID
;
566 hist
.head
.flags
|= HAMMER_IOC_HISTORY_ATKEY
;
568 hist
.nxt_key
= HAMMER_MAX_KEY
;
573 * Save the inode so inode changes can be reported.
579 * Collect a unique set of transaction ids
581 if (ioctl(fd
, HAMMERIOC_GETHISTORY
, &hist
) < 0) {
586 for (i
= 0; i
< hist
.count
; ++i
) {
587 tse
= malloc(sizeof(*tse
));
588 tse
->tse
= hist
.hist_ary
[i
];
589 tse
->inum
= st
.st_ino
;
590 if (RB_INSERT(undo_hist_entry_rb_tree
, tse_tree
, tse
)) {
594 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_EOF
)
596 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_NEXT_KEY
) {
597 hist
.key
= hist
.nxt_key
;
598 hist
.nxt_key
= HAMMER_MAX_KEY
;
600 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_NEXT_TID
)
601 hist
.beg_tid
= hist
.nxt_tid
;
602 if (ioctl(fd
, HAMMERIOC_GETHISTORY
, &hist
) < 0) {
611 collect_history(const char *filename
, int *errorp
,
612 struct undo_hist_entry_rb_tree
*dir_tree
)
616 fd
= open(filename
, O_RDONLY
);
621 __collect_history(fd
, errorp
, dir_tree
);
627 collect_dir_history(const char *filename
, int *errorp
,
628 struct undo_hist_entry_rb_tree
*dir_tree
)
632 if (strrchr(filename
, '/')) {
633 dirname
= strdup(filename
);
634 *strrchr(dirname
, '/') = 0;
636 dirname
= strdup(".");
639 collect_history(dirname
, errorp
, dir_tree
);
645 parse_delta_time(const char *timeStr
, int *flags
, int ind_flag
)
649 tid
= strtoull(timeStr
, NULL
, 0);
650 if (timeStr
[0] == '+')
652 if (timeStr
[0] >= '0' && timeStr
[0] <= '9' && timeStr
[1] != 'x')
659 _fopen(const char *filename
, const char *mode
)
663 fp
= fopen(filename
, mode
);
665 err(1, "%s", filename
);
670 runcmd(int fd
, const char *cmd
, ...)
679 for (ac
= 0; va_arg(va
, void *) != NULL
; ++ac
)
683 av
= malloc((ac
+ 1) * sizeof(char *));
685 for (i
= 0; i
< ac
; ++i
)
686 av
[i
] = va_arg(va
, char *);
690 if ((pid
= fork()) < 0) {
692 } else if (pid
== 0) {
700 while (waitpid(pid
, NULL
, 0) != pid
)
707 * Convert tid to timestamp.
710 timestamp(struct hammer_ioc_hist_entry
*hen
)
712 static char timebuf
[64];
713 time_t t
= (time_t)hen
->time32
;
717 strftime(timebuf
, sizeof(timebuf
), "%d-%b-%Y %H:%M:%S", tp
);
723 undo_hist_entry_compare(struct undo_hist_entry
*he1
,
724 struct undo_hist_entry
*he2
)
726 if (he1
->tse
.tid
< he2
->tse
.tid
)
728 if (he1
->tse
.tid
> he2
->tse
.tid
)
736 fprintf(stderr
, "undo [-adDiuv] [-o outfile] "
737 "[-t transaction-id] [-t transaction-id] path...\n"
738 " -a Iterate all historical segments\n"
741 " -i Dump history transaction ids\n"
742 " -u Generate .undo files\n"
744 " -o file Output to the specified file\n"
745 " -t TID Retrieve as of transaction-id, TID\n"
746 " (a second `-t TID' to diff two)\n"
747 " transaction ids must be prefixed with 0x, and\n"
748 " otherwise may specify an index starting at 0\n"
749 " and iterating backwards through the history.\n"