4
0

buffer.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. /* Buffer management for tar.
  2. Copyright 1988, 1992, 1993, 1994, 1996, 1997, 1999, 2000, 2001 Free
  3. Software Foundation, Inc.
  4. Written by John Gilmore, on 1985-08-25.
  5. This program is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option) any later
  8. version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. Public License for more details.
  13. You should have received a copy of the GNU General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  16. #include "system.h"
  17. #include <signal.h>
  18. #if MSDOS
  19. # include <process.h>
  20. #endif
  21. #if XENIX
  22. # include <sys/inode.h>
  23. #endif
  24. #include <fnmatch.h>
  25. #include <human.h>
  26. #include <quotearg.h>
  27. #include "common.h"
  28. #include "rmt.h"
  29. #define PREAD 0 /* read file descriptor from pipe() */
  30. #define PWRITE 1 /* write file descriptor from pipe() */
  31. /* Number of retries before giving up on read. */
  32. #define READ_ERROR_MAX 10
  33. /* Globbing pattern to append to volume label if initial match failed. */
  34. #define VOLUME_LABEL_APPEND " Volume [1-9]*"
  35. /* Variables. */
  36. static tarlong prev_written; /* bytes written on previous volumes */
  37. static tarlong bytes_written; /* bytes written on this volume */
  38. /* FIXME: The following variables should ideally be static to this
  39. module. However, this cannot be done yet. The cleanup continues! */
  40. union block *record_start; /* start of record of archive */
  41. union block *record_end; /* last+1 block of archive record */
  42. union block *current_block; /* current block of archive */
  43. enum access_mode access_mode; /* how do we handle the archive */
  44. off_t records_read; /* number of records read from this archive */
  45. off_t records_written; /* likewise, for records written */
  46. static struct stat archive_stat; /* stat block for archive file */
  47. static off_t record_start_block; /* block ordinal at record_start */
  48. /* Where we write list messages (not errors, not interactions) to. Stdout
  49. unless we're writing a pipe, in which case stderr. */
  50. FILE *stdlis;
  51. static void backspace_output PARAMS ((void));
  52. static int new_volume PARAMS ((enum access_mode));
  53. static void archive_write_error PARAMS ((ssize_t)) __attribute__ ((noreturn));
  54. static void archive_read_error PARAMS ((void));
  55. #if !MSDOS
  56. /* Obnoxious test to see if dimwit is trying to dump the archive. */
  57. dev_t ar_dev;
  58. ino_t ar_ino;
  59. #endif
  60. /* PID of child program, if compress_option or remote archive access. */
  61. static pid_t child_pid;
  62. /* Error recovery stuff */
  63. static int read_error_count;
  64. /* Have we hit EOF yet? */
  65. static int hit_eof;
  66. /* Checkpointing counter */
  67. static int checkpoint;
  68. /* We're reading, but we just read the last block and its time to update. */
  69. /* As least EXTERN like this one as possible. FIXME! */
  70. extern int time_to_start_writing;
  71. int file_to_switch_to = -1; /* if remote update, close archive, and use
  72. this descriptor to write to */
  73. static int volno = 1; /* which volume of a multi-volume tape we're
  74. on */
  75. static int global_volno = 1; /* volume number to print in external
  76. messages */
  77. /* The pointer save_name, which is set in function dump_file() of module
  78. create.c, points to the original long filename instead of the new,
  79. shorter mangled name that is set in start_header() of module create.c.
  80. The pointer save_name is only used in multi-volume mode when the file
  81. being processed is non-sparse; if a file is split between volumes, the
  82. save_name is used in generating the LF_MULTIVOL record on the second
  83. volume. (From Pierce Cantrell, 1991-08-13.) */
  84. char *save_name; /* name of the file we are currently writing */
  85. off_t save_totsize; /* total size of file we are writing, only
  86. valid if save_name is nonzero */
  87. off_t save_sizeleft; /* where we are in the file we are writing,
  88. only valid if save_name is nonzero */
  89. bool write_archive_to_stdout;
  90. /* Used by flush_read and flush_write to store the real info about saved
  91. names. */
  92. static char *real_s_name;
  93. static off_t real_s_totsize;
  94. static off_t real_s_sizeleft;
  95. /* Functions. */
  96. void
  97. print_total_written (void)
  98. {
  99. tarlong written = prev_written + bytes_written;
  100. char bytes[sizeof (tarlong) * CHAR_BIT];
  101. char abbr[LONGEST_HUMAN_READABLE + 1];
  102. char rate[LONGEST_HUMAN_READABLE + 1];
  103. double seconds;
  104. #if HAVE_CLOCK_GETTIME
  105. struct timespec now;
  106. if (clock_gettime (CLOCK_REALTIME, &now) == 0)
  107. seconds = ((now.tv_sec - start_timespec.tv_sec)
  108. + (now.tv_nsec - start_timespec.tv_nsec) / 1e9);
  109. else
  110. #endif
  111. seconds = time (0) - start_time;
  112. sprintf (bytes, TARLONG_FORMAT, written);
  113. /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
  114. fprintf (stderr, _("Total bytes written: %s (%sB, %sB/s)\n"), bytes,
  115. human_readable ((uintmax_t) written, abbr, 1, -1024),
  116. (0 < seconds && written / seconds < (uintmax_t) -1
  117. ? human_readable ((uintmax_t) (written / seconds), rate, 1, -1024)
  118. : "?"));
  119. }
  120. /* Compute and return the block ordinal at current_block. */
  121. off_t
  122. current_block_ordinal (void)
  123. {
  124. return record_start_block + (current_block - record_start);
  125. }
  126. /* If the EOF flag is set, reset it, as well as current_block, etc. */
  127. void
  128. reset_eof (void)
  129. {
  130. if (hit_eof)
  131. {
  132. hit_eof = 0;
  133. current_block = record_start;
  134. record_end = record_start + blocking_factor;
  135. access_mode = ACCESS_WRITE;
  136. }
  137. }
  138. /* Return the location of the next available input or output block.
  139. Return zero for EOF. Once we have returned zero, we just keep returning
  140. it, to avoid accidentally going on to the next file on the tape. */
  141. union block *
  142. find_next_block (void)
  143. {
  144. if (current_block == record_end)
  145. {
  146. if (hit_eof)
  147. return 0;
  148. flush_archive ();
  149. if (current_block == record_end)
  150. {
  151. hit_eof = 1;
  152. return 0;
  153. }
  154. }
  155. return current_block;
  156. }
  157. /* Indicate that we have used all blocks up thru BLOCK.
  158. FIXME: should the arg have an off-by-1? */
  159. void
  160. set_next_block_after (union block *block)
  161. {
  162. while (block >= current_block)
  163. current_block++;
  164. /* Do *not* flush the archive here. If we do, the same argument to
  165. set_next_block_after could mean the next block (if the input record
  166. is exactly one block long), which is not what is intended. */
  167. if (current_block > record_end)
  168. abort ();
  169. }
  170. /* Return the number of bytes comprising the space between POINTER
  171. through the end of the current buffer of blocks. This space is
  172. available for filling with data, or taking data from. POINTER is
  173. usually (but not always) the result previous find_next_block call. */
  174. size_t
  175. available_space_after (union block *pointer)
  176. {
  177. return record_end->buffer - pointer->buffer;
  178. }
  179. /* Close file having descriptor FD, and abort if close unsuccessful. */
  180. static void
  181. xclose (int fd)
  182. {
  183. if (close (fd) != 0)
  184. close_error (_("(pipe)"));
  185. }
  186. /* Duplicate file descriptor FROM into becoming INTO.
  187. INTO is closed first and has to be the next available slot. */
  188. static void
  189. xdup2 (int from, int into)
  190. {
  191. if (from != into)
  192. {
  193. int status = close (into);
  194. if (status != 0 && errno != EBADF)
  195. {
  196. int e = errno;
  197. FATAL_ERROR ((0, e, _("Cannot close")));
  198. }
  199. status = dup (from);
  200. if (status != into)
  201. {
  202. if (status < 0)
  203. {
  204. int e = errno;
  205. FATAL_ERROR ((0, e, _("Cannot dup")));
  206. }
  207. abort ();
  208. }
  209. xclose (from);
  210. }
  211. }
  212. #if MSDOS
  213. /* Set ARCHIVE for writing, then compressing an archive. */
  214. static void
  215. child_open_for_compress (void)
  216. {
  217. FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
  218. }
  219. /* Set ARCHIVE for uncompressing, then reading an archive. */
  220. static void
  221. child_open_for_uncompress (void)
  222. {
  223. FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
  224. }
  225. #else /* not MSDOS */
  226. /* Return nonzero if NAME is the name of a regular file, or if the file
  227. does not exist (so it would be created as a regular file). */
  228. static int
  229. is_regular_file (const char *name)
  230. {
  231. struct stat stbuf;
  232. if (stat (name, &stbuf) == 0)
  233. return S_ISREG (stbuf.st_mode);
  234. else
  235. return errno == ENOENT;
  236. }
  237. static ssize_t
  238. write_archive_buffer (void)
  239. {
  240. ssize_t status;
  241. ssize_t written = 0;
  242. while (0 <= (status = rmtwrite (archive, record_start->buffer + written,
  243. record_size - written)))
  244. {
  245. written += status;
  246. if (written == record_size
  247. || _isrmt (archive)
  248. || ! (S_ISFIFO (archive_stat.st_mode)
  249. || S_ISSOCK (archive_stat.st_mode)))
  250. break;
  251. }
  252. return written ? written : status;
  253. }
  254. /* Set ARCHIVE for writing, then compressing an archive. */
  255. static void
  256. child_open_for_compress (void)
  257. {
  258. int parent_pipe[2];
  259. int child_pipe[2];
  260. pid_t grandchild_pid;
  261. int wait_status;
  262. xpipe (parent_pipe);
  263. child_pid = xfork ();
  264. if (child_pid > 0)
  265. {
  266. /* The parent tar is still here! Just clean up. */
  267. archive = parent_pipe[PWRITE];
  268. xclose (parent_pipe[PREAD]);
  269. return;
  270. }
  271. /* The new born child tar is here! */
  272. program_name = _("tar (child)");
  273. xdup2 (parent_pipe[PREAD], STDIN_FILENO);
  274. xclose (parent_pipe[PWRITE]);
  275. /* Check if we need a grandchild tar. This happens only if either:
  276. a) we are writing stdout: to force reblocking;
  277. b) the file is to be accessed by rmt: compressor doesn't know how;
  278. c) the file is not a plain file. */
  279. if (strcmp (archive_name_array[0], "-") != 0
  280. && !_remdev (archive_name_array[0])
  281. && is_regular_file (archive_name_array[0]))
  282. {
  283. if (backup_option)
  284. maybe_backup_file (archive_name_array[0], 1);
  285. /* We don't need a grandchild tar. Open the archive and launch the
  286. compressor. */
  287. archive = creat (archive_name_array[0], MODE_RW);
  288. if (archive < 0)
  289. {
  290. int saved_errno = errno;
  291. if (backup_option)
  292. undo_last_backup ();
  293. errno = saved_errno;
  294. open_fatal (archive_name_array[0]);
  295. }
  296. xdup2 (archive, STDOUT_FILENO);
  297. execlp (use_compress_program_option, use_compress_program_option,
  298. (char *) 0);
  299. exec_fatal (use_compress_program_option);
  300. }
  301. /* We do need a grandchild tar. */
  302. xpipe (child_pipe);
  303. grandchild_pid = xfork ();
  304. if (grandchild_pid == 0)
  305. {
  306. /* The newborn grandchild tar is here! Launch the compressor. */
  307. program_name = _("tar (grandchild)");
  308. xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
  309. xclose (child_pipe[PREAD]);
  310. execlp (use_compress_program_option, use_compress_program_option,
  311. (char *) 0);
  312. exec_fatal (use_compress_program_option);
  313. }
  314. /* The child tar is still here! */
  315. /* Prepare for reblocking the data from the compressor into the archive. */
  316. xdup2 (child_pipe[PREAD], STDIN_FILENO);
  317. xclose (child_pipe[PWRITE]);
  318. if (strcmp (archive_name_array[0], "-") == 0)
  319. archive = STDOUT_FILENO;
  320. else
  321. {
  322. archive = rmtcreat (archive_name_array[0], MODE_RW, rsh_command_option);
  323. if (archive < 0)
  324. open_fatal (archive_name_array[0]);
  325. }
  326. /* Let's read out of the stdin pipe and write an archive. */
  327. while (1)
  328. {
  329. ssize_t status = 0;
  330. char *cursor;
  331. size_t length;
  332. /* Assemble a record. */
  333. for (length = 0, cursor = record_start->buffer;
  334. length < record_size;
  335. length += status, cursor += status)
  336. {
  337. size_t size = record_size - length;
  338. if (size < BLOCKSIZE)
  339. size = BLOCKSIZE;
  340. status = safe_read (STDIN_FILENO, cursor, size);
  341. if (status <= 0)
  342. break;
  343. }
  344. if (status < 0)
  345. read_fatal (use_compress_program_option);
  346. /* Copy the record. */
  347. if (status == 0)
  348. {
  349. /* We hit the end of the file. Write last record at
  350. full length, as the only role of the grandchild is
  351. doing proper reblocking. */
  352. if (length > 0)
  353. {
  354. memset (record_start->buffer + length, 0, record_size - length);
  355. status = write_archive_buffer ();
  356. if (status != record_size)
  357. archive_write_error (status);
  358. }
  359. /* There is nothing else to read, break out. */
  360. break;
  361. }
  362. status = write_archive_buffer ();
  363. if (status != record_size)
  364. archive_write_error (status);
  365. }
  366. #if 0
  367. close_archive ();
  368. #endif
  369. /* Propagate any failure of the grandchild back to the parent. */
  370. while (waitpid (grandchild_pid, &wait_status, 0) == -1)
  371. if (errno != EINTR)
  372. {
  373. waitpid_error (use_compress_program_option);
  374. break;
  375. }
  376. if (WIFSIGNALED (wait_status))
  377. {
  378. kill (child_pid, WTERMSIG (wait_status));
  379. exit_status = TAREXIT_FAILURE;
  380. }
  381. else if (WEXITSTATUS (wait_status) != 0)
  382. exit_status = WEXITSTATUS (wait_status);
  383. exit (exit_status);
  384. }
  385. /* Set ARCHIVE for uncompressing, then reading an archive. */
  386. static void
  387. child_open_for_uncompress (void)
  388. {
  389. int parent_pipe[2];
  390. int child_pipe[2];
  391. pid_t grandchild_pid;
  392. int wait_status;
  393. xpipe (parent_pipe);
  394. child_pid = xfork ();
  395. if (child_pid > 0)
  396. {
  397. /* The parent tar is still here! Just clean up. */
  398. read_full_records_option = 1;
  399. archive = parent_pipe[PREAD];
  400. xclose (parent_pipe[PWRITE]);
  401. return;
  402. }
  403. /* The newborn child tar is here! */
  404. program_name = _("tar (child)");
  405. xdup2 (parent_pipe[PWRITE], STDOUT_FILENO);
  406. xclose (parent_pipe[PREAD]);
  407. /* Check if we need a grandchild tar. This happens only if either:
  408. a) we're reading stdin: to force unblocking;
  409. b) the file is to be accessed by rmt: compressor doesn't know how;
  410. c) the file is not a plain file. */
  411. if (strcmp (archive_name_array[0], "-") != 0
  412. && !_remdev (archive_name_array[0])
  413. && is_regular_file (archive_name_array[0]))
  414. {
  415. /* We don't need a grandchild tar. Open the archive and lauch the
  416. uncompressor. */
  417. archive = open (archive_name_array[0], O_RDONLY | O_BINARY, MODE_RW);
  418. if (archive < 0)
  419. open_fatal (archive_name_array[0]);
  420. xdup2 (archive, STDIN_FILENO);
  421. execlp (use_compress_program_option, use_compress_program_option,
  422. "-d", (char *) 0);
  423. exec_fatal (use_compress_program_option);
  424. }
  425. /* We do need a grandchild tar. */
  426. xpipe (child_pipe);
  427. grandchild_pid = xfork ();
  428. if (grandchild_pid == 0)
  429. {
  430. /* The newborn grandchild tar is here! Launch the uncompressor. */
  431. program_name = _("tar (grandchild)");
  432. xdup2 (child_pipe[PREAD], STDIN_FILENO);
  433. xclose (child_pipe[PWRITE]);
  434. execlp (use_compress_program_option, use_compress_program_option,
  435. "-d", (char *) 0);
  436. exec_fatal (use_compress_program_option);
  437. }
  438. /* The child tar is still here! */
  439. /* Prepare for unblocking the data from the archive into the
  440. uncompressor. */
  441. xdup2 (child_pipe[PWRITE], STDOUT_FILENO);
  442. xclose (child_pipe[PREAD]);
  443. if (strcmp (archive_name_array[0], "-") == 0)
  444. archive = STDIN_FILENO;
  445. else
  446. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  447. MODE_RW, rsh_command_option);
  448. if (archive < 0)
  449. open_fatal (archive_name_array[0]);
  450. /* Let's read the archive and pipe it into stdout. */
  451. while (1)
  452. {
  453. char *cursor;
  454. size_t maximum;
  455. size_t count;
  456. ssize_t status;
  457. read_error_count = 0;
  458. error_loop:
  459. status = rmtread (archive, record_start->buffer, record_size);
  460. if (status < 0)
  461. {
  462. archive_read_error ();
  463. goto error_loop;
  464. }
  465. if (status == 0)
  466. break;
  467. cursor = record_start->buffer;
  468. maximum = status;
  469. while (maximum)
  470. {
  471. count = maximum < BLOCKSIZE ? maximum : BLOCKSIZE;
  472. if (full_write (STDOUT_FILENO, cursor, count) != count)
  473. write_error (use_compress_program_option);
  474. cursor += count;
  475. maximum -= count;
  476. }
  477. }
  478. xclose (STDOUT_FILENO);
  479. #if 0
  480. close_archive ();
  481. #endif
  482. /* Propagate any failure of the grandchild back to the parent. */
  483. while (waitpid (grandchild_pid, &wait_status, 0) == -1)
  484. if (errno != EINTR)
  485. {
  486. waitpid_error (use_compress_program_option);
  487. break;
  488. }
  489. if (WIFSIGNALED (wait_status))
  490. {
  491. kill (child_pid, WTERMSIG (wait_status));
  492. exit_status = TAREXIT_FAILURE;
  493. }
  494. else if (WEXITSTATUS (wait_status) != 0)
  495. exit_status = WEXITSTATUS (wait_status);
  496. exit (exit_status);
  497. }
  498. #endif /* not MSDOS */
  499. /* Check the LABEL block against the volume label, seen as a globbing
  500. pattern. Return true if the pattern matches. In case of failure,
  501. retry matching a volume sequence number before giving up in
  502. multi-volume mode. */
  503. static int
  504. check_label_pattern (union block *label)
  505. {
  506. char *string;
  507. int result;
  508. if (! memchr (label->header.name, '\0', sizeof label->header.name))
  509. return 0;
  510. if (fnmatch (volume_label_option, label->header.name, 0) == 0)
  511. return 1;
  512. if (!multi_volume_option)
  513. return 0;
  514. string = xmalloc (strlen (volume_label_option)
  515. + sizeof VOLUME_LABEL_APPEND + 1);
  516. strcpy (string, volume_label_option);
  517. strcat (string, VOLUME_LABEL_APPEND);
  518. result = fnmatch (string, label->header.name, 0) == 0;
  519. free (string);
  520. return result;
  521. }
  522. /* Open an archive file. The argument specifies whether we are
  523. reading or writing, or both. */
  524. void
  525. open_archive (enum access_mode wanted_access)
  526. {
  527. int backed_up_flag = 0;
  528. stdlis = to_stdout_option ? stderr : stdout;
  529. if (record_size == 0)
  530. FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
  531. if (archive_names == 0)
  532. FATAL_ERROR ((0, 0, _("No archive name given")));
  533. current_file_name = 0;
  534. current_link_name = 0;
  535. save_name = 0;
  536. real_s_name = 0;
  537. if (multi_volume_option)
  538. {
  539. if (verify_option)
  540. FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
  541. record_start = valloc (record_size + (2 * BLOCKSIZE));
  542. if (record_start)
  543. record_start += 2;
  544. }
  545. else
  546. record_start = valloc (record_size);
  547. if (!record_start)
  548. FATAL_ERROR ((0, 0, _("Cannot allocate memory for blocking factor %d"),
  549. blocking_factor));
  550. current_block = record_start;
  551. record_end = record_start + blocking_factor;
  552. /* When updating the archive, we start with reading. */
  553. access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
  554. if (use_compress_program_option)
  555. {
  556. if (multi_volume_option)
  557. FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
  558. if (verify_option)
  559. FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
  560. switch (wanted_access)
  561. {
  562. case ACCESS_READ:
  563. child_open_for_uncompress ();
  564. break;
  565. case ACCESS_WRITE:
  566. child_open_for_compress ();
  567. break;
  568. case ACCESS_UPDATE:
  569. FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
  570. break;
  571. }
  572. if (wanted_access == ACCESS_WRITE
  573. && strcmp (archive_name_array[0], "-") == 0)
  574. stdlis = stderr;
  575. }
  576. else if (strcmp (archive_name_array[0], "-") == 0)
  577. {
  578. read_full_records_option = 1; /* could be a pipe, be safe */
  579. if (verify_option)
  580. FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
  581. switch (wanted_access)
  582. {
  583. case ACCESS_READ:
  584. archive = STDIN_FILENO;
  585. break;
  586. case ACCESS_WRITE:
  587. archive = STDOUT_FILENO;
  588. stdlis = stderr;
  589. break;
  590. case ACCESS_UPDATE:
  591. archive = STDIN_FILENO;
  592. stdlis = stderr;
  593. write_archive_to_stdout = 1;
  594. break;
  595. }
  596. }
  597. else if (verify_option)
  598. archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  599. MODE_RW, rsh_command_option);
  600. else
  601. switch (wanted_access)
  602. {
  603. case ACCESS_READ:
  604. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  605. MODE_RW, rsh_command_option);
  606. break;
  607. case ACCESS_WRITE:
  608. if (backup_option)
  609. {
  610. maybe_backup_file (archive_name_array[0], 1);
  611. backed_up_flag = 1;
  612. }
  613. archive = rmtcreat (archive_name_array[0], MODE_RW,
  614. rsh_command_option);
  615. break;
  616. case ACCESS_UPDATE:
  617. archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  618. MODE_RW, rsh_command_option);
  619. break;
  620. }
  621. if (archive < 0
  622. || (! _isrmt (archive) && fstat (archive, &archive_stat) < 0))
  623. {
  624. int saved_errno = errno;
  625. if (backed_up_flag)
  626. undo_last_backup ();
  627. errno = saved_errno;
  628. open_fatal (archive_name_array[0]);
  629. }
  630. #if !MSDOS
  631. /* Detect if outputting to "/dev/null". */
  632. {
  633. static char const dev_null[] = "/dev/null";
  634. struct stat dev_null_stat;
  635. dev_null_output =
  636. (strcmp (archive_name_array[0], dev_null) == 0
  637. || (! _isrmt (archive)
  638. && S_ISCHR (archive_stat.st_mode)
  639. && stat (dev_null, &dev_null_stat) == 0
  640. && archive_stat.st_dev == dev_null_stat.st_dev
  641. && archive_stat.st_ino == dev_null_stat.st_ino));
  642. }
  643. if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
  644. {
  645. ar_dev = archive_stat.st_dev;
  646. ar_ino = archive_stat.st_ino;
  647. }
  648. else
  649. ar_dev = 0;
  650. #endif /* not MSDOS */
  651. #if MSDOS
  652. setmode (archive, O_BINARY);
  653. #endif
  654. switch (wanted_access)
  655. {
  656. case ACCESS_UPDATE:
  657. records_written = 0;
  658. case ACCESS_READ:
  659. records_read = 0;
  660. record_end = record_start; /* set up for 1st record = # 0 */
  661. find_next_block (); /* read it in, check for EOF */
  662. if (volume_label_option)
  663. {
  664. union block *label = find_next_block ();
  665. if (!label)
  666. FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
  667. quote (volume_label_option)));
  668. if (!check_label_pattern (label))
  669. FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
  670. quote_n (0, label->header.name),
  671. quote_n (1, volume_label_option)));
  672. }
  673. break;
  674. case ACCESS_WRITE:
  675. records_written = 0;
  676. if (volume_label_option)
  677. {
  678. memset (record_start, 0, BLOCKSIZE);
  679. if (multi_volume_option)
  680. sprintf (record_start->header.name, "%s Volume 1",
  681. volume_label_option);
  682. else
  683. strcpy (record_start->header.name, volume_label_option);
  684. assign_string (&current_file_name, record_start->header.name);
  685. record_start->header.typeflag = GNUTYPE_VOLHDR;
  686. TIME_TO_CHARS (start_time, record_start->header.mtime);
  687. finish_header (record_start);
  688. #if 0
  689. current_block++;
  690. #endif
  691. }
  692. break;
  693. }
  694. }
  695. /* Perform a write to flush the buffer. */
  696. void
  697. flush_write (void)
  698. {
  699. int copy_back;
  700. ssize_t status;
  701. if (checkpoint_option && !(++checkpoint % 10))
  702. WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
  703. if (tape_length_option && tape_length_option <= bytes_written)
  704. {
  705. errno = ENOSPC;
  706. status = 0;
  707. }
  708. else if (dev_null_output)
  709. status = record_size;
  710. else
  711. status = write_archive_buffer ();
  712. if (status != record_size && !multi_volume_option)
  713. archive_write_error (status);
  714. if (status > 0)
  715. {
  716. records_written++;
  717. bytes_written += status;
  718. }
  719. if (status == record_size)
  720. {
  721. if (multi_volume_option)
  722. {
  723. char *cursor;
  724. if (!save_name)
  725. {
  726. assign_string (&real_s_name, 0);
  727. real_s_totsize = 0;
  728. real_s_sizeleft = 0;
  729. return;
  730. }
  731. cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
  732. while (ISSLASH (*cursor))
  733. cursor++;
  734. assign_string (&real_s_name, cursor);
  735. real_s_totsize = save_totsize;
  736. real_s_sizeleft = save_sizeleft;
  737. }
  738. return;
  739. }
  740. /* We're multivol. Panic if we didn't get the right kind of response. */
  741. /* ENXIO is for the UNIX PC. */
  742. if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
  743. archive_write_error (status);
  744. /* If error indicates a short write, we just move to the next tape. */
  745. if (!new_volume (ACCESS_WRITE))
  746. return;
  747. if (totals_option)
  748. prev_written += bytes_written;
  749. bytes_written = 0;
  750. if (volume_label_option && real_s_name)
  751. {
  752. copy_back = 2;
  753. record_start -= 2;
  754. }
  755. else if (volume_label_option || real_s_name)
  756. {
  757. copy_back = 1;
  758. record_start--;
  759. }
  760. else
  761. copy_back = 0;
  762. if (volume_label_option)
  763. {
  764. memset (record_start, 0, BLOCKSIZE);
  765. sprintf (record_start->header.name, "%s Volume %d",
  766. volume_label_option, volno);
  767. TIME_TO_CHARS (start_time, record_start->header.mtime);
  768. record_start->header.typeflag = GNUTYPE_VOLHDR;
  769. finish_header (record_start);
  770. }
  771. if (real_s_name)
  772. {
  773. int tmp;
  774. if (volume_label_option)
  775. record_start++;
  776. memset (record_start, 0, BLOCKSIZE);
  777. /* FIXME: Michael P Urban writes: [a long name file] is being written
  778. when a new volume rolls around [...] Looks like the wrong value is
  779. being preserved in real_s_name, though. */
  780. strcpy (record_start->header.name, real_s_name);
  781. record_start->header.typeflag = GNUTYPE_MULTIVOL;
  782. OFF_TO_CHARS (real_s_sizeleft, record_start->header.size);
  783. OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
  784. record_start->oldgnu_header.offset);
  785. tmp = verbose_option;
  786. verbose_option = 0;
  787. finish_header (record_start);
  788. verbose_option = tmp;
  789. if (volume_label_option)
  790. record_start--;
  791. }
  792. status = write_archive_buffer ();
  793. if (status != record_size)
  794. archive_write_error (status);
  795. bytes_written += status;
  796. if (copy_back)
  797. {
  798. record_start += copy_back;
  799. memcpy (current_block,
  800. record_start + blocking_factor - copy_back,
  801. copy_back * BLOCKSIZE);
  802. current_block += copy_back;
  803. if (real_s_sizeleft >= copy_back * BLOCKSIZE)
  804. real_s_sizeleft -= copy_back * BLOCKSIZE;
  805. else if ((real_s_sizeleft + BLOCKSIZE - 1) / BLOCKSIZE <= copy_back)
  806. assign_string (&real_s_name, 0);
  807. else
  808. {
  809. char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
  810. while (ISSLASH (*cursor))
  811. cursor++;
  812. assign_string (&real_s_name, cursor);
  813. real_s_sizeleft = save_sizeleft;
  814. real_s_totsize = save_totsize;
  815. }
  816. copy_back = 0;
  817. }
  818. }
  819. /* Handle write errors on the archive. Write errors are always fatal.
  820. Hitting the end of a volume does not cause a write error unless the
  821. write was the first record of the volume. */
  822. static void
  823. archive_write_error (ssize_t status)
  824. {
  825. /* It might be useful to know how much was written before the error
  826. occurred. */
  827. if (totals_option)
  828. {
  829. int e = errno;
  830. print_total_written ();
  831. errno = e;
  832. }
  833. write_fatal_details (*archive_name_cursor, status, record_size);
  834. }
  835. /* Handle read errors on the archive. If the read should be retried,
  836. return to the caller. */
  837. static void
  838. archive_read_error (void)
  839. {
  840. read_error (*archive_name_cursor);
  841. if (record_start_block == 0)
  842. FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
  843. /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
  844. then give up on reading the archive. */
  845. if (read_error_count++ > READ_ERROR_MAX)
  846. FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
  847. return;
  848. }
  849. /* Perform a read to flush the buffer. */
  850. void
  851. flush_read (void)
  852. {
  853. ssize_t status; /* result from system call */
  854. size_t left; /* bytes left */
  855. char *more; /* pointer to next byte to read */
  856. if (checkpoint_option && !(++checkpoint % 10))
  857. WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
  858. /* Clear the count of errors. This only applies to a single call to
  859. flush_read. */
  860. read_error_count = 0; /* clear error count */
  861. if (write_archive_to_stdout && record_start_block != 0)
  862. {
  863. archive = STDOUT_FILENO;
  864. status = write_archive_buffer ();
  865. archive = STDIN_FILENO;
  866. if (status != record_size)
  867. archive_write_error (status);
  868. }
  869. if (multi_volume_option)
  870. {
  871. if (save_name)
  872. {
  873. char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
  874. while (ISSLASH (*cursor))
  875. cursor++;
  876. assign_string (&real_s_name, cursor);
  877. real_s_sizeleft = save_sizeleft;
  878. real_s_totsize = save_totsize;
  879. }
  880. else
  881. {
  882. assign_string (&real_s_name, 0);
  883. real_s_totsize = 0;
  884. real_s_sizeleft = 0;
  885. }
  886. }
  887. error_loop:
  888. status = rmtread (archive, record_start->buffer, record_size);
  889. if (status == record_size)
  890. {
  891. records_read++;
  892. return;
  893. }
  894. if ((status == 0
  895. || (status < 0 && errno == ENOSPC)
  896. || (status > 0 && !read_full_records_option))
  897. && multi_volume_option)
  898. {
  899. union block *cursor;
  900. try_volume:
  901. switch (subcommand_option)
  902. {
  903. case APPEND_SUBCOMMAND:
  904. case CAT_SUBCOMMAND:
  905. case UPDATE_SUBCOMMAND:
  906. if (!new_volume (ACCESS_UPDATE))
  907. return;
  908. break;
  909. default:
  910. if (!new_volume (ACCESS_READ))
  911. return;
  912. break;
  913. }
  914. vol_error:
  915. status = rmtread (archive, record_start->buffer, record_size);
  916. if (status < 0)
  917. {
  918. archive_read_error ();
  919. goto vol_error;
  920. }
  921. if (status != record_size)
  922. goto short_read;
  923. cursor = record_start;
  924. if (cursor->header.typeflag == GNUTYPE_VOLHDR)
  925. {
  926. if (volume_label_option)
  927. {
  928. if (!check_label_pattern (cursor))
  929. {
  930. WARN ((0, 0, _("Volume %s does not match %s"),
  931. quote_n (0, cursor->header.name),
  932. quote_n (1, volume_label_option)));
  933. volno--;
  934. global_volno--;
  935. goto try_volume;
  936. }
  937. }
  938. if (verbose_option)
  939. fprintf (stdlis, _("Reading %s\n"), quote (cursor->header.name));
  940. cursor++;
  941. }
  942. else if (volume_label_option)
  943. WARN ((0, 0, _("WARNING: No volume header")));
  944. if (real_s_name)
  945. {
  946. uintmax_t s1, s2;
  947. if (cursor->header.typeflag != GNUTYPE_MULTIVOL
  948. || strcmp (cursor->header.name, real_s_name))
  949. {
  950. WARN ((0, 0, _("%s is not continued on this volume"),
  951. quote (real_s_name)));
  952. volno--;
  953. global_volno--;
  954. goto try_volume;
  955. }
  956. s1 = UINTMAX_FROM_HEADER (cursor->header.size);
  957. s2 = UINTMAX_FROM_HEADER (cursor->oldgnu_header.offset);
  958. if (real_s_totsize != s1 + s2 || s1 + s2 < s2)
  959. {
  960. char totsizebuf[UINTMAX_STRSIZE_BOUND];
  961. char s1buf[UINTMAX_STRSIZE_BOUND];
  962. char s2buf[UINTMAX_STRSIZE_BOUND];
  963. WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
  964. quote (cursor->header.name),
  965. STRINGIFY_BIGINT (save_totsize, totsizebuf),
  966. STRINGIFY_BIGINT (s1, s1buf),
  967. STRINGIFY_BIGINT (s2, s2buf)));
  968. volno--;
  969. global_volno--;
  970. goto try_volume;
  971. }
  972. if (real_s_totsize - real_s_sizeleft
  973. != OFF_FROM_HEADER (cursor->oldgnu_header.offset))
  974. {
  975. WARN ((0, 0, _("This volume is out of sequence")));
  976. volno--;
  977. global_volno--;
  978. goto try_volume;
  979. }
  980. cursor++;
  981. }
  982. current_block = cursor;
  983. records_read++;
  984. return;
  985. }
  986. else if (status < 0)
  987. {
  988. archive_read_error ();
  989. goto error_loop; /* try again */
  990. }
  991. short_read:
  992. more = record_start->buffer + status;
  993. left = record_size - status;
  994. while (left % BLOCKSIZE != 0
  995. || (left && status && read_full_records_option))
  996. {
  997. if (status)
  998. while ((status = rmtread (archive, more, left)) < 0)
  999. archive_read_error ();
  1000. if (status == 0)
  1001. break;
  1002. if (! read_full_records_option)
  1003. FATAL_ERROR ((0, 0, _("Unaligned block (%lu bytes) in archive"),
  1004. (unsigned long) (record_size - left)));
  1005. /* User warned us about this. Fix up. */
  1006. left -= status;
  1007. more += status;
  1008. }
  1009. /* FIXME: for size=0, multi-volume support. On the first record, warn
  1010. about the problem. */
  1011. if (!read_full_records_option && verbose_option
  1012. && record_start_block == 0 && status > 0)
  1013. WARN ((0, 0, _("Record size = %lu blocks"),
  1014. (unsigned long) ((record_size - left) / BLOCKSIZE)));
  1015. record_end = record_start + (record_size - left) / BLOCKSIZE;
  1016. records_read++;
  1017. }
  1018. /* Flush the current buffer to/from the archive. */
  1019. void
  1020. flush_archive (void)
  1021. {
  1022. record_start_block += record_end - record_start;
  1023. current_block = record_start;
  1024. record_end = record_start + blocking_factor;
  1025. if (access_mode == ACCESS_READ && time_to_start_writing)
  1026. {
  1027. access_mode = ACCESS_WRITE;
  1028. time_to_start_writing = 0;
  1029. if (file_to_switch_to >= 0)
  1030. {
  1031. if (rmtclose (archive) != 0)
  1032. close_warn (*archive_name_cursor);
  1033. archive = file_to_switch_to;
  1034. }
  1035. else
  1036. backspace_output ();
  1037. }
  1038. switch (access_mode)
  1039. {
  1040. case ACCESS_READ:
  1041. flush_read ();
  1042. break;
  1043. case ACCESS_WRITE:
  1044. flush_write ();
  1045. break;
  1046. case ACCESS_UPDATE:
  1047. abort ();
  1048. }
  1049. }
  1050. /* Backspace the archive descriptor by one record worth. If it's a
  1051. tape, MTIOCTOP will work. If it's something else, try to seek on
  1052. it. If we can't seek, we lose! */
  1053. static void
  1054. backspace_output (void)
  1055. {
  1056. #ifdef MTIOCTOP
  1057. {
  1058. struct mtop operation;
  1059. operation.mt_op = MTBSR;
  1060. operation.mt_count = 1;
  1061. if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  1062. return;
  1063. if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  1064. return;
  1065. }
  1066. #endif
  1067. {
  1068. off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
  1069. /* Seek back to the beginning of this record and start writing there. */
  1070. position -= record_size;
  1071. if (position < 0)
  1072. position = 0;
  1073. if (rmtlseek (archive, position, SEEK_SET) != position)
  1074. {
  1075. /* Lseek failed. Try a different method. */
  1076. WARN ((0, 0,
  1077. _("Cannot backspace archive file; it may be unreadable without -i")));
  1078. /* Replace the first part of the record with NULs. */
  1079. if (record_start->buffer != output_start)
  1080. memset (record_start->buffer, 0,
  1081. output_start - record_start->buffer);
  1082. }
  1083. }
  1084. }
  1085. /* Close the archive file. */
  1086. void
  1087. close_archive (void)
  1088. {
  1089. if (time_to_start_writing || access_mode == ACCESS_WRITE)
  1090. flush_archive ();
  1091. #if !MSDOS
  1092. /* Manage to fully drain a pipe we might be reading, so to not break it on
  1093. the producer after the EOF block. FIXME: one of these days, GNU tar
  1094. might become clever enough to just stop working, once there is no more
  1095. work to do, we might have to revise this area in such time. */
  1096. if (access_mode == ACCESS_READ
  1097. && ! _isrmt (archive)
  1098. && (S_ISFIFO (archive_stat.st_mode) || S_ISSOCK (archive_stat.st_mode)))
  1099. while (rmtread (archive, record_start->buffer, record_size) > 0)
  1100. continue;
  1101. #endif
  1102. if (verify_option)
  1103. verify_volume ();
  1104. if (rmtclose (archive) != 0)
  1105. close_warn (*archive_name_cursor);
  1106. #if !MSDOS
  1107. if (child_pid)
  1108. {
  1109. int wait_status;
  1110. while (waitpid (child_pid, &wait_status, 0) == -1)
  1111. if (errno != EINTR)
  1112. {
  1113. waitpid_error (use_compress_program_option);
  1114. break;
  1115. }
  1116. if (WIFSIGNALED (wait_status))
  1117. ERROR ((0, 0, _("Child died with signal %d"),
  1118. WTERMSIG (wait_status)));
  1119. else if (WEXITSTATUS (wait_status) != 0)
  1120. ERROR ((0, 0, _("Child returned status %d"),
  1121. WEXITSTATUS (wait_status)));
  1122. }
  1123. #endif /* !MSDOS */
  1124. if (current_file_name)
  1125. free (current_file_name);
  1126. if (current_link_name)
  1127. free (current_link_name);
  1128. if (save_name)
  1129. free (save_name);
  1130. if (real_s_name)
  1131. free (real_s_name);
  1132. free (multi_volume_option ? record_start - 2 : record_start);
  1133. }
  1134. /* Called to initialize the global volume number. */
  1135. void
  1136. init_volume_number (void)
  1137. {
  1138. FILE *file = fopen (volno_file_option, "r");
  1139. if (file)
  1140. {
  1141. if (fscanf (file, "%d", &global_volno) != 1
  1142. || global_volno < 0)
  1143. FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
  1144. quotearg_colon (volno_file_option)));
  1145. if (ferror (file))
  1146. read_error (volno_file_option);
  1147. if (fclose (file) != 0)
  1148. close_error (volno_file_option);
  1149. }
  1150. else if (errno != ENOENT)
  1151. open_error (volno_file_option);
  1152. }
  1153. /* Called to write out the closing global volume number. */
  1154. void
  1155. closeout_volume_number (void)
  1156. {
  1157. FILE *file = fopen (volno_file_option, "w");
  1158. if (file)
  1159. {
  1160. fprintf (file, "%d\n", global_volno);
  1161. if (ferror (file))
  1162. write_error (volno_file_option);
  1163. if (fclose (file) != 0)
  1164. close_error (volno_file_option);
  1165. }
  1166. else
  1167. open_error (volno_file_option);
  1168. }
  1169. /* We've hit the end of the old volume. Close it and open the next one.
  1170. Return nonzero on success. */
  1171. static int
  1172. new_volume (enum access_mode access)
  1173. {
  1174. static FILE *read_file;
  1175. static int looped;
  1176. if (!read_file && !info_script_option)
  1177. /* FIXME: if fopen is used, it will never be closed. */
  1178. read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
  1179. if (now_verifying)
  1180. return 0;
  1181. if (verify_option)
  1182. verify_volume ();
  1183. if (rmtclose (archive) != 0)
  1184. close_warn (*archive_name_cursor);
  1185. global_volno++;
  1186. if (global_volno < 0)
  1187. FATAL_ERROR ((0, 0, _("Volume number overflow")));
  1188. volno++;
  1189. archive_name_cursor++;
  1190. if (archive_name_cursor == archive_name_array + archive_names)
  1191. {
  1192. archive_name_cursor = archive_name_array;
  1193. looped = 1;
  1194. }
  1195. tryagain:
  1196. if (looped)
  1197. {
  1198. /* We have to prompt from now on. */
  1199. if (info_script_option)
  1200. {
  1201. if (volno_file_option)
  1202. closeout_volume_number ();
  1203. if (system (info_script_option) != 0)
  1204. FATAL_ERROR ((0, 0, _("`%s' command failed"), info_script_option));
  1205. }
  1206. else
  1207. while (1)
  1208. {
  1209. char input_buffer[80];
  1210. fputc ('\007', stderr);
  1211. fprintf (stderr,
  1212. _("Prepare volume #%d for %s and hit return: "),
  1213. global_volno, quote (*archive_name_cursor));
  1214. fflush (stderr);
  1215. if (fgets (input_buffer, sizeof input_buffer, read_file) == 0)
  1216. {
  1217. WARN ((0, 0, _("EOF where user reply was expected")));
  1218. if (subcommand_option != EXTRACT_SUBCOMMAND
  1219. && subcommand_option != LIST_SUBCOMMAND
  1220. && subcommand_option != DIFF_SUBCOMMAND)
  1221. WARN ((0, 0, _("WARNING: Archive is incomplete")));
  1222. fatal_exit ();
  1223. }
  1224. if (input_buffer[0] == '\n'
  1225. || input_buffer[0] == 'y'
  1226. || input_buffer[0] == 'Y')
  1227. break;
  1228. switch (input_buffer[0])
  1229. {
  1230. case '?':
  1231. {
  1232. fprintf (stderr, _("\
  1233. n [name] Give a new file name for the next (and subsequent) volume(s)\n\
  1234. q Abort tar\n\
  1235. ! Spawn a subshell\n\
  1236. ? Print this list\n"));
  1237. }
  1238. break;
  1239. case 'q':
  1240. /* Quit. */
  1241. WARN ((0, 0, _("No new volume; exiting.\n")));
  1242. if (subcommand_option != EXTRACT_SUBCOMMAND
  1243. && subcommand_option != LIST_SUBCOMMAND
  1244. && subcommand_option != DIFF_SUBCOMMAND)
  1245. WARN ((0, 0, _("WARNING: Archive is incomplete")));
  1246. fatal_exit ();
  1247. case 'n':
  1248. /* Get new file name. */
  1249. {
  1250. char *name = &input_buffer[1];
  1251. char *cursor;
  1252. while (*name == ' ' || *name == '\t')
  1253. name++;
  1254. cursor = name;
  1255. while (*cursor && *cursor != '\n')
  1256. cursor++;
  1257. *cursor = '\0';
  1258. /* FIXME: the following allocation is never reclaimed. */
  1259. *archive_name_cursor = xstrdup (name);
  1260. }
  1261. break;
  1262. case '!':
  1263. #if MSDOS
  1264. spawnl (P_WAIT, getenv ("COMSPEC"), "-", 0);
  1265. #else /* not MSDOS */
  1266. {
  1267. pid_t child;
  1268. const char *shell = getenv ("SHELL");
  1269. if (! shell)
  1270. shell = "/bin/sh";
  1271. child = xfork ();
  1272. if (child == 0)
  1273. {
  1274. execlp (shell, "-sh", "-i", 0);
  1275. exec_fatal (shell);
  1276. }
  1277. else
  1278. {
  1279. int wait_status;
  1280. while (waitpid (child, &wait_status, 0) == -1)
  1281. if (errno != EINTR)
  1282. {
  1283. waitpid_error (shell);
  1284. break;
  1285. }
  1286. }
  1287. }
  1288. #endif /* not MSDOS */
  1289. break;
  1290. }
  1291. }
  1292. }
  1293. if (verify_option)
  1294. archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
  1295. rsh_command_option);
  1296. else
  1297. switch (access)
  1298. {
  1299. case ACCESS_READ:
  1300. archive = rmtopen (*archive_name_cursor, O_RDONLY, MODE_RW,
  1301. rsh_command_option);
  1302. break;
  1303. case ACCESS_WRITE:
  1304. if (backup_option)
  1305. maybe_backup_file (*archive_name_cursor, 1);
  1306. archive = rmtcreat (*archive_name_cursor, MODE_RW,
  1307. rsh_command_option);
  1308. break;
  1309. case ACCESS_UPDATE:
  1310. archive = rmtopen (*archive_name_cursor, O_RDWR | O_CREAT, MODE_RW,
  1311. rsh_command_option);
  1312. break;
  1313. }
  1314. if (archive < 0)
  1315. {
  1316. open_warn (*archive_name_cursor);
  1317. if (!verify_option && access == ACCESS_WRITE && backup_option)
  1318. undo_last_backup ();
  1319. goto tryagain;
  1320. }
  1321. #if MSDOS
  1322. setmode (archive, O_BINARY);
  1323. #endif
  1324. return 1;
  1325. }