1 /* 2 * Copyright (c) 2008 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Matthew Dillon <dillon@backplane.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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 16 * distribution. 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. 20 * 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 32 * SUCH DAMAGE. 33 * 34 * $DragonFly: src/usr.bin/undo/undo.c,v 1.6 2008/07/17 21:34:47 thomas Exp $ 35 */ 36 /* 37 * UNDO - retrieve an older version of a file. 38 */ 39 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 #include <sys/wait.h> 43 #include <sys/tree.h> 44 #include <sys/param.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <stdarg.h> 48 #include <string.h> 49 #include <unistd.h> 50 #include <fcntl.h> 51 #include <errno.h> 52 #include <vfs/hammer/hammer_disk.h> 53 #include <vfs/hammer/hammer_ioctl.h> 54 55 /* 56 * Sorted list of transaction ids 57 */ 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); 62 63 struct undo_hist_entry { 64 RB_ENTRY(undo_hist_entry) rbnode; 65 struct hammer_ioc_hist_entry tse; 66 ino_t inum; 67 }; 68 69 enum undo_type { TYPE_FILE, TYPE_DIFF, TYPE_RDIFF, TYPE_HISTORY }; 70 enum undo_cmd { CMD_DUMP, CMD_ITERATEALL }; 71 72 #define UNDO_FLAG_MULT 0x0001 73 #define UNDO_FLAG_INOCHG 0x0002 74 #define UNDO_FLAG_SETTID1 0x0004 75 #define UNDO_FLAG_SETTID2 0x0008 76 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, 95 int ind_flag); 96 static void runcmd(int fd, const char *cmd, ...); 97 static char *timestamp(hammer_ioc_hist_entry_t hen); 98 static void usage(void); 99 100 static int VerboseOpt; 101 102 RB_GENERATE2(undo_hist_entry_rb_tree, undo_hist_entry, rbnode, 103 undo_hist_entry_compare, hammer_tid_t, tse.tid); 104 105 106 int 107 main(int ac, char **av) 108 { 109 const char *outFileName = NULL; 110 const char *outFilePostfix = NULL; 111 enum undo_cmd cmd; 112 enum undo_type type; 113 struct hammer_ioc_hist_entry ts1; 114 struct hammer_ioc_hist_entry ts2; 115 int c; 116 int count_t; 117 int flags; 118 119 bzero(&ts1, sizeof(ts1)); 120 bzero(&ts2, sizeof(ts2)); 121 122 cmd = CMD_DUMP; 123 type = TYPE_FILE; 124 count_t = 0; 125 flags = 0; 126 127 while ((c = getopt(ac, av, "adDiuvo:t:")) != -1) { 128 switch(c) { 129 case 'd': 130 type = TYPE_DIFF; 131 break; 132 case 'D': 133 type = TYPE_RDIFF; 134 break; 135 case 'i': 136 if (type != TYPE_FILE) 137 usage(); 138 type = TYPE_HISTORY; 139 cmd = CMD_ITERATEALL; 140 break; 141 case 'a': 142 cmd = CMD_ITERATEALL; 143 break; 144 case 'u': 145 outFilePostfix = ".undo"; 146 break; 147 case 'v': 148 ++VerboseOpt; 149 break; 150 case 'o': 151 outFileName = optarg; 152 break; 153 case 't': 154 /* 155 * Parse one or two -t options. If two are specified 156 * -d is implied (but may be overridden) 157 */ 158 ++count_t; 159 if (count_t == 1) { 160 ts1.tid = parse_delta_time(optarg, &flags, 161 UNDO_FLAG_SETTID1); 162 } else if (count_t == 2) { 163 ts2.tid = parse_delta_time(optarg, &flags, 164 UNDO_FLAG_SETTID2); 165 if (type == TYPE_FILE) 166 type = TYPE_DIFF; 167 } else { 168 usage(); 169 } 170 break; 171 default: 172 usage(); 173 /* NOT REACHED */ 174 break; 175 } 176 } 177 178 /* 179 * Option validation 180 */ 181 if (outFileName && outFilePostfix) { 182 fprintf(stderr, "The -o option may not be combined with -u\n"); 183 usage(); 184 } 185 186 ac -= optind; 187 av += optind; 188 if (ac > 1) 189 flags |= UNDO_FLAG_MULT; 190 191 if (ac == 0) 192 usage(); 193 194 /* 195 * Validate the output template, if specified. 196 */ 197 if (outFileName && (flags & UNDO_FLAG_MULT)) { 198 const char *ptr = outFileName; 199 int didStr = 0; 200 201 while ((ptr = strchr(ptr, '%')) != NULL) { 202 if (ptr[1] == 's') { 203 if (didStr) { 204 fprintf(stderr, "Malformed output " 205 "template\n"); 206 usage(); 207 } 208 didStr = 1; 209 ++ptr; 210 } else if (ptr[1] != '%') { 211 fprintf(stderr, "Malformed output template\n"); 212 usage(); 213 } else { 214 ptr += 2; 215 } 216 } 217 } 218 219 while (ac) { 220 doiterate(*av, outFileName, outFilePostfix, 221 flags, ts1, ts2, cmd, type); 222 ++av; 223 --ac; 224 } 225 return(0); 226 } 227 228 /* 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. 232 * 233 * Also iterate through the directory's history to locate other inodes that 234 * used the particular file name. 235 */ 236 static 237 void 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) 243 { 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; 249 char *path = NULL; 250 int i; 251 int fd; 252 int error; 253 254 RB_INIT(&dir_tree); 255 RB_INIT(&tse_tree); 256 257 tid_max.tid = HAMMER_MAX_TID; 258 tid_max.time32 = 0; 259 260 /* 261 * Use the directory history to locate all possible versions of 262 * the file. 263 */ 264 collect_dir_history(filename, &error, &dir_tree); 265 RB_FOREACH(tse1, undo_hist_entry_rb_tree, &dir_tree) { 266 asprintf(&path, "%s@@0x%016jx", filename, (uintmax_t)tse1->tse.tid); 267 if ((fd = open(path, O_RDONLY)) > 0) { 268 collect_history(fd, &error, &tse_tree); 269 close(fd); 270 } 271 free(path); 272 } 273 if ((fd = open(filename, O_RDONLY)) > 0) { 274 collect_history(fd, &error, &tse_tree); 275 close(fd); 276 } 277 if (cmd == CMD_DUMP) { 278 if ((ts1.tid == 0 || 279 flags & (UNDO_FLAG_SETTID1|UNDO_FLAG_SETTID2)) && 280 RB_EMPTY(&tse_tree)) { 281 if ((fd = open(filename, O_RDONLY)) > 0) { 282 collect_history(fd, &error, &tse_tree); 283 close(fd); 284 } 285 } 286 /* 287 * Find entry if tid set to placeholder index 288 */ 289 if (flags & UNDO_FLAG_SETTID1){ 290 tse1 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree); 291 while (tse1 && ts1.tid--) { 292 tse1 = RB_PREV(undo_hist_entry_rb_tree, 293 &tse_tree, tse1); 294 } 295 if (tse1) 296 ts1 = tse1->tse; 297 else 298 ts1.tid = 0; 299 } 300 if (flags & UNDO_FLAG_SETTID2){ 301 tse2 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree); 302 while (tse2 && ts2.tid--) { 303 tse2 = RB_PREV(undo_hist_entry_rb_tree, 304 &tse_tree, tse2); 305 } 306 if (tse2) 307 ts2 = tse2->tse; 308 else 309 ts2.tid = 0; 310 } 311 312 /* 313 * Single entry, most recent prior to current 314 */ 315 if (ts1.tid == 0) { 316 tse2 = RB_MAX(undo_hist_entry_rb_tree, &tse_tree); 317 if (tse2) { 318 ts2 = tse2->tse; 319 tse1 = RB_PREV(undo_hist_entry_rb_tree, 320 &tse_tree, tse2); 321 if (tse1) 322 ts1 = tse1->tse; 323 } 324 } 325 if (ts1.tid == 0) { 326 printf("%s: No UNDO history found\n", filename); 327 } else { 328 dogenerate(filename, 329 outFileName, outFilePostfix, 330 0, 0, type, 331 ts1, ts2); 332 } 333 } else if (RB_ROOT(&tse_tree)) { 334 /* 335 * Iterate entire history 336 */ 337 printf("%s: ITERATE ENTIRE HISTORY\n", filename); 338 339 tse1 = NULL; 340 i = 0; 341 RB_FOREACH(tse2, undo_hist_entry_rb_tree, &tse_tree) { 342 if (tse1) { 343 dogenerate(filename, 344 outFileName, outFilePostfix, 345 flags, i, type, 346 tse1->tse, tse2->tse); 347 } 348 if (tse1 && tse2->inum != tse1->inum) 349 flags |= UNDO_FLAG_INOCHG; 350 else 351 flags &= ~UNDO_FLAG_INOCHG; 352 tse1 = tse2; 353 ++i; 354 } 355 /* 356 * There is no delta to print for the last pair, 357 * because they are identical. 358 */ 359 if (type != TYPE_DIFF && type != TYPE_RDIFF) { 360 dogenerate(filename, 361 outFileName, outFilePostfix, 362 flags, i, type, 363 tse1->tse, tid_max); 364 } 365 } else { 366 printf("%s: ITERATE ENTIRE HISTORY: %s\n", 367 filename, strerror(error)); 368 } 369 clean_tree(&dir_tree); 370 clean_tree(&tse_tree); 371 } 372 373 /* 374 * Generate output for a file as-of ts1 (ts1 may be 0!), if diffing then 375 * through ts2. 376 */ 377 static 378 void 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) 384 { 385 struct stat st; 386 const char *elm; 387 char *ipath1 = NULL; 388 char *ipath2 = NULL; 389 FILE *fi; 390 FILE *fp; 391 char *buf; 392 char *path; 393 time_t t; 394 struct tm *tp; 395 char datestr[64]; 396 int n; 397 398 buf = malloc(8192); 399 400 /* 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. 403 */ 404 if (ts1.tid == 0) 405 asprintf(&ipath1, "%s", filename); 406 else 407 asprintf(&ipath1, "%s@@0x%016jx", filename, (uintmax_t)ts1.tid); 408 409 if (ts2.tid == 0) 410 asprintf(&ipath2, "%s", filename); 411 else 412 asprintf(&ipath2, "%s@@0x%016jx", filename, (uintmax_t)ts2.tid); 413 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", 417 ipath1, ipath2); 418 } 419 free(ipath1); 420 free(ipath2); 421 goto done; 422 } 423 424 /* 425 * elm is the last component of the input file name 426 */ 427 if ((elm = strrchr(filename, '/')) != NULL) 428 ++elm; 429 else 430 elm = filename; 431 432 /* 433 * Where do we stuff our output? 434 */ 435 if (outFileName) { 436 if (flags & UNDO_FLAG_MULT) { 437 asprintf(&path, outFileName, elm); 438 fp = fopen(path, "w"); 439 if (fp == NULL) { 440 perror(path); 441 exit(1); 442 } 443 free(path); 444 } else { 445 fp = fopen(outFileName, "w"); 446 if (fp == NULL) { 447 perror(outFileName); 448 exit(1); 449 } 450 } 451 } else if (outFilePostfix) { 452 if (idx >= 0) { 453 asprintf(&path, "%s%s.%04d", filename, 454 outFilePostfix, idx); 455 } else { 456 asprintf(&path, "%s%s", filename, outFilePostfix); 457 } 458 fp = fopen(path, "w"); 459 if (fp == NULL) { 460 perror(path); 461 exit(1); 462 } 463 free(path); 464 } else { 465 if ((flags & UNDO_FLAG_MULT) && type == TYPE_FILE) { 466 if (idx >= 0) { 467 printf("\n>>> %s %04d 0x%016jx %s\n\n", 468 filename, idx, (uintmax_t)ts1.tid, 469 timestamp(&ts1)); 470 } else { 471 printf("\n>>> %s ---- 0x%016jx %s\n\n", 472 filename, (uintmax_t)ts1.tid, 473 timestamp(&ts1)); 474 } 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, 478 timestamp(&ts1)); 479 } 480 fp = stdout; 481 } 482 483 switch(type) { 484 case TYPE_FILE: 485 if ((fi = fopen(ipath1, "r")) != NULL) { 486 while ((n = fread(buf, 1, 8192, fi)) > 0) 487 fwrite(buf, 1, n, fp); 488 fclose(fi); 489 } 490 break; 491 case TYPE_DIFF: 492 printf("diff -N -r -u %s %s (to %s)\n", 493 ipath1, ipath2, timestamp(&ts2)); 494 fflush(stdout); 495 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath1, ipath2, NULL); 496 break; 497 case TYPE_RDIFF: 498 printf("diff -N -r -u %s %s\n", ipath2, ipath1); 499 fflush(stdout); 500 runcmd(fileno(fp), "/usr/bin/diff", "diff", "-N", "-r", "-u", ipath2, ipath1, NULL); 501 break; 502 case TYPE_HISTORY: 503 t = (time_t)ts1.time32; 504 tp = localtime(&t); 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"); 511 printf("\n"); 512 break; 513 } 514 515 if (fp != stdout) 516 fclose(fp); 517 done: 518 free(buf); 519 } 520 521 static 522 void 523 clean_tree(struct undo_hist_entry_rb_tree *tree) 524 { 525 struct undo_hist_entry *tse; 526 527 while ((tse = RB_ROOT(tree)) != NULL) { 528 RB_REMOVE(undo_hist_entry_rb_tree, tree, tse); 529 free(tse); 530 } 531 } 532 533 static 534 void 535 collect_history(int fd, int *errorp, struct undo_hist_entry_rb_tree *tse_tree) 536 { 537 struct hammer_ioc_history hist; 538 struct undo_hist_entry *tse; 539 struct stat st; 540 int istmp; 541 int i; 542 543 /* 544 * Setup 545 */ 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; 550 hist.key = 0; 551 hist.nxt_key = HAMMER_MAX_KEY; 552 553 *errorp = 0; 554 555 if (tse_tree == NULL) { 556 tse_tree = malloc(sizeof(*tse_tree)); 557 RB_INIT(tse_tree); 558 istmp = 1; 559 } else { 560 istmp = 0; 561 } 562 563 /* 564 * Save the inode so inode changes can be reported. 565 */ 566 st.st_ino = 0; 567 fstat(fd, &st); 568 569 /* 570 * Collect a unique set of transaction ids 571 */ 572 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) { 573 *errorp = errno; 574 goto done; 575 } 576 for (;;) { 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)) { 582 free(tse); 583 } 584 } 585 if (hist.head.flags & HAMMER_IOC_HISTORY_EOF) 586 break; 587 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY) { 588 hist.key = hist.nxt_key; 589 hist.nxt_key = HAMMER_MAX_KEY; 590 } 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) { 594 *errorp = errno; 595 break; 596 } 597 } 598 599 /* 600 * Cleanup 601 */ 602 done: 603 if (istmp) { 604 clean_tree(tse_tree); 605 free(tse_tree); 606 } 607 } 608 609 static 610 void 611 collect_dir_history(const char *filename, int *errorp, 612 struct undo_hist_entry_rb_tree *dir_tree) 613 { 614 char *dirname; 615 int fd; 616 int error; 617 618 *errorp = 0; 619 if (strrchr(filename, '/')) { 620 dirname = strdup(filename); 621 *strrchr(dirname, '/') = 0; 622 } else { 623 dirname = strdup("."); 624 } 625 if ((fd = open(dirname, O_RDONLY)) > 0) { 626 collect_history(fd, &error, dir_tree); 627 close(fd); 628 } 629 } 630 631 static 632 hammer_tid_t 633 parse_delta_time(const char *timeStr, int *flags, int ind_flag) 634 { 635 hammer_tid_t tid; 636 637 tid = strtoull(timeStr, NULL, 0); 638 if (timeStr[0] == '+') 639 ++timeStr; 640 if (timeStr[0] >= '0' && timeStr[0] <= '9' && timeStr[1] != 'x') 641 *flags |= ind_flag; 642 return(tid); 643 } 644 645 static void 646 runcmd(int fd, const char *cmd, ...) 647 { 648 va_list va; 649 pid_t pid; 650 char **av; 651 int ac; 652 int i; 653 654 va_start(va, cmd); 655 for (ac = 0; va_arg(va, void *) != NULL; ++ac) 656 ; 657 va_end(va); 658 659 av = malloc((ac + 1) * sizeof(char *)); 660 va_start(va, cmd); 661 for (i = 0; i < ac; ++i) 662 av[i] = va_arg(va, char *); 663 va_end(va); 664 av[i] = NULL; 665 666 if ((pid = fork()) < 0) { 667 perror("fork"); 668 exit(1); 669 } else if (pid == 0) { 670 if (fd != 1) { 671 dup2(fd, 1); 672 close(fd); 673 } 674 execv(cmd, av); 675 _exit(1); 676 } else { 677 while (waitpid(pid, NULL, 0) != pid) 678 ; 679 } 680 free(av); 681 } 682 683 /* 684 * Convert tid to timestamp. 685 */ 686 static char * 687 timestamp(hammer_ioc_hist_entry_t hen) 688 { 689 static char timebuf[64]; 690 time_t t = (time_t)hen->time32; 691 struct tm *tp; 692 693 tp = localtime(&t); 694 strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp); 695 return(timebuf); 696 } 697 698 static 699 int 700 undo_hist_entry_compare(struct undo_hist_entry *he1, 701 struct undo_hist_entry *he2) 702 { 703 if (he1->tse.tid < he2->tse.tid) 704 return(-1); 705 if (he1->tse.tid > he2->tse.tid) 706 return(1); 707 return(0); 708 } 709 710 static void 711 usage(void) 712 { 713 fprintf(stderr, "undo [-adDiuv] [-o outfile] " 714 "[-t transaction-id] [-t transaction-id] path...\n" 715 " -a Iterate all historical segments\n" 716 " -d Forward diff\n" 717 " -D Reverse diff\n" 718 " -i Dump history transaction ids\n" 719 " -u Generate .undo files\n" 720 " -v Verbose\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" 727 ); 728 exit(1); 729 } 730 731