4
0

buffer.c 51 KB

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