buffer.c 44 KB

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