4
0

compare.c 15 KB

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