4
0

buffer.c 32 KB

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