compare.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /* Diff files from a tar archive.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
  3. 2003 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. diff_buffer = valloc (record_size);
  44. if (!diff_buffer)
  45. xalloc_die ();
  46. }
  47. /* Sigh about something that differs by writing a MESSAGE to stdlis,
  48. given MESSAGE is nonzero. Also set the exit status if not already. */
  49. void
  50. report_difference (struct tar_stat_info *st, const char *fmt, ...)
  51. {
  52. if (fmt)
  53. {
  54. va_list ap;
  55. fprintf (stdlis, "%s: ", quotearg_colon (current_stat_info.file_name));
  56. va_start (ap, fmt);
  57. vfprintf (stdlis, fmt, ap);
  58. va_end (ap);
  59. fprintf (stdlis, "\n");
  60. }
  61. if (exit_status == TAREXIT_SUCCESS)
  62. exit_status = TAREXIT_DIFFERS;
  63. }
  64. /* Take a buffer returned by read_and_process and do nothing with it. */
  65. static int
  66. process_noop (size_t size, char *data)
  67. {
  68. /* Yes, I know. SIZE and DATA are unused in this function. Some
  69. compilers may even report it. That's OK, just relax! */
  70. return 1;
  71. }
  72. static int
  73. process_rawdata (size_t bytes, char *buffer)
  74. {
  75. ssize_t status = safe_read (diff_handle, diff_buffer, bytes);
  76. if (status != bytes)
  77. {
  78. if (status < 0)
  79. {
  80. read_error (current_stat_info.file_name);
  81. report_difference (&current_stat_info, NULL);
  82. }
  83. else
  84. {
  85. report_difference (&current_stat_info,
  86. ngettext ("Could only read %lu of %lu byte",
  87. "Could only read %lu of %lu bytes",
  88. bytes),
  89. (unsigned long) status, (unsigned long) bytes);
  90. }
  91. return 0;
  92. }
  93. if (memcmp (buffer, diff_buffer, bytes))
  94. {
  95. report_difference (&current_stat_info,
  96. _("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. /* Diff a file against the archive. */
  165. void
  166. diff_archive (void)
  167. {
  168. struct stat stat_data;
  169. int status;
  170. struct utimbuf restore_times;
  171. set_next_block_after (current_header);
  172. decode_header (current_header, &current_stat_info, &current_format, 1);
  173. /* Print the block from current_header and current_stat_info. */
  174. if (verbose_option)
  175. {
  176. if (now_verifying)
  177. fprintf (stdlis, _("Verify "));
  178. print_header (&current_stat_info, -1);
  179. }
  180. switch (current_header->header.typeflag)
  181. {
  182. default:
  183. ERROR ((0, 0, _("%s: Unknown file type '%c', diffed as normal file"),
  184. quotearg_colon (current_stat_info.file_name),
  185. current_header->header.typeflag));
  186. /* Fall through. */
  187. case AREGTYPE:
  188. case REGTYPE:
  189. case GNUTYPE_SPARSE:
  190. case CONTTYPE:
  191. /* Appears to be a file. See if it's really a directory. */
  192. if (current_stat_info.had_trailing_slash)
  193. goto really_dir;
  194. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  195. {
  196. skip_member ();
  197. goto quit;
  198. }
  199. if (!S_ISREG (stat_data.st_mode))
  200. {
  201. report_difference (&current_stat_info, _("File type differs"));
  202. skip_member ();
  203. goto quit;
  204. }
  205. if ((current_stat_info.stat.st_mode & MODE_ALL) !=
  206. (stat_data.st_mode & MODE_ALL))
  207. report_difference (&current_stat_info, _("Mode differs"));
  208. if (!sys_compare_uid (&stat_data, &current_stat_info.stat))
  209. report_difference (&current_stat_info, _("Uid differs"));
  210. if (!sys_compare_gid (&stat_data, &current_stat_info.stat))
  211. report_difference (&current_stat_info, _("Gid differs"));
  212. if (stat_data.st_mtime != current_stat_info.stat.st_mtime)
  213. report_difference (&current_stat_info, _("Mod time differs"));
  214. if (current_header->header.typeflag != GNUTYPE_SPARSE &&
  215. stat_data.st_size != current_stat_info.stat.st_size)
  216. {
  217. report_difference (&current_stat_info, _("Size differs"));
  218. skip_member ();
  219. goto quit;
  220. }
  221. diff_handle = open (current_stat_info.file_name, O_RDONLY | O_BINARY);
  222. if (diff_handle < 0)
  223. {
  224. open_error (current_stat_info.file_name);
  225. skip_member ();
  226. report_difference (&current_stat_info, NULL);
  227. goto quit;
  228. }
  229. restore_times.actime = stat_data.st_atime;
  230. restore_times.modtime = stat_data.st_mtime;
  231. /* Need to treat sparse files completely differently here. */
  232. if (current_header->header.typeflag == GNUTYPE_SPARSE)
  233. sparse_diff_file (diff_handle, &current_stat_info);
  234. else
  235. {
  236. if (multi_volume_option)
  237. {
  238. assign_string (&save_name, current_stat_info.file_name);
  239. save_totsize = current_stat_info.stat.st_size;
  240. /* save_sizeleft is set in read_and_process. */
  241. }
  242. read_and_process (current_stat_info.stat.st_size, process_rawdata);
  243. if (multi_volume_option)
  244. assign_string (&save_name, 0);
  245. }
  246. status = close (diff_handle);
  247. if (status != 0)
  248. close_error (current_stat_info.file_name);
  249. if (atime_preserve_option)
  250. utime (current_stat_info.file_name, &restore_times);
  251. quit:
  252. break;
  253. case LNKTYPE:
  254. {
  255. struct stat link_data, stat_data;
  256. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  257. break;
  258. if (!get_stat_data (current_stat_info.link_name, &link_data))
  259. break;
  260. if (!sys_compare_links (&stat_data, &link_data))
  261. report_difference (&current_stat_info,
  262. _("Not linked to %s"),
  263. quote (current_stat_info.link_name));
  264. }
  265. break;
  266. #ifdef HAVE_READLINK
  267. case SYMTYPE:
  268. {
  269. size_t len = strlen (current_stat_info.link_name);
  270. char *linkbuf = alloca (len + 1);
  271. status = readlink (current_stat_info.file_name, linkbuf, len + 1);
  272. if (status < 0)
  273. {
  274. if (errno == ENOENT)
  275. readlink_warn (current_stat_info.file_name);
  276. else
  277. readlink_error (current_stat_info.file_name);
  278. report_difference (&current_stat_info, NULL);
  279. }
  280. else if (status != len
  281. || strncmp (current_stat_info.link_name, linkbuf, len) != 0)
  282. report_difference (&current_stat_info, _("Symlink differs"));
  283. break;
  284. }
  285. #endif
  286. case CHRTYPE:
  287. case BLKTYPE:
  288. case FIFOTYPE:
  289. /* FIXME: deal with umask. */
  290. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  291. break;
  292. if (current_header->header.typeflag == CHRTYPE
  293. ? !S_ISCHR (stat_data.st_mode)
  294. : current_header->header.typeflag == BLKTYPE
  295. ? !S_ISBLK (stat_data.st_mode)
  296. : /* current_header->header.typeflag == FIFOTYPE */
  297. !S_ISFIFO (stat_data.st_mode))
  298. {
  299. report_difference (&current_stat_info, _("File type differs"));
  300. break;
  301. }
  302. if ((current_header->header.typeflag == CHRTYPE
  303. || current_header->header.typeflag == BLKTYPE)
  304. && current_stat_info.stat.st_rdev != stat_data.st_rdev)
  305. {
  306. report_difference (&current_stat_info, _("Device number differs"));
  307. break;
  308. }
  309. if ((current_stat_info.stat.st_mode & MODE_ALL) != (stat_data.st_mode & MODE_ALL))
  310. {
  311. report_difference (&current_stat_info, _("Mode differs"));
  312. break;
  313. }
  314. break;
  315. case GNUTYPE_DUMPDIR:
  316. {
  317. char *dumpdir_buffer = get_directory_contents (current_stat_info.file_name, 0);
  318. if (multi_volume_option)
  319. {
  320. assign_string (&save_name, current_stat_info.file_name);
  321. save_totsize = current_stat_info.stat.st_size;
  322. /* save_sizeleft is set in read_and_process. */
  323. }
  324. if (dumpdir_buffer)
  325. {
  326. dumpdir_cursor = dumpdir_buffer;
  327. read_and_process (current_stat_info.stat.st_size, process_dumpdir);
  328. free (dumpdir_buffer);
  329. }
  330. else
  331. read_and_process (current_stat_info.stat.st_size, process_noop);
  332. if (multi_volume_option)
  333. assign_string (&save_name, 0);
  334. /* Fall through. */
  335. }
  336. case DIRTYPE:
  337. really_dir:
  338. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  339. break;
  340. if (!S_ISDIR (stat_data.st_mode))
  341. {
  342. report_difference (&current_stat_info, _("File type differs"));
  343. break;
  344. }
  345. if ((current_stat_info.stat.st_mode & MODE_ALL) != (stat_data.st_mode & MODE_ALL))
  346. {
  347. report_difference (&current_stat_info, _("Mode differs"));
  348. break;
  349. }
  350. break;
  351. case GNUTYPE_VOLHDR:
  352. break;
  353. case GNUTYPE_MULTIVOL:
  354. {
  355. off_t offset;
  356. if (current_stat_info.had_trailing_slash)
  357. goto really_dir;
  358. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  359. break;
  360. if (!S_ISREG (stat_data.st_mode))
  361. {
  362. report_difference (&current_stat_info, _("File type differs"));
  363. skip_member ();
  364. break;
  365. }
  366. offset = OFF_FROM_HEADER (current_header->oldgnu_header.offset);
  367. if (stat_data.st_size != current_stat_info.stat.st_size + offset)
  368. {
  369. report_difference (&current_stat_info, _("Size differs"));
  370. skip_member ();
  371. break;
  372. }
  373. diff_handle = open (current_stat_info.file_name, O_RDONLY | O_BINARY);
  374. if (diff_handle < 0)
  375. {
  376. open_error (current_stat_info.file_name);
  377. report_difference (&current_stat_info, NULL);
  378. skip_member ();
  379. break;
  380. }
  381. if (lseek (diff_handle, offset, SEEK_SET) < 0)
  382. {
  383. seek_error_details (current_stat_info.file_name, offset);
  384. report_difference (&current_stat_info, NULL);
  385. break;
  386. }
  387. if (multi_volume_option)
  388. {
  389. assign_string (&save_name, current_stat_info.file_name);
  390. save_totsize = stat_data.st_size;
  391. /* save_sizeleft is set in read_and_process. */
  392. }
  393. read_and_process (current_stat_info.stat.st_size, process_rawdata);
  394. if (multi_volume_option)
  395. assign_string (&save_name, 0);
  396. status = close (diff_handle);
  397. if (status != 0)
  398. close_error (current_stat_info.file_name);
  399. break;
  400. }
  401. }
  402. }
  403. void
  404. verify_volume (void)
  405. {
  406. if (!diff_buffer)
  407. diff_init ();
  408. /* Verifying an archive is meant to check if the physical media got it
  409. correctly, so try to defeat clever in-memory buffering pertaining to
  410. this particular media. On Linux, for example, the floppy drive would
  411. not even be accessed for the whole verification.
  412. The code was using fsync only when the ioctl is unavailable, but
  413. Marty Leisner says that the ioctl does not work when not preceded by
  414. fsync. So, until we know better, or maybe to please Marty, let's do it
  415. the unbelievable way :-). */
  416. #if HAVE_FSYNC
  417. fsync (archive);
  418. #endif
  419. #ifdef FDFLUSH
  420. ioctl (archive, FDFLUSH);
  421. #endif
  422. #ifdef MTIOCTOP
  423. {
  424. struct mtop operation;
  425. int status;
  426. operation.mt_op = MTBSF;
  427. operation.mt_count = 1;
  428. if (status = rmtioctl (archive, MTIOCTOP, (char *) &operation), status < 0)
  429. {
  430. if (errno != EIO
  431. || (status = rmtioctl (archive, MTIOCTOP, (char *) &operation),
  432. status < 0))
  433. {
  434. #endif
  435. if (rmtlseek (archive, (off_t) 0, SEEK_SET) != 0)
  436. {
  437. /* Lseek failed. Try a different method. */
  438. seek_warn (archive_name_array[0]);
  439. return;
  440. }
  441. #ifdef MTIOCTOP
  442. }
  443. }
  444. }
  445. #endif
  446. access_mode = ACCESS_READ;
  447. now_verifying = 1;
  448. flush_read ();
  449. while (1)
  450. {
  451. enum read_header status = read_header (false);
  452. if (status == HEADER_FAILURE)
  453. {
  454. int counter = 0;
  455. do
  456. {
  457. counter++;
  458. status = read_header (false);
  459. }
  460. while (status == HEADER_FAILURE);
  461. ERROR ((0, 0,
  462. ngettext ("VERIFY FAILURE: %d invalid header detected",
  463. "VERIFY FAILURE: %d invalid headers detected",
  464. counter), counter));
  465. }
  466. if (status == HEADER_ZERO_BLOCK || status == HEADER_END_OF_FILE)
  467. break;
  468. diff_archive ();
  469. }
  470. access_mode = ACCESS_WRITE;
  471. now_verifying = 0;
  472. }