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>
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_TID_INDEX1 0x0004
75 #define UNDO_FLAG_TID_INDEX2 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
, 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 int doiterate_dump(const char *filename
, int flags
,
84 struct undo_hist_entry_rb_tree
*ptse_tree
,
85 struct hammer_ioc_hist_entry ts1
,
86 struct hammer_ioc_hist_entry ts2
,
88 static int doiterate_iterall(const char *filename
, int flags
,
89 struct undo_hist_entry_rb_tree
*ptse_tree
,
91 static void dogenerate(const char *filename
, int flags
,
92 struct hammer_ioc_hist_entry ts1
,
93 struct hammer_ioc_hist_entry ts2
,
94 int idx
, enum undo_type type
);
95 static void __collect_history(int fd
, int *error
,
96 struct undo_hist_entry_rb_tree
*tse_tree
);
97 static void collect_history(const char *filename
, int *errorp
,
98 struct undo_hist_entry_rb_tree
*dir_tree
);
99 static void collect_dir_history(const char *filename
, int *error
,
100 struct undo_hist_entry_rb_tree
*dir_tree
);
101 static void clean_tree(struct undo_hist_entry_rb_tree
*tree
);
102 static hammer_tid_t
parse_delta_time(const char *timeStr
, int *flags
,
104 static FILE *_fopen(const char *filename
, const char *mode
);
105 static void runcmd(int fd
, const char *cmd
, ...);
106 static char *timestamp(struct hammer_ioc_hist_entry
*hen
);
107 static void usage(void);
109 static int VerboseOpt
;
110 static const char *OutFileName
= NULL
;
111 static const char *OutFilePostfix
= NULL
;
113 RB_GENERATE2(undo_hist_entry_rb_tree
, undo_hist_entry
, rbnode
,
114 undo_hist_entry_compare
, hammer_tid_t
, tse
.tid
);
118 main(int ac
, char **av
)
122 struct hammer_ioc_hist_entry ts1
;
123 struct hammer_ioc_hist_entry ts2
;
128 bzero(&ts1
, sizeof(ts1
));
129 bzero(&ts2
, sizeof(ts2
));
136 while ((c
= getopt(ac
, av
, "adDiuvo:t:")) != -1) {
145 if (type
!= TYPE_FILE
)
148 cmd
= CMD_ITERATEALL
;
151 cmd
= CMD_ITERATEALL
;
154 OutFilePostfix
= ".undo";
160 OutFileName
= optarg
;
164 * Parse one or two -t options. If two are specified
165 * -d is implied (but may be overridden)
169 ts1
.tid
= parse_delta_time(optarg
, &flags
,
170 UNDO_FLAG_TID_INDEX1
);
171 } else if (count_t
== 2) {
172 ts2
.tid
= parse_delta_time(optarg
, &flags
,
173 UNDO_FLAG_TID_INDEX2
);
174 if (type
== TYPE_FILE
)
190 if (OutFileName
&& OutFilePostfix
) {
191 fprintf(stderr
, "The -o option may not be combined with -u\n");
198 flags
|= UNDO_FLAG_MULT
;
204 * Validate the output template, if specified.
206 if (OutFileName
&& (flags
& UNDO_FLAG_MULT
)) {
207 const char *ptr
= OutFileName
;
210 while ((ptr
= strchr(ptr
, '%')) != NULL
) {
213 fprintf(stderr
, "Malformed output "
219 } else if (ptr
[1] != '%') {
220 fprintf(stderr
, "Malformed output template\n");
229 doiterate(*av
, flags
, ts1
, ts2
, cmd
, type
);
237 * Iterate through a file's history. If cmd == CMD_DUMP we take the
238 * next-to-last transaction id, unless another given. Otherwise if
239 * cmd == CMD_ITERATEALL we scan all transaction ids.
241 * Also iterate through the directory's history to locate other inodes that
242 * used the particular file name.
246 doiterate(const char *filename
, int flags
,
247 struct hammer_ioc_hist_entry ts1
,
248 struct hammer_ioc_hist_entry ts2
,
249 enum undo_cmd cmd
, enum undo_type type
)
251 struct undo_hist_entry_rb_tree dir_tree
;
252 struct undo_hist_entry_rb_tree tse_tree
;
253 struct undo_hist_entry
*tse
;
262 * Use the directory history to locate all possible versions of
265 collect_dir_history(filename
, &error
, &dir_tree
);
266 RB_FOREACH(tse
, undo_hist_entry_rb_tree
, &dir_tree
) {
267 asprintf(&path
, "%s@@0x%016jx", filename
, (uintmax_t)tse
->tse
.tid
);
268 if (stat(path
, &sb
) == 0 && (sb
.st_mode
& S_IFIFO
)) {
269 fprintf(stderr
, "Warning: fake transaction id %s@@0x%016jx\n",
271 (uintmax_t)tse
->tse
.tid
);
275 collect_history(path
, &error
, &tse_tree
);
278 collect_history(filename
, &error
, &tse_tree
);
282 if (doiterate_dump(filename
, flags
, &tse_tree
, ts1
, ts2
, type
) == -1)
283 printf("%s: No UNDO history found\n", filename
);
286 if (doiterate_iterall(filename
, flags
, &tse_tree
, type
) == -1)
287 printf("%s: No UNDO history found\n", filename
);
290 fprintf(stderr
, "Invalid command %d\n", cmd
);
294 clean_tree(&dir_tree
);
295 clean_tree(&tse_tree
);
300 doiterate_dump(const char *filename
, int flags
,
301 struct undo_hist_entry_rb_tree
*ptse_tree
,
302 struct hammer_ioc_hist_entry ts1
,
303 struct hammer_ioc_hist_entry ts2
,
306 struct undo_hist_entry
*tse1
;
307 struct undo_hist_entry
*tse2
;
310 * Find entry if tid set to placeholder index
312 if (flags
& UNDO_FLAG_TID_INDEX1
) {
313 tse1
= RB_MAX(undo_hist_entry_rb_tree
, ptse_tree
);
314 while (tse1
&& ts1
.tid
--) {
315 tse1
= RB_PREV(undo_hist_entry_rb_tree
,
323 if (flags
& UNDO_FLAG_TID_INDEX2
) {
324 tse2
= RB_MAX(undo_hist_entry_rb_tree
, ptse_tree
);
325 while (tse2
&& ts2
.tid
--) {
326 tse2
= RB_PREV(undo_hist_entry_rb_tree
,
336 * Single entry, most recent prior to current
339 tse2
= RB_MAX(undo_hist_entry_rb_tree
, ptse_tree
);
342 tse1
= RB_PREV(undo_hist_entry_rb_tree
,
350 dogenerate(filename
, 0, ts1
, ts2
, 0, type
);
358 doiterate_iterall(const char *filename
, int flags
,
359 struct undo_hist_entry_rb_tree
*ptse_tree
,
362 struct undo_hist_entry
*tse1
;
363 struct undo_hist_entry
*tse2
;
364 struct hammer_ioc_hist_entry tid_max
;
367 if (RB_ROOT(ptse_tree
) == NULL
)
371 * Iterate entire history
373 printf("%s: ITERATE ENTIRE HISTORY\n", filename
);
377 RB_FOREACH(tse2
, undo_hist_entry_rb_tree
, ptse_tree
) {
379 dogenerate(filename
, flags
, tse1
->tse
, tse2
->tse
, i
, type
);
381 if (tse1
&& tse2
->inum
!= tse1
->inum
)
382 flags
|= UNDO_FLAG_INOCHG
;
384 flags
&= ~UNDO_FLAG_INOCHG
;
390 * There is no delta to print for the last pair,
391 * because they are identical.
393 if (type
!= TYPE_DIFF
&& type
!= TYPE_RDIFF
) {
394 tid_max
.tid
= HAMMER_MAX_TID
;
396 dogenerate(filename
, flags
, tse1
->tse
, tid_max
, i
, type
);
402 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then
407 dogenerate(const char *filename
, int flags
,
408 struct hammer_ioc_hist_entry ts1
,
409 struct hammer_ioc_hist_entry ts2
,
410 int idx
, enum undo_type type
)
426 * Open the input file. If ts1 is 0 try to locate the most recent
427 * version of the file prior to the current version.
430 asprintf(&ipath1
, "%s", filename
);
432 asprintf(&ipath1
, "%s@@0x%016jx", filename
, (uintmax_t)ts1
.tid
);
435 asprintf(&ipath2
, "%s", filename
);
437 asprintf(&ipath2
, "%s@@0x%016jx", filename
, (uintmax_t)ts2
.tid
);
439 if (lstat(ipath1
, &st
) < 0 && lstat(ipath2
, &st
) < 0) {
440 if (idx
== 0 || VerboseOpt
) {
441 fprintf(stderr
, "Unable to access either %s or %s\n",
450 * elm is the last component of the input file name
452 if ((elm
= strrchr(filename
, '/')) != NULL
)
458 * Where do we stuff our output?
461 if (flags
& UNDO_FLAG_MULT
) {
462 asprintf(&path
, OutFileName
, elm
);
463 fp
= _fopen(path
, "w");
466 fp
= _fopen(OutFileName
, "w");
468 } else if (OutFilePostfix
) {
470 asprintf(&path
, "%s%s.%04d", filename
,
471 OutFilePostfix
, idx
);
473 asprintf(&path
, "%s%s", filename
, OutFilePostfix
);
475 fp
= _fopen(path
, "w");
478 if ((flags
& UNDO_FLAG_MULT
) && type
== TYPE_FILE
) {
480 printf("\n>>> %s %04d 0x%016jx %s\n\n",
481 filename
, idx
, (uintmax_t)ts1
.tid
,
484 printf("\n>>> %s ---- 0x%016jx %s\n\n",
485 filename
, (uintmax_t)ts1
.tid
,
488 } else if (idx
>= 0 && type
== TYPE_FILE
) {
489 printf("\n>>> %s %04d 0x%016jx %s\n\n",
490 filename
, idx
, (uintmax_t)ts1
.tid
,
501 if ((fi
= fopen(ipath1
, "r")) != NULL
) {
502 while ((n
= fread(buf
, 1, 8192, fi
)) > 0)
503 fwrite(buf
, 1, n
, fp
);
509 printf("diff -N -r -u %s %s (to %s)\n",
510 ipath1
, ipath2
, timestamp(&ts2
));
512 runcmd(fileno(fp
), "/usr/bin/diff", "diff", "-N", "-r", "-u",
513 ipath1
, ipath2
, NULL
);
516 printf("diff -N -r -u %s %s\n", ipath2
, ipath1
);
518 runcmd(fileno(fp
), "/usr/bin/diff", "diff", "-N", "-r", "-u",
519 ipath2
, ipath1
, NULL
);
522 t
= (time_t)ts1
.time32
;
524 strftime(datestr
, sizeof(datestr
), "%d-%b-%Y %H:%M:%S", tp
);
525 printf("\t0x%016jx %s", (uintmax_t)ts1
.tid
, datestr
);
526 if (flags
& UNDO_FLAG_INOCHG
)
527 printf(" inode-change");
528 if (lstat(ipath1
, &st
) < 0)
529 printf(" file-deleted");
540 clean_tree(struct undo_hist_entry_rb_tree
*tree
)
542 struct undo_hist_entry
*tse
;
544 while ((tse
= RB_ROOT(tree
)) != NULL
) {
545 RB_REMOVE(undo_hist_entry_rb_tree
, tree
, tse
);
552 __collect_history(int fd
, int *errorp
, struct undo_hist_entry_rb_tree
*tse_tree
)
554 struct hammer_ioc_history hist
;
555 struct undo_hist_entry
*tse
;
562 bzero(&hist
, sizeof(hist
));
563 hist
.beg_tid
= HAMMER_MIN_TID
;
564 hist
.end_tid
= HAMMER_MAX_TID
;
565 hist
.head
.flags
|= HAMMER_IOC_HISTORY_ATKEY
;
567 hist
.nxt_key
= HAMMER_MAX_KEY
;
572 * Save the inode so inode changes can be reported.
578 * Collect a unique set of transaction ids
580 if (ioctl(fd
, HAMMERIOC_GETHISTORY
, &hist
) < 0) {
585 for (i
= 0; i
< hist
.count
; ++i
) {
586 tse
= malloc(sizeof(*tse
));
587 tse
->tse
= hist
.hist_ary
[i
];
588 tse
->inum
= st
.st_ino
;
589 if (RB_INSERT(undo_hist_entry_rb_tree
, tse_tree
, tse
)) {
593 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_EOF
)
595 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_NEXT_KEY
) {
596 hist
.key
= hist
.nxt_key
;
597 hist
.nxt_key
= HAMMER_MAX_KEY
;
599 if (hist
.head
.flags
& HAMMER_IOC_HISTORY_NEXT_TID
)
600 hist
.beg_tid
= hist
.nxt_tid
;
601 if (ioctl(fd
, HAMMERIOC_GETHISTORY
, &hist
) < 0) {
610 collect_history(const char *filename
, int *errorp
,
611 struct undo_hist_entry_rb_tree
*dir_tree
)
615 fd
= open(filename
, O_RDONLY
);
620 __collect_history(fd
, errorp
, dir_tree
);
626 collect_dir_history(const char *filename
, int *errorp
,
627 struct undo_hist_entry_rb_tree
*dir_tree
)
631 if (strrchr(filename
, '/')) {
632 dirname
= strdup(filename
);
633 *strrchr(dirname
, '/') = 0;
635 dirname
= strdup(".");
638 collect_history(dirname
, errorp
, dir_tree
);
644 parse_delta_time(const char *timeStr
, int *flags
, int ind_flag
)
648 tid
= strtoull(timeStr
, NULL
, 0);
649 if (timeStr
[0] == '+')
651 if (timeStr
[0] >= '0' && timeStr
[0] <= '9' && timeStr
[1] != 'x')
658 _fopen(const char *filename
, const char *mode
)
662 fp
= fopen(filename
, mode
);
664 err(1, "%s", filename
);
669 runcmd(int fd
, const char *cmd
, ...)
678 for (ac
= 0; va_arg(va
, void *) != NULL
; ++ac
)
682 av
= malloc((ac
+ 1) * sizeof(char *));
684 for (i
= 0; i
< ac
; ++i
)
685 av
[i
] = va_arg(va
, char *);
689 if ((pid
= fork()) < 0) {
691 } else if (pid
== 0) {
699 while (waitpid(pid
, NULL
, 0) != pid
)
706 * Convert tid to timestamp.
709 timestamp(struct hammer_ioc_hist_entry
*hen
)
711 static char timebuf
[64];
712 time_t t
= (time_t)hen
->time32
;
716 strftime(timebuf
, sizeof(timebuf
), "%d-%b-%Y %H:%M:%S", tp
);
722 undo_hist_entry_compare(struct undo_hist_entry
*he1
,
723 struct undo_hist_entry
*he2
)
725 if (he1
->tse
.tid
< he2
->tse
.tid
)
727 if (he1
->tse
.tid
> he2
->tse
.tid
)
735 fprintf(stderr
, "undo [-adDiuv] [-o outfile] "
736 "[-t transaction-id] [-t transaction-id] path...\n"
737 " -a Iterate all historical segments\n"
740 " -i Dump history transaction ids\n"
741 " -u Generate .undo files\n"
743 " -o file Output to the specified file\n"
744 " -t TID Retrieve as of transaction-id, TID\n"
745 " (a second `-t TID' to diff two)\n"
746 " transaction ids must be prefixed with 0x, and\n"
747 " otherwise may specify an index starting at 0\n"
748 " and iterating backwards through the history.\n"