buffer.c 43 KB

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