compare.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /* Diff files from a tar archive.
  2. Copyright (C) 1988, 92, 93, 94, 96, 97 Free Software Foundation, Inc.
  3. Written by John Gilmore, on 1987-04-30.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 59 Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "system.h"
  16. #if HAVE_LINUX_FD_H
  17. # include <linux/fd.h>
  18. #endif
  19. #include "common.h"
  20. #include "rmt.h"
  21. /* Spare space for messages, hopefully safe even after gettext. */
  22. #define MESSAGE_BUFFER_SIZE 100
  23. /* Nonzero if we are verifying at the moment. */
  24. int now_verifying = 0;
  25. /* File descriptor for the file we are diffing. */
  26. static int diff_handle;
  27. /* Area for reading file contents into. */
  28. static char *diff_buffer = NULL;
  29. /*--------------------------------.
  30. | Initialize for a diff operation |
  31. `--------------------------------*/
  32. void
  33. diff_init (void)
  34. {
  35. diff_buffer = (char *) valloc ((unsigned) record_size);
  36. if (!diff_buffer)
  37. FATAL_ERROR ((0, 0,
  38. _("Could not allocate memory for diff buffer of %d bytes"),
  39. record_size));
  40. }
  41. /*------------------------------------------------------------------------.
  42. | Sigh about something that differs by writing a MESSAGE to stdlis, given |
  43. | MESSAGE is not NULL. Also set the exit status if not already. |
  44. `------------------------------------------------------------------------*/
  45. static void
  46. report_difference (const char *message)
  47. {
  48. if (message)
  49. fprintf (stdlis, "%s: %s\n", current_file_name, message);
  50. if (exit_status == TAREXIT_SUCCESS)
  51. exit_status = TAREXIT_DIFFERS;
  52. }
  53. /*-----------------------------------------------------------------------.
  54. | Takes a buffer returned by read_and_process and does nothing with it. |
  55. `-----------------------------------------------------------------------*/
  56. /* Yes, I know. SIZE and DATA are unused in this function. Some compilers
  57. may even report it. That's OK, just relax! */
  58. static int
  59. process_noop (long size, char *data)
  60. {
  61. return 1;
  62. }
  63. /*---.
  64. | ? |
  65. `---*/
  66. static int
  67. process_rawdata (long bytes, char *buffer)
  68. {
  69. int status = read (diff_handle, diff_buffer, (size_t) bytes);
  70. char message[MESSAGE_BUFFER_SIZE];
  71. if (status != bytes)
  72. {
  73. if (status < 0)
  74. {
  75. WARN ((0, errno, _("Cannot read %s"), current_file_name));
  76. report_difference (NULL);
  77. }
  78. else
  79. {
  80. sprintf (message, _("Could only read %d of %ld bytes"),
  81. status, bytes);
  82. report_difference (message);
  83. }
  84. return 0;
  85. }
  86. if (memcmp (buffer, diff_buffer, (size_t) bytes))
  87. {
  88. report_difference (_("Data differs"));
  89. return 0;
  90. }
  91. return 1;
  92. }
  93. /*---.
  94. | ? |
  95. `---*/
  96. /* Directory contents, only for GNUTYPE_DUMPDIR. */
  97. static char *dumpdir_cursor;
  98. static int
  99. process_dumpdir (long bytes, char *buffer)
  100. {
  101. if (memcmp (buffer, dumpdir_cursor, (size_t) bytes))
  102. {
  103. report_difference (_("Data differs"));
  104. return 0;
  105. }
  106. dumpdir_cursor += bytes;
  107. return 1;
  108. }
  109. /*------------------------------------------------------------------------.
  110. | Some other routine wants SIZE bytes in the archive. For each chunk of |
  111. | the archive, call PROCESSOR with the size of the chunk, and the address |
  112. | of the chunk it can work with. The PROCESSOR should return nonzero for |
  113. | success. It it return error once, continue skipping without calling |
  114. | PROCESSOR anymore. |
  115. `------------------------------------------------------------------------*/
  116. static void
  117. read_and_process (long size, int (*processor) (long, char *))
  118. {
  119. union block *data_block;
  120. long data_size;
  121. if (multi_volume_option)
  122. save_sizeleft = size;
  123. while (size)
  124. {
  125. data_block = find_next_block ();
  126. if (data_block == NULL)
  127. {
  128. ERROR ((0, 0, _("Unexpected EOF on archive file")));
  129. return;
  130. }
  131. data_size = available_space_after (data_block);
  132. if (data_size > size)
  133. data_size = size;
  134. if (!(*processor) (data_size, data_block->buffer))
  135. processor = process_noop;
  136. set_next_block_after ((union block *)
  137. (data_block->buffer + data_size - 1));
  138. size -= data_size;
  139. if (multi_volume_option)
  140. save_sizeleft -= data_size;
  141. }
  142. }
  143. /*---.
  144. | ? |
  145. `---*/
  146. /* JK This routine should be used more often than it is ... look into
  147. that. Anyhow, what it does is translate the sparse information on the
  148. header, and in any subsequent extended headers, into an array of
  149. structures with true numbers, as opposed to character strings. It
  150. simply makes our life much easier, doing so many comparisong and such.
  151. */
  152. static void
  153. fill_in_sparse_array (void)
  154. {
  155. int counter;
  156. /* Allocate space for our scratch space; it's initially 10 elements
  157. long, but can change in this routine if necessary. */
  158. sp_array_size = 10;
  159. sparsearray = (struct sp_array *) xmalloc (sp_array_size * sizeof (struct sp_array));
  160. /* There are at most five of these structures in the header itself;
  161. read these in first. */
  162. for (counter = 0; counter < SPARSES_IN_OLDGNU_HEADER; counter++)
  163. {
  164. /* Compare to 0, or use !(int)..., for Pyramid's dumb compiler. */
  165. if (current_header->oldgnu_header.sp[counter].numbytes == 0)
  166. break;
  167. sparsearray[counter].offset =
  168. from_oct (1 + 12, current_header->oldgnu_header.sp[counter].offset);
  169. sparsearray[counter].numbytes =
  170. from_oct (1 + 12, current_header->oldgnu_header.sp[counter].numbytes);
  171. }
  172. /* If the header's extended, we gotta read in exhdr's till we're done. */
  173. if (current_header->oldgnu_header.isextended)
  174. {
  175. /* How far into the sparsearray we are `so far'. */
  176. static int so_far_ind = SPARSES_IN_OLDGNU_HEADER;
  177. union block *exhdr;
  178. while (1)
  179. {
  180. exhdr = find_next_block ();
  181. for (counter = 0; counter < SPARSES_IN_SPARSE_HEADER; counter++)
  182. {
  183. if (counter + so_far_ind > sp_array_size - 1)
  184. {
  185. /* We just ran out of room in our scratch area -
  186. realloc it. */
  187. sp_array_size *= 2;
  188. sparsearray = (struct sp_array *)
  189. xrealloc (sparsearray,
  190. sp_array_size * sizeof (struct sp_array));
  191. }
  192. /* Convert the character strings into longs. */
  193. sparsearray[counter + so_far_ind].offset =
  194. from_oct (1 + 12, exhdr->sparse_header.sp[counter].offset);
  195. sparsearray[counter + so_far_ind].numbytes =
  196. from_oct (1 + 12, exhdr->sparse_header.sp[counter].numbytes);
  197. }
  198. /* If this is the last extended header for this file, we can
  199. stop. */
  200. if (!exhdr->sparse_header.isextended)
  201. break;
  202. so_far_ind += SPARSES_IN_SPARSE_HEADER;
  203. set_next_block_after (exhdr);
  204. }
  205. /* Be sure to skip past the last one. */
  206. set_next_block_after (exhdr);
  207. }
  208. }
  209. /*---.
  210. | ? |
  211. `---*/
  212. /* JK Diff'ing a sparse file with its counterpart on the tar file is a
  213. bit of a different story than a normal file. First, we must know what
  214. areas of the file to skip through, i.e., we need to contruct a
  215. sparsearray, which will hold all the information we need. We must
  216. compare small amounts of data at a time as we find it. */
  217. /* FIXME: This does not look very solid to me, at first glance. Zero areas
  218. are not checked, spurious sparse entries seemingly goes undetected, and
  219. I'm not sure overall identical sparsity is verified. */
  220. static void
  221. diff_sparse_files (int size_of_file)
  222. {
  223. int remaining_size = size_of_file;
  224. char *buffer = (char *) xmalloc (BLOCKSIZE * sizeof (char));
  225. int buffer_size = BLOCKSIZE;
  226. union block *data_block = NULL;
  227. int counter = 0;
  228. int different = 0;
  229. fill_in_sparse_array ();
  230. while (remaining_size > 0)
  231. {
  232. int status;
  233. long chunk_size;
  234. #if 0
  235. int amount_read = 0;
  236. #endif
  237. data_block = find_next_block ();
  238. chunk_size = sparsearray[counter].numbytes;
  239. if (!chunk_size)
  240. break;
  241. lseek (diff_handle, sparsearray[counter].offset, 0);
  242. /* Take care to not run out of room in our buffer. */
  243. while (buffer_size < chunk_size)
  244. {
  245. buffer_size *= 2;
  246. buffer = (char *) xrealloc (buffer, buffer_size * sizeof (char));
  247. }
  248. while (chunk_size > BLOCKSIZE)
  249. {
  250. if (status = read (diff_handle, buffer, BLOCKSIZE),
  251. status != BLOCKSIZE)
  252. {
  253. if (status < 0)
  254. {
  255. WARN ((0, errno, _("Cannot read %s"), current_file_name));
  256. report_difference (NULL);
  257. }
  258. else
  259. {
  260. char message[MESSAGE_BUFFER_SIZE];
  261. sprintf (message, _("Could only read %d of %ld bytes"),
  262. status, chunk_size);
  263. report_difference (message);
  264. }
  265. break;
  266. }
  267. if (memcmp (buffer, data_block->buffer, BLOCKSIZE))
  268. {
  269. different = 1;
  270. break;
  271. }
  272. chunk_size -= status;
  273. remaining_size -= status;
  274. set_next_block_after (data_block);
  275. data_block = find_next_block ();
  276. }
  277. if (status = read (diff_handle, buffer, (size_t) chunk_size),
  278. status != chunk_size)
  279. {
  280. if (status < 0)
  281. {
  282. WARN ((0, errno, _("Cannot read %s"), current_file_name));
  283. report_difference (NULL);
  284. }
  285. else
  286. {
  287. char message[MESSAGE_BUFFER_SIZE];
  288. sprintf (message, _("Could only read %d of %ld bytes"),
  289. status, chunk_size);
  290. report_difference (message);
  291. }
  292. break;
  293. }
  294. if (memcmp (buffer, data_block->buffer, (size_t) chunk_size))
  295. {
  296. different = 1;
  297. break;
  298. }
  299. #if 0
  300. amount_read += chunk_size;
  301. if (amount_read >= BLOCKSIZE)
  302. {
  303. amount_read = 0;
  304. set_next_block_after (data_block);
  305. data_block = find_next_block ();
  306. }
  307. #endif
  308. set_next_block_after (data_block);
  309. counter++;
  310. remaining_size -= chunk_size;
  311. }
  312. #if 0
  313. /* If the number of bytes read isn't the number of bytes supposedly in
  314. the file, they're different. */
  315. if (amount_read != size_of_file)
  316. different = 1;
  317. #endif
  318. set_next_block_after (data_block);
  319. free (sparsearray);
  320. if (different)
  321. report_difference (_("Data differs"));
  322. }
  323. /*---------------------------------------------------------------------.
  324. | Call either stat or lstat over STAT_DATA, depending on --dereference |
  325. | (-h), for a file which should exist. Diagnose any problem. Return |
  326. | nonzero for success, zero otherwise. |
  327. `---------------------------------------------------------------------*/
  328. static int
  329. get_stat_data (struct stat *stat_data)
  330. {
  331. int status = (dereference_option
  332. ? stat (current_file_name, stat_data)
  333. : lstat (current_file_name, stat_data));
  334. if (status < 0)
  335. {
  336. if (errno == ENOENT)
  337. report_difference (_("File does not exist"));
  338. else
  339. {
  340. ERROR ((0, errno, _("Cannot stat file %s"), current_file_name));
  341. report_difference (NULL);
  342. }
  343. #if 0
  344. skip_file ((long) current_stat.st_size);
  345. #endif
  346. return 0;
  347. }
  348. return 1;
  349. }
  350. /*----------------------------------.
  351. | Diff a file against the archive. |
  352. `----------------------------------*/
  353. void
  354. diff_archive (void)
  355. {
  356. struct stat stat_data;
  357. int name_length;
  358. int status;
  359. errno = EPIPE; /* FIXME: errno should be read-only */
  360. /* FIXME: remove perrors */
  361. set_next_block_after (current_header);
  362. decode_header (current_header, &current_stat, &current_format, 1);
  363. /* Print the block from `current_header' and `current_stat'. */
  364. if (verbose_option)
  365. {
  366. if (now_verifying)
  367. fprintf (stdlis, _("Verify "));
  368. print_header ();
  369. }
  370. switch (current_header->header.typeflag)
  371. {
  372. default:
  373. WARN ((0, 0, _("Unknown file type '%c' for %s, diffed as normal file"),
  374. current_header->header.typeflag, current_file_name));
  375. /* Fall through. */
  376. case AREGTYPE:
  377. case REGTYPE:
  378. case GNUTYPE_SPARSE:
  379. case CONTTYPE:
  380. /* Appears to be a file. See if it's really a directory. */
  381. name_length = strlen (current_file_name) - 1;
  382. if (current_file_name[name_length] == '/')
  383. goto really_dir;
  384. if (!get_stat_data (&stat_data))
  385. {
  386. if (current_header->oldgnu_header.isextended)
  387. skip_extended_headers ();
  388. skip_file ((long) current_stat.st_size);
  389. goto quit;
  390. }
  391. if (!S_ISREG (stat_data.st_mode))
  392. {
  393. report_difference (_("Not a regular file"));
  394. skip_file ((long) current_stat.st_size);
  395. goto quit;
  396. }
  397. stat_data.st_mode &= 07777;
  398. if (stat_data.st_mode != current_stat.st_mode)
  399. report_difference (_("Mode differs"));
  400. #if !MSDOS
  401. /* stat() in djgpp's C library gives a constant number of 42 as the
  402. uid and gid of a file. So, comparing an FTP'ed archive just after
  403. unpack would fail on MSDOS. */
  404. if (stat_data.st_uid != current_stat.st_uid)
  405. report_difference (_("Uid differs"));
  406. if (stat_data.st_gid != current_stat.st_gid)
  407. report_difference (_("Gid differs"));
  408. #endif
  409. if (stat_data.st_mtime != current_stat.st_mtime)
  410. report_difference (_("Mod time differs"));
  411. if (current_header->header.typeflag != GNUTYPE_SPARSE &&
  412. stat_data.st_size != current_stat.st_size)
  413. {
  414. report_difference (_("Size differs"));
  415. skip_file ((long) current_stat.st_size);
  416. goto quit;
  417. }
  418. diff_handle = open (current_file_name, O_NDELAY | O_RDONLY | O_BINARY);
  419. if (diff_handle < 0 && !absolute_names_option)
  420. {
  421. char *tmpbuf = xmalloc (strlen (current_file_name) + 2);
  422. *tmpbuf = '/';
  423. strcpy (tmpbuf + 1, current_file_name);
  424. diff_handle = open (tmpbuf, O_NDELAY | O_RDONLY);
  425. free (tmpbuf);
  426. }
  427. if (diff_handle < 0)
  428. {
  429. ERROR ((0, errno, _("Cannot open %s"), current_file_name));
  430. if (current_header->oldgnu_header.isextended)
  431. skip_extended_headers ();
  432. skip_file ((long) current_stat.st_size);
  433. report_difference (NULL);
  434. goto quit;
  435. }
  436. /* Need to treat sparse files completely differently here. */
  437. if (current_header->header.typeflag == GNUTYPE_SPARSE)
  438. diff_sparse_files (current_stat.st_size);
  439. else
  440. {
  441. if (multi_volume_option)
  442. {
  443. assign_string (&save_name, current_file_name);
  444. save_totsize = current_stat.st_size;
  445. /* save_sizeleft is set in read_and_process. */
  446. }
  447. read_and_process ((long) (current_stat.st_size), process_rawdata);
  448. if (multi_volume_option)
  449. assign_string (&save_name, NULL);
  450. }
  451. status = close (diff_handle);
  452. if (status < 0)
  453. ERROR ((0, errno, _("Error while closing %s"), current_file_name));
  454. quit:
  455. break;
  456. #if !MSDOS
  457. case LNKTYPE:
  458. {
  459. dev_t dev;
  460. ino_t ino;
  461. if (!get_stat_data (&stat_data))
  462. break;
  463. dev = stat_data.st_dev;
  464. ino = stat_data.st_ino;
  465. status = stat (current_link_name, &stat_data);
  466. if (status < 0)
  467. {
  468. if (errno == ENOENT)
  469. report_difference (_("Does not exist"));
  470. else
  471. {
  472. WARN ((0, errno, _("Cannot stat file %s"), current_file_name));
  473. report_difference (NULL);
  474. }
  475. break;
  476. }
  477. if (stat_data.st_dev != dev || stat_data.st_ino != ino)
  478. {
  479. char *message = (char *)
  480. xmalloc (MESSAGE_BUFFER_SIZE + strlen (current_link_name));
  481. sprintf (message, _("Not linked to %s"), current_link_name);
  482. report_difference (message);
  483. free (message);
  484. break;
  485. }
  486. break;
  487. }
  488. #endif /* not MSDOS */
  489. #ifdef S_ISLNK
  490. case SYMTYPE:
  491. {
  492. char linkbuf[NAME_FIELD_SIZE + 3]; /* FIXME: may be too short. */
  493. status = readlink (current_file_name, linkbuf, (sizeof linkbuf) - 1);
  494. if (status < 0)
  495. {
  496. if (errno == ENOENT)
  497. report_difference (_("No such file or directory"));
  498. else
  499. {
  500. WARN ((0, errno, _("Cannot read link %s"), current_file_name));
  501. report_difference (NULL);
  502. }
  503. break;
  504. }
  505. linkbuf[status] = '\0'; /* null-terminate it */
  506. if (strncmp (current_link_name, linkbuf, (size_t) status) != 0)
  507. report_difference (_("Symlink differs"));
  508. break;
  509. }
  510. #endif /* not S_ISLNK */
  511. #ifdef S_IFCHR
  512. case CHRTYPE:
  513. current_stat.st_mode |= S_IFCHR;
  514. goto check_node;
  515. #endif /* not S_IFCHR */
  516. #ifdef S_IFBLK
  517. /* If local system doesn't support block devices, use default case. */
  518. case BLKTYPE:
  519. current_stat.st_mode |= S_IFBLK;
  520. goto check_node;
  521. #endif /* not S_IFBLK */
  522. #ifdef S_ISFIFO
  523. /* If local system doesn't support FIFOs, use default case. */
  524. case FIFOTYPE:
  525. # ifdef S_IFIFO
  526. current_stat.st_mode |= S_IFIFO;
  527. # endif
  528. current_stat.st_rdev = 0; /* FIXME: do we need this? */
  529. goto check_node;
  530. #endif /* S_ISFIFO */
  531. check_node:
  532. /* FIXME: deal with umask. */
  533. if (!get_stat_data (&stat_data))
  534. break;
  535. if (current_stat.st_rdev != stat_data.st_rdev)
  536. {
  537. report_difference (_("Device numbers changed"));
  538. break;
  539. }
  540. if (
  541. #ifdef S_IFMT
  542. current_stat.st_mode != stat_data.st_mode
  543. #else
  544. /* POSIX lossage. */
  545. (current_stat.st_mode & 07777) != (stat_data.st_mode & 07777)
  546. #endif
  547. )
  548. {
  549. report_difference (_("Mode or device-type changed"));
  550. break;
  551. }
  552. break;
  553. case GNUTYPE_DUMPDIR:
  554. {
  555. char *dumpdir_buffer = get_directory_contents (current_file_name, 0);
  556. if (multi_volume_option)
  557. {
  558. assign_string (&save_name, current_file_name);
  559. save_totsize = current_stat.st_size;
  560. /* save_sizeleft is set in read_and_process. */
  561. }
  562. if (dumpdir_buffer)
  563. {
  564. dumpdir_cursor = dumpdir_buffer;
  565. read_and_process ((long) (current_stat.st_size), process_dumpdir);
  566. free (dumpdir_buffer);
  567. }
  568. else
  569. read_and_process ((long) (current_stat.st_size), process_noop);
  570. if (multi_volume_option)
  571. assign_string (&save_name, NULL);
  572. /* Fall through. */
  573. }
  574. case DIRTYPE:
  575. /* Check for trailing /. */
  576. name_length = strlen (current_file_name) - 1;
  577. really_dir:
  578. while (name_length && current_file_name[name_length] == '/')
  579. current_file_name[name_length--] = '\0'; /* zap / */
  580. if (!get_stat_data (&stat_data))
  581. break;
  582. if (!S_ISDIR (stat_data.st_mode))
  583. {
  584. report_difference (_("No longer a directory"));
  585. break;
  586. }
  587. if ((stat_data.st_mode & 07777) != (current_stat.st_mode & 07777))
  588. report_difference (_("Mode differs"));
  589. break;
  590. case GNUTYPE_VOLHDR:
  591. break;
  592. case GNUTYPE_MULTIVOL:
  593. {
  594. off_t offset;
  595. name_length = strlen (current_file_name) - 1;
  596. if (current_file_name[name_length] == '/')
  597. goto really_dir;
  598. if (!get_stat_data (&stat_data))
  599. break;
  600. if (!S_ISREG (stat_data.st_mode))
  601. {
  602. report_difference (_("Not a regular file"));
  603. skip_file ((long) current_stat.st_size);
  604. break;
  605. }
  606. stat_data.st_mode &= 07777;
  607. offset = from_oct (1 + 12, current_header->oldgnu_header.offset);
  608. if (stat_data.st_size != current_stat.st_size + offset)
  609. {
  610. report_difference (_("Size differs"));
  611. skip_file ((long) current_stat.st_size);
  612. break;
  613. }
  614. diff_handle = open (current_file_name, O_NDELAY | O_RDONLY | O_BINARY);
  615. if (diff_handle < 0)
  616. {
  617. WARN ((0, errno, _("Cannot open file %s"), current_file_name));
  618. report_difference (NULL);
  619. skip_file ((long) current_stat.st_size);
  620. break;
  621. }
  622. status = lseek (diff_handle, offset, 0);
  623. if (status != offset)
  624. {
  625. WARN ((0, errno, _("Cannot seek to %ld in file %s"),
  626. offset, current_file_name));
  627. report_difference (NULL);
  628. break;
  629. }
  630. if (multi_volume_option)
  631. {
  632. assign_string (&save_name, current_file_name);
  633. save_totsize = stat_data.st_size;
  634. /* save_sizeleft is set in read_and_process. */
  635. }
  636. read_and_process ((long) (current_stat.st_size), process_rawdata);
  637. if (multi_volume_option)
  638. assign_string (&save_name, NULL);
  639. status = close (diff_handle);
  640. if (status < 0)
  641. ERROR ((0, errno, _("Error while closing %s"), current_file_name));
  642. break;
  643. }
  644. }
  645. }
  646. /*---.
  647. | ? |
  648. `---*/
  649. void
  650. verify_volume (void)
  651. {
  652. if (!diff_buffer)
  653. diff_init ();
  654. /* Verifying an archive is meant to check if the physical media got it
  655. correctly, so try to defeat clever in-memory buffering pertaining to
  656. this particular media. On Linux, for example, the floppy drive would
  657. not even be accessed for the whole verification.
  658. The code was using fsync only when the ioctl is unavailable, but
  659. Marty Leisner says that the ioctl does not work when not preceded by
  660. fsync. So, until we know better, or maybe to please Marty, let's do it
  661. the unbelievable way :-). */
  662. #if HAVE_FSYNC
  663. fsync (archive);
  664. #endif
  665. #ifdef FDFLUSH
  666. ioctl (archive, FDFLUSH);
  667. #endif
  668. #ifdef MTIOCTOP
  669. {
  670. struct mtop operation;
  671. int status;
  672. operation.mt_op = MTBSF;
  673. operation.mt_count = 1;
  674. if (status = rmtioctl (archive, MTIOCTOP, (char *) &operation), status < 0)
  675. {
  676. if (errno != EIO
  677. || (status = rmtioctl (archive, MTIOCTOP, (char *) &operation),
  678. status < 0))
  679. {
  680. #endif
  681. if (rmtlseek (archive, 0L, 0) != 0)
  682. {
  683. /* Lseek failed. Try a different method. */
  684. WARN ((0, errno,
  685. _("Could not rewind archive file for verify")));
  686. return;
  687. }
  688. #ifdef MTIOCTOP
  689. }
  690. }
  691. }
  692. #endif
  693. access_mode = ACCESS_READ;
  694. now_verifying = 1;
  695. flush_read ();
  696. while (1)
  697. {
  698. enum read_header status = read_header ();
  699. if (status == HEADER_FAILURE)
  700. {
  701. int counter = 0;
  702. while (status == HEADER_FAILURE);
  703. {
  704. counter++;
  705. status = read_header ();
  706. }
  707. ERROR ((0, 0,
  708. _("VERIFY FAILURE: %d invalid header(s) detected"), counter));
  709. }
  710. if (status == HEADER_ZERO_BLOCK || status == HEADER_END_OF_FILE)
  711. break;
  712. diff_archive ();
  713. }
  714. access_mode = ACCESS_WRITE;
  715. now_verifying = 0;
  716. }