compare.c 15 KB

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