buffer.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  1. /* Buffer management for tar.
  2. Copyright 1988, 92, 93, 94, 96, 97, 99, 2000 Free Software Foundation, Inc.
  3. Written by John Gilmore, on 1985-08-25.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "system.h"
  16. #include <signal.h>
  17. #include <time.h>
  18. #ifndef time
  19. time_t time ();
  20. #endif
  21. #if MSDOS
  22. # include <process.h>
  23. #endif
  24. #if XENIX
  25. # include <sys/inode.h>
  26. #endif
  27. #include <fnmatch.h>
  28. #include <human.h>
  29. #include <quotearg.h>
  30. #include "common.h"
  31. #include "rmt.h"
  32. #define PREAD 0 /* read file descriptor from pipe() */
  33. #define PWRITE 1 /* write file descriptor from pipe() */
  34. /* Number of retries before giving up on read. */
  35. #define READ_ERROR_MAX 10
  36. /* Globbing pattern to append to volume label if initial match failed. */
  37. #define VOLUME_LABEL_APPEND " Volume [1-9]*"
  38. /* Variables. */
  39. static tarlong prev_written; /* bytes written on previous volumes */
  40. static tarlong bytes_written; /* bytes written on this volume */
  41. /* FIXME: The following four variables should ideally be static to this
  42. module. However, this cannot be done yet, as update.c uses the first
  43. three a lot, and compare.c uses the fourth. The cleanup continues! */
  44. union block *record_start; /* start of record of archive */
  45. union block *record_end; /* last+1 block of archive record */
  46. union block *current_block; /* current block of archive */
  47. enum access_mode access_mode; /* how do we handle the archive */
  48. static struct stat archive_stat; /* stat block for archive file */
  49. static off_t record_start_block; /* block ordinal at record_start */
  50. /* Where we write list messages (not errors, not interactions) to. Stdout
  51. unless we're writing a pipe, in which case stderr. */
  52. FILE *stdlis;
  53. static void backspace_output PARAMS ((void));
  54. static int new_volume PARAMS ((enum access_mode));
  55. static void archive_write_error PARAMS ((ssize_t)) __attribute__ ((noreturn));
  56. static void archive_read_error PARAMS ((void));
  57. #if !MSDOS
  58. /* Obnoxious test to see if dimwit is trying to dump the archive. */
  59. dev_t ar_dev;
  60. ino_t ar_ino;
  61. #endif
  62. /* PID of child program, if compress_option or remote archive access. */
  63. static pid_t child_pid;
  64. /* Error recovery stuff */
  65. static int read_error_count;
  66. /* Have we hit EOF yet? */
  67. static int hit_eof;
  68. /* Checkpointing counter */
  69. static int checkpoint;
  70. /* We're reading, but we just read the last block and its time to update. */
  71. /* As least EXTERN like this one as possible. FIXME! */
  72. extern int time_to_start_writing;
  73. int file_to_switch_to = -1; /* if remote update, close archive, and use
  74. this descriptor to write to */
  75. static int volno = 1; /* which volume of a multi-volume tape we're
  76. on */
  77. static int global_volno = 1; /* volume number to print in external
  78. messages */
  79. /* The pointer save_name, which is set in function dump_file() of module
  80. create.c, points to the original long filename instead of the new,
  81. shorter mangled name that is set in start_header() of module create.c.
  82. The pointer save_name is only used in multi-volume mode when the file
  83. being processed is non-sparse; if a file is split between volumes, the
  84. save_name is used in generating the LF_MULTIVOL record on the second
  85. volume. (From Pierce Cantrell, 1991-08-13.) */
  86. char *save_name; /* name of the file we are currently writing */
  87. off_t save_totsize; /* total size of file we are writing, only
  88. valid if save_name is nonzero */
  89. off_t save_sizeleft; /* where we are in the file we are writing,
  90. only valid if save_name is nonzero */
  91. int write_archive_to_stdout;
  92. /* Used by flush_read and flush_write to store the real info about saved
  93. names. */
  94. static char *real_s_name;
  95. static off_t real_s_totsize;
  96. static off_t real_s_sizeleft;
  97. /* Functions. */
  98. void
  99. print_total_written (void)
  100. {
  101. tarlong written = prev_written + bytes_written;
  102. char bytes[sizeof (tarlong) * CHAR_BIT];
  103. char abbr[LONGEST_HUMAN_READABLE + 1];
  104. char rate[LONGEST_HUMAN_READABLE + 1];
  105. double seconds;
  106. #if HAVE_CLOCK_GETTIME
  107. struct timespec now;
  108. if (clock_gettime (CLOCK_REALTIME, &now) == 0)
  109. seconds = ((now.tv_sec - start_timespec.tv_sec)
  110. + (now.tv_nsec - start_timespec.tv_nsec) / 1e9);
  111. else
  112. #endif
  113. seconds = time (0) - start_time;
  114. sprintf (bytes, TARLONG_FORMAT, written);
  115. /* Amanda 2.4.1p1 looks for "Total bytes written: [0-9][0-9]*". */
  116. fprintf (stderr, _("Total bytes written: %s (%sB, %sB/s)\n"), bytes,
  117. human_readable ((uintmax_t) written, abbr, 1, -1024),
  118. (0 < seconds && written / seconds < (uintmax_t) -1
  119. ? human_readable ((uintmax_t) (written / seconds), rate, 1, -1024)
  120. : "?"));
  121. }
  122. /* Compute and return the block ordinal at current_block. */
  123. off_t
  124. current_block_ordinal (void)
  125. {
  126. return record_start_block + (current_block - record_start);
  127. }
  128. /* If the EOF flag is set, reset it, as well as current_block, etc. */
  129. void
  130. reset_eof (void)
  131. {
  132. if (hit_eof)
  133. {
  134. hit_eof = 0;
  135. current_block = record_start;
  136. record_end = record_start + blocking_factor;
  137. access_mode = ACCESS_WRITE;
  138. }
  139. }
  140. /* Return the location of the next available input or output block.
  141. Return zero for EOF. Once we have returned zero, we just keep returning
  142. it, to avoid accidentally going on to the next file on the tape. */
  143. union block *
  144. find_next_block (void)
  145. {
  146. if (current_block == record_end)
  147. {
  148. if (hit_eof)
  149. return 0;
  150. flush_archive ();
  151. if (current_block == record_end)
  152. {
  153. hit_eof = 1;
  154. return 0;
  155. }
  156. }
  157. return current_block;
  158. }
  159. /* Indicate that we have used all blocks up thru BLOCK.
  160. FIXME: should the arg have an off-by-1? */
  161. void
  162. set_next_block_after (union block *block)
  163. {
  164. while (block >= current_block)
  165. current_block++;
  166. /* Do *not* flush the archive here. If we do, the same argument to
  167. set_next_block_after could mean the next block (if the input record
  168. is exactly one block long), which is not what is intended. */
  169. if (current_block > record_end)
  170. abort ();
  171. }
  172. /* Return the number of bytes comprising the space between POINTER
  173. through the end of the current buffer of blocks. This space is
  174. available for filling with data, or taking data from. POINTER is
  175. usually (but not always) the result previous find_next_block call. */
  176. size_t
  177. available_space_after (union block *pointer)
  178. {
  179. return record_end->buffer - pointer->buffer;
  180. }
  181. /* Close file having descriptor FD, and abort if close unsuccessful. */
  182. static void
  183. xclose (int fd)
  184. {
  185. if (close (fd) != 0)
  186. close_error (_("(pipe)"));
  187. }
  188. /* Duplicate file descriptor FROM into becoming INTO.
  189. INTO is closed first and has to be the next available slot. */
  190. static void
  191. xdup2 (int from, int into)
  192. {
  193. if (from != into)
  194. {
  195. int status = close (into);
  196. if (status != 0 && errno != EBADF)
  197. {
  198. int e = errno;
  199. FATAL_ERROR ((0, e, _("Cannot close")));
  200. }
  201. status = dup (from);
  202. if (status != into)
  203. {
  204. if (status < 0)
  205. {
  206. int e = errno;
  207. FATAL_ERROR ((0, e, _("Cannot dup")));
  208. }
  209. abort ();
  210. }
  211. xclose (from);
  212. }
  213. }
  214. #if MSDOS
  215. /* Set ARCHIVE for writing, then compressing an archive. */
  216. static void
  217. child_open_for_compress (void)
  218. {
  219. FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
  220. }
  221. /* Set ARCHIVE for uncompressing, then reading an archive. */
  222. static void
  223. child_open_for_uncompress (void)
  224. {
  225. FATAL_ERROR ((0, 0, _("Cannot use compressed or remote archives")));
  226. }
  227. #else /* not MSDOS */
  228. /* Return nonzero if NAME is the name of a regular file, or if the file
  229. does not exist (so it would be created as a regular file). */
  230. static int
  231. is_regular_file (const char *name)
  232. {
  233. struct stat stbuf;
  234. if (stat (name, &stbuf) == 0)
  235. return S_ISREG (stbuf.st_mode);
  236. else
  237. return errno == ENOENT;
  238. }
  239. static ssize_t
  240. write_archive_buffer (void)
  241. {
  242. ssize_t status;
  243. ssize_t written = 0;
  244. while (0 <= (status = rmtwrite (archive, record_start->buffer + written,
  245. record_size - written)))
  246. {
  247. written += status;
  248. if (written == record_size
  249. || _isrmt (archive) || ! S_ISFIFO (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. status = full_write (STDOUT_FILENO, cursor, count);
  473. if (status < 0)
  474. write_error (use_compress_program_option);
  475. if (status != count)
  476. {
  477. ERROR ((0, 0, _("Write to compression program short %lu bytes"),
  478. (unsigned long) (count - status)));
  479. count = status;
  480. }
  481. cursor += count;
  482. maximum -= count;
  483. }
  484. }
  485. xclose (STDOUT_FILENO);
  486. #if 0
  487. close_archive ();
  488. #endif
  489. /* Propagate any failure of the grandchild back to the parent. */
  490. while (waitpid (grandchild_pid, &wait_status, 0) == -1)
  491. if (errno != EINTR)
  492. {
  493. waitpid_error (use_compress_program_option);
  494. break;
  495. }
  496. if (WIFSIGNALED (wait_status))
  497. {
  498. kill (child_pid, WTERMSIG (wait_status));
  499. exit_status = TAREXIT_FAILURE;
  500. }
  501. else if (WEXITSTATUS (wait_status) != 0)
  502. exit_status = WEXITSTATUS (wait_status);
  503. exit (exit_status);
  504. }
  505. #endif /* not MSDOS */
  506. /* Check the LABEL block against the volume label, seen as a globbing
  507. pattern. Return true if the pattern matches. In case of failure,
  508. retry matching a volume sequence number before giving up in
  509. multi-volume mode. */
  510. static int
  511. check_label_pattern (union block *label)
  512. {
  513. char *string;
  514. int result;
  515. if (! memchr (label->header.name, '\0', sizeof label->header.name))
  516. return 0;
  517. if (fnmatch (volume_label_option, label->header.name, 0) == 0)
  518. return 1;
  519. if (!multi_volume_option)
  520. return 0;
  521. string = xmalloc (strlen (volume_label_option)
  522. + sizeof VOLUME_LABEL_APPEND + 1);
  523. strcpy (string, volume_label_option);
  524. strcat (string, VOLUME_LABEL_APPEND);
  525. result = fnmatch (string, label->header.name, 0) == 0;
  526. free (string);
  527. return result;
  528. }
  529. /* Open an archive file. The argument specifies whether we are
  530. reading or writing, or both. */
  531. void
  532. open_archive (enum access_mode wanted_access)
  533. {
  534. int backed_up_flag = 0;
  535. stdlis = to_stdout_option ? stderr : stdout;
  536. if (record_size == 0)
  537. FATAL_ERROR ((0, 0, _("Invalid value for record_size")));
  538. if (archive_names == 0)
  539. FATAL_ERROR ((0, 0, _("No archive name given")));
  540. current_file_name = 0;
  541. current_link_name = 0;
  542. save_name = 0;
  543. real_s_name = 0;
  544. if (multi_volume_option)
  545. {
  546. if (verify_option)
  547. FATAL_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
  548. record_start = valloc (record_size + (2 * BLOCKSIZE));
  549. if (record_start)
  550. record_start += 2;
  551. }
  552. else
  553. record_start = valloc (record_size);
  554. if (!record_start)
  555. FATAL_ERROR ((0, 0, _("Cannot allocate memory for blocking factor %d"),
  556. blocking_factor));
  557. current_block = record_start;
  558. record_end = record_start + blocking_factor;
  559. /* When updating the archive, we start with reading. */
  560. access_mode = wanted_access == ACCESS_UPDATE ? ACCESS_READ : wanted_access;
  561. if (use_compress_program_option)
  562. {
  563. if (multi_volume_option)
  564. FATAL_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
  565. if (verify_option)
  566. FATAL_ERROR ((0, 0, _("Cannot verify compressed archives")));
  567. switch (wanted_access)
  568. {
  569. case ACCESS_READ:
  570. child_open_for_uncompress ();
  571. break;
  572. case ACCESS_WRITE:
  573. child_open_for_compress ();
  574. break;
  575. case ACCESS_UPDATE:
  576. FATAL_ERROR ((0, 0, _("Cannot update compressed archives")));
  577. break;
  578. }
  579. if (wanted_access == ACCESS_WRITE
  580. && strcmp (archive_name_array[0], "-") == 0)
  581. stdlis = stderr;
  582. }
  583. else if (strcmp (archive_name_array[0], "-") == 0)
  584. {
  585. read_full_records_option = 1; /* could be a pipe, be safe */
  586. if (verify_option)
  587. FATAL_ERROR ((0, 0, _("Cannot verify stdin/stdout archive")));
  588. switch (wanted_access)
  589. {
  590. case ACCESS_READ:
  591. archive = STDIN_FILENO;
  592. break;
  593. case ACCESS_WRITE:
  594. archive = STDOUT_FILENO;
  595. stdlis = stderr;
  596. break;
  597. case ACCESS_UPDATE:
  598. archive = STDIN_FILENO;
  599. stdlis = stderr;
  600. write_archive_to_stdout = 1;
  601. break;
  602. }
  603. }
  604. else if (verify_option)
  605. archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  606. MODE_RW, rsh_command_option);
  607. else
  608. switch (wanted_access)
  609. {
  610. case ACCESS_READ:
  611. archive = rmtopen (archive_name_array[0], O_RDONLY | O_BINARY,
  612. MODE_RW, rsh_command_option);
  613. break;
  614. case ACCESS_WRITE:
  615. if (backup_option)
  616. {
  617. maybe_backup_file (archive_name_array[0], 1);
  618. backed_up_flag = 1;
  619. }
  620. archive = rmtcreat (archive_name_array[0], MODE_RW,
  621. rsh_command_option);
  622. break;
  623. case ACCESS_UPDATE:
  624. archive = rmtopen (archive_name_array[0], O_RDWR | O_CREAT | O_BINARY,
  625. MODE_RW, rsh_command_option);
  626. break;
  627. }
  628. if (archive < 0
  629. || (! _isrmt (archive) && fstat (archive, &archive_stat) < 0))
  630. {
  631. int saved_errno = errno;
  632. if (backed_up_flag)
  633. undo_last_backup ();
  634. errno = saved_errno;
  635. open_fatal (archive_name_array[0]);
  636. }
  637. #if !MSDOS
  638. /* Detect if outputting to "/dev/null". */
  639. {
  640. static char const dev_null[] = "/dev/null";
  641. struct stat dev_null_stat;
  642. dev_null_output =
  643. (strcmp (archive_name_array[0], dev_null) == 0
  644. || (! _isrmt (archive)
  645. && S_ISCHR (archive_stat.st_mode)
  646. && stat (dev_null, &dev_null_stat) == 0
  647. && archive_stat.st_dev == dev_null_stat.st_dev
  648. && archive_stat.st_ino == dev_null_stat.st_ino));
  649. }
  650. if (!_isrmt (archive) && S_ISREG (archive_stat.st_mode))
  651. {
  652. ar_dev = archive_stat.st_dev;
  653. ar_ino = archive_stat.st_ino;
  654. }
  655. else
  656. ar_dev = 0;
  657. #endif /* not MSDOS */
  658. #if MSDOS
  659. setmode (archive, O_BINARY);
  660. #endif
  661. switch (wanted_access)
  662. {
  663. case ACCESS_READ:
  664. case ACCESS_UPDATE:
  665. record_end = record_start; /* set up for 1st record = # 0 */
  666. find_next_block (); /* read it in, check for EOF */
  667. if (volume_label_option)
  668. {
  669. union block *label = find_next_block ();
  670. if (!label)
  671. FATAL_ERROR ((0, 0, _("Archive not labeled to match %s"),
  672. quote (volume_label_option)));
  673. if (!check_label_pattern (label))
  674. FATAL_ERROR ((0, 0, _("Volume %s does not match %s"),
  675. quote_n (0, label->header.name),
  676. quote_n (1, volume_label_option)));
  677. }
  678. break;
  679. case ACCESS_WRITE:
  680. if (volume_label_option)
  681. {
  682. memset (record_start, 0, BLOCKSIZE);
  683. if (multi_volume_option)
  684. sprintf (record_start->header.name, "%s Volume 1",
  685. volume_label_option);
  686. else
  687. strcpy (record_start->header.name, volume_label_option);
  688. assign_string (&current_file_name, record_start->header.name);
  689. record_start->header.typeflag = GNUTYPE_VOLHDR;
  690. TIME_TO_CHARS (start_time, record_start->header.mtime);
  691. finish_header (record_start);
  692. #if 0
  693. current_block++;
  694. #endif
  695. }
  696. break;
  697. }
  698. }
  699. /* Perform a write to flush the buffer. */
  700. void
  701. flush_write (void)
  702. {
  703. int copy_back;
  704. ssize_t status;
  705. if (checkpoint_option && !(++checkpoint % 10))
  706. WARN ((0, 0, _("Write checkpoint %d"), checkpoint));
  707. if (tape_length_option && tape_length_option <= bytes_written)
  708. {
  709. errno = ENOSPC;
  710. status = 0;
  711. }
  712. else if (dev_null_output)
  713. status = record_size;
  714. else
  715. status = write_archive_buffer ();
  716. if (status != record_size && !multi_volume_option)
  717. archive_write_error (status);
  718. if (status > 0)
  719. bytes_written += status;
  720. if (status == record_size)
  721. {
  722. if (multi_volume_option)
  723. {
  724. char *cursor;
  725. if (!save_name)
  726. {
  727. assign_string (&real_s_name, 0);
  728. real_s_totsize = 0;
  729. real_s_sizeleft = 0;
  730. return;
  731. }
  732. cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
  733. while (*cursor == '/')
  734. cursor++;
  735. assign_string (&real_s_name, cursor);
  736. real_s_totsize = save_totsize;
  737. real_s_sizeleft = save_sizeleft;
  738. }
  739. return;
  740. }
  741. /* We're multivol. Panic if we didn't get the right kind of response. */
  742. /* ENXIO is for the UNIX PC. */
  743. if (status < 0 && errno != ENOSPC && errno != EIO && errno != ENXIO)
  744. archive_write_error (status);
  745. /* If error indicates a short write, we just move to the next tape. */
  746. if (!new_volume (ACCESS_WRITE))
  747. return;
  748. if (totals_option)
  749. prev_written += bytes_written;
  750. bytes_written = 0;
  751. if (volume_label_option && real_s_name)
  752. {
  753. copy_back = 2;
  754. record_start -= 2;
  755. }
  756. else if (volume_label_option || real_s_name)
  757. {
  758. copy_back = 1;
  759. record_start--;
  760. }
  761. else
  762. copy_back = 0;
  763. if (volume_label_option)
  764. {
  765. memset (record_start, 0, BLOCKSIZE);
  766. sprintf (record_start->header.name, "%s Volume %d",
  767. volume_label_option, volno);
  768. TIME_TO_CHARS (start_time, record_start->header.mtime);
  769. record_start->header.typeflag = GNUTYPE_VOLHDR;
  770. finish_header (record_start);
  771. }
  772. if (real_s_name)
  773. {
  774. int tmp;
  775. if (volume_label_option)
  776. record_start++;
  777. memset (record_start, 0, BLOCKSIZE);
  778. /* FIXME: Michael P Urban writes: [a long name file] is being written
  779. when a new volume rolls around [...] Looks like the wrong value is
  780. being preserved in real_s_name, though. */
  781. strcpy (record_start->header.name, real_s_name);
  782. record_start->header.typeflag = GNUTYPE_MULTIVOL;
  783. OFF_TO_CHARS (real_s_sizeleft, record_start->header.size);
  784. OFF_TO_CHARS (real_s_totsize - real_s_sizeleft,
  785. record_start->oldgnu_header.offset);
  786. tmp = verbose_option;
  787. verbose_option = 0;
  788. finish_header (record_start);
  789. verbose_option = tmp;
  790. if (volume_label_option)
  791. record_start--;
  792. }
  793. status = write_archive_buffer ();
  794. if (status != record_size)
  795. archive_write_error (status);
  796. bytes_written += status;
  797. if (copy_back)
  798. {
  799. record_start += copy_back;
  800. memcpy (current_block,
  801. record_start + blocking_factor - copy_back,
  802. copy_back * BLOCKSIZE);
  803. current_block += copy_back;
  804. if (real_s_sizeleft >= copy_back * BLOCKSIZE)
  805. real_s_sizeleft -= copy_back * BLOCKSIZE;
  806. else if ((real_s_sizeleft + BLOCKSIZE - 1) / BLOCKSIZE <= copy_back)
  807. assign_string (&real_s_name, 0);
  808. else
  809. {
  810. char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
  811. while (*cursor == '/')
  812. cursor++;
  813. assign_string (&real_s_name, cursor);
  814. real_s_sizeleft = save_sizeleft;
  815. real_s_totsize = save_totsize;
  816. }
  817. copy_back = 0;
  818. }
  819. }
  820. /* Handle write errors on the archive. Write errors are always fatal.
  821. Hitting the end of a volume does not cause a write error unless the
  822. write was the first record of the volume. */
  823. static void
  824. archive_write_error (ssize_t status)
  825. {
  826. /* It might be useful to know how much was written before the error
  827. occurred. */
  828. if (totals_option)
  829. {
  830. int e = errno;
  831. print_total_written ();
  832. errno = e;
  833. }
  834. write_fatal_details (*archive_name_cursor, status, record_size);
  835. }
  836. /* Handle read errors on the archive. If the read should be retried,
  837. return to the caller. */
  838. static void
  839. archive_read_error (void)
  840. {
  841. read_error (*archive_name_cursor);
  842. if (record_start_block == 0)
  843. FATAL_ERROR ((0, 0, _("At beginning of tape, quitting now")));
  844. /* Read error in mid archive. We retry up to READ_ERROR_MAX times and
  845. then give up on reading the archive. */
  846. if (read_error_count++ > READ_ERROR_MAX)
  847. FATAL_ERROR ((0, 0, _("Too many errors, quitting")));
  848. return;
  849. }
  850. /* Perform a read to flush the buffer. */
  851. void
  852. flush_read (void)
  853. {
  854. ssize_t status; /* result from system call */
  855. size_t left; /* bytes left */
  856. char *more; /* pointer to next byte to read */
  857. if (checkpoint_option && !(++checkpoint % 10))
  858. WARN ((0, 0, _("Read checkpoint %d"), checkpoint));
  859. /* Clear the count of errors. This only applies to a single call to
  860. flush_read. */
  861. read_error_count = 0; /* clear error count */
  862. if (write_archive_to_stdout && record_start_block != 0)
  863. {
  864. archive = STDOUT_FILENO;
  865. status = write_archive_buffer ();
  866. archive = STDIN_FILENO;
  867. if (status != record_size)
  868. archive_write_error (status);
  869. }
  870. if (multi_volume_option)
  871. {
  872. if (save_name)
  873. {
  874. char *cursor = save_name + FILESYSTEM_PREFIX_LEN (save_name);
  875. while (*cursor == '/')
  876. cursor++;
  877. assign_string (&real_s_name, cursor);
  878. real_s_sizeleft = save_sizeleft;
  879. real_s_totsize = save_totsize;
  880. }
  881. else
  882. {
  883. assign_string (&real_s_name, 0);
  884. real_s_totsize = 0;
  885. real_s_sizeleft = 0;
  886. }
  887. }
  888. error_loop:
  889. status = rmtread (archive, record_start->buffer, record_size);
  890. if (status == record_size)
  891. return;
  892. if ((status == 0
  893. || (status < 0 && errno == ENOSPC)
  894. || (status > 0 && !read_full_records_option))
  895. && multi_volume_option)
  896. {
  897. union block *cursor;
  898. try_volume:
  899. switch (subcommand_option)
  900. {
  901. case APPEND_SUBCOMMAND:
  902. case CAT_SUBCOMMAND:
  903. case UPDATE_SUBCOMMAND:
  904. if (!new_volume (ACCESS_UPDATE))
  905. return;
  906. break;
  907. default:
  908. if (!new_volume (ACCESS_READ))
  909. return;
  910. break;
  911. }
  912. vol_error:
  913. status = rmtread (archive, record_start->buffer, record_size);
  914. if (status < 0)
  915. {
  916. archive_read_error ();
  917. goto vol_error;
  918. }
  919. if (status != record_size)
  920. goto short_read;
  921. cursor = record_start;
  922. if (cursor->header.typeflag == GNUTYPE_VOLHDR)
  923. {
  924. if (volume_label_option)
  925. {
  926. if (!check_label_pattern (cursor))
  927. {
  928. WARN ((0, 0, _("Volume %s does not match %s"),
  929. quote_n (0, cursor->header.name),
  930. quote_n (1, volume_label_option)));
  931. volno--;
  932. global_volno--;
  933. goto try_volume;
  934. }
  935. }
  936. if (verbose_option)
  937. fprintf (stdlis, _("Reading %s\n"), quote (cursor->header.name));
  938. cursor++;
  939. }
  940. else if (volume_label_option)
  941. WARN ((0, 0, _("WARNING: No volume header")));
  942. if (real_s_name)
  943. {
  944. uintmax_t s1, s2;
  945. if (cursor->header.typeflag != GNUTYPE_MULTIVOL
  946. || strcmp (cursor->header.name, real_s_name))
  947. {
  948. WARN ((0, 0, _("%s is not continued on this volume"),
  949. quote (real_s_name)));
  950. volno--;
  951. global_volno--;
  952. goto try_volume;
  953. }
  954. s1 = UINTMAX_FROM_HEADER (cursor->header.size);
  955. s2 = UINTMAX_FROM_HEADER (cursor->oldgnu_header.offset);
  956. if (real_s_totsize != s1 + s2 || s1 + s2 < s2)
  957. {
  958. char totsizebuf[UINTMAX_STRSIZE_BOUND];
  959. char s1buf[UINTMAX_STRSIZE_BOUND];
  960. char s2buf[UINTMAX_STRSIZE_BOUND];
  961. WARN ((0, 0, _("%s is the wrong size (%s != %s + %s)"),
  962. quote (cursor->header.name),
  963. STRINGIFY_BIGINT (save_totsize, totsizebuf),
  964. STRINGIFY_BIGINT (s1, s1buf),
  965. STRINGIFY_BIGINT (s2, s2buf)));
  966. volno--;
  967. global_volno--;
  968. goto try_volume;
  969. }
  970. if (real_s_totsize - real_s_sizeleft
  971. != OFF_FROM_HEADER (cursor->oldgnu_header.offset))
  972. {
  973. WARN ((0, 0, _("This volume is out of sequence")));
  974. volno--;
  975. global_volno--;
  976. goto try_volume;
  977. }
  978. cursor++;
  979. }
  980. current_block = cursor;
  981. return;
  982. }
  983. else if (status < 0)
  984. {
  985. archive_read_error ();
  986. goto error_loop; /* try again */
  987. }
  988. short_read:
  989. more = record_start->buffer + status;
  990. left = record_size - status;
  991. while (left % BLOCKSIZE != 0
  992. || (left && status && read_full_records_option))
  993. {
  994. if (status)
  995. while ((status = rmtread (archive, more, left)) < 0)
  996. archive_read_error ();
  997. if (status == 0)
  998. {
  999. if (left % BLOCKSIZE != 0)
  1000. ERROR ((0, 0, _("%d garbage bytes ignored at end of archive"),
  1001. (int) ((record_size - left) % BLOCKSIZE)));
  1002. break;
  1003. }
  1004. if (! read_full_records_option)
  1005. FATAL_ERROR ((0, 0, _("Unaligned block (%lu bytes) in archive"),
  1006. (unsigned long) (record_size - left)));
  1007. /* User warned us about this. Fix up. */
  1008. left -= status;
  1009. more += status;
  1010. }
  1011. /* FIXME: for size=0, multi-volume support. On the first record, warn
  1012. about the problem. */
  1013. if (!read_full_records_option && verbose_option
  1014. && record_start_block == 0 && status > 0)
  1015. WARN ((0, 0, _("Record size = %lu blocks"),
  1016. (unsigned long) ((record_size - left) / BLOCKSIZE)));
  1017. record_end = record_start + (record_size - left) / BLOCKSIZE;
  1018. }
  1019. /* Flush the current buffer to/from the archive. */
  1020. void
  1021. flush_archive (void)
  1022. {
  1023. record_start_block += record_end - record_start;
  1024. current_block = record_start;
  1025. record_end = record_start + blocking_factor;
  1026. if (access_mode == ACCESS_READ && time_to_start_writing)
  1027. {
  1028. access_mode = ACCESS_WRITE;
  1029. time_to_start_writing = 0;
  1030. if (file_to_switch_to >= 0)
  1031. {
  1032. if (rmtclose (archive) != 0)
  1033. close_warn (*archive_name_cursor);
  1034. archive = file_to_switch_to;
  1035. }
  1036. else
  1037. backspace_output ();
  1038. }
  1039. switch (access_mode)
  1040. {
  1041. case ACCESS_READ:
  1042. flush_read ();
  1043. break;
  1044. case ACCESS_WRITE:
  1045. flush_write ();
  1046. break;
  1047. case ACCESS_UPDATE:
  1048. abort ();
  1049. }
  1050. }
  1051. /* Backspace the archive descriptor by one record worth. If it's a
  1052. tape, MTIOCTOP will work. If it's something else, try to seek on
  1053. it. If we can't seek, we lose! */
  1054. static void
  1055. backspace_output (void)
  1056. {
  1057. #ifdef MTIOCTOP
  1058. {
  1059. struct mtop operation;
  1060. operation.mt_op = MTBSR;
  1061. operation.mt_count = 1;
  1062. if (rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  1063. return;
  1064. if (errno == EIO && rmtioctl (archive, MTIOCTOP, (char *) &operation) >= 0)
  1065. return;
  1066. }
  1067. #endif
  1068. {
  1069. off_t position = rmtlseek (archive, (off_t) 0, SEEK_CUR);
  1070. /* Seek back to the beginning of this record and start writing there. */
  1071. position -= record_size;
  1072. if (position < 0)
  1073. position = 0;
  1074. if (rmtlseek (archive, position, SEEK_SET) != position)
  1075. {
  1076. /* Lseek failed. Try a different method. */
  1077. WARN ((0, 0,
  1078. _("Cannot backspace archive file; it may be unreadable without -i")));
  1079. /* Replace the first part of the record with NULs. */
  1080. if (record_start->buffer != output_start)
  1081. memset (record_start->buffer, 0,
  1082. output_start - record_start->buffer);
  1083. }
  1084. }
  1085. }
  1086. /* Close the archive file. */
  1087. void
  1088. close_archive (void)
  1089. {
  1090. if (time_to_start_writing || access_mode == ACCESS_WRITE)
  1091. flush_archive ();
  1092. #if !MSDOS
  1093. /* Manage to fully drain a pipe we might be reading, so to not break it on
  1094. the producer after the EOF block. FIXME: one of these days, GNU tar
  1095. might become clever enough to just stop working, once there is no more
  1096. work to do, we might have to revise this area in such time. */
  1097. if (access_mode == ACCESS_READ
  1098. && ! _isrmt (archive)
  1099. && S_ISFIFO (archive_stat.st_mode))
  1100. while (rmtread (archive, record_start->buffer, record_size) > 0)
  1101. continue;
  1102. #endif
  1103. if (verify_option)
  1104. verify_volume ();
  1105. if (rmtclose (archive) != 0)
  1106. close_warn (*archive_name_cursor);
  1107. #if !MSDOS
  1108. if (child_pid)
  1109. {
  1110. int wait_status;
  1111. while (waitpid (child_pid, &wait_status, 0) == -1)
  1112. if (errno != EINTR)
  1113. {
  1114. waitpid_error (use_compress_program_option);
  1115. break;
  1116. }
  1117. if (WIFSIGNALED (wait_status))
  1118. ERROR ((0, 0, _("Child died with signal %d"),
  1119. WTERMSIG (wait_status)));
  1120. else if (WEXITSTATUS (wait_status) != 0)
  1121. ERROR ((0, 0, _("Child returned status %d"),
  1122. WEXITSTATUS (wait_status)));
  1123. }
  1124. #endif /* !MSDOS */
  1125. if (current_file_name)
  1126. free (current_file_name);
  1127. if (current_link_name)
  1128. free (current_link_name);
  1129. if (save_name)
  1130. free (save_name);
  1131. if (real_s_name)
  1132. free (real_s_name);
  1133. free (multi_volume_option ? record_start - 2 : record_start);
  1134. }
  1135. /* Called to initialize the global volume number. */
  1136. void
  1137. init_volume_number (void)
  1138. {
  1139. FILE *file = fopen (volno_file_option, "r");
  1140. if (file)
  1141. {
  1142. if (fscanf (file, "%d", &global_volno) != 1
  1143. || global_volno < 0)
  1144. FATAL_ERROR ((0, 0, _("%s: contains invalid volume number"),
  1145. quotearg_colon (volno_file_option)));
  1146. if (ferror (file))
  1147. read_error (volno_file_option);
  1148. if (fclose (file) != 0)
  1149. close_error (volno_file_option);
  1150. }
  1151. else if (errno != ENOENT)
  1152. open_error (volno_file_option);
  1153. }
  1154. /* Called to write out the closing global volume number. */
  1155. void
  1156. closeout_volume_number (void)
  1157. {
  1158. FILE *file = fopen (volno_file_option, "w");
  1159. if (file)
  1160. {
  1161. fprintf (file, "%d\n", global_volno);
  1162. if (ferror (file))
  1163. write_error (volno_file_option);
  1164. if (fclose (file) != 0)
  1165. close_error (volno_file_option);
  1166. }
  1167. else
  1168. open_error (volno_file_option);
  1169. }
  1170. /* We've hit the end of the old volume. Close it and open the next one.
  1171. Return nonzero on success. */
  1172. static int
  1173. new_volume (enum access_mode access)
  1174. {
  1175. static FILE *read_file;
  1176. static int looped;
  1177. if (!read_file && !info_script_option)
  1178. /* FIXME: if fopen is used, it will never be closed. */
  1179. read_file = archive == STDIN_FILENO ? fopen (TTY_NAME, "r") : stdin;
  1180. if (now_verifying)
  1181. return 0;
  1182. if (verify_option)
  1183. verify_volume ();
  1184. if (rmtclose (archive) != 0)
  1185. close_warn (*archive_name_cursor);
  1186. global_volno++;
  1187. if (global_volno < 0)
  1188. FATAL_ERROR ((0, 0, _("Volume number overflow")));
  1189. volno++;
  1190. archive_name_cursor++;
  1191. if (archive_name_cursor == archive_name_array + archive_names)
  1192. {
  1193. archive_name_cursor = archive_name_array;
  1194. looped = 1;
  1195. }
  1196. tryagain:
  1197. if (looped)
  1198. {
  1199. /* We have to prompt from now on. */
  1200. if (info_script_option)
  1201. {
  1202. if (volno_file_option)
  1203. closeout_volume_number ();
  1204. system (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. }