compare.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /* Diff files from a tar archive.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
  4. Written by John Gilmore, on 1987-04-30.
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. #include <system.h>
  17. #include <system-ioctl.h>
  18. #if HAVE_LINUX_FD_H
  19. # include <linux/fd.h>
  20. #endif
  21. #include "common.h"
  22. #include <quotearg.h>
  23. #include <rmt.h>
  24. #include <stdarg.h>
  25. /* Nonzero if we are verifying at the moment. */
  26. bool now_verifying;
  27. /* File descriptor for the file we are diffing. */
  28. static int diff_handle;
  29. /* Area for reading file contents into. */
  30. static char *diff_buffer;
  31. /* Initialize for a diff operation. */
  32. void
  33. diff_init (void)
  34. {
  35. void *ptr;
  36. diff_buffer = page_aligned_alloc (&ptr, record_size);
  37. if (listed_incremental_option)
  38. read_directory_file ();
  39. }
  40. /* Sigh about something that differs by writing a MESSAGE to stdlis,
  41. given MESSAGE is nonzero. Also set the exit status if not already. */
  42. void
  43. report_difference (struct tar_stat_info *st, const char *fmt, ...)
  44. {
  45. if (fmt)
  46. {
  47. va_list ap;
  48. fprintf (stdlis, "%s: ", quotearg_colon (st->file_name));
  49. va_start (ap, fmt);
  50. vfprintf (stdlis, fmt, ap);
  51. va_end (ap);
  52. fprintf (stdlis, "\n");
  53. }
  54. set_exit_status (TAREXIT_DIFFERS);
  55. }
  56. /* Take a buffer returned by read_and_process and do nothing with it. */
  57. static int
  58. process_noop (size_t size __attribute__ ((unused)),
  59. char *data __attribute__ ((unused)))
  60. {
  61. return 1;
  62. }
  63. static int
  64. process_rawdata (size_t bytes, char *buffer)
  65. {
  66. size_t status = safe_read (diff_handle, diff_buffer, bytes);
  67. if (status != bytes)
  68. {
  69. if (status == SAFE_READ_ERROR)
  70. {
  71. read_error (current_stat_info.file_name);
  72. report_difference (&current_stat_info, NULL);
  73. }
  74. else
  75. {
  76. report_difference (&current_stat_info,
  77. ngettext ("Could only read %lu of %lu byte",
  78. "Could only read %lu of %lu bytes",
  79. bytes),
  80. (unsigned long) status, (unsigned long) bytes);
  81. }
  82. return 0;
  83. }
  84. if (memcmp (buffer, diff_buffer, bytes))
  85. {
  86. report_difference (&current_stat_info, _("Contents differ"));
  87. return 0;
  88. }
  89. return 1;
  90. }
  91. /* Some other routine wants SIZE bytes in the archive. For each chunk
  92. of the archive, call PROCESSOR with the size of the chunk, and the
  93. address of the chunk it can work with. The PROCESSOR should return
  94. nonzero for success. Once it returns error, continue skipping
  95. without calling PROCESSOR anymore. */
  96. static void
  97. read_and_process (struct tar_stat_info *st, int (*processor) (size_t, char *))
  98. {
  99. union block *data_block;
  100. size_t data_size;
  101. off_t size = st->stat.st_size;
  102. mv_begin (st);
  103. while (size)
  104. {
  105. data_block = find_next_block ();
  106. if (! data_block)
  107. {
  108. ERROR ((0, 0, _("Unexpected EOF in archive")));
  109. return;
  110. }
  111. data_size = available_space_after (data_block);
  112. if (data_size > size)
  113. data_size = size;
  114. if (!(*processor) (data_size, data_block->buffer))
  115. processor = process_noop;
  116. set_next_block_after ((union block *)
  117. (data_block->buffer + data_size - 1));
  118. size -= data_size;
  119. mv_size_left (size);
  120. }
  121. mv_end ();
  122. }
  123. /* Call either stat or lstat over STAT_DATA, depending on
  124. --dereference (-h), for a file which should exist. Diagnose any
  125. problem. Return nonzero for success, zero otherwise. */
  126. static int
  127. get_stat_data (char const *file_name, struct stat *stat_data)
  128. {
  129. int status = deref_stat (dereference_option, file_name, stat_data);
  130. if (status != 0)
  131. {
  132. if (errno == ENOENT)
  133. stat_warn (file_name);
  134. else
  135. stat_error (file_name);
  136. report_difference (&current_stat_info, NULL);
  137. return 0;
  138. }
  139. return 1;
  140. }
  141. static void
  142. diff_dir (void)
  143. {
  144. struct stat stat_data;
  145. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  146. return;
  147. if (!S_ISDIR (stat_data.st_mode))
  148. report_difference (&current_stat_info, _("File type differs"));
  149. else if ((current_stat_info.stat.st_mode & MODE_ALL) !=
  150. (stat_data.st_mode & MODE_ALL))
  151. report_difference (&current_stat_info, _("Mode differs"));
  152. }
  153. static void
  154. diff_file (void)
  155. {
  156. char const *file_name = current_stat_info.file_name;
  157. struct stat stat_data;
  158. if (!get_stat_data (file_name, &stat_data))
  159. skip_member ();
  160. else if (!S_ISREG (stat_data.st_mode))
  161. {
  162. report_difference (&current_stat_info, _("File type differs"));
  163. skip_member ();
  164. }
  165. else
  166. {
  167. if ((current_stat_info.stat.st_mode & MODE_ALL) !=
  168. (stat_data.st_mode & MODE_ALL))
  169. report_difference (&current_stat_info, _("Mode differs"));
  170. if (!sys_compare_uid (&stat_data, &current_stat_info.stat))
  171. report_difference (&current_stat_info, _("Uid differs"));
  172. if (!sys_compare_gid (&stat_data, &current_stat_info.stat))
  173. report_difference (&current_stat_info, _("Gid differs"));
  174. if (tar_timespec_cmp (get_stat_mtime (&stat_data),
  175. current_stat_info.mtime))
  176. report_difference (&current_stat_info, _("Mod time differs"));
  177. if (current_header->header.typeflag != GNUTYPE_SPARSE
  178. && stat_data.st_size != current_stat_info.stat.st_size)
  179. {
  180. report_difference (&current_stat_info, _("Size differs"));
  181. skip_member ();
  182. }
  183. else
  184. {
  185. int atime_flag =
  186. (atime_preserve_option == system_atime_preserve
  187. ? O_NOATIME
  188. : 0);
  189. diff_handle = open (file_name, O_RDONLY | O_BINARY | atime_flag);
  190. if (diff_handle < 0)
  191. {
  192. open_error (file_name);
  193. skip_member ();
  194. report_difference (&current_stat_info, NULL);
  195. }
  196. else
  197. {
  198. int status;
  199. if (current_stat_info.is_sparse)
  200. sparse_diff_file (diff_handle, &current_stat_info);
  201. else
  202. read_and_process (&current_stat_info, process_rawdata);
  203. if (atime_preserve_option == replace_atime_preserve)
  204. {
  205. struct timespec ts[2];
  206. ts[0] = get_stat_atime (&stat_data);
  207. ts[1] = get_stat_mtime (&stat_data);
  208. if (set_file_atime (diff_handle, file_name, ts) != 0)
  209. utime_error (file_name);
  210. }
  211. status = close (diff_handle);
  212. if (status != 0)
  213. close_error (file_name);
  214. }
  215. }
  216. }
  217. }
  218. static void
  219. diff_link (void)
  220. {
  221. struct stat file_data;
  222. struct stat link_data;
  223. if (get_stat_data (current_stat_info.file_name, &file_data)
  224. && get_stat_data (current_stat_info.link_name, &link_data)
  225. && !sys_compare_links (&file_data, &link_data))
  226. report_difference (&current_stat_info,
  227. _("Not linked to %s"),
  228. quote (current_stat_info.link_name));
  229. }
  230. #ifdef HAVE_READLINK
  231. static void
  232. diff_symlink (void)
  233. {
  234. size_t len = strlen (current_stat_info.link_name);
  235. char *linkbuf = alloca (len + 1);
  236. int status = readlink (current_stat_info.file_name, linkbuf, len + 1);
  237. if (status < 0)
  238. {
  239. if (errno == ENOENT)
  240. readlink_warn (current_stat_info.file_name);
  241. else
  242. readlink_error (current_stat_info.file_name);
  243. report_difference (&current_stat_info, NULL);
  244. }
  245. else if (status != len
  246. || strncmp (current_stat_info.link_name, linkbuf, len) != 0)
  247. report_difference (&current_stat_info, _("Symlink differs"));
  248. }
  249. #endif
  250. static void
  251. diff_special (void)
  252. {
  253. struct stat stat_data;
  254. /* FIXME: deal with umask. */
  255. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  256. return;
  257. if (current_header->header.typeflag == CHRTYPE
  258. ? !S_ISCHR (stat_data.st_mode)
  259. : current_header->header.typeflag == BLKTYPE
  260. ? !S_ISBLK (stat_data.st_mode)
  261. : /* current_header->header.typeflag == FIFOTYPE */
  262. !S_ISFIFO (stat_data.st_mode))
  263. {
  264. report_difference (&current_stat_info, _("File type differs"));
  265. return;
  266. }
  267. if ((current_header->header.typeflag == CHRTYPE
  268. || current_header->header.typeflag == BLKTYPE)
  269. && current_stat_info.stat.st_rdev != stat_data.st_rdev)
  270. {
  271. report_difference (&current_stat_info, _("Device number differs"));
  272. return;
  273. }
  274. if ((current_stat_info.stat.st_mode & MODE_ALL) !=
  275. (stat_data.st_mode & MODE_ALL))
  276. report_difference (&current_stat_info, _("Mode differs"));
  277. }
  278. static int
  279. dumpdir_cmp (const char *a, const char *b)
  280. {
  281. size_t len;
  282. while (*a)
  283. switch (*a)
  284. {
  285. case 'Y':
  286. case 'N':
  287. if (!strchr ("YN", *b))
  288. return 1;
  289. if (strcmp(a + 1, b + 1))
  290. return 1;
  291. len = strlen (a) + 1;
  292. a += len;
  293. b += len;
  294. break;
  295. case 'D':
  296. if (strcmp(a, b))
  297. return 1;
  298. len = strlen (a) + 1;
  299. a += len;
  300. b += len;
  301. break;
  302. case 'R':
  303. case 'T':
  304. case 'X':
  305. return *b;
  306. }
  307. return *b;
  308. }
  309. static void
  310. diff_dumpdir (void)
  311. {
  312. const char *dumpdir_buffer;
  313. dev_t dev = 0;
  314. struct stat stat_data;
  315. if (deref_stat (true, current_stat_info.file_name, &stat_data))
  316. {
  317. if (errno == ENOENT)
  318. stat_warn (current_stat_info.file_name);
  319. else
  320. stat_error (current_stat_info.file_name);
  321. }
  322. else
  323. dev = stat_data.st_dev;
  324. dumpdir_buffer = directory_contents
  325. (scan_directory (current_stat_info.file_name, dev, false));
  326. if (dumpdir_buffer)
  327. {
  328. if (dumpdir_cmp (current_stat_info.dumpdir, dumpdir_buffer))
  329. report_difference (&current_stat_info, _("Contents differ"));
  330. }
  331. else
  332. read_and_process (&current_stat_info, process_noop);
  333. }
  334. static void
  335. diff_multivol (void)
  336. {
  337. struct stat stat_data;
  338. int fd, status;
  339. off_t offset;
  340. if (current_stat_info.had_trailing_slash)
  341. {
  342. diff_dir ();
  343. return;
  344. }
  345. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  346. return;
  347. if (!S_ISREG (stat_data.st_mode))
  348. {
  349. report_difference (&current_stat_info, _("File type differs"));
  350. skip_member ();
  351. return;
  352. }
  353. offset = OFF_FROM_HEADER (current_header->oldgnu_header.offset);
  354. if (stat_data.st_size != current_stat_info.stat.st_size + offset)
  355. {
  356. report_difference (&current_stat_info, _("Size differs"));
  357. skip_member ();
  358. return;
  359. }
  360. fd = open (current_stat_info.file_name, O_RDONLY | O_BINARY);
  361. if (fd < 0)
  362. {
  363. open_error (current_stat_info.file_name);
  364. report_difference (&current_stat_info, NULL);
  365. skip_member ();
  366. return;
  367. }
  368. if (lseek (fd, offset, SEEK_SET) < 0)
  369. {
  370. seek_error_details (current_stat_info.file_name, offset);
  371. report_difference (&current_stat_info, NULL);
  372. return;
  373. }
  374. read_and_process (&current_stat_info, process_rawdata);
  375. status = close (fd);
  376. if (status != 0)
  377. close_error (current_stat_info.file_name);
  378. }
  379. /* Diff a file against the archive. */
  380. void
  381. diff_archive (void)
  382. {
  383. set_next_block_after (current_header);
  384. /* Print the block from current_header and current_stat_info. */
  385. if (verbose_option)
  386. {
  387. if (now_verifying)
  388. fprintf (stdlis, _("Verify "));
  389. print_header (&current_stat_info, current_header, -1);
  390. }
  391. switch (current_header->header.typeflag)
  392. {
  393. default:
  394. ERROR ((0, 0, _("%s: Unknown file type `%c', diffed as normal file"),
  395. quotearg_colon (current_stat_info.file_name),
  396. current_header->header.typeflag));
  397. /* Fall through. */
  398. case AREGTYPE:
  399. case REGTYPE:
  400. case GNUTYPE_SPARSE:
  401. case CONTTYPE:
  402. /* Appears to be a file. See if it's really a directory. */
  403. if (current_stat_info.had_trailing_slash)
  404. diff_dir ();
  405. else
  406. diff_file ();
  407. break;
  408. case LNKTYPE:
  409. diff_link ();
  410. break;
  411. #ifdef HAVE_READLINK
  412. case SYMTYPE:
  413. diff_symlink ();
  414. break;
  415. #endif
  416. case CHRTYPE:
  417. case BLKTYPE:
  418. case FIFOTYPE:
  419. diff_special ();
  420. break;
  421. case GNUTYPE_DUMPDIR:
  422. case DIRTYPE:
  423. if (is_dumpdir (&current_stat_info))
  424. diff_dumpdir ();
  425. diff_dir ();
  426. break;
  427. case GNUTYPE_VOLHDR:
  428. break;
  429. case GNUTYPE_MULTIVOL:
  430. diff_multivol ();
  431. }
  432. }
  433. void
  434. verify_volume (void)
  435. {
  436. if (removed_prefixes_p ())
  437. {
  438. WARN((0, 0,
  439. _("Archive contains file names with leading prefixes removed.")));
  440. WARN((0, 0,
  441. _("Verification may fail to locate original files.")));
  442. }
  443. if (!diff_buffer)
  444. diff_init ();
  445. /* Verifying an archive is meant to check if the physical media got it
  446. correctly, so try to defeat clever in-memory buffering pertaining to
  447. this particular media. On Linux, for example, the floppy drive would
  448. not even be accessed for the whole verification.
  449. The code was using fsync only when the ioctl is unavailable, but
  450. Marty Leisner says that the ioctl does not work when not preceded by
  451. fsync. So, until we know better, or maybe to please Marty, let's do it
  452. the unbelievable way :-). */
  453. #if HAVE_FSYNC
  454. fsync (archive);
  455. #endif
  456. #ifdef FDFLUSH
  457. ioctl (archive, FDFLUSH);
  458. #endif
  459. #ifdef MTIOCTOP
  460. {
  461. struct mtop operation;
  462. int status;
  463. operation.mt_op = MTBSF;
  464. operation.mt_count = 1;
  465. if (status = rmtioctl (archive, MTIOCTOP, (char *) &operation), status < 0)
  466. {
  467. if (errno != EIO
  468. || (status = rmtioctl (archive, MTIOCTOP, (char *) &operation),
  469. status < 0))
  470. {
  471. #endif
  472. if (rmtlseek (archive, (off_t) 0, SEEK_SET) != 0)
  473. {
  474. /* Lseek failed. Try a different method. */
  475. seek_warn (archive_name_array[0]);
  476. return;
  477. }
  478. #ifdef MTIOCTOP
  479. }
  480. }
  481. }
  482. #endif
  483. access_mode = ACCESS_READ;
  484. now_verifying = 1;
  485. flush_read ();
  486. while (1)
  487. {
  488. enum read_header status = read_header (&current_header,
  489. &current_stat_info,
  490. read_header_auto);
  491. if (status == HEADER_FAILURE)
  492. {
  493. int counter = 0;
  494. do
  495. {
  496. counter++;
  497. set_next_block_after (current_header);
  498. status = read_header (&current_header, &current_stat_info,
  499. read_header_auto);
  500. }
  501. while (status == HEADER_FAILURE);
  502. ERROR ((0, 0,
  503. ngettext ("VERIFY FAILURE: %d invalid header detected",
  504. "VERIFY FAILURE: %d invalid headers detected",
  505. counter), counter));
  506. }
  507. if (status == HEADER_END_OF_FILE)
  508. break;
  509. if (status == HEADER_ZERO_BLOCK)
  510. {
  511. set_next_block_after (current_header);
  512. if (!ignore_zeros_option)
  513. {
  514. char buf[UINTMAX_STRSIZE_BOUND];
  515. status = read_header (&current_header, &current_stat_info,
  516. read_header_auto);
  517. if (status == HEADER_ZERO_BLOCK)
  518. break;
  519. WARNOPT (WARN_ALONE_ZERO_BLOCK,
  520. (0, 0, _("A lone zero block at %s"),
  521. STRINGIFY_BIGINT (current_block_ordinal (), buf)));
  522. }
  523. }
  524. diff_archive ();
  525. tar_stat_destroy (&current_stat_info);
  526. }
  527. access_mode = ACCESS_WRITE;
  528. now_verifying = 0;
  529. }