4
0

buffer.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  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, 2010 Free Software
  4. Foundation, Inc.
  5. Written by John Gilmore, on 1985-08-25.
  6. This program is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 3, or (at your option) any later
  9. version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  13. Public License for more details.
  14. You should have received a copy of the GNU General Public License along
  15. with this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  17. #include <system.h>
  18. #include <system-ioctl.h>
  19. #include <signal.h>
  20. #include <closeout.h>
  21. #include <fnmatch.h>
  22. #include <human.h>
  23. #include <quotearg.h>
  24. #include "common.h"
  25. #include <rmt.h>
  26. /* Number of retries before giving up on read. */
  27. #define READ_ERROR_MAX 10
  28. /* Variables. */
  29. static tarlong prev_written; /* bytes written on previous volumes */
  30. static tarlong bytes_written; /* bytes written on this volume */
  31. static void *record_buffer[2]; /* allocated memory */
  32. union block *record_buffer_aligned[2];
  33. static int record_index;
  34. /* FIXME: The following variables should ideally be static to this
  35. module. However, this cannot be done yet. The cleanup continues! */
  36. union block *record_start; /* start of record of archive */
  37. union block *record_end; /* last+1 block of archive record */
  38. union block *current_block; /* current block of archive */
  39. enum access_mode access_mode; /* how do we handle the archive */
  40. off_t records_read; /* number of records read from this archive */
  41. off_t records_written; /* likewise, for records written */
  42. extern off_t records_skipped; /* number of records skipped at the start
  43. of the archive, defined in delete.c */
  44. static off_t record_start_block; /* block ordinal at record_start */
  45. /* Where we write list messages (not errors, not interactions) to. */
  46. FILE *stdlis;
  47. static void backspace_output (void);
  48. /* PID of child program, if compress_option or remote archive access. */
  49. static pid_t child_pid;
  50. /* Error recovery stuff */
  51. static int read_error_count;
  52. /* Have we hit EOF yet? */
  53. static bool hit_eof;
  54. static bool read_full_records = false;
  55. /* We're reading, but we just read the last block and it's time to update.
  56. Declared in update.c
  57. As least EXTERN like this one as possible. (?? --gray)
  58. FIXME: Either eliminate it or move it to common.h.
  59. */
  60. extern bool time_to_start_writing;
  61. bool write_archive_to_stdout;
  62. void (*flush_write_ptr) (size_t);
  63. void (*flush_read_ptr) (void);
  64. char *volume_label;
  65. char *continued_file_name;
  66. uintmax_t continued_file_size;
  67. uintmax_t continued_file_offset;
  68. static int volno = 1; /* which volume of a multi-volume tape we're
  69. on */
  70. static int global_volno = 1; /* volume number to print in external
  71. messages */
  72. bool write_archive_to_stdout;
  73. /* Used by flush_read and flush_write to store the real info about saved
  74. names. */
  75. static char *real_s_name;
  76. static off_t real_s_totsize;
  77. static off_t real_s_sizeleft;
  78. /* Multi-volume tracking support */
  79. static char *save_name; /* name of the file we are currently writing */
  80. static off_t save_totsize; /* total size of file we are writing, only
  81. valid if save_name is nonzero */
  82. static off_t save_sizeleft; /* where we are in the file we are writing,
  83. only valid if save_name is nonzero */
  84. static struct tar_stat_info dummy;
  85. void
  86. buffer_write_global_xheader ()
  87. {
  88. xheader_write_global (&dummy.xhdr);
  89. }
  90. void
  91. mv_begin (struct tar_stat_info *st)
  92. {
  93. if (multi_volume_option)
  94. {
  95. assign_string (&save_name, st->orig_file_name);
  96. save_totsize = save_sizeleft = st->stat.st_size;
  97. }
  98. }
  99. void
  100. mv_end ()
  101. {
  102. if (multi_volume_option)
  103. assign_string (&save_name, 0);
  104. }
  105. void
  106. mv_total_size (off_t size)
  107. {
  108. save_totsize = size;
  109. }
  110. void
  111. mv_size_left (off_t size)
  112. {
  113. save_sizeleft = size;
  114. }
  115. /* Functions. */
  116. void
  117. clear_read_error_count (void)
  118. {
  119. read_error_count = 0;
  120. }
  121. /* Time-related functions */
  122. double duration;
  123. void
  124. set_start_time ()
  125. {
  126. gettime (&start_time);
  127. volume_start_time = start_time;
  128. last_stat_time = start_time;
  129. }
  130. void
  131. set_volume_start_time ()
  132. {
  133. gettime (&volume_start_time);
  134. last_stat_time = volume_start_time;
  135. }
  136. void
  137. compute_duration ()
  138. {
  139. struct timespec now;
  140. gettime (&now);
  141. duration += ((now.tv_sec - last_stat_time.tv_sec)
  142. + (now.tv_nsec - last_stat_time.tv_nsec) / 1e9);
  143. gettime (&last_stat_time);
  144. }
  145. /* Compression detection */
  146. enum compress_type {
  147. ct_tar, /* Plain tar file */
  148. ct_none, /* Unknown compression type */
  149. ct_compress,
  150. ct_gzip,
  151. ct_bzip2,
  152. ct_lzma,
  153. ct_lzop,
  154. ct_xz
  155. };
  156. struct zip_magic
  157. {
  158. enum compress_type type;
  159. size_t length;
  160. char *magic;
  161. char *program;
  162. char *option;
  163. };
  164. static struct zip_magic const magic[] = {
  165. { ct_tar },
  166. { ct_none, },
  167. { ct_compress, 2, "\037\235", COMPRESS_PROGRAM, "-Z" },
  168. { ct_gzip, 2, "\037\213", GZIP_PROGRAM, "-z" },
  169. { ct_bzip2, 3, "BZh", BZIP2_PROGRAM, "-j" },
  170. { ct_lzma, 6, "\xFFLZMA", LZMA_PROGRAM, "--lzma" },
  171. { ct_lzop, 4, "\211LZO", LZOP_PROGRAM, "--lzop" },
  172. { ct_xz, 6, "\0xFD7zXZ", XZ_PROGRAM, "-J" },
  173. };
  174. #define NMAGIC (sizeof(magic)/sizeof(magic[0]))
  175. #define compress_option(t) magic[t].option
  176. #define compress_program(t) magic[t].program
  177. /* Check if the file ARCHIVE is a compressed archive. */
  178. enum compress_type
  179. check_compressed_archive (bool *pshort)
  180. {
  181. struct zip_magic const *p;
  182. bool sfr;
  183. bool temp;
  184. if (!pshort)
  185. pshort = &temp;
  186. /* Prepare global data needed for find_next_block: */
  187. record_end = record_start; /* set up for 1st record = # 0 */
  188. sfr = read_full_records;
  189. read_full_records = true; /* Suppress fatal error on reading a partial
  190. record */
  191. *pshort = find_next_block () == 0;
  192. /* Restore global values */
  193. read_full_records = sfr;
  194. if (tar_checksum (record_start, true) == HEADER_SUCCESS)
  195. /* Probably a valid header */
  196. return ct_tar;
  197. for (p = magic + 2; p < magic + NMAGIC; p++)
  198. if (memcmp (record_start->buffer, p->magic, p->length) == 0)
  199. return p->type;
  200. return ct_none;
  201. }
  202. /* Guess if the archive is seekable. */
  203. static void
  204. guess_seekable_archive ()
  205. {
  206. struct stat st;
  207. if (subcommand_option == DELETE_SUBCOMMAND)
  208. {
  209. /* The current code in delete.c is based on the assumption that
  210. skip_member() reads all data from the archive. So, we should
  211. make sure it won't use seeks. On the other hand, the same code
  212. depends on the ability to backspace a record in the archive,
  213. so setting seekable_archive to false is technically incorrect.
  214. However, it is tested only in skip_member(), so it's not a
  215. problem. */
  216. seekable_archive = false;
  217. }
  218. if (seek_option != -1)
  219. {
  220. seekable_archive = !!seek_option;
  221. return;
  222. }
  223. if (!multi_volume_option && !use_compress_program_option
  224. && fstat (archive, &st) == 0)
  225. seekable_archive = S_ISREG (st.st_mode);
  226. else
  227. seekable_archive = false;
  228. }
  229. /* Open an archive named archive_name_array[0]. Detect if it is
  230. a compressed archive of known type and use corresponding decompression
  231. program if so */
  232. int
  233. open_compressed_archive ()
  234. {
  235. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  236. MODE_RW, rsh_command_option);
  237. if (archive == -1)
  238. return archive;
  239. if (!multi_volume_option)
  240. {
  241. if (!use_compress_program_option)
  242. {
  243. bool shortfile;
  244. enum compress_type type = check_compressed_archive (&shortfile);
  245. switch (type)
  246. {
  247. case ct_tar:
  248. if (shortfile)
  249. ERROR ((0, 0, _("This does not look like a tar archive")));
  250. return archive;
  251. case ct_none:
  252. if (shortfile)
  253. ERROR ((0, 0, _("This does not look like a tar archive")));
  254. set_comression_program_by_suffix (archive_name_array[0], NULL);
  255. if (!use_compress_program_option)
  256. return archive;
  257. break;
  258. default:
  259. use_compress_program_option = compress_program (type);
  260. break;
  261. }
  262. }
  263. /* FD is not needed any more */
  264. rmtclose (archive);
  265. hit_eof = false; /* It might have been set by find_next_block in
  266. check_compressed_archive */
  267. /* Open compressed archive */
  268. child_pid = sys_child_open_for_uncompress ();
  269. read_full_records = true;
  270. }
  271. records_read = 0;
  272. record_end = record_start; /* set up for 1st record = # 0 */
  273. return archive;
  274. }
  275. static void
  276. print_stats (FILE *fp, const char *text, tarlong numbytes)
  277. {
  278. char bytes[sizeof (tarlong) * CHAR_BIT];
  279. char abbr[LONGEST_HUMAN_READABLE + 1];
  280. char rate[LONGEST_HUMAN_READABLE + 1];
  281. int human_opts = human_autoscale | human_base_1024 | human_SI | human_B;
  282. sprintf (bytes, TARLONG_FORMAT, numbytes);
  283. fprintf (fp, "%s: %s (%s, %s/s)\n",
  284. text, bytes,
  285. human_readable (numbytes, abbr, human_opts, 1, 1),
  286. (0 < duration && numbytes / duration < (uintmax_t) -1
  287. ? human_readable (numbytes / duration, rate, human_opts, 1, 1)
  288. : "?"));
  289. }
  290. void
  291. print_total_stats ()
  292. {
  293. switch (subcommand_option)
  294. {
  295. case CREATE_SUBCOMMAND:
  296. case CAT_SUBCOMMAND:
  297. case UPDATE_SUBCOMMAND:
  298. case APPEND_SUBCOMMAND:
  299. /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
  300. print_stats (stderr, _("Total bytes written"),
  301. prev_written + bytes_written);
  302. break;
  303. case DELETE_SUBCOMMAND:
  304. {
  305. char buf[UINTMAX_STRSIZE_BOUND];
  306. print_stats (stderr, _("Total bytes read"),
  307. records_read * record_size);
  308. print_stats (stderr, _("Total bytes written"),
  309. prev_written + bytes_written);
  310. fprintf (stderr, _("Total bytes deleted: %s\n"),
  311. STRINGIFY_BIGINT ((records_read - records_skipped)
  312. * record_size
  313. - (prev_written + bytes_written), buf));
  314. }
  315. break;
  316. case EXTRACT_SUBCOMMAND:
  317. case LIST_SUBCOMMAND:
  318. case DIFF_SUBCOMMAND:
  319. print_stats (stderr, _("Total bytes read"),
  320. records_read * record_size);
  321. break;
  322. default:
  323. abort ();
  324. }
  325. }
  326. /* Compute and return the block ordinal at current_block. */
  327. off_t
  328. current_block_ordinal (void)
  329. {
  330. return record_start_block + (current_block - record_start);
  331. }
  332. /* If the EOF flag is set, reset it, as well as current_block, etc. */
  333. void
  334. reset_eof (void)
  335. {
  336. if (hit_eof)
  337. {
  338. hit_eof = false;
  339. current_block = record_start;
  340. record_end = record_start + blocking_factor;
  341. access_mode = ACCESS_WRITE;
  342. }
  343. }
  344. /* Return the location of the next available input or output block.
  345. Return zero for EOF. Once we have returned zero, we just keep returning
  346. it, to avoid accidentally going on to the next file on the tape. */
  347. union block *
  348. find_next_block (void)
  349. {
  350. if (current_block == record_end)
  351. {
  352. if (hit_eof)
  353. return 0;
  354. flush_archive ();
  355. if (current_block == record_end)
  356. {
  357. hit_eof = true;
  358. return 0;
  359. }
  360. }
  361. return current_block;
  362. }
  363. /* Indicate that we have used all blocks up thru BLOCK. */
  364. void
  365. set_next_block_after (union block *block)
  366. {
  367. while (block >= current_block)
  368. current_block++;
  369. /* Do *not* flush the archive here. If we do, the same argument to
  370. set_next_block_after could mean the next block (if the input record
  371. is exactly one block long), which is not what is intended. */
  372. if (current_block > record_end)
  373. abort ();
  374. }
  375. /* Return the number of bytes comprising the space between POINTER
  376. through the end of the current buffer of blocks. This space is
  377. available for filling with data, or taking data from. POINTER is
  378. usually (but not always) the result of previous find_next_block call. */
  379. size_t
  380. available_space_after (union block *pointer)
  381. {
  382. return record_end->buffer - pointer->buffer;
  383. }
  384. /* Close file having descriptor FD, and abort if close unsuccessful. */
  385. void
  386. xclose (int fd)
  387. {
  388. if (close (fd) != 0)
  389. close_error (_("(pipe)"));
  390. }
  391. static void
  392. init_buffer ()
  393. {
  394. if (! record_buffer_aligned[record_index])
  395. record_buffer_aligned[record_index] =
  396. page_aligned_alloc (&record_buffer[record_index], record_size);
  397. record_start = record_buffer_aligned[record_index];
  398. current_block = record_start;
  399. record_end = record_start + blocking_factor;
  400. }
  401. /* Open an archive file. The argument specifies whether we are
  402. reading or writing, or both. */
  403. static void
  404. _open_archive (enum access_mode wanted_access)
  405. {
  406. int backed_up_flag = 0;
  407. if (record_size == 0)
  408. FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
  409. if (archive_names == 0)
  410. FATAL_ERROR ((0, 0, _("No archive name given")));
  411. tar_stat_destroy (&current_stat_info);
  412. save_name = 0;
  413. real_s_name = 0;
  414. record_index = 0;
  415. init_buffer ();
  416. /* When updating the archive, we start with reading. */
  417. access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
  418. read_full_records = read_full_records_option;
  419. records_read = 0;
  420. if (use_compress_program_option)
  421. {
  422. switch (wanted_access)
  423. {
  424. case ACCESS_READ:
  425. child_pid = sys_child_open_for_uncompress ();
  426. read_full_records = true;
  427. record_end = record_start; /* set up for 1st record = # 0 */
  428. break;
  429. case ACCESS_WRITE:
  430. child_pid = sys_child_open_for_compress ();
  431. break;
  432. case ACCESS_UPDATE:
  433. abort (); /* Should not happen */
  434. break;
  435. }
  436. if (!index_file_name
  437. && wanted_access == ACCESS_WRITE
  438. && strcmp (archive_name_array[0], "-") == 0)
  439. stdlis = stderr;
  440. }
  441. else if (strcmp (archive_name_array[0], "-") == 0)
  442. {
  443. read_full_records = true; /* could be a pipe, be safe */
  444. if (verify_option)
  445. FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
  446. switch (wanted_access)
  447. {
  448. case ACCESS_READ:
  449. {
  450. bool shortfile;
  451. enum compress_type type;
  452. archive = STDIN_FILENO;
  453. type = check_compressed_archive (&shortfile);
  454. if (type != ct_tar && type != ct_none)
  455. FATAL_ERROR ((0, 0,
  456. _("Archive is compressed. Use %s option"),
  457. compress_option (type)));
  458. if (shortfile)
  459. ERROR ((0, 0, _("This does not look like a tar archive")));
  460. }
  461. break;
  462. case ACCESS_WRITE:
  463. archive = STDOUT_FILENO;
  464. if (!index_file_name)
  465. stdlis = stderr;
  466. break;
  467. case ACCESS_UPDATE:
  468. archive = STDIN_FILENO;
  469. write_archive_to_stdout = true;
  470. record_end = record_start; /* set up for 1st record = # 0 */
  471. if (!index_file_name)
  472. stdlis = stderr;
  473. break;
  474. }
  475. }
  476. else if (verify_option)
  477. archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  478. MODE_RW, rsh_command_option);
  479. else
  480. switch (wanted_access)
  481. {
  482. case ACCESS_READ:
  483. archive = open_compressed_archive ();
  484. if (archive >= 0)
  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 (&current_header, info, read_header_auto);
  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. tar_stat_init (&dummy);
  1001. if (read_header (&header, &dummy, read_header_x_global)
  1002. != HEADER_SUCCESS_EXTENDED)
  1003. {
  1004. ERROR ((0, 0, _("This does not look like a tar archive")));
  1005. return false;
  1006. }
  1007. xheader_decode (&dummy); /* decodes values from the global header */
  1008. tar_stat_destroy (&dummy);
  1009. /* The initial global header must be immediately followed by
  1010. an extended PAX header for the first member in this volume.
  1011. However, in some cases tar may split volumes in the middle
  1012. of a PAX header. This is incorrect, and should be fixed
  1013. in the future versions. In the meantime we must be
  1014. prepared to correctly list and extract such archives.
  1015. If this happens, the following call to read_header returns
  1016. HEADER_FAILURE, which is ignored.
  1017. See also tests/multiv07.at */
  1018. switch (read_header (&header, &dummy, read_header_auto))
  1019. {
  1020. case HEADER_SUCCESS:
  1021. set_next_block_after (header);
  1022. break;
  1023. case HEADER_FAILURE:
  1024. break;
  1025. default:
  1026. ERROR ((0, 0, _("This does not look like a tar archive")));
  1027. return false;
  1028. }
  1029. break;
  1030. }
  1031. case GNUTYPE_VOLHDR:
  1032. if (!read_header0 (&dummy))
  1033. return false;
  1034. tar_stat_destroy (&dummy);
  1035. assign_string (&volume_label, current_header->header.name);
  1036. set_next_block_after (header);
  1037. header = find_next_block ();
  1038. if (header->header.typeflag != GNUTYPE_MULTIVOL)
  1039. break;
  1040. /* FALL THROUGH */
  1041. case GNUTYPE_MULTIVOL:
  1042. if (!read_header0 (&dummy))
  1043. return false;
  1044. tar_stat_destroy (&dummy);
  1045. assign_string (&continued_file_name, current_header->header.name);
  1046. continued_file_size =
  1047. UINTMAX_FROM_HEADER (current_header->header.size);
  1048. continued_file_offset =
  1049. UINTMAX_FROM_HEADER (current_header->oldgnu_header.offset);
  1050. break;
  1051. default:
  1052. break;
  1053. }
  1054. if (real_s_name)
  1055. {
  1056. uintmax_t s;
  1057. if (!continued_file_name
  1058. || strcmp (continued_file_name, real_s_name))
  1059. {
  1060. if ((archive_format == GNU_FORMAT || archive_format == OLDGNU_FORMAT)
  1061. && strlen (real_s_name) >= NAME_FIELD_SIZE
  1062. && strncmp (continued_file_name, real_s_name,
  1063. NAME_FIELD_SIZE) == 0)
  1064. WARN ((0, 0,
  1065. _("%s is possibly continued on this volume: header contains truncated name"),
  1066. quote (real_s_name)));
  1067. else
  1068. {
  1069. WARN ((0, 0, _("%s is not continued on this volume"),
  1070. quote (real_s_name)));
  1071. return false;
  1072. }
  1073. }
  1074. s = continued_file_size + continued_file_offset;
  1075. if (real_s_totsize != s || s < continued_file_offset)
  1076. {
  1077. char totsizebuf[UINTMAX_STRSIZE_BOUND];
  1078. char s1buf[UINTMAX_STRSIZE_BOUND];
  1079. char s2buf[UINTMAX_STRSIZE_BOUND];
  1080. WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
  1081. quote (continued_file_name),
  1082. STRINGIFY_BIGINT (save_totsize, totsizebuf),
  1083. STRINGIFY_BIGINT (continued_file_size, s1buf),
  1084. STRINGIFY_BIGINT (continued_file_offset, s2buf)));
  1085. return false;
  1086. }
  1087. if (real_s_totsize - real_s_sizeleft != continued_file_offset)
  1088. {
  1089. char totsizebuf[UINTMAX_STRSIZE_BOUND];
  1090. char s1buf[UINTMAX_STRSIZE_BOUND];
  1091. char s2buf[UINTMAX_STRSIZE_BOUND];
  1092. WARN ((0, 0, _("This volume is out of sequence (%s - %s != %s)"),
  1093. STRINGIFY_BIGINT (real_s_totsize, totsizebuf),
  1094. STRINGIFY_BIGINT (real_s_sizeleft, s1buf),
  1095. STRINGIFY_BIGINT (continued_file_offset, s2buf)));
  1096. return false;
  1097. }
  1098. }
  1099. increase_volume_number ();
  1100. return true;
  1101. }
  1102. #define VOLUME_TEXT " Volume "
  1103. #define VOLUME_TEXT_LEN (sizeof VOLUME_TEXT - 1)
  1104. char *
  1105. drop_volume_label_suffix (const char *label)
  1106. {
  1107. const char *p;
  1108. size_t len = strlen (label);
  1109. if (len < 1)
  1110. return NULL;
  1111. for (p = label + len - 1; p > label && isdigit ((unsigned char) *p); p--)
  1112. ;
  1113. if (p > label && p - (VOLUME_TEXT_LEN - 1) > label)
  1114. {
  1115. p -= VOLUME_TEXT_LEN - 1;
  1116. if (memcmp (p, VOLUME_TEXT, VOLUME_TEXT_LEN) == 0)
  1117. {
  1118. char *s = xmalloc ((len = p - label) + 1);
  1119. memcpy (s, label, len);
  1120. s[len] = 0;
  1121. return s;
  1122. }
  1123. }
  1124. return NULL;
  1125. }
  1126. /* Check LABEL against the volume label, seen as a globbing
  1127. pattern. Return true if the pattern matches. In case of failure,
  1128. retry matching a volume sequence number before giving up in
  1129. multi-volume mode. */
  1130. static bool
  1131. check_label_pattern (const char *label)
  1132. {
  1133. char *string;
  1134. bool result;
  1135. if (fnmatch (volume_label_option, label, 0) == 0)
  1136. return true;
  1137. if (!multi_volume_option)
  1138. return false;
  1139. string = drop_volume_label_suffix (label);
  1140. if (string)
  1141. {
  1142. result = fnmatch (string, volume_label_option, 0) == 0;
  1143. free (string);
  1144. }
  1145. return result;
  1146. }
  1147. /* Check if the next block contains a volume label and if this matches
  1148. the one given in the command line */
  1149. static void
  1150. match_volume_label (void)
  1151. {
  1152. if (!volume_label)
  1153. {
  1154. union block *label = find_next_block ();
  1155. if (!label)
  1156. FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
  1157. quote (volume_label_option)));
  1158. if (label->header.typeflag == GNUTYPE_VOLHDR)
  1159. {
  1160. if (memchr (label->header.name, '\0', sizeof label->header.name))
  1161. assign_string (&volume_label, label->header.name);
  1162. else
  1163. {
  1164. volume_label = xmalloc (sizeof (label->header.name) + 1);
  1165. memcpy (volume_label, label->header.name,
  1166. sizeof (label->header.name));
  1167. volume_label[sizeof (label->header.name)] = 0;
  1168. }
  1169. }
  1170. else if (label->header.typeflag == XGLTYPE)
  1171. {
  1172. struct tar_stat_info st;
  1173. tar_stat_init (&st);
  1174. xheader_read (&st.xhdr, label,
  1175. OFF_FROM_HEADER (label->header.size));
  1176. xheader_decode (&st);
  1177. tar_stat_destroy (&st);
  1178. }
  1179. }
  1180. if (!volume_label)
  1181. FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
  1182. quote (volume_label_option)));
  1183. if (!check_label_pattern (volume_label))
  1184. FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
  1185. quote_n (0, volume_label),
  1186. quote_n (1, volume_label_option)));
  1187. }
  1188. /* Mark the archive with volume label STR. */
  1189. static void
  1190. _write_volume_label (const char *str)
  1191. {
  1192. if (archive_format == POSIX_FORMAT)
  1193. xheader_store ("GNU.volume.label", &dummy, str);
  1194. else
  1195. {
  1196. union block *label = find_next_block ();
  1197. memset (label, 0, BLOCKSIZE);
  1198. strcpy (label->header.name, str);
  1199. assign_string (&current_stat_info.file_name,
  1200. label->header.name);
  1201. current_stat_info.had_trailing_slash =
  1202. strip_trailing_slashes (current_stat_info.file_name);
  1203. label->header.typeflag = GNUTYPE_VOLHDR;
  1204. TIME_TO_CHARS (start_time.tv_sec, label->header.mtime);
  1205. finish_header (&current_stat_info, label, -1);
  1206. set_next_block_after (label);
  1207. }
  1208. }
  1209. #define VOL_SUFFIX "Volume"
  1210. /* Add a volume label to a part of multi-volume archive */
  1211. static void
  1212. add_volume_label (void)
  1213. {
  1214. char buf[UINTMAX_STRSIZE_BOUND];
  1215. char *p = STRINGIFY_BIGINT (volno, buf);
  1216. char *s = xmalloc (strlen (volume_label_option) + sizeof VOL_SUFFIX
  1217. + strlen (p) + 2);
  1218. sprintf (s, "%s %s %s", volume_label_option, VOL_SUFFIX, p);
  1219. _write_volume_label (s);
  1220. free (s);
  1221. }
  1222. static void
  1223. add_chunk_header ()
  1224. {
  1225. if (archive_format == POSIX_FORMAT)
  1226. {
  1227. off_t block_ordinal;
  1228. union block *blk;
  1229. struct tar_stat_info st;
  1230. static size_t real_s_part_no; /* FIXME */
  1231. real_s_part_no++;
  1232. memset (&st, 0, sizeof st);
  1233. st.orig_file_name = st.file_name = real_s_name;
  1234. st.stat.st_mode = S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
  1235. st.stat.st_uid = getuid ();
  1236. st.stat.st_gid = getgid ();
  1237. st.orig_file_name = xheader_format_name (&st,
  1238. "%d/GNUFileParts.%p/%f.%n",
  1239. real_s_part_no);
  1240. st.file_name = st.orig_file_name;
  1241. st.archive_file_size = st.stat.st_size = real_s_sizeleft;
  1242. block_ordinal = current_block_ordinal ();
  1243. blk = start_header (&st);
  1244. if (!blk)
  1245. abort (); /* FIXME */
  1246. finish_header (&st, blk, block_ordinal);
  1247. free (st.orig_file_name);
  1248. }
  1249. }
  1250. /* Add a volume label to the current archive */
  1251. static void
  1252. write_volume_label (void)
  1253. {
  1254. if (multi_volume_option)
  1255. add_volume_label ();
  1256. else
  1257. _write_volume_label (volume_label_option);
  1258. }
  1259. /* Write GNU multi-volume header */
  1260. static void
  1261. gnu_add_multi_volume_header (void)
  1262. {
  1263. int tmp;
  1264. union block *block = find_next_block ();
  1265. if (strlen (real_s_name) > NAME_FIELD_SIZE)
  1266. WARN ((0, 0,
  1267. _("%s: file name too long to be stored in a GNU multivolume header, truncated"),
  1268. quotearg_colon (real_s_name)));
  1269. memset (block, 0, BLOCKSIZE);
  1270. /* FIXME: Michael P Urban writes: [a long name file] is being written
  1271. when a new volume rolls around [...] Looks like the wrong value is
  1272. being preserved in real_s_name, though. */
  1273. strncpy (block->header.name, real_s_name, NAME_FIELD_SIZE);
  1274. block->header.typeflag = GNUTYPE_MULTIVOL;
  1275. OFF_TO_CHARS (real_s_sizeleft, block->header.size);
  1276. OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
  1277. block->oldgnu_header.offset);
  1278. tmp = verbose_option;
  1279. verbose_option = 0;
  1280. finish_header (&current_stat_info, block, -1);
  1281. verbose_option = tmp;
  1282. set_next_block_after (block);
  1283. }
  1284. /* Add a multi volume header to the current archive. The exact header format
  1285. depends on the archive format. */
  1286. static void
  1287. add_multi_volume_header (void)
  1288. {
  1289. if (archive_format == POSIX_FORMAT)
  1290. {
  1291. off_t d = real_s_totsize - real_s_sizeleft;
  1292. xheader_store ("GNU.volume.filename", &dummy, real_s_name);
  1293. xheader_store ("GNU.volume.size", &dummy, &real_s_sizeleft);
  1294. xheader_store ("GNU.volume.offset", &dummy, &d);
  1295. }
  1296. else
  1297. gnu_add_multi_volume_header ();
  1298. }
  1299. /* Synchronize multi-volume globals */
  1300. static void
  1301. multi_volume_sync ()
  1302. {
  1303. if (multi_volume_option)
  1304. {
  1305. if (save_name)
  1306. {
  1307. assign_string (&real_s_name,
  1308. safer_name_suffix (save_name, false,
  1309. absolute_names_option));
  1310. real_s_totsize = save_totsize;
  1311. real_s_sizeleft = save_sizeleft;
  1312. }
  1313. else
  1314. {
  1315. assign_string (&real_s_name, 0);
  1316. real_s_totsize = 0;
  1317. real_s_sizeleft = 0;
  1318. }
  1319. }
  1320. }
  1321. /* Low-level flush functions */
  1322. /* Simple flush read (no multi-volume or label extensions) */
  1323. static void
  1324. simple_flush_read (void)
  1325. {
  1326. size_t status; /* result from system call */
  1327. checkpoint_run (false);
  1328. /* Clear the count of errors. This only applies to a single call to
  1329. flush_read. */
  1330. read_error_count = 0; /* clear error count */
  1331. if (write_archive_to_stdout && record_start_block != 0)
  1332. {
  1333. archive = STDOUT_FILENO;
  1334. status = sys_write_archive_buffer ();
  1335. archive = STDIN_FILENO;
  1336. if (status != record_size)
  1337. archive_write_error (status);
  1338. }
  1339. for (;;)
  1340. {
  1341. status = rmtread (archive, record_start->buffer, record_size);
  1342. if (status == record_size)
  1343. {
  1344. records_read++;
  1345. return;
  1346. }
  1347. if (status == SAFE_READ_ERROR)
  1348. {
  1349. archive_read_error ();
  1350. continue; /* try again */
  1351. }
  1352. break;
  1353. }
  1354. short_read (status);
  1355. }
  1356. /* Simple flush write (no multi-volume or label extensions) */
  1357. static void
  1358. simple_flush_write (size_t level __attribute__((unused)))
  1359. {
  1360. ssize_t status;
  1361. status = _flush_write ();
  1362. if (status != record_size)
  1363. archive_write_error (status);
  1364. else
  1365. {
  1366. records_written++;
  1367. bytes_written += status;
  1368. }
  1369. }
  1370. /* GNU flush functions. These support multi-volume and archive labels in
  1371. GNU and PAX archive formats. */
  1372. static void
  1373. _gnu_flush_read (void)
  1374. {
  1375. size_t status; /* result from system call */
  1376. checkpoint_run (false);
  1377. /* Clear the count of errors. This only applies to a single call to
  1378. flush_read. */
  1379. read_error_count = 0; /* clear error count */
  1380. if (write_archive_to_stdout && record_start_block != 0)
  1381. {
  1382. archive = STDOUT_FILENO;
  1383. status = sys_write_archive_buffer ();
  1384. archive = STDIN_FILENO;
  1385. if (status != record_size)
  1386. archive_write_error (status);
  1387. }
  1388. multi_volume_sync ();
  1389. for (;;)
  1390. {
  1391. status = rmtread (archive, record_start->buffer, record_size);
  1392. if (status == record_size)
  1393. {
  1394. records_read++;
  1395. return;
  1396. }
  1397. /* The condition below used to include
  1398. || (status > 0 && !read_full_records)
  1399. This is incorrect since even if new_volume() succeeds, the
  1400. subsequent call to rmtread will overwrite the chunk of data
  1401. already read in the buffer, so the processing will fail */
  1402. if ((status == 0
  1403. || (status == SAFE_READ_ERROR && errno == ENOSPC))
  1404. && multi_volume_option)
  1405. {
  1406. while (!try_new_volume ())
  1407. ;
  1408. if (current_block == record_end)
  1409. /* Necessary for blocking_factor == 1 */
  1410. flush_archive();
  1411. return;
  1412. }
  1413. else if (status == SAFE_READ_ERROR)
  1414. {
  1415. archive_read_error ();
  1416. continue;
  1417. }
  1418. break;
  1419. }
  1420. short_read (status);
  1421. }
  1422. static void
  1423. gnu_flush_read (void)
  1424. {
  1425. flush_read_ptr = simple_flush_read; /* Avoid recursion */
  1426. _gnu_flush_read ();
  1427. flush_read_ptr = gnu_flush_read;
  1428. }
  1429. static void
  1430. _gnu_flush_write (size_t buffer_level)
  1431. {
  1432. ssize_t status;
  1433. union block *header;
  1434. char *copy_ptr;
  1435. size_t copy_size;
  1436. size_t bufsize;
  1437. tarlong wrt;
  1438. status = _flush_write ();
  1439. if (status != record_size && !multi_volume_option)
  1440. archive_write_error (status);
  1441. else
  1442. {
  1443. if (status)
  1444. records_written++;
  1445. bytes_written += status;
  1446. }
  1447. if (status == record_size)
  1448. {
  1449. multi_volume_sync ();
  1450. return;
  1451. }
  1452. if (status % BLOCKSIZE)
  1453. {
  1454. ERROR ((0, 0, _("write did not end on a block boundary")));
  1455. archive_write_error (status);
  1456. }
  1457. /* In multi-volume mode. */
  1458. /* ENXIO is for the UNIX PC. */
  1459. if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
  1460. archive_write_error (status);
  1461. real_s_sizeleft -= status;
  1462. if (!new_volume (ACCESS_WRITE))
  1463. return;
  1464. tar_stat_destroy (&dummy);
  1465. increase_volume_number ();
  1466. prev_written += bytes_written;
  1467. bytes_written = 0;
  1468. copy_ptr = record_start->buffer + status;
  1469. copy_size = buffer_level - status;
  1470. /* Switch to the next buffer */
  1471. record_index = !record_index;
  1472. init_buffer ();
  1473. if (volume_label_option)
  1474. add_volume_label ();
  1475. if (real_s_name)
  1476. add_multi_volume_header ();
  1477. write_extended (true, &dummy, find_next_block ());
  1478. tar_stat_destroy (&dummy);
  1479. if (real_s_name)
  1480. add_chunk_header ();
  1481. wrt = bytes_written;
  1482. header = find_next_block ();
  1483. bufsize = available_space_after (header);
  1484. while (bufsize < copy_size)
  1485. {
  1486. memcpy (header->buffer, copy_ptr, bufsize);
  1487. copy_ptr += bufsize;
  1488. copy_size -= bufsize;
  1489. set_next_block_after (header + (bufsize - 1) / BLOCKSIZE);
  1490. header = find_next_block ();
  1491. bufsize = available_space_after (header);
  1492. }
  1493. memcpy (header->buffer, copy_ptr, copy_size);
  1494. memset (header->buffer + copy_size, 0, bufsize - copy_size);
  1495. set_next_block_after (header + (copy_size - 1) / BLOCKSIZE);
  1496. if (multi_volume_option && wrt < bytes_written)
  1497. {
  1498. /* The value of bytes_written has changed while moving data;
  1499. that means that flush_archive was executed at least once in
  1500. between, and, as a consequence, copy_size bytes were not written
  1501. to disk. We need to update sizeleft variables to compensate for
  1502. that. */
  1503. save_sizeleft += copy_size;
  1504. multi_volume_sync ();
  1505. }
  1506. find_next_block ();
  1507. }
  1508. static void
  1509. gnu_flush_write (size_t buffer_level)
  1510. {
  1511. flush_write_ptr = simple_flush_write; /* Avoid recursion */
  1512. _gnu_flush_write (buffer_level);
  1513. flush_write_ptr = gnu_flush_write;
  1514. }
  1515. void
  1516. flush_read ()
  1517. {
  1518. flush_read_ptr ();
  1519. }
  1520. void
  1521. flush_write ()
  1522. {
  1523. flush_write_ptr (record_size);
  1524. }
  1525. void
  1526. open_archive (enum access_mode wanted_access)
  1527. {
  1528. flush_read_ptr = gnu_flush_read;
  1529. flush_write_ptr = gnu_flush_write;
  1530. _open_archive (wanted_access);
  1531. switch (wanted_access)
  1532. {
  1533. case ACCESS_READ:
  1534. if (volume_label_option)
  1535. match_volume_label ();
  1536. break;
  1537. case ACCESS_WRITE:
  1538. records_written = 0;
  1539. if (volume_label_option)
  1540. write_volume_label ();
  1541. break;
  1542. default:
  1543. break;
  1544. }
  1545. set_volume_start_time ();
  1546. }