buffer.c 44 KB

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