buffer.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. /* Buffer management for tar.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001,
  3. 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 3, 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 <system-ioctl.h>
  18. #include <signal.h>
  19. #include <closeout.h>
  20. #include <fnmatch.h>
  21. #include <human.h>
  22. #include <quotearg.h>
  23. #include "common.h"
  24. #include <rmt.h>
  25. /* Number of retries before giving up on read. */
  26. #define READ_ERROR_MAX 10
  27. /* Globbing pattern to append to volume label if initial match failed. */
  28. #define VOLUME_LABEL_APPEND " Volume [1-9]*"
  29. /* Variables. */
  30. static tarlong prev_written; /* bytes written on previous volumes */
  31. static tarlong bytes_written; /* bytes written on this volume */
  32. static void *record_buffer[2]; /* allocated memory */
  33. union block *record_buffer_aligned[2];
  34. static int record_index;
  35. /* FIXME: The following variables should ideally be static to this
  36. module. However, this cannot be done yet. The cleanup continues! */
  37. union block *record_start; /* start of record of archive */
  38. union block *record_end; /* last+1 block of archive record */
  39. union block *current_block; /* current block of archive */
  40. enum access_mode access_mode; /* how do we handle the archive */
  41. off_t records_read; /* number of records read from this archive */
  42. off_t records_written; /* likewise, for records written */
  43. extern off_t records_skipped; /* number of records skipped at the start
  44. of the archive, defined in delete.c */
  45. static off_t record_start_block; /* block ordinal at record_start */
  46. /* Where we write list messages (not errors, not interactions) to. */
  47. FILE *stdlis;
  48. static void backspace_output (void);
  49. /* PID of child program, if compress_option or remote archive access. */
  50. static pid_t child_pid;
  51. /* Error recovery stuff */
  52. static int read_error_count;
  53. /* Have we hit EOF yet? */
  54. static bool hit_eof;
  55. static bool read_full_records = false;
  56. /* We're reading, but we just read the last block and it's time to update.
  57. Declared in update.c
  58. As least EXTERN like this one as possible. (?? --gray)
  59. FIXME: Either eliminate it or move it to common.h.
  60. */
  61. extern bool time_to_start_writing;
  62. bool write_archive_to_stdout;
  63. void (*flush_write_ptr) (size_t);
  64. void (*flush_read_ptr) (void);
  65. char *volume_label;
  66. char *continued_file_name;
  67. uintmax_t continued_file_size;
  68. uintmax_t continued_file_offset;
  69. static int volno = 1; /* which volume of a multi-volume tape we're
  70. on */
  71. static int global_volno = 1; /* volume number to print in external
  72. messages */
  73. bool write_archive_to_stdout;
  74. /* Used by flush_read and flush_write to store the real info about saved
  75. names. */
  76. static char *real_s_name;
  77. static off_t real_s_totsize;
  78. static off_t real_s_sizeleft;
  79. /* Multi-volume tracking support */
  80. static char *save_name; /* name of the file we are currently writing */
  81. static off_t save_totsize; /* total size of file we are writing, only
  82. valid if save_name is nonzero */
  83. static off_t save_sizeleft; /* where we are in the file we are writing,
  84. only valid if save_name is nonzero */
  85. static struct tar_stat_info dummy;
  86. void
  87. buffer_write_global_xheader ()
  88. {
  89. xheader_write_global (&dummy.xhdr);
  90. }
  91. void
  92. mv_begin (struct tar_stat_info *st)
  93. {
  94. if (multi_volume_option)
  95. {
  96. assign_string (&save_name, st->orig_file_name);
  97. save_totsize = save_sizeleft = st->stat.st_size;
  98. }
  99. }
  100. void
  101. mv_end ()
  102. {
  103. if (multi_volume_option)
  104. assign_string (&save_name, 0);
  105. }
  106. void
  107. mv_total_size (off_t size)
  108. {
  109. save_totsize = size;
  110. }
  111. void
  112. mv_size_left (off_t size)
  113. {
  114. save_sizeleft = size;
  115. }
  116. /* Functions. */
  117. void
  118. clear_read_error_count (void)
  119. {
  120. read_error_count = 0;
  121. }
  122. /* Time-related functions */
  123. double duration;
  124. void
  125. set_start_time ()
  126. {
  127. gettime (&start_time);
  128. volume_start_time = start_time;
  129. last_stat_time = start_time;
  130. }
  131. void
  132. set_volume_start_time ()
  133. {
  134. gettime (&volume_start_time);
  135. last_stat_time = volume_start_time;
  136. }
  137. void
  138. compute_duration ()
  139. {
  140. struct timespec now;
  141. gettime (&now);
  142. duration += ((now.tv_sec - last_stat_time.tv_sec)
  143. + (now.tv_nsec - last_stat_time.tv_nsec) / 1e9);
  144. gettime (&last_stat_time);
  145. }
  146. /* Compression detection */
  147. enum compress_type {
  148. ct_tar, /* Plain tar file */
  149. ct_none, /* Unknown compression type */
  150. ct_compress,
  151. ct_gzip,
  152. ct_bzip2,
  153. ct_lzma,
  154. ct_lzop,
  155. ct_xz
  156. };
  157. struct zip_magic
  158. {
  159. enum compress_type type;
  160. size_t length;
  161. char *magic;
  162. char *program;
  163. char *option;
  164. };
  165. static struct zip_magic const magic[] = {
  166. { ct_tar },
  167. { ct_none, },
  168. { ct_compress, 2, "\037\235", COMPRESS_PROGRAM, "-Z" },
  169. { ct_gzip, 2, "\037\213", GZIP_PROGRAM, "-z" },
  170. { ct_bzip2, 3, "BZh", BZIP2_PROGRAM, "-j" },
  171. { ct_lzma, 6, "\xFFLZMA", LZMA_PROGRAM, "--lzma" },
  172. { ct_lzop, 4, "\211LZO", LZOP_PROGRAM, "--lzop" },
  173. { ct_xz, 6, "\0xFD7zXZ", XZ_PROGRAM, "-J" },
  174. };
  175. #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
  176. #define compress_option(t) magic[t].option
  177. #define compress_program(t) magic[t].program
  178. /* Check if the file ARCHIVE is a compressed archive. */
  179. enum compress_type
  180. check_compressed_archive (bool *pshort)
  181. {
  182. struct zip_magic const *p;
  183. bool sfr;
  184. bool temp;
  185. if (!pshort)
  186. pshort = &temp;
  187. /* Prepare global data needed for find_next_block: */
  188. record_end = record_start; /* set up for 1st record = # 0 */
  189. sfr = read_full_records;
  190. read_full_records = true; /* Suppress fatal error on reading a partial
  191. record */
  192. *pshort = find_next_block () == 0;
  193. /* Restore global values */
  194. read_full_records = sfr;
  195. if (tar_checksum (record_start, true) == HEADER_SUCCESS)
  196. /* Probably a valid header */
  197. return ct_tar;
  198. for (p = magic + 2; p < magic + NMAGIC; p++)
  199. if (memcmp (record_start->buffer, p->magic, p->length) == 0)
  200. return p->type;
  201. return ct_none;
  202. }
  203. /* Guess if the archive is seekable. */
  204. static void
  205. guess_seekable_archive ()
  206. {
  207. struct stat st;
  208. if (subcommand_option == DELETE_SUBCOMMAND)
  209. {
  210. /* The current code in delete.c is based on the assumption that
  211. skip_member() reads all data from the archive. So, we should
  212. make sure it won't use seeks. On the other hand, the same code
  213. depends on the ability to backspace a record in the archive,
  214. so setting seekable_archive to false is technically incorrect.
  215. However, it is tested only in skip_member(), so it's not a
  216. problem. */
  217. seekable_archive = false;
  218. }
  219. if (seek_option != -1)
  220. {
  221. seekable_archive = !!seek_option;
  222. return;
  223. }
  224. if (!multi_volume_option && !use_compress_program_option
  225. && fstat (archive, &st) == 0)
  226. seekable_archive = S_ISREG (st.st_mode);
  227. else
  228. seekable_archive = false;
  229. }
  230. /* Open an archive named archive_name_array[0]. Detect if it is
  231. a compressed archive of known type and use corresponding decompression
  232. program if so */
  233. int
  234. open_compressed_archive ()
  235. {
  236. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  237. MODE_RW, rsh_command_option);
  238. if (archive == -1)
  239. return archive;
  240. if (!multi_volume_option)
  241. {
  242. if (!use_compress_program_option)
  243. {
  244. bool shortfile;
  245. enum compress_type type = check_compressed_archive (&shortfile);
  246. switch (type)
  247. {
  248. case ct_tar:
  249. if (shortfile)
  250. ERROR ((0, 0, _("This does not look like a tar archive")));
  251. return archive;
  252. case ct_none:
  253. if (shortfile)
  254. ERROR ((0, 0, _("This does not look like a tar archive")));
  255. set_comression_program_by_suffix (archive_name_array[0], NULL);
  256. if (!use_compress_program_option)
  257. return archive;
  258. break;
  259. default:
  260. use_compress_program_option = compress_program (type);
  261. break;
  262. }
  263. }
  264. /* FD is not needed any more */
  265. rmtclose (archive);
  266. hit_eof = false; /* It might have been set by find_next_block in
  267. check_compressed_archive */
  268. /* Open compressed archive */
  269. child_pid = sys_child_open_for_uncompress ();
  270. read_full_records = true;
  271. }
  272. records_read = 0;
  273. record_end = record_start; /* set up for 1st record = # 0 */
  274. return archive;
  275. }
  276. static void
  277. print_stats (FILE *fp, const char *text, tarlong numbytes)
  278. {
  279. char bytes[sizeof (tarlong) * CHAR_BIT];
  280. char abbr[LONGEST_HUMAN_READABLE + 1];
  281. char rate[LONGEST_HUMAN_READABLE + 1];
  282. int human_opts = human_autoscale | human_base_1024 | human_SI | human_B;
  283. sprintf (bytes, TARLONG_FORMAT, numbytes);
  284. fprintf (fp, "%s: %s (%s, %s/s)\n",
  285. text, bytes,
  286. human_readable (numbytes, abbr, human_opts, 1, 1),
  287. (0 < duration && numbytes / duration < (uintmax_t) -1
  288. ? human_readable (numbytes / duration, rate, human_opts, 1, 1)
  289. : "?"));
  290. }
  291. void
  292. print_total_stats ()
  293. {
  294. switch (subcommand_option)
  295. {
  296. case CREATE_SUBCOMMAND:
  297. case CAT_SUBCOMMAND:
  298. case UPDATE_SUBCOMMAND:
  299. case APPEND_SUBCOMMAND:
  300. /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
  301. print_stats (stderr, _("Total bytes written"),
  302. prev_written + bytes_written);
  303. break;
  304. case DELETE_SUBCOMMAND:
  305. {
  306. char buf[UINTMAX_STRSIZE_BOUND];
  307. print_stats (stderr, _("Total bytes read"),
  308. records_read * record_size);
  309. print_stats (stderr, _("Total bytes written"),
  310. prev_written + bytes_written);
  311. fprintf (stderr, _("Total bytes deleted: %s\n"),
  312. STRINGIFY_BIGINT ((records_read - records_skipped)
  313. * record_size
  314. - (prev_written + bytes_written), buf));
  315. }
  316. break;
  317. case EXTRACT_SUBCOMMAND:
  318. case LIST_SUBCOMMAND:
  319. case DIFF_SUBCOMMAND:
  320. print_stats (stderr, _("Total bytes read"),
  321. records_read * record_size);
  322. break;
  323. default:
  324. abort ();
  325. }
  326. }
  327. /* Compute and return the block ordinal at current_block. */
  328. off_t
  329. current_block_ordinal (void)
  330. {
  331. return record_start_block + (current_block - record_start);
  332. }
  333. /* If the EOF flag is set, reset it, as well as current_block, etc. */
  334. void
  335. reset_eof (void)
  336. {
  337. if (hit_eof)
  338. {
  339. hit_eof = false;
  340. current_block = record_start;
  341. record_end = record_start + blocking_factor;
  342. access_mode = ACCESS_WRITE;
  343. }
  344. }
  345. /* Return the location of the next available input or output block.
  346. Return zero for EOF. Once we have returned zero, we just keep returning
  347. it, to avoid accidentally going on to the next file on the tape. */
  348. union block *
  349. find_next_block (void)
  350. {
  351. if (current_block == record_end)
  352. {
  353. if (hit_eof)
  354. return 0;
  355. flush_archive ();
  356. if (current_block == record_end)
  357. {
  358. hit_eof = true;
  359. return 0;
  360. }
  361. }
  362. return current_block;
  363. }
  364. /* Indicate that we have used all blocks up thru BLOCK. */
  365. void
  366. set_next_block_after (union block *block)
  367. {
  368. while (block >= current_block)
  369. current_block++;
  370. /* Do *not* flush the archive here. If we do, the same argument to
  371. set_next_block_after could mean the next block (if the input record
  372. is exactly one block long), which is not what is intended. */
  373. if (current_block > record_end)
  374. abort ();
  375. }
  376. /* Return the number of bytes comprising the space between POINTER
  377. through the end of the current buffer of blocks. This space is
  378. available for filling with data, or taking data from. POINTER is
  379. usually (but not always) the result of previous find_next_block call. */
  380. size_t
  381. available_space_after (union block *pointer)
  382. {
  383. return record_end->buffer - pointer->buffer;
  384. }
  385. /* Close file having descriptor FD, and abort if close unsuccessful. */
  386. void
  387. xclose (int fd)
  388. {
  389. if (close (fd) != 0)
  390. close_error (_("(pipe)"));
  391. }
  392. static void
  393. init_buffer ()
  394. {
  395. if (! record_buffer_aligned[record_index])
  396. record_buffer_aligned[record_index] =
  397. page_aligned_alloc (&record_buffer[record_index], record_size);
  398. record_start = record_buffer_aligned[record_index];
  399. current_block = record_start;
  400. record_end = record_start + blocking_factor;
  401. }
  402. /* Open an archive file. The argument specifies whether we are
  403. reading or writing, or both. */
  404. static void
  405. _open_archive (enum access_mode wanted_access)
  406. {
  407. int backed_up_flag = 0;
  408. if (record_size == 0)
  409. FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
  410. if (archive_names == 0)
  411. FATAL_ERROR ((0, 0, _("No archive name given")));
  412. tar_stat_destroy (&current_stat_info);
  413. save_name = 0;
  414. real_s_name = 0;
  415. record_index = 0;
  416. init_buffer ();
  417. /* When updating the archive, we start with reading. */
  418. access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
  419. read_full_records = read_full_records_option;
  420. records_read = 0;
  421. if (use_compress_program_option)
  422. {
  423. switch (wanted_access)
  424. {
  425. case ACCESS_READ:
  426. child_pid = sys_child_open_for_uncompress ();
  427. read_full_records = true;
  428. record_end = record_start; /* set up for 1st record = # 0 */
  429. break;
  430. case ACCESS_WRITE:
  431. child_pid = sys_child_open_for_compress ();
  432. break;
  433. case ACCESS_UPDATE:
  434. abort (); /* Should not happen */
  435. break;
  436. }
  437. if (!index_file_name
  438. && wanted_access == ACCESS_WRITE
  439. && strcmp (archive_name_array[0], "-") == 0)
  440. stdlis = stderr;
  441. }
  442. else if (strcmp (archive_name_array[0], "-") == 0)
  443. {
  444. read_full_records = true; /* could be a pipe, be safe */
  445. if (verify_option)
  446. FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
  447. switch (wanted_access)
  448. {
  449. case ACCESS_READ:
  450. {
  451. bool shortfile;
  452. enum compress_type type;
  453. archive = STDIN_FILENO;
  454. type = check_compressed_archive (&shortfile);
  455. if (type != ct_tar && type != ct_none)
  456. FATAL_ERROR ((0, 0,
  457. _("Archive is compressed. Use %s option"),
  458. compress_option (type)));
  459. if (shortfile)
  460. ERROR ((0, 0, _("This does not look like a tar archive")));
  461. }
  462. break;
  463. case ACCESS_WRITE:
  464. archive = STDOUT_FILENO;
  465. if (!index_file_name)
  466. stdlis = stderr;
  467. break;
  468. case ACCESS_UPDATE:
  469. archive = STDIN_FILENO;
  470. write_archive_to_stdout = true;
  471. record_end = record_start; /* set up for 1st record = # 0 */
  472. if (!index_file_name)
  473. stdlis = stderr;
  474. break;
  475. }
  476. }
  477. else if (verify_option)
  478. archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  479. MODE_RW, rsh_command_option);
  480. else
  481. switch (wanted_access)
  482. {
  483. case ACCESS_READ:
  484. archive = open_compressed_archive ();
  485. guess_seekable_archive ();
  486. break;
  487. case ACCESS_WRITE:
  488. if (backup_option)
  489. {
  490. maybe_backup_file (archive_name_array[0], 1);
  491. backed_up_flag = 1;
  492. }
  493. archive = rmtcreat (archive_name_array[0], MODE_RW,
  494. rsh_command_option);
  495. break;
  496. case ACCESS_UPDATE:
  497. archive = rmtopen (archive_name_array[0],
  498. O_RDWR | O_CREAT | O_BINARY,
  499. MODE_RW, rsh_command_option);
  500. switch (check_compressed_archive (NULL))
  501. {
  502. case ct_none:
  503. case ct_tar:
  504. break;
  505. default:
  506. FATAL_ERROR ((0, 0,
  507. _("Cannot update compressed archives")));
  508. }
  509. break;
  510. }
  511. if (archive < 0
  512. || (! _isrmt (archive) && !sys_get_archive_stat ()))
  513. {
  514. int saved_errno = errno;
  515. if (backed_up_flag)
  516. undo_last_backup ();
  517. errno = saved_errno;
  518. open_fatal (archive_name_array[0]);
  519. }
  520. sys_detect_dev_null_output ();
  521. sys_save_archive_dev_ino ();
  522. SET_BINARY_MODE (archive);
  523. switch (wanted_access)
  524. {
  525. case ACCESS_READ:
  526. find_next_block (); /* read it in, check for EOF */
  527. break;
  528. case ACCESS_UPDATE:
  529. case ACCESS_WRITE:
  530. records_written = 0;
  531. break;
  532. }
  533. }
  534. /* Perform a write to flush the buffer. */
  535. ssize_t
  536. _flush_write (void)
  537. {
  538. ssize_t status;
  539. checkpoint_run (true);
  540. if (tape_length_option && tape_length_option <= bytes_written)
  541. {
  542. errno = ENOSPC;
  543. status = 0;
  544. }
  545. else if (dev_null_output)
  546. status = record_size;
  547. else
  548. status = sys_write_archive_buffer ();
  549. return status;
  550. }
  551. /* Handle write errors on the archive. Write errors are always fatal.
  552. Hitting the end of a volume does not cause a write error unless the
  553. write was the first record of the volume. */
  554. void
  555. archive_write_error (ssize_t status)
  556. {
  557. /* It might be useful to know how much was written before the error
  558. occurred. */
  559. if (totals_option)
  560. {
  561. int e = errno;
  562. print_total_stats ();
  563. errno = e;
  564. }
  565. write_fatal_details (*archive_name_cursor, status, record_size);
  566. }
  567. /* Handle read errors on the archive. If the read should be retried,
  568. return to the caller. */
  569. void
  570. archive_read_error (void)
  571. {
  572. read_error (*archive_name_cursor);
  573. if (record_start_block == 0)
  574. FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
  575. /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
  576. then give up on reading the archive. */
  577. if (read_error_count++ > READ_ERROR_MAX)
  578. FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
  579. return;
  580. }
  581. static bool
  582. archive_is_dev ()
  583. {
  584. struct stat st;
  585. if (fstat (archive, &st))
  586. {
  587. stat_diag (*archive_name_cursor);
  588. return false;
  589. }
  590. return S_ISBLK (st.st_mode) || S_ISCHR (st.st_mode);
  591. }
  592. static void
  593. short_read (size_t status)
  594. {
  595. size_t left; /* bytes left */
  596. char *more; /* pointer to next byte to read */
  597. more = record_start->buffer + status;
  598. left = record_size - status;
  599. if (left && left % BLOCKSIZE == 0
  600. && verbose_option
  601. && record_start_block == 0 && status != 0
  602. && archive_is_dev ())
  603. {
  604. unsigned long rsize = status / BLOCKSIZE;
  605. WARN ((0, 0,
  606. ngettext ("Record size = %lu block",
  607. "Record size = %lu blocks",
  608. rsize),
  609. rsize));
  610. }
  611. while (left % BLOCKSIZE != 0
  612. || (left && status && read_full_records))
  613. {
  614. if (status)
  615. while ((status = rmtread (archive, more, left)) == SAFE_READ_ERROR)
  616. archive_read_error ();
  617. if (status == 0)
  618. break;
  619. if (! read_full_records)
  620. {
  621. unsigned long rest = record_size - left;
  622. FATAL_ERROR ((0, 0,
  623. ngettext ("Unaligned block (%lu byte) in archive",
  624. "Unaligned block (%lu bytes) in archive",
  625. rest),
  626. rest));
  627. }
  628. left -= status;
  629. more += status;
  630. }
  631. record_end = record_start + (record_size - left) / BLOCKSIZE;
  632. records_read++;
  633. }
  634. /* Flush the current buffer to/from the archive. */
  635. void
  636. flush_archive (void)
  637. {
  638. size_t buffer_level = current_block->buffer - record_start->buffer;
  639. record_start_block += record_end - record_start;
  640. current_block = record_start;
  641. record_end = record_start + blocking_factor;
  642. if (access_mode == ACCESS_READ && time_to_start_writing)
  643. {
  644. access_mode = ACCESS_WRITE;
  645. time_to_start_writing = false;
  646. backspace_output ();
  647. }
  648. switch (access_mode)
  649. {
  650. case ACCESS_READ:
  651. flush_read ();
  652. break;
  653. case ACCESS_WRITE:
  654. flush_write_ptr (buffer_level);
  655. break;
  656. case ACCESS_UPDATE:
  657. abort ();
  658. }
  659. }
  660. /* Backspace the archive descriptor by one record worth. If it's a
  661. tape, MTIOCTOP will work. If it's something else, try to seek on
  662. it. If we can't seek, we lose! */
  663. static void
  664. backspace_output (void)
  665. {
  666. #ifdef MTIOCTOP
  667. {
  668. struct mtop operation;
  669. operation.mt_op = MTBSR;
  670. operation.mt_count = 1;
  671. if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  672. return;
  673. if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  674. return;
  675. }
  676. #endif
  677. {
  678. off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
  679. /* Seek back to the beginning of this record and start writing there. */
  680. position -= record_size;
  681. if (position < 0)
  682. position = 0;
  683. if (rmtlseek (archive, position, SEEK_SET) != position)
  684. {
  685. /* Lseek failed. Try a different method. */
  686. WARN ((0, 0,
  687. _("Cannot backspace archive file; it may be unreadable without -i")));
  688. /* Replace the first part of the record with NULs. */
  689. if (record_start->buffer != output_start)
  690. memset (record_start->buffer, 0,
  691. output_start - record_start->buffer);
  692. }
  693. }
  694. }
  695. off_t
  696. seek_archive (off_t size)
  697. {
  698. off_t start = current_block_ordinal ();
  699. off_t offset;
  700. off_t nrec, nblk;
  701. off_t skipped = (blocking_factor - (current_block - record_start));
  702. size -= skipped * BLOCKSIZE;
  703. if (size < record_size)
  704. return 0;
  705. /* FIXME: flush? */
  706. /* Compute number of records to skip */
  707. nrec = size / record_size;
  708. offset = rmtlseek (archive, nrec * record_size, SEEK_CUR);
  709. if (offset < 0)
  710. return offset;
  711. if (offset % record_size)
  712. FATAL_ERROR ((0, 0, _("rmtlseek not stopped at a record boundary")));
  713. /* Convert to number of records */
  714. offset /= BLOCKSIZE;
  715. /* Compute number of skipped blocks */
  716. nblk = offset - start;
  717. /* Update buffering info */
  718. records_read += nblk / blocking_factor;
  719. record_start_block = offset - blocking_factor;
  720. current_block = record_end;
  721. return nblk;
  722. }
  723. /* Close the archive file. */
  724. void
  725. close_archive (void)
  726. {
  727. if (time_to_start_writing || access_mode == ACCESS_WRITE)
  728. {
  729. flush_archive ();
  730. if (current_block > record_start)
  731. flush_archive ();
  732. }
  733. compute_duration ();
  734. if (verify_option)
  735. verify_volume ();
  736. if (rmtclose (archive) != 0)
  737. close_error (*archive_name_cursor);
  738. sys_wait_for_child (child_pid, hit_eof);
  739. tar_stat_destroy (&current_stat_info);
  740. if (save_name)
  741. free (save_name);
  742. if (real_s_name)
  743. free (real_s_name);
  744. free (record_buffer[0]);
  745. free (record_buffer[1]);
  746. }
  747. /* Called to initialize the global volume number. */
  748. void
  749. init_volume_number (void)
  750. {
  751. FILE *file = fopen (volno_file_option, "r");
  752. if (file)
  753. {
  754. if (fscanf (file, "%d", &global_volno) != 1
  755. || global_volno < 0)
  756. FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
  757. quotearg_colon (volno_file_option)));
  758. if (ferror (file))
  759. read_error (volno_file_option);
  760. if (fclose (file) != 0)
  761. close_error (volno_file_option);
  762. }
  763. else if (errno != ENOENT)
  764. open_error (volno_file_option);
  765. }
  766. /* Called to write out the closing global volume number. */
  767. void
  768. closeout_volume_number (void)
  769. {
  770. FILE *file = fopen (volno_file_option, "w");
  771. if (file)
  772. {
  773. fprintf (file, "%d\n", global_volno);
  774. if (ferror (file))
  775. write_error (volno_file_option);
  776. if (fclose (file) != 0)
  777. close_error (volno_file_option);
  778. }
  779. else
  780. open_error (volno_file_option);
  781. }
  782. static void
  783. increase_volume_number ()
  784. {
  785. global_volno++;
  786. if (global_volno < 0)
  787. FATAL_ERROR ((0, 0, _("Volume number overflow")));
  788. volno++;
  789. }
  790. void
  791. change_tape_menu (FILE *read_file)
  792. {
  793. char *input_buffer = NULL;
  794. size_t size = 0;
  795. bool stop = false;
  796. while (!stop)
  797. {
  798. fputc ('\007', stderr);
  799. fprintf (stderr,
  800. _("Prepare volume #%d for %s and hit return: "),
  801. global_volno + 1, quote (*archive_name_cursor));
  802. fflush (stderr);
  803. if (getline (&input_buffer, &size, read_file) <= 0)
  804. {
  805. WARN ((0, 0, _("EOF where user reply was expected")));
  806. if (subcommand_option != EXTRACT_SUBCOMMAND
  807. && subcommand_option != LIST_SUBCOMMAND
  808. && subcommand_option != DIFF_SUBCOMMAND)
  809. WARN ((0, 0, _("WARNING: Archive is incomplete")));
  810. fatal_exit ();
  811. }
  812. if (input_buffer[0] == '\n'
  813. || input_buffer[0] == 'y'
  814. || input_buffer[0] == 'Y')
  815. break;
  816. switch (input_buffer[0])
  817. {
  818. case '?':
  819. {
  820. fprintf (stderr, _("\
  821. n name Give a new file name for the next (and subsequent) volume(s)\n\
  822. q Abort tar\n\
  823. y or newline Continue operation\n"));
  824. if (!restrict_option)
  825. fprintf (stderr, _(" ! Spawn a subshell\n"));
  826. fprintf (stderr, _(" ? Print this list\n"));
  827. }
  828. break;
  829. case 'q':
  830. /* Quit. */
  831. WARN ((0, 0, _("No new volume; exiting.\n")));
  832. if (subcommand_option != EXTRACT_SUBCOMMAND
  833. && subcommand_option != LIST_SUBCOMMAND
  834. && subcommand_option != DIFF_SUBCOMMAND)
  835. WARN ((0, 0, _("WARNING: Archive is incomplete")));
  836. fatal_exit ();
  837. case 'n':
  838. /* Get new file name. */
  839. {
  840. char *name;
  841. char *cursor;
  842. for (name = input_buffer + 1;
  843. *name == ' ' || *name == '\t';
  844. name++)
  845. ;
  846. for (cursor = name; *cursor && *cursor != '\n'; cursor++)
  847. ;
  848. *cursor = '\0';
  849. if (name[0])
  850. {
  851. /* FIXME: the following allocation is never reclaimed. */
  852. *archive_name_cursor = xstrdup (name);
  853. stop = true;
  854. }
  855. else
  856. fprintf (stderr, "%s",
  857. _("File name not specified. Try again.\n"));
  858. }
  859. break;
  860. case '!':
  861. if (!restrict_option)
  862. {
  863. sys_spawn_shell ();
  864. break;
  865. }
  866. /* FALL THROUGH */
  867. default:
  868. fprintf (stderr, _("Invalid input. Type ? for help.\n"));
  869. }
  870. }
  871. free (input_buffer);
  872. }
  873. /* We've hit the end of the old volume. Close it and open the next one.
  874. Return nonzero on success.
  875. */
  876. static bool
  877. new_volume (enum access_mode mode)
  878. {
  879. static FILE *read_file;
  880. static int looped;
  881. int prompt;
  882. if (!read_file && !info_script_option)
  883. /* FIXME: if fopen is used, it will never be closed. */
  884. read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
  885. if (now_verifying)
  886. return false;
  887. if (verify_option)
  888. verify_volume ();
  889. assign_string (&volume_label, NULL);
  890. assign_string (&continued_file_name, NULL);
  891. continued_file_size = continued_file_offset = 0;
  892. current_block = record_start;
  893. if (rmtclose (archive) != 0)
  894. close_error (*archive_name_cursor);
  895. archive_name_cursor++;
  896. if (archive_name_cursor == archive_name_array + archive_names)
  897. {
  898. archive_name_cursor = archive_name_array;
  899. looped = 1;
  900. }
  901. prompt = looped;
  902. tryagain:
  903. if (prompt)
  904. {
  905. /* We have to prompt from now on. */
  906. if (info_script_option)
  907. {
  908. if (volno_file_option)
  909. closeout_volume_number ();
  910. if (sys_exec_info_script (archive_name_cursor, global_volno+1))
  911. FATAL_ERROR ((0, 0, _("%s command failed"),
  912. quote (info_script_option)));
  913. }
  914. else
  915. change_tape_menu (read_file);
  916. }
  917. if (strcmp (archive_name_cursor[0], "-") == 0)
  918. {
  919. read_full_records = true;
  920. archive = STDIN_FILENO;
  921. }
  922. else if (verify_option)
  923. archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
  924. rsh_command_option);
  925. else
  926. switch (mode)
  927. {
  928. case ACCESS_READ:
  929. archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
  930. rsh_command_option);
  931. guess_seekable_archive ();
  932. break;
  933. case ACCESS_WRITE:
  934. if (backup_option)
  935. maybe_backup_file (*archive_name_cursor, 1);
  936. archive = rmtcreat (*archive_name_cursor, MODE_RW,
  937. rsh_command_option);
  938. break;
  939. case ACCESS_UPDATE:
  940. archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
  941. rsh_command_option);
  942. break;
  943. }
  944. if (archive < 0)
  945. {
  946. open_warn (*archive_name_cursor);
  947. if (!verify_option && mode == ACCESS_WRITE && backup_option)
  948. undo_last_backup ();
  949. prompt = 1;
  950. goto tryagain;
  951. }
  952. SET_BINARY_MODE (archive);
  953. return true;
  954. }
  955. static bool
  956. read_header0 (struct tar_stat_info *info)
  957. {
  958. enum read_header rc;
  959. tar_stat_init (info);
  960. rc = read_header_primitive (false, info);
  961. if (rc == HEADER_SUCCESS)
  962. {
  963. set_next_block_after (current_header);
  964. return true;
  965. }
  966. ERROR ((0, 0, _("This does not look like a tar archive")));
  967. return false;
  968. }
  969. bool
  970. try_new_volume ()
  971. {
  972. size_t status;
  973. union block *header;
  974. enum access_mode acc;
  975. switch (subcommand_option)
  976. {
  977. case APPEND_SUBCOMMAND:
  978. case CAT_SUBCOMMAND:
  979. case UPDATE_SUBCOMMAND:
  980. acc = ACCESS_UPDATE;
  981. break;
  982. default:
  983. acc = ACCESS_READ;
  984. break;
  985. }
  986. if (!new_volume (acc))
  987. return true;
  988. while ((status = rmtread (archive, record_start->buffer, record_size))
  989. == SAFE_READ_ERROR)
  990. archive_read_error ();
  991. if (status != record_size)
  992. short_read (status);
  993. header = find_next_block ();
  994. if (!header)
  995. return false;
  996. switch (header->header.typeflag)
  997. {
  998. case XGLTYPE:
  999. {
  1000. if (!read_header0 (&dummy))
  1001. return false;
  1002. xheader_decode (&dummy); /* decodes values from the global header */
  1003. tar_stat_destroy (&dummy);
  1004. if (!real_s_name)
  1005. {
  1006. /* We have read the extended header of the first member in
  1007. this volume. Put it back, so next read_header works as
  1008. expected. */
  1009. current_block = record_start;
  1010. }
  1011. break;
  1012. }
  1013. case GNUTYPE_VOLHDR:
  1014. if (!read_header0 (&dummy))
  1015. return false;
  1016. tar_stat_destroy (&dummy);
  1017. assign_string (&volume_label, current_header->header.name);
  1018. set_next_block_after (header);
  1019. header = find_next_block ();
  1020. if (header->header.typeflag != GNUTYPE_MULTIVOL)
  1021. break;
  1022. /* FALL THROUGH */
  1023. case GNUTYPE_MULTIVOL:
  1024. if (!read_header0 (&dummy))
  1025. return false;
  1026. tar_stat_destroy (&dummy);
  1027. assign_string (&continued_file_name, current_header->header.name);
  1028. continued_file_size =
  1029. UINTMAX_FROM_HEADER (current_header->header.size);
  1030. continued_file_offset =
  1031. UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset);
  1032. break;
  1033. default:
  1034. break;
  1035. }
  1036. if (real_s_name)
  1037. {
  1038. uintmax_t s;
  1039. if (!continued_file_name
  1040. || strcmp (continued_file_name, real_s_name))
  1041. {
  1042. if ((archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
  1043. && strlen (real_s_name) >= NAME_FIELD_SIZE
  1044. && strncmp (continued_file_name, real_s_name,
  1045. NAME_FIELD_SIZE) == 0)
  1046. WARN ((0, 0,
  1047. _("%s is possibly continued on this volume: header contains truncated name"),
  1048. quote (real_s_name)));
  1049. else
  1050. {
  1051. WARN ((0, 0, _("%s is not continued on this volume"),
  1052. quote (real_s_name)));
  1053. return false;
  1054. }
  1055. }
  1056. s = continued_file_size + continued_file_offset;
  1057. if (real_s_totsize != s || s < continued_file_offset)
  1058. {
  1059. char totsizebuf[UINTMAX_STRSIZE_BOUND];
  1060. char s1buf[UINTMAX_STRSIZE_BOUND];
  1061. char s2buf[UINTMAX_STRSIZE_BOUND];
  1062. WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
  1063. quote (continued_file_name),
  1064. STRINGIFY_BIGINT (save_totsize, totsizebuf),
  1065. STRINGIFY_BIGINT (continued_file_size, s1buf),
  1066. STRINGIFY_BIGINT (continued_file_offset, s2buf)));
  1067. return false;
  1068. }
  1069. if (real_s_totsize - real_s_sizeleft != continued_file_offset)
  1070. {
  1071. char totsizebuf[UINTMAX_STRSIZE_BOUND];
  1072. char s1buf[UINTMAX_STRSIZE_BOUND];
  1073. char s2buf[UINTMAX_STRSIZE_BOUND];
  1074. WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
  1075. STRINGIFY_BIGINT (real_s_totsize, totsizebuf),
  1076. STRINGIFY_BIGINT (real_s_sizeleft, s1buf),
  1077. STRINGIFY_BIGINT (continued_file_offset, s2buf)));
  1078. return false;
  1079. }
  1080. }
  1081. increase_volume_number ();
  1082. return true;
  1083. }
  1084. /* Check the LABEL block against the volume label, seen as a globbing
  1085. pattern. Return true if the pattern matches. In case of failure,
  1086. retry matching a volume sequence number before giving up in
  1087. multi-volume mode. */
  1088. static bool
  1089. check_label_pattern (union block *label)
  1090. {
  1091. char *string;
  1092. bool result;
  1093. if (! memchr (label->header.name, '\0', sizeof label->header.name))
  1094. return false;
  1095. if (fnmatch (volume_label_option, label->header.name, 0) == 0)
  1096. return true;
  1097. if (!multi_volume_option)
  1098. return false;
  1099. string = xmalloc (strlen (volume_label_option)
  1100. + sizeof VOLUME_LABEL_APPEND + 1);
  1101. strcpy (string, volume_label_option);
  1102. strcat (string, VOLUME_LABEL_APPEND);
  1103. result = fnmatch (string, label->header.name, 0) == 0;
  1104. free (string);
  1105. return result;
  1106. }
  1107. /* Check if the next block contains a volume label and if this matches
  1108. the one given in the command line */
  1109. static void
  1110. match_volume_label (void)
  1111. {
  1112. union block *label = find_next_block ();
  1113. if (!label)
  1114. FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
  1115. quote (volume_label_option)));
  1116. if (!check_label_pattern (label))
  1117. FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
  1118. quote_n (0, label->header.name),
  1119. quote_n (1, volume_label_option)));
  1120. }
  1121. /* Mark the archive with volume label STR. */
  1122. static void
  1123. _write_volume_label (const char *str)
  1124. {
  1125. if (archive_format == POSIX_FORMAT)
  1126. xheader_store ("GNU.volume.label", &dummy, str);
  1127. else
  1128. {
  1129. union block *label = find_next_block ();
  1130. memset (label, 0, BLOCKSIZE);
  1131. strcpy (label->header.name, str);
  1132. assign_string (&current_stat_info.file_name,
  1133. label->header.name);
  1134. current_stat_info.had_trailing_slash =
  1135. strip_trailing_slashes (current_stat_info.file_name);
  1136. label->header.typeflag = GNUTYPE_VOLHDR;
  1137. TIME_TO_CHARS (start_time.tv_sec, label->header.mtime);
  1138. finish_header (&current_stat_info, label, -1);
  1139. set_next_block_after (label);
  1140. }
  1141. }
  1142. #define VOL_SUFFIX "Volume"
  1143. /* Add a volume label to a part of multi-volume archive */
  1144. static void
  1145. add_volume_label (void)
  1146. {
  1147. char buf[UINTMAX_STRSIZE_BOUND];
  1148. char *p = STRINGIFY_BIGINT (volno, buf);
  1149. char *s = xmalloc (strlen (volume_label_option) + sizeof VOL_SUFFIX
  1150. + strlen (p) + 2);
  1151. sprintf (s, "%s %s %s", volume_label_option, VOL_SUFFIX, p);
  1152. _write_volume_label (s);
  1153. free (s);
  1154. }
  1155. static void
  1156. add_chunk_header ()
  1157. {
  1158. if (archive_format == POSIX_FORMAT)
  1159. {
  1160. off_t block_ordinal;
  1161. union block *blk;
  1162. struct tar_stat_info st;
  1163. static size_t real_s_part_no; /* FIXME */
  1164. real_s_part_no++;
  1165. memset (&st, 0, sizeof st);
  1166. st.orig_file_name = st.file_name = real_s_name;
  1167. st.stat.st_mode = S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
  1168. st.stat.st_uid = getuid ();
  1169. st.stat.st_gid = getgid ();
  1170. st.orig_file_name = xheader_format_name (&st,
  1171. "%d/GNUFileParts.%p/%f.%n",
  1172. real_s_part_no);
  1173. st.file_name = st.orig_file_name;
  1174. st.archive_file_size = st.stat.st_size = real_s_sizeleft;
  1175. block_ordinal = current_block_ordinal ();
  1176. blk = start_header (&st);
  1177. if (!blk)
  1178. abort (); /* FIXME */
  1179. finish_header (&st, blk, block_ordinal);
  1180. free (st.orig_file_name);
  1181. }
  1182. }
  1183. /* Add a volume label to the current archive */
  1184. static void
  1185. write_volume_label (void)
  1186. {
  1187. if (multi_volume_option)
  1188. add_volume_label ();
  1189. else
  1190. _write_volume_label (volume_label_option);
  1191. }
  1192. /* Write GNU multi-volume header */
  1193. static void
  1194. gnu_add_multi_volume_header (void)
  1195. {
  1196. int tmp;
  1197. union block *block = find_next_block ();
  1198. if (strlen (real_s_name) > NAME_FIELD_SIZE)
  1199. WARN ((0, 0,
  1200. _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
  1201. quotearg_colon (real_s_name)));
  1202. memset (block, 0, BLOCKSIZE);
  1203. /* FIXME: Michael P Urban writes: [a long name file] is being written
  1204. when a new volume rolls around [...] Looks like the wrong value is
  1205. being preserved in real_s_name, though. */
  1206. strncpy (block->header.name, real_s_name, NAME_FIELD_SIZE);
  1207. block->header.typeflag = GNUTYPE_MULTIVOL;
  1208. OFF_TO_CHARS (real_s_sizeleft, block->header.size);
  1209. OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
  1210. block->oldgnu_header.offset);
  1211. tmp = verbose_option;
  1212. verbose_option = 0;
  1213. finish_header (&current_stat_info, block, -1);
  1214. verbose_option = tmp;
  1215. set_next_block_after (block);
  1216. }
  1217. /* Add a multi volume header to the current archive. The exact header format
  1218. depends on the archive format. */
  1219. static void
  1220. add_multi_volume_header (void)
  1221. {
  1222. if (archive_format == POSIX_FORMAT)
  1223. {
  1224. off_t d = real_s_totsize - real_s_sizeleft;
  1225. xheader_store ("GNU.volume.filename", &dummy, real_s_name);
  1226. xheader_store ("GNU.volume.size", &dummy, &real_s_sizeleft);
  1227. xheader_store ("GNU.volume.offset", &dummy, &d);
  1228. }
  1229. else
  1230. gnu_add_multi_volume_header ();
  1231. }
  1232. /* Synchronize multi-volume globals */
  1233. static void
  1234. multi_volume_sync ()
  1235. {
  1236. if (multi_volume_option)
  1237. {
  1238. if (save_name)
  1239. {
  1240. assign_string (&real_s_name,
  1241. safer_name_suffix (save_name, false,
  1242. absolute_names_option));
  1243. real_s_totsize = save_totsize;
  1244. real_s_sizeleft = save_sizeleft;
  1245. }
  1246. else
  1247. {
  1248. assign_string (&real_s_name, 0);
  1249. real_s_totsize = 0;
  1250. real_s_sizeleft = 0;
  1251. }
  1252. }
  1253. }
  1254. /* Low-level flush functions */
  1255. /* Simple flush read (no multi-volume or label extensions) */
  1256. static void
  1257. simple_flush_read (void)
  1258. {
  1259. size_t status; /* result from system call */
  1260. checkpoint_run (false);
  1261. /* Clear the count of errors. This only applies to a single call to
  1262. flush_read. */
  1263. read_error_count = 0; /* clear error count */
  1264. if (write_archive_to_stdout && record_start_block != 0)
  1265. {
  1266. archive = STDOUT_FILENO;
  1267. status = sys_write_archive_buffer ();
  1268. archive = STDIN_FILENO;
  1269. if (status != record_size)
  1270. archive_write_error (status);
  1271. }
  1272. for (;;)
  1273. {
  1274. status = rmtread (archive, record_start->buffer, record_size);
  1275. if (status == record_size)
  1276. {
  1277. records_read++;
  1278. return;
  1279. }
  1280. if (status == SAFE_READ_ERROR)
  1281. {
  1282. archive_read_error ();
  1283. continue; /* try again */
  1284. }
  1285. break;
  1286. }
  1287. short_read (status);
  1288. }
  1289. /* Simple flush write (no multi-volume or label extensions) */
  1290. static void
  1291. simple_flush_write (size_t level __attribute__((unused)))
  1292. {
  1293. ssize_t status;
  1294. status = _flush_write ();
  1295. if (status != record_size)
  1296. archive_write_error (status);
  1297. else
  1298. {
  1299. records_written++;
  1300. bytes_written += status;
  1301. }
  1302. }
  1303. /* GNU flush functions. These support multi-volume and archive labels in
  1304. GNU and PAX archive formats. */
  1305. static void
  1306. _gnu_flush_read (void)
  1307. {
  1308. size_t status; /* result from system call */
  1309. checkpoint_run (false);
  1310. /* Clear the count of errors. This only applies to a single call to
  1311. flush_read. */
  1312. read_error_count = 0; /* clear error count */
  1313. if (write_archive_to_stdout && record_start_block != 0)
  1314. {
  1315. archive = STDOUT_FILENO;
  1316. status = sys_write_archive_buffer ();
  1317. archive = STDIN_FILENO;
  1318. if (status != record_size)
  1319. archive_write_error (status);
  1320. }
  1321. multi_volume_sync ();
  1322. for (;;)
  1323. {
  1324. status = rmtread (archive, record_start->buffer, record_size);
  1325. if (status == record_size)
  1326. {
  1327. records_read++;
  1328. return;
  1329. }
  1330. /* The condition below used to include
  1331. || (status > 0 && !read_full_records)
  1332. This is incorrect since even if new_volume() succeeds, the
  1333. subsequent call to rmtread will overwrite the chunk of data
  1334. already read in the buffer, so the processing will fail */
  1335. if ((status == 0
  1336. || (status == SAFE_READ_ERROR && errno == ENOSPC))
  1337. && multi_volume_option)
  1338. {
  1339. while (!try_new_volume ())
  1340. ;
  1341. if (current_block == record_end)
  1342. /* Necessary for blocking_factor == 1 */
  1343. flush_archive();
  1344. return;
  1345. }
  1346. else if (status == SAFE_READ_ERROR)
  1347. {
  1348. archive_read_error ();
  1349. continue;
  1350. }
  1351. break;
  1352. }
  1353. short_read (status);
  1354. }
  1355. static void
  1356. gnu_flush_read (void)
  1357. {
  1358. flush_read_ptr = simple_flush_read; /* Avoid recursion */
  1359. _gnu_flush_read ();
  1360. flush_read_ptr = gnu_flush_read;
  1361. }
  1362. static void
  1363. _gnu_flush_write (size_t buffer_level)
  1364. {
  1365. ssize_t status;
  1366. union block *header;
  1367. char *copy_ptr;
  1368. size_t copy_size;
  1369. size_t bufsize;
  1370. tarlong wrt;
  1371. status = _flush_write ();
  1372. if (status != record_size && !multi_volume_option)
  1373. archive_write_error (status);
  1374. else
  1375. {
  1376. if (status)
  1377. records_written++;
  1378. bytes_written += status;
  1379. }
  1380. if (status == record_size)
  1381. {
  1382. multi_volume_sync ();
  1383. return;
  1384. }
  1385. if (status % BLOCKSIZE)
  1386. {
  1387. ERROR ((0, 0, _("write did not end on a block boundary")));
  1388. archive_write_error (status);
  1389. }
  1390. /* In multi-volume mode. */
  1391. /* ENXIO is for the UNIX PC. */
  1392. if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
  1393. archive_write_error (status);
  1394. real_s_sizeleft -= status;
  1395. if (!new_volume (ACCESS_WRITE))
  1396. return;
  1397. tar_stat_destroy (&dummy);
  1398. increase_volume_number ();
  1399. prev_written += bytes_written;
  1400. bytes_written = 0;
  1401. copy_ptr = record_start->buffer + status;
  1402. copy_size = buffer_level - status;
  1403. /* Switch to the next buffer */
  1404. record_index = !record_index;
  1405. init_buffer ();
  1406. if (volume_label_option)
  1407. add_volume_label ();
  1408. if (real_s_name)
  1409. add_multi_volume_header ();
  1410. write_extended (true, &dummy, find_next_block ());
  1411. tar_stat_destroy (&dummy);
  1412. if (real_s_name)
  1413. add_chunk_header ();
  1414. wrt = bytes_written;
  1415. header = find_next_block ();
  1416. bufsize = available_space_after (header);
  1417. while (bufsize < copy_size)
  1418. {
  1419. memcpy (header->buffer, copy_ptr, bufsize);
  1420. copy_ptr += bufsize;
  1421. copy_size -= bufsize;
  1422. set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
  1423. header = find_next_block ();
  1424. bufsize = available_space_after (header);
  1425. }
  1426. memcpy (header->buffer, copy_ptr, copy_size);
  1427. memset (header->buffer + copy_size, 0, bufsize - copy_size);
  1428. set_next_block_after (header + (copy_size - 1) / BLOCKSIZE);
  1429. if (multi_volume_option && wrt < bytes_written)
  1430. {
  1431. /* The value of bytes_written has changed while moving data;
  1432. that means that flush_archive was executed at least once in
  1433. between, and, as a consequence, copy_size bytes were not written
  1434. to disk. We need to update sizeleft variables to compensate for
  1435. that. */
  1436. save_sizeleft += copy_size;
  1437. multi_volume_sync ();
  1438. }
  1439. find_next_block ();
  1440. }
  1441. static void
  1442. gnu_flush_write (size_t buffer_level)
  1443. {
  1444. flush_write_ptr = simple_flush_write; /* Avoid recursion */
  1445. _gnu_flush_write (buffer_level);
  1446. flush_write_ptr = gnu_flush_write;
  1447. }
  1448. void
  1449. flush_read ()
  1450. {
  1451. flush_read_ptr ();
  1452. }
  1453. void
  1454. flush_write ()
  1455. {
  1456. flush_write_ptr (record_size);
  1457. }
  1458. void
  1459. open_archive (enum access_mode wanted_access)
  1460. {
  1461. flush_read_ptr = gnu_flush_read;
  1462. flush_write_ptr = gnu_flush_write;
  1463. _open_archive (wanted_access);
  1464. switch (wanted_access)
  1465. {
  1466. case ACCESS_READ:
  1467. if (volume_label_option)
  1468. match_volume_label ();
  1469. break;
  1470. case ACCESS_WRITE:
  1471. records_written = 0;
  1472. if (volume_label_option)
  1473. write_volume_label ();
  1474. break;
  1475. default:
  1476. break;
  1477. }
  1478. set_volume_start_time ();
  1479. }