compare.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /* Diff files from a tar archive.
  2. Copyright 1988-2022 Free Software Foundation, Inc.
  3. This file is part of GNU tar.
  4. GNU tar is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. GNU tar is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. Written by John Gilmore, on 1987-04-30. */
  15. #include <system.h>
  16. #include <system-ioctl.h>
  17. #if HAVE_LINUX_FD_H
  18. # include <linux/fd.h>
  19. #endif
  20. #include "common.h"
  21. #include <quotearg.h>
  22. #include <rmt.h>
  23. #include <stdarg.h>
  24. /* Nonzero if we are verifying at the moment. */
  25. bool now_verifying;
  26. /* File descriptor for the file we are diffing. */
  27. static int diff_handle;
  28. /* Area for reading file contents into. */
  29. static char *diff_buffer;
  30. /* Initialize for a diff operation. */
  31. void
  32. diff_init (void)
  33. {
  34. void *ptr;
  35. diff_buffer = page_aligned_alloc (&ptr, record_size);
  36. if (listed_incremental_option)
  37. read_directory_file ();
  38. }
  39. enum { QUOTE_ARG, QUOTE_NAME };
  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: ", quote_n_colon (QUOTE_NAME, 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 MAYBE_UNUSED, char *data MAYBE_UNUSED)
  59. {
  60. return 1;
  61. }
  62. static int
  63. process_rawdata (size_t bytes, char *buffer)
  64. {
  65. size_t status = blocking_read (diff_handle, diff_buffer, bytes);
  66. if (status != bytes)
  67. {
  68. if (status == SAFE_READ_ERROR)
  69. {
  70. read_error (current_stat_info.file_name);
  71. report_difference (&current_stat_info, NULL);
  72. }
  73. else
  74. {
  75. report_difference (&current_stat_info,
  76. ngettext ("Could only read %lu of %lu byte",
  77. "Could only read %lu of %lu bytes",
  78. bytes),
  79. (unsigned long) status, (unsigned long) bytes);
  80. }
  81. return 0;
  82. }
  83. if (memcmp (buffer, diff_buffer, bytes))
  84. {
  85. report_difference (&current_stat_info, _("Contents differ"));
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. /* Some other routine wants SIZE bytes in the archive. For each chunk
  91. of the archive, call PROCESSOR with the size of the chunk, and the
  92. address of the chunk it can work with. The PROCESSOR should return
  93. nonzero for success. Once it returns error, continue skipping
  94. without calling PROCESSOR anymore. */
  95. static void
  96. read_and_process (struct tar_stat_info *st, int (*processor) (size_t, char *))
  97. {
  98. union block *data_block;
  99. size_t data_size;
  100. off_t size = st->stat.st_size;
  101. mv_begin_read (st);
  102. while (size)
  103. {
  104. data_block = find_next_block ();
  105. if (! data_block)
  106. {
  107. ERROR ((0, 0, _("Unexpected EOF in archive")));
  108. return;
  109. }
  110. data_size = available_space_after (data_block);
  111. if (data_size > size)
  112. data_size = size;
  113. if (!(*processor) (data_size, data_block->buffer))
  114. processor = process_noop;
  115. set_next_block_after ((union block *)
  116. (data_block->buffer + data_size - 1));
  117. size -= data_size;
  118. mv_size_left (size);
  119. }
  120. mv_end ();
  121. }
  122. /* Call either stat or lstat over STAT_DATA, depending on
  123. --dereference (-h), for a file which should exist. Diagnose any
  124. problem. Return nonzero for success, zero otherwise. */
  125. static int
  126. get_stat_data (char const *file_name, struct stat *stat_data)
  127. {
  128. int status = deref_stat (file_name, stat_data);
  129. if (status != 0)
  130. {
  131. if (errno == ENOENT)
  132. stat_warn (file_name);
  133. else
  134. stat_error (file_name);
  135. report_difference (&current_stat_info, NULL);
  136. return 0;
  137. }
  138. return 1;
  139. }
  140. static void
  141. diff_dir (void)
  142. {
  143. struct stat stat_data;
  144. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  145. return;
  146. if (!S_ISDIR (stat_data.st_mode))
  147. report_difference (&current_stat_info, _("File type differs"));
  148. else if ((current_stat_info.stat.st_mode & MODE_ALL) !=
  149. (stat_data.st_mode & MODE_ALL))
  150. report_difference (&current_stat_info, _("Mode differs"));
  151. }
  152. static void
  153. diff_file (void)
  154. {
  155. char const *file_name = current_stat_info.file_name;
  156. struct stat stat_data;
  157. if (!get_stat_data (file_name, &stat_data))
  158. skip_member ();
  159. else if (!S_ISREG (stat_data.st_mode))
  160. {
  161. report_difference (&current_stat_info, _("File type differs"));
  162. skip_member ();
  163. }
  164. else
  165. {
  166. if ((current_stat_info.stat.st_mode & MODE_ALL) !=
  167. (stat_data.st_mode & MODE_ALL))
  168. report_difference (&current_stat_info, _("Mode differs"));
  169. if (!sys_compare_uid (&stat_data, &current_stat_info.stat))
  170. report_difference (&current_stat_info, _("Uid differs"));
  171. if (!sys_compare_gid (&stat_data, &current_stat_info.stat))
  172. report_difference (&current_stat_info, _("Gid differs"));
  173. if (tar_timespec_cmp (get_stat_mtime (&stat_data),
  174. current_stat_info.mtime))
  175. report_difference (&current_stat_info, _("Mod time differs"));
  176. if (current_header->header.typeflag != GNUTYPE_SPARSE
  177. && stat_data.st_size != current_stat_info.stat.st_size)
  178. {
  179. report_difference (&current_stat_info, _("Size differs"));
  180. skip_member ();
  181. }
  182. else
  183. {
  184. diff_handle = openat (chdir_fd, file_name, open_read_flags);
  185. if (diff_handle < 0)
  186. {
  187. open_error (file_name);
  188. skip_member ();
  189. report_difference (&current_stat_info, NULL);
  190. }
  191. else
  192. {
  193. int status;
  194. if (current_stat_info.is_sparse)
  195. sparse_diff_file (diff_handle, &current_stat_info);
  196. else
  197. read_and_process (&current_stat_info, process_rawdata);
  198. if (atime_preserve_option == replace_atime_preserve
  199. && stat_data.st_size != 0)
  200. {
  201. struct timespec atime = get_stat_atime (&stat_data);
  202. if (set_file_atime (diff_handle, chdir_fd, file_name, atime)
  203. != 0)
  204. utime_error (file_name);
  205. }
  206. status = close (diff_handle);
  207. if (status != 0)
  208. close_error (file_name);
  209. }
  210. }
  211. }
  212. }
  213. static void
  214. diff_link (void)
  215. {
  216. struct stat file_data;
  217. struct stat link_data;
  218. if (get_stat_data (current_stat_info.file_name, &file_data)
  219. && get_stat_data (current_stat_info.link_name, &link_data)
  220. && !sys_compare_links (&file_data, &link_data))
  221. report_difference (&current_stat_info,
  222. _("Not linked to %s"),
  223. quote_n_colon (QUOTE_ARG,
  224. 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. FALLTHROUGH;
  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. if (!mtioseek (true, -1) && rmtlseek (archive, 0, SEEK_SET) != 0)
  479. {
  480. /* Lseek failed. Try a different method. */
  481. seek_warn (archive_name_array[0]);
  482. return;
  483. }
  484. access_mode = ACCESS_READ;
  485. now_verifying = 1;
  486. flush_read ();
  487. while (1)
  488. {
  489. enum read_header status = read_header (&current_header,
  490. &current_stat_info,
  491. read_header_auto);
  492. if (status == HEADER_FAILURE)
  493. {
  494. int counter = 0;
  495. do
  496. {
  497. counter++;
  498. set_next_block_after (current_header);
  499. status = read_header (&current_header, &current_stat_info,
  500. read_header_auto);
  501. }
  502. while (status == HEADER_FAILURE);
  503. ERROR ((0, 0,
  504. ngettext ("VERIFY FAILURE: %d invalid header detected",
  505. "VERIFY FAILURE: %d invalid headers detected",
  506. counter), counter));
  507. }
  508. if (status == HEADER_END_OF_FILE)
  509. break;
  510. if (status == HEADER_ZERO_BLOCK)
  511. {
  512. set_next_block_after (current_header);
  513. if (!ignore_zeros_option)
  514. {
  515. char buf[UINTMAX_STRSIZE_BOUND];
  516. status = read_header (&current_header, &current_stat_info,
  517. read_header_auto);
  518. if (status == HEADER_ZERO_BLOCK)
  519. break;
  520. WARNOPT (WARN_ALONE_ZERO_BLOCK,
  521. (0, 0, _("A lone zero block at %s"),
  522. STRINGIFY_BIGINT (current_block_ordinal (), buf)));
  523. }
  524. continue;
  525. }
  526. decode_header (current_header, &current_stat_info, &current_format, 1);
  527. diff_archive ();
  528. tar_stat_destroy (&current_stat_info);
  529. }
  530. access_mode = ACCESS_WRITE;
  531. now_verifying = 0;
  532. }