compare.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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 (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 (NULL);
  82. }
  83. else
  84. {
  85. report_difference (ngettext ("Could only read %lu of %lu byte",
  86. "Could only read %lu of %lu bytes",
  87. bytes),
  88. (unsigned long) status, (unsigned long) bytes);
  89. }
  90. return 0;
  91. }
  92. if (memcmp (buffer, diff_buffer, bytes))
  93. {
  94. report_difference (_("Contents differ"));
  95. return 0;
  96. }
  97. return 1;
  98. }
  99. /* Directory contents, only for GNUTYPE_DUMPDIR. */
  100. static char *dumpdir_cursor;
  101. static int
  102. process_dumpdir (size_t bytes, char *buffer)
  103. {
  104. if (memcmp (buffer, dumpdir_cursor, bytes))
  105. {
  106. report_difference (_("Contents differ"));
  107. return 0;
  108. }
  109. dumpdir_cursor += bytes;
  110. return 1;
  111. }
  112. /* Some other routine wants SIZE bytes in the archive. For each chunk
  113. of the archive, call PROCESSOR with the size of the chunk, and the
  114. address of the chunk it can work with. The PROCESSOR should return
  115. nonzero for success. It it return error once, continue skipping
  116. without calling PROCESSOR anymore. */
  117. static void
  118. read_and_process (off_t size, int (*processor) (size_t, char *))
  119. {
  120. union block *data_block;
  121. size_t data_size;
  122. if (multi_volume_option)
  123. save_sizeleft = size;
  124. while (size)
  125. {
  126. data_block = find_next_block ();
  127. if (! data_block)
  128. {
  129. ERROR ((0, 0, _("Unexpected EOF in archive")));
  130. return;
  131. }
  132. data_size = available_space_after (data_block);
  133. if (data_size > size)
  134. data_size = size;
  135. if (!(*processor) (data_size, data_block->buffer))
  136. processor = process_noop;
  137. set_next_block_after ((union block *)
  138. (data_block->buffer + data_size - 1));
  139. size -= data_size;
  140. if (multi_volume_option)
  141. save_sizeleft -= data_size;
  142. }
  143. }
  144. static void
  145. diff_sparse_files (void)
  146. {
  147. /*FIXME!!*/abort();
  148. }
  149. /* Call either stat or lstat over STAT_DATA, depending on
  150. --dereference (-h), for a file which should exist. Diagnose any
  151. problem. Return nonzero for success, zero otherwise. */
  152. static int
  153. get_stat_data (char const *file_name, struct stat *stat_data)
  154. {
  155. int status = deref_stat (dereference_option, file_name, stat_data);
  156. if (status != 0)
  157. {
  158. if (errno == ENOENT)
  159. stat_warn (file_name);
  160. else
  161. stat_error (file_name);
  162. report_difference (NULL);
  163. return 0;
  164. }
  165. return 1;
  166. }
  167. /* Diff a file against the archive. */
  168. void
  169. diff_archive (void)
  170. {
  171. struct stat stat_data;
  172. int status;
  173. struct utimbuf restore_times;
  174. set_next_block_after (current_header);
  175. decode_header (current_header, &current_stat_info, &current_format, 1);
  176. /* Print the block from current_header and current_stat_info. */
  177. if (verbose_option)
  178. {
  179. if (now_verifying)
  180. fprintf (stdlis, _("Verify "));
  181. print_header (&current_stat_info, -1);
  182. }
  183. switch (current_header->header.typeflag)
  184. {
  185. default:
  186. ERROR ((0, 0, _("%s: Unknown file type '%c', diffed as normal file"),
  187. quotearg_colon (current_stat_info.file_name),
  188. current_header->header.typeflag));
  189. /* Fall through. */
  190. case AREGTYPE:
  191. case REGTYPE:
  192. case GNUTYPE_SPARSE:
  193. case CONTTYPE:
  194. /* Appears to be a file. See if it's really a directory. */
  195. if (current_stat_info.had_trailing_slash)
  196. goto really_dir;
  197. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  198. {
  199. skip_member ();
  200. goto quit;
  201. }
  202. if (!S_ISREG (stat_data.st_mode))
  203. {
  204. report_difference (_("File type differs"));
  205. skip_member ();
  206. goto quit;
  207. }
  208. if ((current_stat_info.stat.st_mode & MODE_ALL) != (stat_data.st_mode & MODE_ALL))
  209. report_difference (_("Mode differs"));
  210. sys_compare_uid_gid (&stat_data, &current_stat_info.stat);
  211. if (stat_data.st_mtime != current_stat_info.stat.st_mtime)
  212. report_difference (_("Mod time differs"));
  213. if (current_header->header.typeflag != GNUTYPE_SPARSE &&
  214. stat_data.st_size != current_stat_info.stat.st_size)
  215. {
  216. report_difference (_("Size differs"));
  217. skip_member ();
  218. goto quit;
  219. }
  220. diff_handle = open (current_stat_info.file_name, O_RDONLY | O_BINARY);
  221. if (diff_handle < 0)
  222. {
  223. open_error (current_stat_info.file_name);
  224. skip_member ();
  225. report_difference (NULL);
  226. goto quit;
  227. }
  228. restore_times.actime = stat_data.st_atime;
  229. restore_times.modtime = stat_data.st_mtime;
  230. /* Need to treat sparse files completely differently here. */
  231. if (current_header->header.typeflag == GNUTYPE_SPARSE)
  232. diff_sparse_files ();
  233. else
  234. {
  235. if (multi_volume_option)
  236. {
  237. assign_string (&save_name, current_stat_info.file_name);
  238. save_totsize = current_stat_info.stat.st_size;
  239. /* save_sizeleft is set in read_and_process. */
  240. }
  241. read_and_process (current_stat_info.stat.st_size, process_rawdata);
  242. if (multi_volume_option)
  243. assign_string (&save_name, 0);
  244. }
  245. status = close (diff_handle);
  246. if (status != 0)
  247. close_error (current_stat_info.file_name);
  248. if (atime_preserve_option)
  249. utime (current_stat_info.file_name, &restore_times);
  250. quit:
  251. break;
  252. case LNKTYPE:
  253. {
  254. struct stat link_data, stat_data;
  255. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  256. break;
  257. if (!get_stat_data (current_stat_info.link_name, &link_data))
  258. break;
  259. sys_compare_links (&stat_data, &link_data);
  260. }
  261. break;
  262. #ifdef HAVE_READLINK
  263. case SYMTYPE:
  264. {
  265. size_t len = strlen (current_stat_info.link_name);
  266. char *linkbuf = alloca (len + 1);
  267. status = readlink (current_stat_info.file_name, linkbuf, len + 1);
  268. if (status < 0)
  269. {
  270. if (errno == ENOENT)
  271. readlink_warn (current_stat_info.file_name);
  272. else
  273. readlink_error (current_stat_info.file_name);
  274. report_difference (NULL);
  275. }
  276. else if (status != len
  277. || strncmp (current_stat_info.link_name, linkbuf, len) != 0)
  278. report_difference (_("Symlink differs"));
  279. break;
  280. }
  281. #endif
  282. case CHRTYPE:
  283. case BLKTYPE:
  284. case FIFOTYPE:
  285. /* FIXME: deal with umask. */
  286. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  287. break;
  288. if (current_header->header.typeflag == CHRTYPE
  289. ? !S_ISCHR (stat_data.st_mode)
  290. : current_header->header.typeflag == BLKTYPE
  291. ? !S_ISBLK (stat_data.st_mode)
  292. : /* current_header->header.typeflag == FIFOTYPE */
  293. !S_ISFIFO (stat_data.st_mode))
  294. {
  295. report_difference (_("File type differs"));
  296. break;
  297. }
  298. if ((current_header->header.typeflag == CHRTYPE
  299. || current_header->header.typeflag == BLKTYPE)
  300. && current_stat_info.stat.st_rdev != stat_data.st_rdev)
  301. {
  302. report_difference (_("Device number differs"));
  303. break;
  304. }
  305. if ((current_stat_info.stat.st_mode & MODE_ALL) != (stat_data.st_mode & MODE_ALL))
  306. {
  307. report_difference (_("Mode differs"));
  308. break;
  309. }
  310. break;
  311. case GNUTYPE_DUMPDIR:
  312. {
  313. char *dumpdir_buffer = get_directory_contents (current_stat_info.file_name, 0);
  314. if (multi_volume_option)
  315. {
  316. assign_string (&save_name, current_stat_info.file_name);
  317. save_totsize = current_stat_info.stat.st_size;
  318. /* save_sizeleft is set in read_and_process. */
  319. }
  320. if (dumpdir_buffer)
  321. {
  322. dumpdir_cursor = dumpdir_buffer;
  323. read_and_process (current_stat_info.stat.st_size, process_dumpdir);
  324. free (dumpdir_buffer);
  325. }
  326. else
  327. read_and_process (current_stat_info.stat.st_size, process_noop);
  328. if (multi_volume_option)
  329. assign_string (&save_name, 0);
  330. /* Fall through. */
  331. }
  332. case DIRTYPE:
  333. really_dir:
  334. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  335. break;
  336. if (!S_ISDIR (stat_data.st_mode))
  337. {
  338. report_difference (_("File type differs"));
  339. break;
  340. }
  341. if ((current_stat_info.stat.st_mode & MODE_ALL) != (stat_data.st_mode & MODE_ALL))
  342. {
  343. report_difference (_("Mode differs"));
  344. break;
  345. }
  346. break;
  347. case GNUTYPE_VOLHDR:
  348. break;
  349. case GNUTYPE_MULTIVOL:
  350. {
  351. off_t offset;
  352. if (current_stat_info.had_trailing_slash)
  353. goto really_dir;
  354. if (!get_stat_data (current_stat_info.file_name, &stat_data))
  355. break;
  356. if (!S_ISREG (stat_data.st_mode))
  357. {
  358. report_difference (_("File type differs"));
  359. skip_member ();
  360. break;
  361. }
  362. offset = OFF_FROM_HEADER (current_header->oldgnu_header.offset);
  363. if (stat_data.st_size != current_stat_info.stat.st_size + offset)
  364. {
  365. report_difference (_("Size differs"));
  366. skip_member ();
  367. break;
  368. }
  369. diff_handle = open (current_stat_info.file_name, O_RDONLY | O_BINARY);
  370. if (diff_handle < 0)
  371. {
  372. open_error (current_stat_info.file_name);
  373. report_difference (NULL);
  374. skip_member ();
  375. break;
  376. }
  377. if (lseek (diff_handle, offset, SEEK_SET) < 0)
  378. {
  379. seek_error_details (current_stat_info.file_name, offset);
  380. report_difference (NULL);
  381. break;
  382. }
  383. if (multi_volume_option)
  384. {
  385. assign_string (&save_name, current_stat_info.file_name);
  386. save_totsize = stat_data.st_size;
  387. /* save_sizeleft is set in read_and_process. */
  388. }
  389. read_and_process (current_stat_info.stat.st_size, process_rawdata);
  390. if (multi_volume_option)
  391. assign_string (&save_name, 0);
  392. status = close (diff_handle);
  393. if (status != 0)
  394. close_error (current_stat_info.file_name);
  395. break;
  396. }
  397. }
  398. }
  399. void
  400. verify_volume (void)
  401. {
  402. if (!diff_buffer)
  403. diff_init ();
  404. /* Verifying an archive is meant to check if the physical media got it
  405. correctly, so try to defeat clever in-memory buffering pertaining to
  406. this particular media. On Linux, for example, the floppy drive would
  407. not even be accessed for the whole verification.
  408. The code was using fsync only when the ioctl is unavailable, but
  409. Marty Leisner says that the ioctl does not work when not preceded by
  410. fsync. So, until we know better, or maybe to please Marty, let's do it
  411. the unbelievable way :-). */
  412. #if HAVE_FSYNC
  413. fsync (archive);
  414. #endif
  415. #ifdef FDFLUSH
  416. ioctl (archive, FDFLUSH);
  417. #endif
  418. #ifdef MTIOCTOP
  419. {
  420. struct mtop operation;
  421. int status;
  422. operation.mt_op = MTBSF;
  423. operation.mt_count = 1;
  424. if (status = rmtioctl (archive, MTIOCTOP, (char *) &operation), status < 0)
  425. {
  426. if (errno != EIO
  427. || (status = rmtioctl (archive, MTIOCTOP, (char *) &operation),
  428. status < 0))
  429. {
  430. #endif
  431. if (rmtlseek (archive, (off_t) 0, SEEK_SET) != 0)
  432. {
  433. /* Lseek failed. Try a different method. */
  434. seek_warn (archive_name_array[0]);
  435. return;
  436. }
  437. #ifdef MTIOCTOP
  438. }
  439. }
  440. }
  441. #endif
  442. access_mode = ACCESS_READ;
  443. now_verifying = 1;
  444. flush_read ();
  445. while (1)
  446. {
  447. enum read_header status = read_header (false);
  448. if (status == HEADER_FAILURE)
  449. {
  450. int counter = 0;
  451. do
  452. {
  453. counter++;
  454. status = read_header (false);
  455. }
  456. while (status == HEADER_FAILURE);
  457. ERROR ((0, 0,
  458. ngettext ("VERIFY FAILURE: %d invalid header detected",
  459. "VERIFY FAILURE: %d invalid headers detected",
  460. counter), counter));
  461. }
  462. if (status == HEADER_ZERO_BLOCK || status == HEADER_END_OF_FILE)
  463. break;
  464. diff_archive ();
  465. }
  466. access_mode = ACCESS_WRITE;
  467. now_verifying = 0;
  468. }