buffer.c 43 KB

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