1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
|
/* virt-diff
* Copyright (C) 2013 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include <getopt.h>
#include <fcntl.h>
#include <errno.h>
#include <error.h>
#include <locale.h>
#include <assert.h>
#include <time.h>
#include <libintl.h>
#include <sys/wait.h>
#if MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#elif MAJOR_IN_SYSMACROS
#include <sys/sysmacros.h>
#else
#include <sys/types.h>
#endif
#include "c-ctype.h"
#include "human.h"
#include "getprogname.h"
#include "guestfs.h"
#include "options.h"
#include "display-options.h"
#include "guestfs-utils.h"
#include "visit.h"
/* Internal tree structure built for each guest. */
struct tree;
static struct tree *visit_guest (guestfs_h *g);
static int diff_guests (struct tree *t1, struct tree *t2);
static void free_tree (struct tree *);
/* Libguestfs handles for two source guests. */
guestfs_h *g, *g2;
int read_only = 1;
int live = 0;
int verbose = 0;
int keys_from_stdin = 0;
int echo_keys = 0;
const char *libvirt_uri = NULL;
int inspector = 1;
int in_guestfish = 0;
int in_virt_rescue = 0;
static int atime = 0;
static int csv = 0;
static int dir_links = 0;
static int dir_times = 0;
static int human = 0;
static int enable_extra_stats = 0;
static int enable_times = 0;
static int enable_uids = 0;
static int enable_xattrs = 0;
static int time_t_output = 0;
static int time_relative = 0; /* 1 = seconds, 2 = days */
static const char *checksum = NULL;
static time_t now;
static void output_start_line (void);
static void output_end_line (void);
static void output_flush (void);
static void output_int64 (int64_t);
static void output_int64_dev (int64_t);
static void output_int64_perms (int64_t);
static void output_int64_size (int64_t);
static void output_int64_time (int64_t secs, int64_t nsecs);
static void output_int64_uid (int64_t);
static void output_string (const char *);
static void output_string_link (const char *);
static void output_binary (const char *, size_t len);
static void __attribute__((noreturn))
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try ‘%s --help’ for more information.\n"),
getprogname ());
else {
printf (_("%s: list differences between virtual machines\n"
"Copyright (C) 2010-2020 Red Hat Inc.\n"
"Usage:\n"
" %s [--options] -d domain1 -D domain2\n"
" %s [--options] -a disk1.img -A disk2.img [-a|-A ...]\n"
"Options:\n"
" -a|--add image Add image from first guest\n"
" --all Same as: --extra-stats --times --uids --xattrs\n"
" --atime Don't ignore access time changes\n"
" -A image Add image from second guest\n"
" --blocksize[=512|4096]\n"
" Set sector size of the disk for -a or -A option\n"
" --checksum[=...] Use checksum of file content\n"
" -c|--connect uri Specify libvirt URI for -d option\n"
" --csv Comma-Separated Values output\n"
" --dir-links Don't ignore directory nlink changes\n"
" --dir-times Don't ignore directory time changes\n"
" -d|--domain guest Add disks from first libvirt guest\n"
" -D guest Add disks from second libvirt guest\n"
" --echo-keys Don't turn off echo for passphrases\n"
" --extra-stats Display extra stats\n"
" --format[=raw|..] Force disk format for -a or -A option\n"
" --help Display brief help\n"
" -h|--human-readable Human-readable sizes in output\n"
" --key selector Specify a LUKS key\n"
" --keys-from-stdin Read passphrases from stdin\n"
" --times Display file times\n"
" --time-days Display file times as days before now\n"
" --time-relative Display file times as seconds before now\n"
" --time-t Display file times as time_t's\n"
" --uids Display UID, GID\n"
" -v|--verbose Verbose messages\n"
" -V|--version Display version and exit\n"
" -x Trace libguestfs API calls\n"
" --xattrs Display extended attributes\n"
"For more information, see the manpage %s(1).\n"),
getprogname (), getprogname (),
getprogname (), getprogname ());
}
exit (status);
}
int
main (int argc, char *argv[])
{
/* Current time for --time-days, --time-relative output. */
time (&now);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEBASEDIR);
textdomain (PACKAGE);
enum { HELP_OPTION = CHAR_MAX + 1 };
static const char options[] = "a:A:c:d:D:hvVx";
static const struct option long_options[] = {
{ "add", 1, 0, 'a' },
{ "all", 0, 0, 0 },
{ "atime", 0, 0, 0 },
{ "blocksize", 2, 0, 0 },
{ "checksum", 2, 0, 0 },
{ "checksums", 2, 0, 0 },
{ "csv", 0, 0, 0 },
{ "connect", 1, 0, 'c' },
{ "dir-link", 0, 0, 0 },
{ "dir-links", 0, 0, 0 },
{ "dir-nlink", 0, 0, 0 },
{ "dir-nlinks", 0, 0, 0 },
{ "dir-time", 0, 0, 0 },
{ "dir-times", 0, 0, 0 },
{ "domain", 1, 0, 'd' },
{ "echo-keys", 0, 0, 0 },
{ "extra-stat", 0, 0, 0 },
{ "extra-stats", 0, 0, 0 },
{ "format", 2, 0, 0 },
{ "help", 0, 0, HELP_OPTION },
{ "human-readable", 0, 0, 'h' },
{ "long-options", 0, 0, 0 },
{ "key", 1, 0, 0 },
{ "keys-from-stdin", 0, 0, 0 },
{ "short-options", 0, 0, 0 },
{ "time", 0, 0, 0 },
{ "times", 0, 0, 0 },
{ "time-days", 0, 0, 0 },
{ "time-relative", 0, 0, 0 },
{ "time-t", 0, 0, 0 },
{ "uid", 0, 0, 0 },
{ "uids", 0, 0, 0 },
{ "verbose", 0, 0, 'v' },
{ "version", 0, 0, 'V' },
{ "xattr", 0, 0, 0 },
{ "xattrs", 0, 0, 0 },
{ 0, 0, 0, 0 }
};
struct drv *drvs = NULL; /* First guest. */
struct drv *drvs2 = NULL; /* Second guest. */
const char *format = NULL;
bool format_consumed = true;
int blocksize = 0;
bool blocksize_consumed = true;
int c;
int option_index;
struct tree *tree1, *tree2;
struct key_store *ks = NULL;
g = guestfs_create ();
if (g == NULL)
error (EXIT_FAILURE, errno, "guestfs_create");
guestfs_set_identifier (g, "g1");
g2 = guestfs_create ();
if (g2 == NULL)
error (EXIT_FAILURE, errno, "guestfs_create");
guestfs_set_identifier (g2, "g2");
for (;;) {
c = getopt_long (argc, argv, options, long_options, &option_index);
if (c == -1) break;
switch (c) {
case 0: /* options which are long only */
if (STREQ (long_options[option_index].name, "long-options"))
display_long_options (long_options);
else if (STREQ (long_options[option_index].name, "short-options"))
display_short_options (options);
else if (STREQ (long_options[option_index].name, "keys-from-stdin")) {
keys_from_stdin = 1;
} else if (STREQ (long_options[option_index].name, "echo-keys")) {
echo_keys = 1;
} else if (STREQ (long_options[option_index].name, "format")) {
OPTION_format;
} else if (STREQ (long_options[option_index].name, "blocksize")) {
OPTION_blocksize;
} else if (STREQ (long_options[option_index].name, "all")) {
enable_extra_stats = enable_times = enable_uids = enable_xattrs = 1;
} else if (STREQ (long_options[option_index].name, "atime")) {
atime = 1;
} else if (STREQ (long_options[option_index].name, "csv")) {
csv = 1;
} else if (STREQ (long_options[option_index].name, "checksum") ||
STREQ (long_options[option_index].name, "checksums")) {
if (!optarg || STREQ (optarg, ""))
checksum = "md5";
else
checksum = optarg;
} else if (STREQ (long_options[option_index].name, "dir-link") ||
STREQ (long_options[option_index].name, "dir-links") ||
STREQ (long_options[option_index].name, "dir-nlink") ||
STREQ (long_options[option_index].name, "dir-nlinks")) {
dir_links = 1;
} else if (STREQ (long_options[option_index].name, "dir-time") ||
STREQ (long_options[option_index].name, "dir-times")) {
dir_times = 1;
} else if (STREQ (long_options[option_index].name, "extra-stat") ||
STREQ (long_options[option_index].name, "extra-stats")) {
enable_extra_stats = 1;
} else if (STREQ (long_options[option_index].name, "time") ||
STREQ (long_options[option_index].name, "times")) {
enable_times = 1;
} else if (STREQ (long_options[option_index].name, "time-t")) {
enable_times = 1;
time_t_output = 1;
} else if (STREQ (long_options[option_index].name, "time-relative")) {
enable_times = 1;
time_t_output = 1;
time_relative = 1;
} else if (STREQ (long_options[option_index].name, "time-days")) {
enable_times = 1;
time_t_output = 1;
time_relative = 2;
} else if (STREQ (long_options[option_index].name, "uid") ||
STREQ (long_options[option_index].name, "uids")) {
enable_uids = 1;
} else if (STREQ (long_options[option_index].name, "xattr") ||
STREQ (long_options[option_index].name, "xattrs")) {
enable_xattrs = 1;
} else if (STREQ (long_options[option_index].name, "key")) {
OPTION_key;
} else
error (EXIT_FAILURE, 0,
_("unknown long option: %s (%d)"),
long_options[option_index].name, option_index);
break;
case 'a':
OPTION_a;
break;
case 'A':
OPTION_A;
break;
case 'c':
OPTION_c;
break;
case 'd':
OPTION_d;
break;
case 'D':
OPTION_D;
break;
case 'h':
human = 1;
break;
case 'v':
/* OPTION_v; */
verbose++;
guestfs_set_verbose (g, verbose);
guestfs_set_verbose (g2, verbose);
break;
case 'V':
OPTION_V;
break;
case 'x':
/* OPTION_x; */
guestfs_set_trace (g, 1);
guestfs_set_trace (g2, 1);
break;
case HELP_OPTION:
usage (EXIT_SUCCESS);
default:
usage (EXIT_FAILURE);
}
}
if (drvs == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -a or -d option.\n"),
getprogname ());
usage (EXIT_FAILURE);
}
if (drvs2 == NULL) {
fprintf (stderr, _("%s: error: you must specify at least one -A or -D option.\n"),
getprogname ());
usage (EXIT_FAILURE);
}
/* CSV && human is unsafe because spreadsheets fail to parse these
* fields correctly. (RHBZ#600977).
*/
if (human && csv)
error (EXIT_FAILURE, 0, _("you cannot use -h and --csv options together."));
if (optind != argc) {
fprintf (stderr, _("%s: error: extra argument ‘%s’ on command line.\n"
"Make sure to specify the argument for --checksum or --format "
"like '--format=%s'.\n"),
getprogname (), argv[optind], argv[optind]);
usage (EXIT_FAILURE);
}
/* These are really constants, but they have to be variables for the
* options parsing code. Assert here that they have known-good
* values.
*/
assert (read_only == 1);
assert (inspector == 1);
assert (live == 0);
CHECK_OPTION_format_consumed;
CHECK_OPTION_blocksize_consumed;
unsigned errors = 0;
/* Mount up first guest. */
add_drives (drvs);
if (guestfs_launch (g) == -1)
exit (EXIT_FAILURE);
inspect_mount ();
if ((tree1 = visit_guest (g)) == NULL)
errors++;
/* Mount up second guest. */
add_drives_handle (g2, drvs2, 0);
if (guestfs_launch (g2) == -1)
exit (EXIT_FAILURE);
inspect_mount_handle (g2, ks);
if ((tree2 = visit_guest (g2)) == NULL)
errors++;
if (errors == 0) {
if (diff_guests (tree1, tree2) == -1)
errors++;
}
free_tree (tree1);
free_tree (tree2);
free_drives (drvs);
free_drives (drvs2);
free_key_store (ks);
guestfs_close (g);
guestfs_close (g2);
exit (errors == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}
struct tree {
/* We store the handle here in case we need to go and dig into
* the disk to get file content.
*/
guestfs_h *g;
/* List of files found, sorted by path. */
struct file *files;
size_t nr_files, allocated;
};
struct file {
char *path;
struct guestfs_statns *stat;
struct guestfs_xattr_list *xattrs;
char *csum; /* Checksum. If NULL, use file times and size. */
};
static void
free_tree (struct tree *t)
{
size_t i;
if (t == NULL)
return;
for (i = 0; i < t->nr_files; ++i) {
free (t->files[i].path);
guestfs_free_statns (t->files[i].stat);
guestfs_free_xattr_list (t->files[i].xattrs);
free (t->files[i].csum);
}
free (t->files);
free (t);
}
static int visit_entry (const char *dir, const char *name, const struct guestfs_statns *stat, const struct guestfs_xattr_list *xattrs, void *vt);
static struct tree *
visit_guest (guestfs_h *g)
{
struct tree *t = malloc (sizeof *t);
if (t == NULL) {
perror ("malloc");
return NULL;
}
t->g = g;
t->files = NULL;
t->nr_files = t->allocated = 0;
if (visit (g, "/", visit_entry, t) == -1) {
free_tree (t);
return NULL;
}
if (verbose)
fprintf (stderr, "read %zu entries from guest\n", t->nr_files);
return t;
}
/* Visit each directory/file/etc entry in the tree. This just stores
* the data in the tree. Note we don't store file content, but we
* keep the guestfs handle open so we can pull that out later if we
* need to.
*/
static int
visit_entry (const char *dir, const char *name,
const struct guestfs_statns *stat_orig,
const struct guestfs_xattr_list *xattrs_orig,
void *vt)
{
struct tree *t = vt;
char *path = NULL, *csum = NULL;
struct guestfs_statns *stat = NULL;
struct guestfs_xattr_list *xattrs = NULL;
size_t i;
path = guestfs_int_full_path (dir, name);
if (!path) {
perror ("guestfs_int_full_path");
goto error;
}
/* Copy the stats and xattrs because the visit function will
* free them after we return.
*/
stat = guestfs_copy_statns (stat_orig);
if (stat == NULL)
goto error;
xattrs = guestfs_copy_xattr_list (xattrs_orig);
if (xattrs == NULL)
goto error;
if (checksum && guestfs_int_is_reg (stat->st_mode)) {
csum = guestfs_checksum (t->g, checksum, path);
if (!csum)
goto error;
}
/* If --atime option was NOT passed, flatten the atime field. */
if (!atime)
stat->st_atime_sec = stat->st_atime_nsec = 0;
/* If --dir-links option was NOT passed, flatten nlink field in
* directories.
*/
if (!dir_links && guestfs_int_is_dir (stat->st_mode))
stat->st_nlink = 0;
/* If --dir-times option was NOT passed, flatten time fields in
* directories.
*/
if (!dir_times && guestfs_int_is_dir (stat->st_mode))
stat->st_atime_sec = stat->st_mtime_sec = stat->st_ctime_sec =
stat->st_atime_nsec = stat->st_mtime_nsec = stat->st_ctime_nsec = 0;
/* Add the pathname and stats to the list. */
i = t->nr_files++;
if (i >= t->allocated) {
struct file *old_files = t->files;
const size_t old_allocated = t->allocated;
/* Number of entries in an F15 guest was 111524, and in a
* Windows guest was 10709.
*/
if (old_allocated == 0)
t->allocated = 1024;
else
t->allocated = old_allocated * 2;
t->files = realloc (old_files, t->allocated * sizeof (struct file));
if (t->files == NULL) {
perror ("realloc");
t->files = old_files;
t->allocated = old_allocated;
goto error;
}
}
t->files[i].path = path;
t->files[i].stat = stat;
t->files[i].xattrs = xattrs;
t->files[i].csum = csum;
return 0;
error:
free (path);
free (csum);
guestfs_free_statns (stat);
guestfs_free_xattr_list (xattrs);
return -1;
}
static void deleted (guestfs_h *, struct file *);
static void added (guestfs_h *, struct file *);
static int compare_stats (struct file *, struct file *);
static void changed (guestfs_h *, struct file *, guestfs_h *, struct file *, int st, int cst);
static void diff (struct file *, guestfs_h *, struct file *, guestfs_h *);
static void output_file (guestfs_h *, struct file *);
static int
diff_guests (struct tree *t1, struct tree *t2)
{
struct file *i1 = &t1->files[0];
struct file *i2 = &t2->files[0];
struct file *end1 = &t1->files[t1->nr_files];
struct file *end2 = &t2->files[t2->nr_files];
while (i1 < end1 || i2 < end2) {
if (i1 < end1 && i2 < end2) {
const int comp = strcmp (i1->path, i2->path);
/* i1->path < i2->path. i1 catches up with i2 (files deleted) */
if (comp < 0) {
deleted (t1->g, i1);
i1++;
}
/* i1->path > i2->path. i2 catches up with i1 (files added) */
else if (comp > 0) {
added (t2->g, i2);
i2++;
}
/* Otherwise i1->path == i2->path, compare in detail. */
else {
const int st = compare_stats (i1, i2);
if (st != 0)
changed (t1->g, i1, t2->g, i2, st, 0);
else if (i1->csum && i2->csum) {
const int cst = strcmp (i1->csum, i2->csum);
changed (t1->g, i1, t2->g, i2, 0, cst);
}
i1++;
i2++;
}
}
/* Reached end of i2 list (files deleted). */
else if (i1 < end1) {
deleted (t1->g, i1);
i1++;
}
/* Reached end of i1 list (files added). */
else {
added (t2->g, i2);
i2++;
}
}
output_flush ();
return 0;
}
static void
deleted (guestfs_h *g, struct file *file)
{
output_start_line ();
output_string ("-");
output_file (g, file);
output_end_line ();
}
static void
added (guestfs_h *g, struct file *file)
{
output_start_line ();
output_string ("+");
output_file (g, file);
output_end_line ();
}
static int
compare_stats (struct file *file1, struct file *file2)
{
int r;
r = guestfs_compare_statns (file1->stat, file2->stat);
if (r != 0)
return r;
r = guestfs_compare_xattr_list (file1->xattrs, file2->xattrs);
if (r != 0)
return r;
return 0;
}
static void
changed (guestfs_h *g1, struct file *file1,
guestfs_h *g2, struct file *file2,
int st, int cst)
{
/* Did file content change? */
if (cst != 0 ||
(guestfs_int_is_reg (file1->stat->st_mode) &&
guestfs_int_is_reg (file2->stat->st_mode) &&
(file1->stat->st_mtime_sec != file2->stat->st_mtime_sec ||
file1->stat->st_ctime_sec != file2->stat->st_ctime_sec ||
file1->stat->st_size != file2->stat->st_size))) {
output_start_line ();
output_string ("=");
output_file (g1, file1);
output_end_line ();
if (!csv) {
/* Display file changes. */
output_flush ();
diff (file1, g1, file2, g2);
}
}
/* Did just stats change? */
else if (st != 0) {
output_start_line ();
output_string ("-");
output_file (g1, file1);
output_end_line ();
output_start_line ();
output_string ("+");
output_file (g2, file2);
output_end_line ();
/* Display stats fields that changed. */
output_start_line ();
output_string ("#");
output_string ("changed:");
#define COMPARE_STAT(n) \
if (file1->stat->n != file2->stat->n) output_string (#n)
COMPARE_STAT (st_dev);
COMPARE_STAT (st_ino);
COMPARE_STAT (st_mode);
COMPARE_STAT (st_nlink);
COMPARE_STAT (st_uid);
COMPARE_STAT (st_gid);
COMPARE_STAT (st_rdev);
COMPARE_STAT (st_size);
COMPARE_STAT (st_blksize);
COMPARE_STAT (st_blocks);
COMPARE_STAT (st_atime_sec);
COMPARE_STAT (st_mtime_sec);
COMPARE_STAT (st_ctime_sec);
#undef COMPARE_STAT
if (guestfs_compare_xattr_list (file1->xattrs, file2->xattrs))
output_string ("xattrs");
output_end_line ();
}
}
/* Run a diff on two files. */
static void
diff (struct file *file1, guestfs_h *g1, struct file *file2, guestfs_h *g2)
{
CLEANUP_FREE char *tmpdir = guestfs_get_tmpdir (g1);
CLEANUP_FREE char *tmpd, *tmpda = NULL, *tmpdb = NULL, *cmd = NULL;
int r;
assert (guestfs_int_is_reg (file1->stat->st_mode));
assert (guestfs_int_is_reg (file2->stat->st_mode));
if (asprintf (&tmpd, "%s/virtdiffXXXXXX", tmpdir) < 0)
error (EXIT_FAILURE, errno, "asprintf");
if (mkdtemp (tmpd) == NULL)
error (EXIT_FAILURE, errno, "mkdtemp");
if (asprintf (&tmpda, "%s/a", tmpd) < 0 ||
asprintf (&tmpdb, "%s/b", tmpd) < 0)
error (EXIT_FAILURE, errno, "asprintf");
if (guestfs_download (g1, file1->path, tmpda) == -1)
goto out;
if (guestfs_download (g2, file2->path, tmpdb) == -1)
goto out;
/* Note that the tmpdir is safe, and the rest of the path
* should not need quoting.
*/
if (asprintf (&cmd, "diff -u '%s' '%s' | tail -n +3", tmpda, tmpdb) < 0)
error (EXIT_FAILURE, errno, "asprintf");
if (verbose)
fprintf (stderr, "%s\n", cmd);
r = system (cmd);
if (!WIFEXITED (r) || WEXITSTATUS (r) != 0) {
fprintf (stderr, _("%s: external diff command failed\n"), getprogname ());
goto out;
}
printf ("@@ %s @@\n", _("End of diff"));
out:
unlink (tmpda);
unlink (tmpdb);
rmdir (tmpd);
}
static void
output_file (guestfs_h *g, struct file *file)
{
const char *filetype;
size_t i;
CLEANUP_FREE char *link = NULL;
if (guestfs_int_is_reg (file->stat->st_mode))
filetype = "-";
else if (guestfs_int_is_dir (file->stat->st_mode))
filetype = "d";
else if (guestfs_int_is_chr (file->stat->st_mode))
filetype = "c";
else if (guestfs_int_is_blk (file->stat->st_mode))
filetype = "b";
else if (guestfs_int_is_fifo (file->stat->st_mode))
filetype = "p";
else if (guestfs_int_is_lnk (file->stat->st_mode))
filetype = "l";
else if (guestfs_int_is_sock (file->stat->st_mode))
filetype = "s";
else
filetype = "u";
output_string (filetype);
output_int64_perms (file->stat->st_mode & 07777);
output_int64_size (file->stat->st_size);
/* Display extra fields when enabled. */
if (enable_uids) {
output_int64_uid (file->stat->st_uid);
output_int64_uid (file->stat->st_gid);
}
if (enable_times) {
if (atime)
output_int64_time (file->stat->st_atime_sec, file->stat->st_atime_nsec);
output_int64_time (file->stat->st_mtime_sec, file->stat->st_mtime_nsec);
output_int64_time (file->stat->st_ctime_sec, file->stat->st_ctime_nsec);
}
if (enable_extra_stats) {
output_int64_dev (file->stat->st_dev);
output_int64 (file->stat->st_ino);
output_int64 (file->stat->st_nlink);
output_int64_dev (file->stat->st_rdev);
output_int64 (file->stat->st_blocks);
}
if (file->csum)
output_string (file->csum);
output_string (file->path);
if (guestfs_int_is_lnk (file->stat->st_mode)) {
/* XXX Fix this for NTFS. */
link = guestfs_readlink (g, file->path);
if (link)
output_string_link (link);
}
if (enable_xattrs) {
for (i = 0; i < file->xattrs->len; ++i) {
output_string (file->xattrs->val[i].attrname);
output_binary (file->xattrs->val[i].attrval,
file->xattrs->val[i].attrval_len);
}
}
}
/* Output functions.
*
* Note that we have to be careful to check return values from printf
* in these functions, because we want to catch ENOSPC errors.
*/
static int field;
static void
next_field (void)
{
const int c = csv ? ',' : ' ';
field++;
if (field == 1) return;
if (putchar (c) == EOF)
error (EXIT_FAILURE, errno, "putchar");
}
static void
output_start_line (void)
{
field = 0;
}
static void
output_end_line (void)
{
if (printf ("\n") < 0)
error (EXIT_FAILURE, errno, "printf");
}
static void
output_flush (void)
{
if (fflush (stdout) == EOF)
error (EXIT_FAILURE, errno, "fflush");
}
static void
output_string (const char *s)
{
next_field ();
if (!csv) {
print_no_quoting:
if (printf ("%s", s) < 0)
error (EXIT_FAILURE, errno, "printf");
}
else {
/* Quote CSV string without requiring an external module. */
size_t i, len;
int needs_quoting = 0;
len = strlen (s);
for (i = 0; i < len; ++i) {
if (s[i] == ' ' || s[i] == '"' ||
s[i] == '\n' || s[i] == ',') {
needs_quoting = 1;
break;
}
}
if (!needs_quoting)
goto print_no_quoting;
/* Quoting for CSV fields. */
if (putchar ('"') == EOF)
error (EXIT_FAILURE, errno, "putchar");
for (i = 0; i < len; ++i) {
if (s[i] == '"') {
if (putchar ('"') == EOF || putchar ('"') == EOF)
error (EXIT_FAILURE, errno, "putchar");
} else {
if (putchar (s[i]) == EOF)
error (EXIT_FAILURE, errno, "putchar");
}
}
if (putchar ('"') == EOF)
error (EXIT_FAILURE, errno, "putchar");
}
}
static void
output_string_link (const char *link)
{
if (csv)
output_string (link);
else {
next_field ();
if (printf ("-> %s", link) < 0)
error (EXIT_FAILURE, errno, "printf");
}
}
static void
output_binary (const char *s, size_t len)
{
size_t i;
next_field ();
if (!csv) {
print_no_quoting:
for (i = 0; i < len; ++i) {
if (c_isprint (s[i])) {
if (putchar (s[i]) == EOF)
error (EXIT_FAILURE, errno, "putchar");
} else {
if (printf ("\\x%02x", (unsigned char) s[i]) < 0)
error (EXIT_FAILURE, errno, "putchar");
}
}
}
else {
/* Quote CSV string without requiring an external module. */
int needs_quoting = 0;
for (i = 0; i < len; ++i) {
if (!c_isprint (s[i]) || s[i] == ' ' || s[i] == '"' ||
s[i] == '\n' || s[i] == ',') {
needs_quoting = 1;
break;
}
}
if (!needs_quoting)
goto print_no_quoting;
/* Quoting for CSV fields. */
if (putchar ('"') == EOF)
error (EXIT_FAILURE, errno, "putchar");
for (i = 0; i < len; ++i) {
if (s[i] == '"') {
if (putchar ('"') == EOF || putchar ('"') == EOF)
error (EXIT_FAILURE, errno, "putchar");
} else {
if (c_isprint (s[i])) {
if (putchar (s[i]) == EOF)
error (EXIT_FAILURE, errno, "putchar");
} else {
if (printf ("\\x%2x", (unsigned) s[i]) < 0)
error (EXIT_FAILURE, errno, "printf");
}
}
}
if (putchar ('"') == EOF)
error (EXIT_FAILURE, errno, "putchar");
}
}
static void
output_int64 (int64_t i)
{
next_field ();
/* csv doesn't need escaping */
if (printf ("%" PRIi64, i) < 0)
error (EXIT_FAILURE, errno, "printf");
}
static void
output_int64_size (int64_t size)
{
char buf[LONGEST_HUMAN_READABLE];
const int hopts =
human_round_to_nearest|human_autoscale|human_base_1024|human_SI;
int r;
next_field ();
if (!csv) {
if (!human)
r = printf ("%10" PRIi64, size);
else
r = printf ("%10s",
human_readable ((uintmax_t) size, buf, hopts, 1, 1));
} else {
/* CSV is the same as non-CSV but we don't need to right-align. */
if (!human)
r = printf ("%" PRIi64, size);
else
r = printf ("%s",
human_readable ((uintmax_t) size, buf, hopts, 1, 1));
}
if (r < 0)
error (EXIT_FAILURE, errno, "printf");
}
static void
output_int64_perms (int64_t i)
{
next_field ();
/* csv doesn't need escaping */
if (printf ("%04" PRIo64, (uint64_t) i) < 0)
error (EXIT_FAILURE, errno, "printf");
}
static void
output_int64_time (int64_t secs, int64_t nsecs)
{
int r;
next_field ();
/* csv doesn't need escaping */
if (time_t_output) {
switch (time_relative) {
case 0: /* --time-t */
r = printf ("%10" PRIi64, secs);
break;
case 1: /* --time-relative */
r = printf ("%8" PRIi64, now - secs);
break;
case 2: /* --time-days */
default:
r = printf ("%3" PRIi64, (now - secs) / 86400);
break;
}
}
else {
time_t t = (time_t) secs;
char buf[64];
struct tm *tm;
tm = localtime (&t);
if (tm == NULL)
error (EXIT_FAILURE, errno, "localtime");
if (strftime (buf, sizeof buf, "%F %T", tm) == 0)
error (EXIT_FAILURE, errno, "strftime");
r = printf ("%s", buf);
}
if (r < 0)
error (EXIT_FAILURE, errno, "printf");
}
static void
output_int64_uid (int64_t i)
{
next_field ();
/* csv doesn't need escaping */
if (printf (csv ? "%" PRIi64 : "%4" PRIi64, i) < 0)
error (EXIT_FAILURE, errno, "printf");
}
static void
output_int64_dev (int64_t i)
{
dev_t dev = i;
next_field ();
/* csv doesn't need escaping */
if (printf ("%ju:%ju",
(uintmax_t) major (dev), (uintmax_t) minor (dev)) < 0)
error (EXIT_FAILURE, errno, "printf");
}
|