compare.c 14 KB

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