buffer.c 42 KB

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