buffer.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  1. /* Buffer management for tar.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
  3. 2003 Free Software Foundation, Inc.
  4. Written by John Gilmore, on 1985-08-25.
  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. #include <signal.h>
  18. #include <fnmatch.h>
  19. #include <human.h>
  20. #include <quotearg.h>
  21. #include "common.h"
  22. #include "rmt.h"
  23. /* Number of retries before giving up on read. */
  24. #define READ_ERROR_MAX 10
  25. /* Globbing pattern to append to volume label if initial match failed. */
  26. #define VOLUME_LABEL_APPEND " Volume [1-9]*"
  27. /* Variables. */
  28. static tarlong prev_written; /* bytes written on previous volumes */
  29. static tarlong bytes_written; /* bytes written on this volume */
  30. /* FIXME: The following variables should ideally be static to this
  31. module. However, this cannot be done yet. The cleanup continues! */
  32. union block *record_start; /* start of record of archive */
  33. union block *record_end; /* last+1 block of archive record */
  34. union block *current_block; /* current block of archive */
  35. enum access_mode access_mode; /* how do we handle the archive */
  36. off_t records_read; /* number of records read from this archive */
  37. off_t records_written; /* likewise, for records written */
  38. static off_t record_start_block; /* block ordinal at record_start */
  39. /* Where we write list messages (not errors, not interactions) to. */
  40. FILE *stdlis;
  41. static void backspace_output (void);
  42. static int new_volume (enum access_mode);
  43. /* PID of child program, if compress_option or remote archive access. */
  44. static pid_t child_pid;
  45. /* Error recovery stuff */
  46. static int read_error_count;
  47. /* Have we hit EOF yet? */
  48. static int hit_eof;
  49. /* Checkpointing counter */
  50. static int checkpoint;
  51. /* We're reading, but we just read the last block and its time to update. */
  52. /* As least EXTERN like this one as possible. FIXME! */
  53. extern int time_to_start_writing;
  54. int file_to_switch_to = -1; /* if remote update, close archive, and use
  55. this descriptor to write to */
  56. static int volno = 1; /* which volume of a multi-volume tape we're
  57. on */
  58. static int global_volno = 1; /* volume number to print in external
  59. messages */
  60. /* The pointer save_name, which is set in function dump_file() of module
  61. create.c, points to the original long filename instead of the new,
  62. shorter mangled name that is set in start_header() of module create.c.
  63. The pointer save_name is only used in multi-volume mode when the file
  64. being processed is non-sparse; if a file is split between volumes, the
  65. save_name is used in generating the LF_MULTIVOL record on the second
  66. volume. (From Pierce Cantrell, 1991-08-13.) */
  67. char *save_name; /* name of the file we are currently writing */
  68. off_t save_totsize; /* total size of file we are writing, only
  69. valid if save_name is nonzero */
  70. off_t save_sizeleft; /* where we are in the file we are writing,
  71. only valid if save_name is nonzero */
  72. bool write_archive_to_stdout;
  73. /* Used by flush_read and flush_write to store the real info about saved
  74. names. */
  75. static char *real_s_name;
  76. static off_t real_s_totsize;
  77. static off_t real_s_sizeleft;
  78. /* Functions. */
  79. void
  80. clear_read_error_count ()
  81. {
  82. read_error_count = 0;
  83. }
  84. void
  85. print_total_written (void)
  86. {
  87. tarlong written = prev_written + bytes_written;
  88. char bytes[sizeof (tarlong) * CHAR_BIT];
  89. char abbr[LONGEST_HUMAN_READABLE + 1];
  90. char rate[LONGEST_HUMAN_READABLE + 1];
  91. double seconds;
  92. int human_opts = human_autoscale | human_base_1024 | human_SI | human_B;
  93. #if HAVE_CLOCK_GETTIME
  94. struct timespec now;
  95. if (clock_gettime (CLOCK_REALTIME, &now) == 0)
  96. seconds = ((now.tv_sec - start_timespec.tv_sec)
  97. + (now.tv_nsec - start_timespec.tv_nsec) / 1e9);
  98. else
  99. #endif
  100. seconds = time (0) - start_time;
  101. sprintf (bytes, TARLONG_FORMAT, written);
  102. /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
  103. fprintf (stderr, _("Total bytes written: %s (%s, %s/s)\n"), bytes,
  104. human_readable (written, abbr, human_opts, 1, 1),
  105. (0 < seconds && written / seconds < (uintmax_t) -1
  106. ? human_readable (written / seconds, rate, human_opts, 1, 1)
  107. : "?"));
  108. }
  109. /* Compute and return the block ordinal at current_block. */
  110. off_t
  111. current_block_ordinal (void)
  112. {
  113. return record_start_block + (current_block - record_start);
  114. }
  115. /* If the EOF flag is set, reset it, as well as current_block, etc. */
  116. void
  117. reset_eof (void)
  118. {
  119. if (hit_eof)
  120. {
  121. hit_eof = 0;
  122. current_block = record_start;
  123. record_end = record_start + blocking_factor;
  124. access_mode = ACCESS_WRITE;
  125. }
  126. }
  127. /* Return the location of the next available input or output block.
  128. Return zero for EOF. Once we have returned zero, we just keep returning
  129. it, to avoid accidentally going on to the next file on the tape. */
  130. union block *
  131. find_next_block (void)
  132. {
  133. if (current_block == record_end)
  134. {
  135. if (hit_eof)
  136. return 0;
  137. flush_archive ();
  138. if (current_block == record_end)
  139. {
  140. hit_eof = 1;
  141. return 0;
  142. }
  143. }
  144. return current_block;
  145. }
  146. /* Indicate that we have used all blocks up thru BLOCK.
  147. FIXME: should the arg have an off-by-1? */
  148. void
  149. set_next_block_after (union block *block)
  150. {
  151. while (block >= current_block)
  152. current_block++;
  153. /* Do *not* flush the archive here. If we do, the same argument to
  154. set_next_block_after could mean the next block (if the input record
  155. is exactly one block long), which is not what is intended. */
  156. if (current_block > record_end)
  157. abort ();
  158. }
  159. /* Return the number of bytes comprising the space between POINTER
  160. through the end of the current buffer of blocks. This space is
  161. available for filling with data, or taking data from. POINTER is
  162. usually (but not always) the result previous find_next_block call. */
  163. size_t
  164. available_space_after (union block *pointer)
  165. {
  166. return record_end->buffer - pointer->buffer;
  167. }
  168. /* Close file having descriptor FD, and abort if close unsuccessful. */
  169. void
  170. xclose (int fd)
  171. {
  172. if (close (fd) != 0)
  173. close_error (_("(pipe)"));
  174. }
  175. /* Check the LABEL block against the volume label, seen as a globbing
  176. pattern. Return true if the pattern matches. In case of failure,
  177. retry matching a volume sequence number before giving up in
  178. multi-volume mode. */
  179. static int
  180. check_label_pattern (union block *label)
  181. {
  182. char *string;
  183. int result;
  184. if (! memchr (label->header.name, '\0', sizeof label->header.name))
  185. return 0;
  186. if (fnmatch (volume_label_option, label->header.name, 0) == 0)
  187. return 1;
  188. if (!multi_volume_option)
  189. return 0;
  190. string = xmalloc (strlen (volume_label_option)
  191. + sizeof VOLUME_LABEL_APPEND + 1);
  192. strcpy (string, volume_label_option);
  193. strcat (string, VOLUME_LABEL_APPEND);
  194. result = fnmatch (string, label->header.name, 0) == 0;
  195. free (string);
  196. return result;
  197. }
  198. /* Open an archive file. The argument specifies whether we are
  199. reading or writing, or both. */
  200. void
  201. open_archive (enum access_mode wanted_access)
  202. {
  203. int backed_up_flag = 0;
  204. if (index_file_name)
  205. {
  206. stdlis = fopen (index_file_name, "w");
  207. if (! stdlis)
  208. open_error (index_file_name);
  209. }
  210. else
  211. stdlis = to_stdout_option ? stderr : stdout;
  212. if (record_size == 0)
  213. FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
  214. if (archive_names == 0)
  215. FATAL_ERROR ((0, 0, _("No archive name given")));
  216. tar_stat_destroy (&current_stat_info);
  217. save_name = 0;
  218. real_s_name = 0;
  219. if (multi_volume_option)
  220. {
  221. if (verify_option)
  222. FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
  223. record_start = valloc (record_size + (2 * BLOCKSIZE));
  224. if (record_start)
  225. record_start += 2;
  226. }
  227. else
  228. record_start = valloc (record_size);
  229. if (!record_start)
  230. FATAL_ERROR ((0, 0, _("Cannot allocate memory for blocking factor %d"),
  231. blocking_factor));
  232. current_block = record_start;
  233. record_end = record_start + blocking_factor;
  234. /* When updating the archive, we start with reading. */
  235. access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
  236. if (use_compress_program_option)
  237. {
  238. if (multi_volume_option)
  239. FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
  240. if (verify_option)
  241. FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
  242. switch (wanted_access)
  243. {
  244. case ACCESS_READ:
  245. child_pid = sys_child_open_for_uncompress ();
  246. break;
  247. case ACCESS_WRITE:
  248. child_pid = sys_child_open_for_compress ();
  249. break;
  250. case ACCESS_UPDATE:
  251. FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
  252. break;
  253. }
  254. if (wanted_access == ACCESS_WRITE
  255. && strcmp (archive_name_array[0], "-") == 0)
  256. stdlis = stderr;
  257. }
  258. else if (strcmp (archive_name_array[0], "-") == 0)
  259. {
  260. read_full_records_option = 1; /* could be a pipe, be safe */
  261. if (verify_option)
  262. FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
  263. switch (wanted_access)
  264. {
  265. case ACCESS_READ:
  266. archive = STDIN_FILENO;
  267. break;
  268. case ACCESS_WRITE:
  269. archive = STDOUT_FILENO;
  270. stdlis = stderr;
  271. break;
  272. case ACCESS_UPDATE:
  273. archive = STDIN_FILENO;
  274. stdlis = stderr;
  275. write_archive_to_stdout = 1;
  276. break;
  277. }
  278. }
  279. else if (verify_option)
  280. archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  281. MODE_RW, rsh_command_option);
  282. else
  283. switch (wanted_access)
  284. {
  285. case ACCESS_READ:
  286. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  287. MODE_RW, rsh_command_option);
  288. break;
  289. case ACCESS_WRITE:
  290. if (backup_option)
  291. {
  292. maybe_backup_file (archive_name_array[0], 1);
  293. backed_up_flag = 1;
  294. }
  295. archive = rmtcreat (archive_name_array[0], MODE_RW,
  296. rsh_command_option);
  297. break;
  298. case ACCESS_UPDATE:
  299. archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  300. MODE_RW, rsh_command_option);
  301. break;
  302. }
  303. if (archive < 0
  304. || (! _isrmt (archive) && !sys_get_archive_stat ()))
  305. {
  306. int saved_errno = errno;
  307. if (backed_up_flag)
  308. undo_last_backup ();
  309. errno = saved_errno;
  310. open_fatal (archive_name_array[0]);
  311. }
  312. sys_detect_dev_null_output ();
  313. sys_save_archive_dev_ino ();
  314. SET_BINARY_MODE (archive);
  315. switch (wanted_access)
  316. {
  317. case ACCESS_UPDATE:
  318. records_written = 0;
  319. case ACCESS_READ:
  320. records_read = 0;
  321. record_end = record_start; /* set up for 1st record = # 0 */
  322. find_next_block (); /* read it in, check for EOF */
  323. if (volume_label_option)
  324. {
  325. union block *label = find_next_block ();
  326. if (!label)
  327. FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
  328. quote (volume_label_option)));
  329. if (!check_label_pattern (label))
  330. FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
  331. quote_n (0, label->header.name),
  332. quote_n (1, volume_label_option)));
  333. }
  334. break;
  335. case ACCESS_WRITE:
  336. records_written = 0;
  337. if (volume_label_option)
  338. {
  339. memset (record_start, 0, BLOCKSIZE);
  340. if (multi_volume_option)
  341. sprintf (record_start->header.name, "%s Volume 1",
  342. volume_label_option);
  343. else
  344. strcpy (record_start->header.name, volume_label_option);
  345. assign_string (&current_stat_info.file_name, record_start->header.name);
  346. current_stat_info.had_trailing_slash = strip_trailing_slashes (current_stat_info.file_name);
  347. record_start->header.typeflag = GNUTYPE_VOLHDR;
  348. TIME_TO_CHARS (start_time, record_start->header.mtime);
  349. finish_header (&current_stat_info, record_start, -1);
  350. #if 0
  351. current_block++;
  352. #endif
  353. }
  354. break;
  355. }
  356. }
  357. /* Perform a write to flush the buffer. */
  358. void
  359. flush_write (void)
  360. {
  361. int copy_back;
  362. ssize_t status;
  363. if (checkpoint_option && !(++checkpoint % 10))
  364. WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
  365. if (tape_length_option && tape_length_option <= bytes_written)
  366. {
  367. errno = ENOSPC;
  368. status = 0;
  369. }
  370. else if (dev_null_output)
  371. status = record_size;
  372. else
  373. status = sys_write_archive_buffer ();
  374. if (status != record_size && !multi_volume_option)
  375. archive_write_error (status);
  376. if (status > 0)
  377. {
  378. records_written++;
  379. bytes_written += status;
  380. }
  381. if (status == record_size)
  382. {
  383. if (multi_volume_option)
  384. {
  385. if (save_name)
  386. {
  387. assign_string (&real_s_name, safer_name_suffix (save_name, 0));
  388. real_s_totsize = save_totsize;
  389. real_s_sizeleft = save_sizeleft;
  390. }
  391. else
  392. {
  393. assign_string (&real_s_name, 0);
  394. real_s_totsize = 0;
  395. real_s_sizeleft = 0;
  396. }
  397. }
  398. return;
  399. }
  400. /* We're multivol. Panic if we didn't get the right kind of response. */
  401. /* ENXIO is for the UNIX PC. */
  402. if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
  403. archive_write_error (status);
  404. /* If error indicates a short write, we just move to the next tape. */
  405. if (!new_volume (ACCESS_WRITE))
  406. return;
  407. if (totals_option)
  408. prev_written += bytes_written;
  409. bytes_written = 0;
  410. if (volume_label_option && real_s_name)
  411. {
  412. copy_back = 2;
  413. record_start -= 2;
  414. }
  415. else if (volume_label_option || real_s_name)
  416. {
  417. copy_back = 1;
  418. record_start--;
  419. }
  420. else
  421. copy_back = 0;
  422. if (volume_label_option)
  423. {
  424. memset (record_start, 0, BLOCKSIZE);
  425. sprintf (record_start->header.name, "%s Volume %d",
  426. volume_label_option, volno);
  427. TIME_TO_CHARS (start_time, record_start->header.mtime);
  428. record_start->header.typeflag = GNUTYPE_VOLHDR;
  429. finish_header (&current_stat_info, record_start, -1);
  430. }
  431. if (real_s_name)
  432. {
  433. int tmp;
  434. if (volume_label_option)
  435. record_start++;
  436. memset (record_start, 0, BLOCKSIZE);
  437. /* FIXME: Michael P Urban writes: [a long name file] is being written
  438. when a new volume rolls around [...] Looks like the wrong value is
  439. being preserved in real_s_name, though. */
  440. strcpy (record_start->header.name, real_s_name);
  441. record_start->header.typeflag = GNUTYPE_MULTIVOL;
  442. OFF_TO_CHARS (real_s_sizeleft, record_start->header.size);
  443. OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
  444. record_start->oldgnu_header.offset);
  445. tmp = verbose_option;
  446. verbose_option = 0;
  447. finish_header (&current_stat_info, record_start, -1);
  448. verbose_option = tmp;
  449. if (volume_label_option)
  450. record_start--;
  451. }
  452. status = sys_write_archive_buffer ();
  453. if (status != record_size)
  454. archive_write_error (status);
  455. bytes_written += status;
  456. if (copy_back)
  457. {
  458. record_start += copy_back;
  459. memcpy (current_block,
  460. record_start + blocking_factor - copy_back,
  461. copy_back * BLOCKSIZE);
  462. current_block += copy_back;
  463. if (real_s_sizeleft >= copy_back * BLOCKSIZE)
  464. real_s_sizeleft -= copy_back * BLOCKSIZE;
  465. else if ((real_s_sizeleft + BLOCKSIZE - 1) / BLOCKSIZE <= copy_back)
  466. assign_string (&real_s_name, 0);
  467. else
  468. {
  469. assign_string (&real_s_name, safer_name_suffix (save_name, 0));
  470. real_s_sizeleft = save_sizeleft;
  471. real_s_totsize = save_totsize;
  472. }
  473. copy_back = 0;
  474. }
  475. }
  476. /* Handle write errors on the archive. Write errors are always fatal.
  477. Hitting the end of a volume does not cause a write error unless the
  478. write was the first record of the volume. */
  479. void
  480. archive_write_error (ssize_t status)
  481. {
  482. /* It might be useful to know how much was written before the error
  483. occurred. */
  484. if (totals_option)
  485. {
  486. int e = errno;
  487. print_total_written ();
  488. errno = e;
  489. }
  490. write_fatal_details (*archive_name_cursor, status, record_size);
  491. }
  492. /* Handle read errors on the archive. If the read should be retried,
  493. return to the caller. */
  494. void
  495. archive_read_error (void)
  496. {
  497. read_error (*archive_name_cursor);
  498. if (record_start_block == 0)
  499. FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
  500. /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
  501. then give up on reading the archive. */
  502. if (read_error_count++ > READ_ERROR_MAX)
  503. FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
  504. return;
  505. }
  506. static void
  507. short_read (ssize_t status)
  508. {
  509. size_t left; /* bytes left */
  510. char *more; /* pointer to next byte to read */
  511. more = record_start->buffer + status;
  512. left = record_size - status;
  513. while (left % BLOCKSIZE != 0
  514. || (left && status && read_full_records_option))
  515. {
  516. if (status)
  517. while ((status = rmtread (archive, more, left)) < 0)
  518. archive_read_error ();
  519. if (status == 0)
  520. break;
  521. if (! read_full_records_option)
  522. {
  523. unsigned long rest = record_size - left;
  524. FATAL_ERROR ((0, 0,
  525. ngettext ("Unaligned block (%lu byte) in archive",
  526. "Unaligned block (%lu bytes) in archive",
  527. rest),
  528. rest));
  529. }
  530. /* User warned us about this. Fix up. */
  531. left -= status;
  532. more += status;
  533. }
  534. /* FIXME: for size=0, multi-volume support. On the first record, warn
  535. about the problem. */
  536. if (!read_full_records_option && verbose_option
  537. && record_start_block == 0 && status > 0)
  538. {
  539. unsigned long rsize = (record_size - left) / BLOCKSIZE;
  540. WARN ((0, 0,
  541. ngettext ("Record size = %lu block",
  542. "Record size = %lu blocks",
  543. rsize),
  544. rsize));
  545. }
  546. record_end = record_start + (record_size - left) / BLOCKSIZE;
  547. records_read++;
  548. }
  549. /* Perform a read to flush the buffer. */
  550. void
  551. flush_read (void)
  552. {
  553. ssize_t status; /* result from system call */
  554. if (checkpoint_option && !(++checkpoint % 10))
  555. WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
  556. /* Clear the count of errors. This only applies to a single call to
  557. flush_read. */
  558. read_error_count = 0; /* clear error count */
  559. if (write_archive_to_stdout && record_start_block != 0)
  560. {
  561. archive = STDOUT_FILENO;
  562. status = sys_write_archive_buffer ();
  563. archive = STDIN_FILENO;
  564. if (status != record_size)
  565. archive_write_error (status);
  566. }
  567. if (multi_volume_option)
  568. {
  569. if (save_name)
  570. {
  571. assign_string (&real_s_name, safer_name_suffix (save_name, 0));
  572. real_s_sizeleft = save_sizeleft;
  573. real_s_totsize = save_totsize;
  574. }
  575. else
  576. {
  577. assign_string (&real_s_name, 0);
  578. real_s_totsize = 0;
  579. real_s_sizeleft = 0;
  580. }
  581. }
  582. error_loop:
  583. status = rmtread (archive, record_start->buffer, record_size);
  584. if (status == record_size)
  585. {
  586. records_read++;
  587. return;
  588. }
  589. if ((status == 0
  590. || (status < 0 && errno == ENOSPC)
  591. || (status > 0 && !read_full_records_option))
  592. && multi_volume_option)
  593. {
  594. union block *cursor;
  595. try_volume:
  596. switch (subcommand_option)
  597. {
  598. case APPEND_SUBCOMMAND:
  599. case CAT_SUBCOMMAND:
  600. case UPDATE_SUBCOMMAND:
  601. if (!new_volume (ACCESS_UPDATE))
  602. return;
  603. break;
  604. default:
  605. if (!new_volume (ACCESS_READ))
  606. return;
  607. break;
  608. }
  609. vol_error:
  610. status = rmtread (archive, record_start->buffer, record_size);
  611. if (status < 0)
  612. {
  613. archive_read_error ();
  614. goto vol_error;
  615. }
  616. if (status != record_size)
  617. short_read (status);
  618. cursor = record_start;
  619. if (cursor->header.typeflag == GNUTYPE_VOLHDR)
  620. {
  621. if (volume_label_option)
  622. {
  623. if (!check_label_pattern (cursor))
  624. {
  625. WARN ((0, 0, _("Volume %s does not match %s"),
  626. quote_n (0, cursor->header.name),
  627. quote_n (1, volume_label_option)));
  628. volno--;
  629. global_volno--;
  630. goto try_volume;
  631. }
  632. }
  633. if (verbose_option)
  634. fprintf (stdlis, _("Reading %s\n"), quote (cursor->header.name));
  635. cursor++;
  636. }
  637. else if (volume_label_option)
  638. WARN ((0, 0, _("WARNING: No volume header")));
  639. if (real_s_name)
  640. {
  641. uintmax_t s1, s2;
  642. if (cursor->header.typeflag != GNUTYPE_MULTIVOL
  643. || strcmp (cursor->header.name, real_s_name))
  644. {
  645. WARN ((0, 0, _("%s is not continued on this volume"),
  646. quote (real_s_name)));
  647. volno--;
  648. global_volno--;
  649. goto try_volume;
  650. }
  651. s1 = UINTMAX_FROM_HEADER (cursor->header.size);
  652. s2 = UINTMAX_FROM_HEADER (cursor->oldgnu_header.offset);
  653. if (real_s_totsize != s1 + s2 || s1 + s2 < s2)
  654. {
  655. char totsizebuf[UINTMAX_STRSIZE_BOUND];
  656. char s1buf[UINTMAX_STRSIZE_BOUND];
  657. char s2buf[UINTMAX_STRSIZE_BOUND];
  658. WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
  659. quote (cursor->header.name),
  660. STRINGIFY_BIGINT (save_totsize, totsizebuf),
  661. STRINGIFY_BIGINT (s1, s1buf),
  662. STRINGIFY_BIGINT (s2, s2buf)));
  663. volno--;
  664. global_volno--;
  665. goto try_volume;
  666. }
  667. if (real_s_totsize - real_s_sizeleft
  668. != OFF_FROM_HEADER (cursor->oldgnu_header.offset))
  669. {
  670. WARN ((0, 0, _("This volume is out of sequence")));
  671. volno--;
  672. global_volno--;
  673. goto try_volume;
  674. }
  675. cursor++;
  676. }
  677. current_block = cursor;
  678. records_read++;
  679. return;
  680. }
  681. else if (status < 0)
  682. {
  683. archive_read_error ();
  684. goto error_loop; /* try again */
  685. }
  686. short_read (status);
  687. }
  688. /* Flush the current buffer to/from the archive. */
  689. void
  690. flush_archive (void)
  691. {
  692. record_start_block += record_end - record_start;
  693. current_block = record_start;
  694. record_end = record_start + blocking_factor;
  695. if (access_mode == ACCESS_READ && time_to_start_writing)
  696. {
  697. access_mode = ACCESS_WRITE;
  698. time_to_start_writing = 0;
  699. if (file_to_switch_to >= 0)
  700. {
  701. if (rmtclose (archive) != 0)
  702. close_warn (*archive_name_cursor);
  703. archive = file_to_switch_to;
  704. }
  705. else
  706. backspace_output ();
  707. }
  708. switch (access_mode)
  709. {
  710. case ACCESS_READ:
  711. flush_read ();
  712. break;
  713. case ACCESS_WRITE:
  714. flush_write ();
  715. break;
  716. case ACCESS_UPDATE:
  717. abort ();
  718. }
  719. }
  720. /* Backspace the archive descriptor by one record worth. If it's a
  721. tape, MTIOCTOP will work. If it's something else, try to seek on
  722. it. If we can't seek, we lose! */
  723. static void
  724. backspace_output (void)
  725. {
  726. #ifdef MTIOCTOP
  727. {
  728. struct mtop operation;
  729. operation.mt_op = MTBSR;
  730. operation.mt_count = 1;
  731. if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  732. return;
  733. if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  734. return;
  735. }
  736. #endif
  737. {
  738. off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
  739. /* Seek back to the beginning of this record and start writing there. */
  740. position -= record_size;
  741. if (position < 0)
  742. position = 0;
  743. if (rmtlseek (archive, position, SEEK_SET) != position)
  744. {
  745. /* Lseek failed. Try a different method. */
  746. WARN ((0, 0,
  747. _("Cannot backspace archive file; it may be unreadable without -i")));
  748. /* Replace the first part of the record with NULs. */
  749. if (record_start->buffer != output_start)
  750. memset (record_start->buffer, 0,
  751. output_start - record_start->buffer);
  752. }
  753. }
  754. }
  755. /* Close the archive file. */
  756. void
  757. close_archive (void)
  758. {
  759. if (time_to_start_writing || access_mode == ACCESS_WRITE)
  760. flush_archive ();
  761. sys_drain_input_pipe ();
  762. if (verify_option)
  763. verify_volume ();
  764. if (rmtclose (archive) != 0)
  765. close_warn (*archive_name_cursor);
  766. sys_wait_for_child (child_pid);
  767. tar_stat_destroy (&current_stat_info);
  768. if (save_name)
  769. free (save_name);
  770. if (real_s_name)
  771. free (real_s_name);
  772. free (multi_volume_option ? record_start - 2 : record_start);
  773. }
  774. /* Called to initialize the global volume number. */
  775. void
  776. init_volume_number (void)
  777. {
  778. FILE *file = fopen (volno_file_option, "r");
  779. if (file)
  780. {
  781. if (fscanf (file, "%d", &global_volno) != 1
  782. || global_volno < 0)
  783. FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
  784. quotearg_colon (volno_file_option)));
  785. if (ferror (file))
  786. read_error (volno_file_option);
  787. if (fclose (file) != 0)
  788. close_error (volno_file_option);
  789. }
  790. else if (errno != ENOENT)
  791. open_error (volno_file_option);
  792. }
  793. /* Called to write out the closing global volume number. */
  794. void
  795. closeout_volume_number (void)
  796. {
  797. FILE *file = fopen (volno_file_option, "w");
  798. if (file)
  799. {
  800. fprintf (file, "%d\n", global_volno);
  801. if (ferror (file))
  802. write_error (volno_file_option);
  803. if (fclose (file) != 0)
  804. close_error (volno_file_option);
  805. }
  806. else
  807. open_error (volno_file_option);
  808. }
  809. /* We've hit the end of the old volume. Close it and open the next one.
  810. Return nonzero on success. */
  811. static int
  812. new_volume (enum access_mode access)
  813. {
  814. static FILE *read_file;
  815. static int looped;
  816. if (!read_file && !info_script_option)
  817. /* FIXME: if fopen is used, it will never be closed. */
  818. read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
  819. if (now_verifying)
  820. return 0;
  821. if (verify_option)
  822. verify_volume ();
  823. if (rmtclose (archive) != 0)
  824. close_warn (*archive_name_cursor);
  825. global_volno++;
  826. if (global_volno < 0)
  827. FATAL_ERROR ((0, 0, _("Volume number overflow")));
  828. volno++;
  829. archive_name_cursor++;
  830. if (archive_name_cursor == archive_name_array + archive_names)
  831. {
  832. archive_name_cursor = archive_name_array;
  833. looped = 1;
  834. }
  835. tryagain:
  836. if (looped)
  837. {
  838. /* We have to prompt from now on. */
  839. if (info_script_option)
  840. {
  841. if (volno_file_option)
  842. closeout_volume_number ();
  843. if (system (info_script_option) != 0)
  844. FATAL_ERROR ((0, 0, _("`%s' command failed"), info_script_option));
  845. }
  846. else
  847. while (1)
  848. {
  849. char input_buffer[80];
  850. fputc ('\007', stderr);
  851. fprintf (stderr,
  852. _("Prepare volume #%d for %s and hit return: "),
  853. global_volno, quote (*archive_name_cursor));
  854. fflush (stderr);
  855. if (fgets (input_buffer, sizeof input_buffer, read_file) == 0)
  856. {
  857. WARN ((0, 0, _("EOF where user reply was expected")));
  858. if (subcommand_option != EXTRACT_SUBCOMMAND
  859. && subcommand_option != LIST_SUBCOMMAND
  860. && subcommand_option != DIFF_SUBCOMMAND)
  861. WARN ((0, 0, _("WARNING: Archive is incomplete")));
  862. fatal_exit ();
  863. }
  864. if (input_buffer[0] == '\n'
  865. || input_buffer[0] == 'y'
  866. || input_buffer[0] == 'Y')
  867. break;
  868. switch (input_buffer[0])
  869. {
  870. case '?':
  871. {
  872. fprintf (stderr, _("\
  873. n [name] Give a new file name for the next (and subsequent) volume(s)\n\
  874. q Abort tar\n\
  875. ! Spawn a subshell\n\
  876. ? Print this list\n"));
  877. }
  878. break;
  879. case 'q':
  880. /* Quit. */
  881. WARN ((0, 0, _("No new volume; exiting.\n")));
  882. if (subcommand_option != EXTRACT_SUBCOMMAND
  883. && subcommand_option != LIST_SUBCOMMAND
  884. && subcommand_option != DIFF_SUBCOMMAND)
  885. WARN ((0, 0, _("WARNING: Archive is incomplete")));
  886. fatal_exit ();
  887. case 'n':
  888. /* Get new file name. */
  889. {
  890. char *name = &input_buffer[1];
  891. char *cursor;
  892. while (*name == ' ' || *name == '\t')
  893. name++;
  894. cursor = name;
  895. while (*cursor && *cursor != '\n')
  896. cursor++;
  897. *cursor = '\0';
  898. /* FIXME: the following allocation is never reclaimed. */
  899. *archive_name_cursor = xstrdup (name);
  900. }
  901. break;
  902. case '!':
  903. sys_spawn_shell ();
  904. break;
  905. }
  906. }
  907. }
  908. if (strcmp (archive_name_cursor[0], "-") == 0)
  909. {
  910. read_full_records_option = true;
  911. archive = STDIN_FILENO;
  912. }
  913. else if (verify_option)
  914. archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
  915. rsh_command_option);
  916. else
  917. switch (access)
  918. {
  919. case ACCESS_READ:
  920. archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
  921. rsh_command_option);
  922. break;
  923. case ACCESS_WRITE:
  924. if (backup_option)
  925. maybe_backup_file (*archive_name_cursor, 1);
  926. archive = rmtcreat (*archive_name_cursor, MODE_RW,
  927. rsh_command_option);
  928. break;
  929. case ACCESS_UPDATE:
  930. archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
  931. rsh_command_option);
  932. break;
  933. }
  934. if (archive < 0)
  935. {
  936. open_warn (*archive_name_cursor);
  937. if (!verify_option && access == ACCESS_WRITE && backup_option)
  938. undo_last_backup ();
  939. goto tryagain;
  940. }
  941. SET_BINARY_MODE (archive);
  942. return 1;
  943. }