compare.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /* Diff files from a tar archive.
  2. Copyright 1988, 1992-1994, 1996-1997, 1999-2001, 2003-2007,
  3. 2009-2010, 2012-2014, 2016 Free Software Foundation, Inc.
  4. This file is part of GNU tar.
  5. GNU tar is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. GNU tar is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. Written by John Gilmore, on 1987-04-30. */
  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 = blocking_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_read (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 (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. diff_handle = openat (chdir_fd, file_name, open_read_flags);
  186. if (diff_handle < 0)
  187. {
  188. open_error (file_name);
  189. skip_member ();
  190. report_difference (&current_stat_info, NULL);
  191. }
  192. else
  193. {
  194. int status;
  195. if (current_stat_info.is_sparse)
  196. sparse_diff_file (diff_handle, &current_stat_info);
  197. else
  198. read_and_process (&current_stat_info, process_rawdata);
  199. if (atime_preserve_option == replace_atime_preserve
  200. && stat_data.st_size != 0)
  201. {
  202. struct timespec atime = get_stat_atime (&stat_data);
  203. if (set_file_atime (diff_handle, chdir_fd, file_name, atime)
  204. != 0)
  205. utime_error (file_name);
  206. }
  207. status = close (diff_handle);
  208. if (status != 0)
  209. close_error (file_name);
  210. }
  211. }
  212. }
  213. }
  214. static void
  215. diff_link (void)
  216. {
  217. struct stat file_data;
  218. struct stat link_data;
  219. if (get_stat_data (current_stat_info.file_name, &file_data)
  220. && get_stat_data (current_stat_info.link_name, &link_data)
  221. && !sys_compare_links (&file_data, &link_data))
  222. report_difference (&current_stat_info,
  223. _("Not linked to %s"),
  224. quote (current_stat_info.link_name));
  225. }
  226. #ifdef HAVE_READLINK
  227. static void
  228. diff_symlink (void)
  229. {
  230. char buf[1024];
  231. size_t len = strlen (current_stat_info.link_name);
  232. char *linkbuf = len < sizeof buf ? buf : xmalloc (len + 1);
  233. ssize_t status = readlinkat (chdir_fd, current_stat_info.file_name,
  234. linkbuf, len + 1);
  235. if (status < 0)
  236. {
  237. if (errno == ENOENT)
  238. readlink_warn (current_stat_info.file_name);
  239. else
  240. readlink_error (current_stat_info.file_name);
  241. report_difference (&current_stat_info, NULL);
  242. }
  243. else if (status != len
  244. || memcmp (current_stat_info.link_name, linkbuf, len) != 0)
  245. report_difference (&current_stat_info, _("Symlink differs"));
  246. if (linkbuf != buf)
  247. free (linkbuf);
  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 (struct tar_stat_info *dir)
  311. {
  312. const char *dumpdir_buffer;
  313. if (dir->fd == 0)
  314. {
  315. void (*diag) (char const *) = NULL;
  316. int fd = subfile_open (dir->parent, dir->orig_file_name, open_read_flags);
  317. if (fd < 0)
  318. diag = open_diag;
  319. else if (fstat (fd, &dir->stat))
  320. {
  321. diag = stat_diag;
  322. close (fd);
  323. }
  324. else
  325. dir->fd = fd;
  326. if (diag)
  327. {
  328. file_removed_diag (dir->orig_file_name, false, diag);
  329. return;
  330. }
  331. }
  332. dumpdir_buffer = directory_contents (scan_directory (dir));
  333. if (dumpdir_buffer)
  334. {
  335. if (dumpdir_cmp (dir->dumpdir, dumpdir_buffer))
  336. report_difference (dir, _("Contents differ"));
  337. }
  338. else
  339. read_and_process (dir, process_noop);
  340. }
  341. static void
  342. diff_multivol (void)
  343. {
  344. struct stat stat_data;
  345. int fd, status;
  346. off_t offset;
  347. if (current_stat_info.had_trailing_slash)
  348. {
  349. diff_dir ();
  350. return;
  351. }
  352. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  353. return;
  354. if (!S_ISREG (stat_data.st_mode))
  355. {
  356. report_difference (&current_stat_info, _("File type differs"));
  357. skip_member ();
  358. return;
  359. }
  360. offset = OFF_FROM_HEADER (current_header->oldgnu_header.offset);
  361. if (offset < 0
  362. || INT_ADD_OVERFLOW (current_stat_info.stat.st_size, offset)
  363. || stat_data.st_size != current_stat_info.stat.st_size + offset)
  364. {
  365. report_difference (&current_stat_info, _("Size differs"));
  366. skip_member ();
  367. return;
  368. }
  369. fd = openat (chdir_fd, current_stat_info.file_name, open_read_flags);
  370. if (fd < 0)
  371. {
  372. open_error (current_stat_info.file_name);
  373. report_difference (&current_stat_info, NULL);
  374. skip_member ();
  375. return;
  376. }
  377. if (lseek (fd, offset, SEEK_SET) < 0)
  378. {
  379. seek_error_details (current_stat_info.file_name, offset);
  380. report_difference (&current_stat_info, NULL);
  381. }
  382. else
  383. read_and_process (&current_stat_info, process_rawdata);
  384. status = close (fd);
  385. if (status != 0)
  386. close_error (current_stat_info.file_name);
  387. }
  388. /* Diff a file against the archive. */
  389. void
  390. diff_archive (void)
  391. {
  392. set_next_block_after (current_header);
  393. /* Print the block from current_header and current_stat_info. */
  394. if (verbose_option)
  395. {
  396. if (now_verifying)
  397. fprintf (stdlis, _("Verify "));
  398. print_header (&current_stat_info, current_header, -1);
  399. }
  400. switch (current_header->header.typeflag)
  401. {
  402. default:
  403. ERROR ((0, 0, _("%s: Unknown file type '%c', diffed as normal file"),
  404. quotearg_colon (current_stat_info.file_name),
  405. current_header->header.typeflag));
  406. /* Fall through. */
  407. case AREGTYPE:
  408. case REGTYPE:
  409. case GNUTYPE_SPARSE:
  410. case CONTTYPE:
  411. /* Appears to be a file. See if it's really a directory. */
  412. if (current_stat_info.had_trailing_slash)
  413. diff_dir ();
  414. else
  415. diff_file ();
  416. break;
  417. case LNKTYPE:
  418. diff_link ();
  419. break;
  420. #ifdef HAVE_READLINK
  421. case SYMTYPE:
  422. diff_symlink ();
  423. break;
  424. #endif
  425. case CHRTYPE:
  426. case BLKTYPE:
  427. case FIFOTYPE:
  428. diff_special ();
  429. break;
  430. case GNUTYPE_DUMPDIR:
  431. case DIRTYPE:
  432. if (is_dumpdir (&current_stat_info))
  433. diff_dumpdir (&current_stat_info);
  434. diff_dir ();
  435. break;
  436. case GNUTYPE_VOLHDR:
  437. break;
  438. case GNUTYPE_MULTIVOL:
  439. diff_multivol ();
  440. }
  441. }
  442. void
  443. verify_volume (void)
  444. {
  445. int may_fail = 0;
  446. if (removed_prefixes_p ())
  447. {
  448. WARN((0, 0,
  449. _("Archive contains file names with leading prefixes removed.")));
  450. may_fail = 1;
  451. }
  452. if (transform_program_p ())
  453. {
  454. WARN((0, 0,
  455. _("Archive contains transformed file names.")));
  456. may_fail = 1;
  457. }
  458. if (may_fail)
  459. WARN((0, 0,
  460. _("Verification may fail to locate original files.")));
  461. clear_directory_table ();
  462. if (!diff_buffer)
  463. diff_init ();
  464. /* Verifying an archive is meant to check if the physical media got it
  465. correctly, so try to defeat clever in-memory buffering pertaining to
  466. this particular media. On Linux, for example, the floppy drive would
  467. not even be accessed for the whole verification.
  468. The code was using fsync only when the ioctl is unavailable, but
  469. Marty Leisner says that the ioctl does not work when not preceded by
  470. fsync. So, until we know better, or maybe to please Marty, let's do it
  471. the unbelievable way :-). */
  472. #if HAVE_FSYNC
  473. fsync (archive);
  474. #endif
  475. #ifdef FDFLUSH
  476. ioctl (archive, FDFLUSH);
  477. #endif
  478. #ifdef MTIOCTOP
  479. {
  480. struct mtop operation;
  481. int status;
  482. operation.mt_op = MTBSF;
  483. operation.mt_count = 1;
  484. if (status = rmtioctl (archive, MTIOCTOP, (char *) &operation), status < 0)
  485. {
  486. if (errno != EIO
  487. || (status = rmtioctl (archive, MTIOCTOP, (char *) &operation),
  488. status < 0))
  489. {
  490. #endif
  491. if (rmtlseek (archive, (off_t) 0, SEEK_SET) != 0)
  492. {
  493. /* Lseek failed. Try a different method. */
  494. seek_warn (archive_name_array[0]);
  495. return;
  496. }
  497. #ifdef MTIOCTOP
  498. }
  499. }
  500. }
  501. #endif
  502. access_mode = ACCESS_READ;
  503. now_verifying = 1;
  504. flush_read ();
  505. while (1)
  506. {
  507. enum read_header status = read_header (&current_header,
  508. &current_stat_info,
  509. read_header_auto);
  510. if (status == HEADER_FAILURE)
  511. {
  512. int counter = 0;
  513. do
  514. {
  515. counter++;
  516. set_next_block_after (current_header);
  517. status = read_header (&current_header, &current_stat_info,
  518. read_header_auto);
  519. }
  520. while (status == HEADER_FAILURE);
  521. ERROR ((0, 0,
  522. ngettext ("VERIFY FAILURE: %d invalid header detected",
  523. "VERIFY FAILURE: %d invalid headers detected",
  524. counter), counter));
  525. }
  526. if (status == HEADER_END_OF_FILE)
  527. break;
  528. if (status == HEADER_ZERO_BLOCK)
  529. {
  530. set_next_block_after (current_header);
  531. if (!ignore_zeros_option)
  532. {
  533. char buf[UINTMAX_STRSIZE_BOUND];
  534. status = read_header (&current_header, &current_stat_info,
  535. read_header_auto);
  536. if (status == HEADER_ZERO_BLOCK)
  537. break;
  538. WARNOPT (WARN_ALONE_ZERO_BLOCK,
  539. (0, 0, _("A lone zero block at %s"),
  540. STRINGIFY_BIGINT (current_block_ordinal (), buf)));
  541. }
  542. continue;
  543. }
  544. decode_header (current_header, &current_stat_info, &current_format, 1);
  545. diff_archive ();
  546. tar_stat_destroy (&current_stat_info);
  547. }
  548. access_mode = ACCESS_WRITE;
  549. now_verifying = 0;
  550. }