]>
Commit | Line | Data |
---|---|---|
40bf83fe | 1 | /* $OpenBSD: ar_io.c,v 1.38 2008/06/11 00:49:08 pvalchev Exp $ */ |
44a7a5ab A |
2 | /* $NetBSD: ar_io.c,v 1.5 1996/03/26 23:54:13 mrg Exp $ */ |
3 | ||
4 | /*- | |
5 | * Copyright (c) 1992 Keith Muller. | |
6 | * Copyright (c) 1992, 1993 | |
7 | * The Regents of the University of California. All rights reserved. | |
8 | * | |
9 | * This code is derived from software contributed to Berkeley by | |
10 | * Keith Muller of the University of California, San Diego. | |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or without | |
13 | * modification, are permitted provided that the following conditions | |
14 | * are met: | |
15 | * 1. Redistributions of source code must retain the above copyright | |
16 | * notice, this list of conditions and the following disclaimer. | |
17 | * 2. Redistributions in binary form must reproduce the above copyright | |
18 | * notice, this list of conditions and the following disclaimer in the | |
19 | * documentation and/or other materials provided with the distribution. | |
864a4b6e | 20 | * 3. Neither the name of the University nor the names of its contributors |
44a7a5ab A |
21 | * may be used to endorse or promote products derived from this software |
22 | * without specific prior written permission. | |
23 | * | |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
34 | * SUCH DAMAGE. | |
35 | */ | |
36 | ||
00337e45 | 37 | #include <sys/cdefs.h> |
44a7a5ab A |
38 | #ifndef lint |
39 | #if 0 | |
864a4b6e | 40 | static const char sccsid[] = "@(#)ar_io.c 8.2 (Berkeley) 4/18/94"; |
44a7a5ab | 41 | #else |
00337e45 | 42 | __used static const char rcsid[] = "$OpenBSD: ar_io.c,v 1.38 2008/06/11 00:49:08 pvalchev Exp $"; |
44a7a5ab A |
43 | #endif |
44 | #endif /* not lint */ | |
45 | ||
46 | #include <sys/types.h> | |
47 | #include <sys/time.h> | |
48 | #include <sys/stat.h> | |
49 | #include <sys/ioctl.h> | |
40bf83fe | 50 | #ifndef __APPLE__ |
44a7a5ab | 51 | #include <sys/mtio.h> |
40bf83fe | 52 | #endif /* !__APPLE__ */ |
44a7a5ab | 53 | #include <sys/param.h> |
864a4b6e | 54 | #include <sys/wait.h> |
44a7a5ab A |
55 | #include <signal.h> |
56 | #include <string.h> | |
57 | #include <fcntl.h> | |
58 | #include <unistd.h> | |
59 | #include <stdio.h> | |
60 | #include <errno.h> | |
61 | #include <stdlib.h> | |
62 | #include <err.h> | |
40bf83fe | 63 | #include <stdint.h> |
44a7a5ab A |
64 | #include "pax.h" |
65 | #include "options.h" | |
66 | #include "extern.h" | |
67 | ||
68 | /* | |
69 | * Routines which deal directly with the archive I/O device/file. | |
70 | */ | |
71 | ||
72 | #define DMOD 0666 /* default mode of created archives */ | |
73 | #define EXT_MODE O_RDONLY /* open mode for list/extract */ | |
74 | #define AR_MODE (O_WRONLY | O_CREAT | O_TRUNC) /* mode for archive */ | |
75 | #define APP_MODE O_RDWR /* mode for append */ | |
864a4b6e A |
76 | #define STDO "<STDOUT>" /* pseudo name for stdout */ |
77 | #define STDN "<STDIN>" /* pseudo name for stdin */ | |
40bf83fe | 78 | #define _NONE "<NONE>" /* pseudo name for no files */ |
44a7a5ab A |
79 | static int arfd = -1; /* archive file descriptor */ |
80 | static int artyp = ISREG; /* archive type: file/FIFO/tape */ | |
81 | static int arvol = 1; /* archive volume number */ | |
82 | static int lstrval = -1; /* return value from last i/o */ | |
83 | static int io_ok; /* i/o worked on volume after resync */ | |
84 | static int did_io; /* did i/o ever occur on volume? */ | |
85 | static int done; /* set via tty termination */ | |
86 | static struct stat arsb; /* stat of archive device at open */ | |
87 | static int invld_rec; /* tape has out of spec record size */ | |
88 | static int wr_trail = 1; /* trailer was rewritten in append */ | |
89 | static int can_unlnk = 0; /* do we unlink null archives? */ | |
864a4b6e A |
90 | const char *arcname; /* printable name of archive */ |
91 | const char *gzip_program; /* name of gzip program */ | |
92 | static pid_t zpid = -1; /* pid of child process */ | |
93 | int force_one_volume; /* 1 if we ignore volume changes */ | |
44a7a5ab | 94 | |
40bf83fe | 95 | #ifndef __APPLE__ |
864a4b6e | 96 | static int get_phys(void); |
40bf83fe | 97 | #endif /* __APPLE__ */ |
44a7a5ab | 98 | extern sigset_t s_mask; |
864a4b6e | 99 | static void ar_start_gzip(int, const char *, int); |
44a7a5ab A |
100 | |
101 | /* | |
102 | * ar_open() | |
103 | * Opens the next archive volume. Determines the type of the device and | |
104 | * sets up block sizes as required by the archive device and the format. | |
105 | * Note: we may be called with name == NULL on the first open only. | |
106 | * Return: | |
107 | * -1 on failure, 0 otherwise | |
108 | */ | |
109 | ||
44a7a5ab | 110 | int |
864a4b6e | 111 | ar_open(const char *name) |
44a7a5ab | 112 | { |
40bf83fe | 113 | #ifndef __APPLE__ |
44a7a5ab | 114 | struct mtget mb; |
40bf83fe | 115 | #endif /* __APPLE__ */ |
44a7a5ab A |
116 | if (arfd != -1) |
117 | (void)close(arfd); | |
118 | arfd = -1; | |
119 | can_unlnk = did_io = io_ok = invld_rec = 0; | |
120 | artyp = ISREG; | |
121 | flcnt = 0; | |
122 | ||
123 | /* | |
124 | * open based on overall operation mode | |
125 | */ | |
126 | switch (act) { | |
127 | case LIST: | |
128 | case EXTRACT: | |
129 | if (name == NULL) { | |
130 | arfd = STDIN_FILENO; | |
131 | arcname = STDN; | |
864a4b6e A |
132 | } else if ((arfd = open(name, EXT_MODE, DMOD)) < 0) |
133 | syswarn(1, errno, "Failed open to read on %s", name); | |
134 | if (arfd != -1 && gzip_program != NULL) | |
135 | ar_start_gzip(arfd, gzip_program, 0); | |
44a7a5ab A |
136 | break; |
137 | case ARCHIVE: | |
138 | if (name == NULL) { | |
139 | arfd = STDOUT_FILENO; | |
140 | arcname = STDO; | |
141 | } else if ((arfd = open(name, AR_MODE, DMOD)) < 0) | |
864a4b6e | 142 | syswarn(1, errno, "Failed open to write on %s", name); |
44a7a5ab A |
143 | else |
144 | can_unlnk = 1; | |
864a4b6e A |
145 | if (arfd != -1 && gzip_program != NULL) |
146 | ar_start_gzip(arfd, gzip_program, 1); | |
44a7a5ab A |
147 | break; |
148 | case APPND: | |
44a7a5ab A |
149 | if (name == NULL) { |
150 | arfd = STDOUT_FILENO; | |
151 | arcname = STDO; | |
152 | } else if ((arfd = open(name, APP_MODE, DMOD)) < 0) | |
864a4b6e | 153 | syswarn(1, errno, "Failed open to read/write on %s", |
44a7a5ab A |
154 | name); |
155 | break; | |
156 | case COPY: | |
157 | /* | |
158 | * arfd not used in COPY mode | |
159 | */ | |
40bf83fe | 160 | arcname = _NONE; |
44a7a5ab A |
161 | lstrval = 1; |
162 | return(0); | |
163 | } | |
164 | if (arfd < 0) | |
165 | return(-1); | |
166 | ||
167 | if (chdname != NULL) | |
40bf83fe | 168 | if (dochdir(chdname) == -1) { |
864a4b6e A |
169 | return(-1); |
170 | } | |
44a7a5ab A |
171 | /* |
172 | * set up is based on device type | |
173 | */ | |
174 | if (fstat(arfd, &arsb) < 0) { | |
175 | syswarn(0, errno, "Failed stat on %s", arcname); | |
176 | (void)close(arfd); | |
177 | arfd = -1; | |
178 | can_unlnk = 0; | |
179 | return(-1); | |
180 | } | |
181 | if (S_ISDIR(arsb.st_mode)) { | |
182 | paxwarn(0, "Cannot write an archive on top of a directory %s", | |
183 | arcname); | |
184 | (void)close(arfd); | |
185 | arfd = -1; | |
186 | can_unlnk = 0; | |
187 | return(-1); | |
188 | } | |
189 | ||
40bf83fe | 190 | #ifndef __APPLE__ |
44a7a5ab A |
191 | if (S_ISCHR(arsb.st_mode)) |
192 | artyp = ioctl(arfd, MTIOCGET, &mb) ? ISCHR : ISTAPE; | |
40bf83fe A |
193 | else |
194 | #endif /* !__APPLE__ */ | |
195 | if (S_ISBLK(arsb.st_mode)) | |
44a7a5ab A |
196 | artyp = ISBLK; |
197 | else if ((lseek(arfd, (off_t)0L, SEEK_CUR) == -1) && (errno == ESPIPE)) | |
198 | artyp = ISPIPE; | |
199 | else | |
200 | artyp = ISREG; | |
201 | ||
202 | /* | |
203 | * make sure we beyond any doubt that we only can unlink regular files | |
204 | * we created | |
205 | */ | |
206 | if (artyp != ISREG) | |
207 | can_unlnk = 0; | |
208 | /* | |
209 | * if we are writing, we are done | |
210 | */ | |
211 | if (act == ARCHIVE) { | |
212 | blksz = rdblksz = wrblksz; | |
213 | lstrval = 1; | |
214 | return(0); | |
215 | } | |
216 | ||
217 | /* | |
218 | * set default blksz on read. APPNDs writes rdblksz on the last volume | |
219 | * On all new archive volumes, we shift to wrblksz (if the user | |
864a4b6e A |
220 | * specified one, otherwise we will continue to use rdblksz). We |
221 | * must set blocksize based on what kind of device the archive is | |
44a7a5ab A |
222 | * stored. |
223 | */ | |
864a4b6e | 224 | switch (artyp) { |
44a7a5ab A |
225 | case ISTAPE: |
226 | /* | |
227 | * Tape drives come in at least two flavors. Those that support | |
228 | * variable sized records and those that have fixed sized | |
229 | * records. They must be treated differently. For tape drives | |
230 | * that support variable sized records, we must make large | |
231 | * reads to make sure we get the entire record, otherwise we | |
232 | * will just get the first part of the record (up to size we | |
233 | * asked). Tapes with fixed sized records may or may not return | |
234 | * multiple records in a single read. We really do not care | |
235 | * what the physical record size is UNLESS we are going to | |
236 | * append. (We will need the physical block size to rewrite | |
237 | * the trailer). Only when we are appending do we go to the | |
238 | * effort to figure out the true PHYSICAL record size. | |
239 | */ | |
240 | blksz = rdblksz = MAXBLK; | |
241 | break; | |
242 | case ISPIPE: | |
243 | case ISBLK: | |
244 | case ISCHR: | |
245 | /* | |
246 | * Blocksize is not a major issue with these devices (but must | |
247 | * be kept a multiple of 512). If the user specified a write | |
248 | * block size, we use that to read. Under append, we must | |
249 | * always keep blksz == rdblksz. Otherwise we go ahead and use | |
250 | * the device optimal blocksize as (and if) returned by stat | |
251 | * and if it is within pax specs. | |
252 | */ | |
253 | if ((act == APPND) && wrblksz) { | |
254 | blksz = rdblksz = wrblksz; | |
255 | break; | |
256 | } | |
257 | ||
258 | if ((arsb.st_blksize > 0) && (arsb.st_blksize < MAXBLK) && | |
259 | ((arsb.st_blksize % BLKMULT) == 0)) | |
260 | rdblksz = arsb.st_blksize; | |
261 | else | |
262 | rdblksz = DEVBLK; | |
263 | /* | |
264 | * For performance go for large reads when we can without harm | |
265 | */ | |
266 | if ((act == APPND) || (artyp == ISCHR)) | |
267 | blksz = rdblksz; | |
268 | else | |
269 | blksz = MAXBLK; | |
270 | break; | |
271 | case ISREG: | |
272 | /* | |
273 | * if the user specified wrblksz works, use it. Under appends | |
274 | * we must always keep blksz == rdblksz | |
275 | */ | |
276 | if ((act == APPND) && wrblksz && ((arsb.st_size%wrblksz)==0)){ | |
277 | blksz = rdblksz = wrblksz; | |
278 | break; | |
279 | } | |
280 | /* | |
281 | * See if we can find the blocking factor from the file size | |
282 | */ | |
283 | for (rdblksz = MAXBLK; rdblksz > 0; rdblksz -= BLKMULT) | |
284 | if ((arsb.st_size % rdblksz) == 0) | |
285 | break; | |
286 | /* | |
864a4b6e | 287 | * When we cannot find a match, we may have a flawed archive. |
44a7a5ab A |
288 | */ |
289 | if (rdblksz <= 0) | |
290 | rdblksz = FILEBLK; | |
291 | /* | |
292 | * for performance go for large reads when we can | |
293 | */ | |
294 | if (act == APPND) | |
295 | blksz = rdblksz; | |
296 | else | |
297 | blksz = MAXBLK; | |
298 | break; | |
299 | default: | |
300 | /* | |
864a4b6e | 301 | * should never happen, worst case, slow... |
44a7a5ab A |
302 | */ |
303 | blksz = rdblksz = BLKMULT; | |
304 | break; | |
305 | } | |
306 | lstrval = 1; | |
307 | return(0); | |
308 | } | |
309 | ||
310 | /* | |
311 | * ar_close() | |
312 | * closes archive device, increments volume number, and prints i/o summary | |
313 | */ | |
44a7a5ab A |
314 | void |
315 | ar_close(void) | |
44a7a5ab | 316 | { |
864a4b6e | 317 | int status; |
44a7a5ab A |
318 | |
319 | if (arfd < 0) { | |
320 | did_io = io_ok = flcnt = 0; | |
321 | return; | |
322 | } | |
323 | ||
44a7a5ab A |
324 | /* |
325 | * Close archive file. This may take a LONG while on tapes (we may be | |
326 | * forced to wait for the rewind to complete) so tell the user what is | |
327 | * going on (this avoids the user hitting control-c thinking pax is | |
328 | * broken). | |
329 | */ | |
330 | if (vflag && (artyp == ISTAPE)) { | |
331 | if (vfpart) | |
864a4b6e A |
332 | (void)putc('\n', listf); |
333 | (void)fprintf(listf, | |
44a7a5ab A |
334 | "%s: Waiting for tape drive close to complete...", |
335 | argv0); | |
864a4b6e | 336 | (void)fflush(listf); |
44a7a5ab A |
337 | } |
338 | ||
339 | /* | |
340 | * if nothing was written to the archive (and we created it), we remove | |
341 | * it | |
342 | */ | |
343 | if (can_unlnk && (fstat(arfd, &arsb) == 0) && (S_ISREG(arsb.st_mode)) && | |
344 | (arsb.st_size == 0)) { | |
345 | (void)unlink(arcname); | |
346 | can_unlnk = 0; | |
347 | } | |
348 | ||
864a4b6e A |
349 | /* |
350 | * for a quick extract/list, pax frequently exits before the child | |
351 | * process is done | |
352 | */ | |
353 | if ((act == LIST || act == EXTRACT) && nflag && zpid > 0) | |
354 | kill(zpid, SIGINT); | |
355 | ||
44a7a5ab A |
356 | (void)close(arfd); |
357 | ||
864a4b6e A |
358 | /* Do not exit before child to ensure data integrity */ |
359 | if (zpid > 0) | |
360 | waitpid(zpid, &status, 0); | |
361 | ||
44a7a5ab | 362 | if (vflag && (artyp == ISTAPE)) { |
864a4b6e | 363 | (void)fputs("done.\n", listf); |
44a7a5ab | 364 | vfpart = 0; |
864a4b6e | 365 | (void)fflush(listf); |
44a7a5ab A |
366 | } |
367 | arfd = -1; | |
368 | ||
369 | if (!io_ok && !did_io) { | |
370 | flcnt = 0; | |
371 | return; | |
372 | } | |
373 | did_io = io_ok = 0; | |
374 | ||
375 | /* | |
376 | * The volume number is only increased when the last device has data | |
377 | * and we have already determined the archive format. | |
378 | */ | |
379 | if (frmt != NULL) | |
380 | ++arvol; | |
381 | ||
382 | if (!vflag) { | |
383 | flcnt = 0; | |
384 | return; | |
385 | } | |
386 | ||
387 | /* | |
388 | * Print out a summary of I/O for this archive volume. | |
389 | */ | |
390 | if (vfpart) { | |
864a4b6e | 391 | (void)putc('\n', listf); |
44a7a5ab A |
392 | vfpart = 0; |
393 | } | |
394 | ||
395 | /* | |
396 | * If we have not determined the format yet, we just say how many bytes | |
397 | * we have skipped over looking for a header to id. there is no way we | |
398 | * could have written anything yet. | |
399 | */ | |
400 | if (frmt == NULL) { | |
864a4b6e A |
401 | # ifdef LONG_OFF_T |
402 | (void)fprintf(listf, "%s: unknown format, %lu bytes skipped.\n", | |
44a7a5ab | 403 | # else |
864a4b6e | 404 | (void)fprintf(listf, "%s: unknown format, %qu bytes skipped.\n", |
44a7a5ab A |
405 | # endif |
406 | argv0, rdcnt); | |
864a4b6e | 407 | (void)fflush(listf); |
44a7a5ab A |
408 | flcnt = 0; |
409 | return; | |
410 | } | |
411 | ||
412 | if (strcmp(NM_CPIO, argv0) == 0) | |
864a4b6e A |
413 | (void)fprintf(listf, "%qu blocks\n", (rdcnt ? rdcnt : wrcnt) / 5120); |
414 | else if (strcmp(NM_TAR, argv0) != 0 && strcmp(NM_PAX, argv0) != 0) | |
415 | (void)fprintf(listf, | |
416 | # ifdef LONG_OFF_T | |
44a7a5ab | 417 | "%s: %s vol %d, %lu files, %lu bytes read, %lu bytes written.\n", |
40bf83fe | 418 | argv0, frmt->name, arvol-1, flcnt, rdcnt, wrcnt); |
44a7a5ab | 419 | # else |
40bf83fe A |
420 | "%s: %s vol %d, %lu files, %ju bytes read, %ju bytes written.\n", |
421 | argv0, frmt->name, arvol-1, flcnt, (uintmax_t)rdcnt, (uintmax_t)wrcnt); | |
44a7a5ab | 422 | # endif |
864a4b6e | 423 | (void)fflush(listf); |
44a7a5ab A |
424 | flcnt = 0; |
425 | } | |
426 | ||
427 | /* | |
428 | * ar_drain() | |
429 | * drain any archive format independent padding from an archive read | |
430 | * from a socket or a pipe. This is to prevent the process on the | |
431 | * other side of the pipe from getting a SIGPIPE (pax will stop | |
432 | * reading an archive once a format dependent trailer is detected). | |
433 | */ | |
44a7a5ab A |
434 | void |
435 | ar_drain(void) | |
44a7a5ab | 436 | { |
864a4b6e | 437 | int res; |
44a7a5ab A |
438 | char drbuf[MAXBLK]; |
439 | ||
440 | /* | |
441 | * we only drain from a pipe/socket. Other devices can be closed | |
442 | * without reading up to end of file. We sure hope that pipe is closed | |
443 | * on the other side so we will get an EOF. | |
444 | */ | |
445 | if ((artyp != ISPIPE) || (lstrval <= 0)) | |
446 | return; | |
447 | ||
448 | /* | |
449 | * keep reading until pipe is drained | |
450 | */ | |
451 | while ((res = read(arfd, drbuf, sizeof(drbuf))) > 0) | |
452 | ; | |
453 | lstrval = res; | |
454 | } | |
455 | ||
456 | /* | |
457 | * ar_set_wr() | |
458 | * Set up device right before switching from read to write in an append. | |
459 | * device dependent code (if required) to do this should be added here. | |
460 | * For all archive devices we are already positioned at the place we want | |
461 | * to start writing when this routine is called. | |
462 | * Return: | |
463 | * 0 if all ready to write, -1 otherwise | |
464 | */ | |
465 | ||
44a7a5ab A |
466 | int |
467 | ar_set_wr(void) | |
44a7a5ab A |
468 | { |
469 | off_t cpos; | |
470 | ||
471 | /* | |
472 | * we must make sure the trailer is rewritten on append, ar_next() | |
473 | * will stop us if the archive containing the trailer was not written | |
474 | */ | |
475 | wr_trail = 0; | |
476 | ||
864a4b6e | 477 | /* |
44a7a5ab A |
478 | * Add any device dependent code as required here |
479 | */ | |
480 | if (artyp != ISREG) | |
481 | return(0); | |
482 | /* | |
483 | * Ok we have an archive in a regular file. If we were rewriting a | |
484 | * file, we must get rid of all the stuff after the current offset | |
485 | * (it was not written by pax). | |
486 | */ | |
487 | if (((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) || | |
488 | (ftruncate(arfd, cpos) < 0)) { | |
489 | syswarn(1, errno, "Unable to truncate archive file"); | |
490 | return(-1); | |
491 | } | |
492 | return(0); | |
493 | } | |
494 | ||
495 | /* | |
496 | * ar_app_ok() | |
497 | * check if the last volume in the archive allows appends. We cannot check | |
864a4b6e | 498 | * this until we are ready to write since there is no spec that says all |
44a7a5ab A |
499 | * volumes in a single archive have to be of the same type... |
500 | * Return: | |
501 | * 0 if we can append, -1 otherwise. | |
502 | */ | |
503 | ||
44a7a5ab A |
504 | int |
505 | ar_app_ok(void) | |
44a7a5ab A |
506 | { |
507 | if (artyp == ISPIPE) { | |
508 | paxwarn(1, "Cannot append to an archive obtained from a pipe."); | |
509 | return(-1); | |
510 | } | |
511 | ||
512 | if (!invld_rec) | |
513 | return(0); | |
514 | paxwarn(1,"Cannot append, device record size %d does not support %s spec", | |
515 | rdblksz, argv0); | |
516 | return(-1); | |
517 | } | |
518 | ||
519 | /* | |
520 | * ar_read() | |
521 | * read up to a specified number of bytes from the archive into the | |
522 | * supplied buffer. When dealing with tapes we may not always be able to | |
523 | * read what we want. | |
524 | * Return: | |
525 | * Number of bytes in buffer. 0 for end of file, -1 for a read error. | |
526 | */ | |
527 | ||
44a7a5ab | 528 | int |
864a4b6e | 529 | ar_read(char *buf, int cnt) |
44a7a5ab | 530 | { |
864a4b6e | 531 | int res = 0; |
44a7a5ab A |
532 | |
533 | /* | |
534 | * if last i/o was in error, no more reads until reset or new volume | |
535 | */ | |
536 | if (lstrval <= 0) | |
537 | return(lstrval); | |
538 | ||
539 | /* | |
540 | * how we read must be based on device type | |
541 | */ | |
542 | switch (artyp) { | |
543 | case ISTAPE: | |
544 | if ((res = read(arfd, buf, cnt)) > 0) { | |
545 | /* | |
546 | * CAUTION: tape systems may not always return the same | |
547 | * sized records so we leave blksz == MAXBLK. The | |
548 | * physical record size that a tape drive supports is | |
549 | * very hard to determine in a uniform and portable | |
550 | * manner. | |
551 | */ | |
552 | io_ok = 1; | |
553 | if (res != rdblksz) { | |
554 | /* | |
864a4b6e | 555 | * Record size changed. If this happens on |
44a7a5ab A |
556 | * any record after the first, we probably have |
557 | * a tape drive which has a fixed record size | |
864a4b6e | 558 | * (we are getting multiple records in a single |
44a7a5ab A |
559 | * read). Watch out for record blocking that |
560 | * violates pax spec (must be a multiple of | |
561 | * BLKMULT). | |
562 | */ | |
563 | rdblksz = res; | |
564 | if (rdblksz % BLKMULT) | |
565 | invld_rec = 1; | |
566 | } | |
567 | return(res); | |
568 | } | |
569 | break; | |
570 | case ISREG: | |
571 | case ISBLK: | |
572 | case ISCHR: | |
573 | case ISPIPE: | |
574 | default: | |
575 | /* | |
576 | * Files are so easy to deal with. These other things cannot | |
577 | * be trusted at all. So when we are dealing with character | |
578 | * devices and pipes we just take what they have ready for us | |
579 | * and return. Trying to do anything else with them runs the | |
580 | * risk of failure. | |
581 | */ | |
582 | if ((res = read(arfd, buf, cnt)) > 0) { | |
583 | io_ok = 1; | |
584 | return(res); | |
585 | } | |
586 | break; | |
587 | } | |
588 | ||
589 | /* | |
590 | * We are in trouble at this point, something is broken... | |
591 | */ | |
592 | lstrval = res; | |
593 | if (res < 0) | |
594 | syswarn(1, errno, "Failed read on archive volume %d", arvol); | |
595 | else | |
596 | paxwarn(0, "End of archive volume %d reached", arvol); | |
597 | return(res); | |
598 | } | |
599 | ||
600 | /* | |
601 | * ar_write() | |
602 | * Write a specified number of bytes in supplied buffer to the archive | |
603 | * device so it appears as a single "block". Deals with errors and tries | |
604 | * to recover when faced with short writes. | |
605 | * Return: | |
606 | * Number of bytes written. 0 indicates end of volume reached and with no | |
607 | * flaws (as best that can be detected). A -1 indicates an unrecoverable | |
864a4b6e | 608 | * error in the archive occurred. |
44a7a5ab A |
609 | */ |
610 | ||
44a7a5ab | 611 | int |
864a4b6e | 612 | ar_write(char *buf, int bsz) |
44a7a5ab | 613 | { |
864a4b6e | 614 | int res; |
44a7a5ab A |
615 | off_t cpos; |
616 | ||
617 | /* | |
618 | * do not allow pax to create a "bad" archive. Once a write fails on | |
619 | * an archive volume prevent further writes to it. | |
620 | */ | |
621 | if (lstrval <= 0) | |
622 | return(lstrval); | |
623 | ||
624 | if ((res = write(arfd, buf, bsz)) == bsz) { | |
625 | wr_trail = 1; | |
626 | io_ok = 1; | |
627 | return(bsz); | |
c59d3020 A |
628 | } else if (res < 0 && artyp == ISPIPE && errno == EPIPE) { /* ignore it */ |
629 | wr_trail = 1; | |
630 | io_ok = 1; | |
631 | errno = 0; | |
632 | arfd = open("/dev/null", AR_MODE, DMOD); | |
633 | artyp = ISREG; | |
634 | return bsz; | |
864a4b6e | 635 | } |
c59d3020 | 636 | |
44a7a5ab A |
637 | /* |
638 | * write broke, see what we can do with it. We try to send any partial | |
639 | * writes that may violate pax spec to the next archive volume. | |
640 | */ | |
641 | if (res < 0) | |
642 | lstrval = res; | |
643 | else | |
644 | lstrval = 0; | |
645 | ||
646 | switch (artyp) { | |
647 | case ISREG: | |
648 | if ((res > 0) && (res % BLKMULT)) { | |
649 | /* | |
864a4b6e | 650 | * try to fix up partial writes which are not BLKMULT |
44a7a5ab A |
651 | * in size by forcing the runt record to next archive |
652 | * volume | |
864a4b6e | 653 | */ |
44a7a5ab A |
654 | if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) |
655 | break; | |
656 | cpos -= (off_t)res; | |
657 | if (ftruncate(arfd, cpos) < 0) | |
658 | break; | |
659 | res = lstrval = 0; | |
660 | break; | |
661 | } | |
662 | if (res >= 0) | |
663 | break; | |
664 | /* | |
665 | * if file is out of space, handle it like a return of 0 | |
666 | */ | |
667 | if ((errno == ENOSPC) || (errno == EFBIG) || (errno == EDQUOT)) | |
668 | res = lstrval = 0; | |
669 | break; | |
670 | case ISTAPE: | |
671 | case ISCHR: | |
672 | case ISBLK: | |
673 | if (res >= 0) | |
674 | break; | |
675 | if (errno == EACCES) { | |
676 | paxwarn(0, "Write failed, archive is write protected."); | |
40bf83fe | 677 | lstrval = 0; |
44a7a5ab A |
678 | return(0); |
679 | } | |
680 | /* | |
681 | * see if we reached the end of media, if so force a change to | |
682 | * the next volume | |
683 | */ | |
684 | if ((errno == ENOSPC) || (errno == EIO) || (errno == ENXIO)) | |
685 | res = lstrval = 0; | |
686 | break; | |
687 | case ISPIPE: | |
688 | default: | |
689 | /* | |
690 | * we cannot fix errors to these devices | |
691 | */ | |
692 | break; | |
693 | } | |
694 | ||
695 | /* | |
696 | * Better tell the user the bad news... | |
697 | * if this is a block aligned archive format, we may have a bad archive | |
864a4b6e | 698 | * if the format wants the header to start at a BLKMULT boundary.. While |
44a7a5ab A |
699 | * we can deal with the mis-aligned data, it violates spec and other |
700 | * archive readers will likely fail. if the format is not block | |
701 | * aligned, the user may be lucky (and the archive is ok). | |
702 | */ | |
703 | if (res >= 0) { | |
704 | if (res > 0) | |
705 | wr_trail = 1; | |
706 | io_ok = 1; | |
707 | } | |
708 | ||
709 | /* | |
710 | * If we were trying to rewrite the trailer and it didn't work, we | |
711 | * must quit right away. | |
712 | */ | |
713 | if (!wr_trail && (res <= 0)) { | |
714 | paxwarn(1,"Unable to append, trailer re-write failed. Quitting."); | |
715 | return(res); | |
716 | } | |
717 | ||
718 | if (res == 0) | |
719 | paxwarn(0, "End of archive volume %d reached", arvol); | |
720 | else if (res < 0) | |
721 | syswarn(1, errno, "Failed write to archive volume: %d", arvol); | |
722 | else if (!frmt->blkalgn || ((res % frmt->blkalgn) == 0)) | |
723 | paxwarn(0,"WARNING: partial archive write. Archive MAY BE FLAWED"); | |
724 | else | |
725 | paxwarn(1,"WARNING: partial archive write. Archive IS FLAWED"); | |
726 | return(res); | |
727 | } | |
728 | ||
729 | /* | |
730 | * ar_rdsync() | |
731 | * Try to move past a bad spot on a flawed archive as needed to continue | |
732 | * I/O. Clears error flags to allow I/O to continue. | |
733 | * Return: | |
734 | * 0 when ok to try i/o again, -1 otherwise. | |
735 | */ | |
736 | ||
44a7a5ab A |
737 | int |
738 | ar_rdsync(void) | |
44a7a5ab A |
739 | { |
740 | long fsbz; | |
741 | off_t cpos; | |
742 | off_t mpos; | |
40bf83fe | 743 | #ifndef __APPLE__ |
44a7a5ab | 744 | struct mtop mb; |
40bf83fe | 745 | #endif /* !__APPLE__ */ |
44a7a5ab A |
746 | |
747 | /* | |
864a4b6e | 748 | * Fail resync attempts at user request (done) or if this is going to be |
44a7a5ab A |
749 | * an update/append to a existing archive. if last i/o hit media end, |
750 | * we need to go to the next volume not try a resync | |
751 | */ | |
752 | if ((done > 0) || (lstrval == 0)) | |
753 | return(-1); | |
754 | ||
755 | if ((act == APPND) || (act == ARCHIVE)) { | |
756 | paxwarn(1, "Cannot allow updates to an archive with flaws."); | |
757 | return(-1); | |
758 | } | |
759 | if (io_ok) | |
760 | did_io = 1; | |
761 | ||
864a4b6e | 762 | switch (artyp) { |
40bf83fe | 763 | #ifndef __APPLE__ |
44a7a5ab A |
764 | case ISTAPE: |
765 | /* | |
766 | * if the last i/o was a successful data transfer, we assume | |
767 | * the fault is just a bad record on the tape that we are now | |
768 | * past. If we did not get any data since the last resync try | |
864a4b6e | 769 | * to move the tape forward one PHYSICAL record past any |
44a7a5ab A |
770 | * damaged tape section. Some tape drives are stubborn and need |
771 | * to be pushed. | |
772 | */ | |
773 | if (io_ok) { | |
774 | io_ok = 0; | |
775 | lstrval = 1; | |
776 | break; | |
777 | } | |
778 | mb.mt_op = MTFSR; | |
779 | mb.mt_count = 1; | |
780 | if (ioctl(arfd, MTIOCTOP, &mb) < 0) | |
781 | break; | |
782 | lstrval = 1; | |
783 | break; | |
40bf83fe | 784 | #endif /* !__APPLE__ */ |
44a7a5ab A |
785 | case ISREG: |
786 | case ISCHR: | |
787 | case ISBLK: | |
788 | /* | |
789 | * try to step over the bad part of the device. | |
790 | */ | |
791 | io_ok = 0; | |
792 | if (((fsbz = arsb.st_blksize) <= 0) || (artyp != ISREG)) | |
793 | fsbz = BLKMULT; | |
794 | if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) | |
795 | break; | |
796 | mpos = fsbz - (cpos % (off_t)fsbz); | |
797 | if (lseek(arfd, mpos, SEEK_CUR) < 0) | |
798 | break; | |
799 | lstrval = 1; | |
800 | break; | |
801 | case ISPIPE: | |
802 | default: | |
803 | /* | |
804 | * cannot recover on these archive device types | |
805 | */ | |
806 | io_ok = 0; | |
807 | break; | |
808 | } | |
809 | if (lstrval <= 0) { | |
810 | paxwarn(1, "Unable to recover from an archive read failure."); | |
811 | return(-1); | |
812 | } | |
813 | paxwarn(0, "Attempting to recover from an archive read failure."); | |
814 | return(0); | |
815 | } | |
816 | ||
817 | /* | |
818 | * ar_fow() | |
864a4b6e | 819 | * Move the I/O position within the archive forward the specified number of |
44a7a5ab A |
820 | * bytes as supported by the device. If we cannot move the requested |
821 | * number of bytes, return the actual number of bytes moved in skipped. | |
822 | * Return: | |
823 | * 0 if moved the requested distance, -1 on complete failure, 1 on | |
824 | * partial move (the amount moved is in skipped) | |
825 | */ | |
826 | ||
44a7a5ab A |
827 | int |
828 | ar_fow(off_t sksz, off_t *skipped) | |
44a7a5ab A |
829 | { |
830 | off_t cpos; | |
831 | off_t mpos; | |
832 | ||
833 | *skipped = 0; | |
834 | if (sksz <= 0) | |
835 | return(0); | |
836 | ||
837 | /* | |
864a4b6e | 838 | * we cannot move forward at EOF or error |
44a7a5ab A |
839 | */ |
840 | if (lstrval <= 0) | |
841 | return(lstrval); | |
842 | ||
843 | /* | |
844 | * Safer to read forward on devices where it is hard to find the end of | |
845 | * the media without reading to it. With tapes we cannot be sure of the | |
846 | * number of physical blocks to skip (we do not know physical block | |
864a4b6e | 847 | * size at this point), so we must only read forward on tapes! |
44a7a5ab A |
848 | */ |
849 | if (artyp != ISREG) | |
850 | return(0); | |
851 | ||
852 | /* | |
853 | * figure out where we are in the archive | |
854 | */ | |
855 | if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) >= 0) { | |
864a4b6e A |
856 | /* |
857 | * we can be asked to move farther than there are bytes in this | |
44a7a5ab A |
858 | * volume, if so, just go to file end and let normal buf_fill() |
859 | * deal with the end of file (it will go to next volume by | |
860 | * itself) | |
864a4b6e | 861 | */ |
44a7a5ab A |
862 | if ((mpos = cpos + sksz) > arsb.st_size) { |
863 | *skipped = arsb.st_size - cpos; | |
864 | mpos = arsb.st_size; | |
865 | } else | |
866 | *skipped = sksz; | |
867 | if (lseek(arfd, mpos, SEEK_SET) >= 0) | |
868 | return(0); | |
869 | } | |
864a4b6e | 870 | syswarn(1, errno, "Forward positioning operation on archive failed"); |
44a7a5ab A |
871 | lstrval = -1; |
872 | return(-1); | |
873 | } | |
874 | ||
875 | /* | |
876 | * ar_rev() | |
877 | * move the i/o position within the archive backwards the specified byte | |
878 | * count as supported by the device. With tapes drives we RESET rdblksz to | |
879 | * the PHYSICAL blocksize. | |
880 | * NOTE: We should only be called to move backwards so we can rewrite the | |
881 | * last records (the trailer) of an archive (APPEND). | |
882 | * Return: | |
883 | * 0 if moved the requested distance, -1 on complete failure | |
884 | */ | |
885 | ||
44a7a5ab A |
886 | int |
887 | ar_rev(off_t sksz) | |
44a7a5ab A |
888 | { |
889 | off_t cpos; | |
40bf83fe | 890 | #ifndef __APPLE__ |
44a7a5ab | 891 | struct mtop mb; |
864a4b6e | 892 | int phyblk; |
40bf83fe | 893 | #endif /* __APPLE__ */ |
44a7a5ab A |
894 | |
895 | /* | |
896 | * make sure we do not have try to reverse on a flawed archive | |
897 | */ | |
898 | if (lstrval < 0) | |
899 | return(lstrval); | |
900 | ||
864a4b6e | 901 | switch (artyp) { |
44a7a5ab A |
902 | case ISPIPE: |
903 | if (sksz <= 0) | |
904 | break; | |
905 | /* | |
906 | * cannot go backwards on these critters | |
907 | */ | |
908 | paxwarn(1, "Reverse positioning on pipes is not supported."); | |
909 | lstrval = -1; | |
910 | return(-1); | |
911 | case ISREG: | |
912 | case ISBLK: | |
913 | case ISCHR: | |
914 | default: | |
915 | if (sksz <= 0) | |
916 | break; | |
917 | ||
918 | /* | |
919 | * For things other than files, backwards movement has a very | |
920 | * high probability of failure as we really do not know the | |
921 | * true attributes of the device we are talking to (the device | |
922 | * may not even have the ability to lseek() in any direction). | |
923 | * First we figure out where we are in the archive. | |
924 | */ | |
925 | if ((cpos = lseek(arfd, (off_t)0L, SEEK_CUR)) < 0) { | |
926 | syswarn(1, errno, | |
927 | "Unable to obtain current archive byte offset"); | |
928 | lstrval = -1; | |
929 | return(-1); | |
930 | } | |
931 | ||
932 | /* | |
933 | * we may try to go backwards past the start when the archive | |
864a4b6e A |
934 | * is only a single record. If this happens and we are on a |
935 | * multi-volume archive, we need to go to the end of the | |
44a7a5ab A |
936 | * previous volume and continue our movement backwards from |
937 | * there. | |
938 | */ | |
939 | if ((cpos -= sksz) < (off_t)0L) { | |
940 | if (arvol > 1) { | |
941 | /* | |
942 | * this should never happen | |
943 | */ | |
944 | paxwarn(1,"Reverse position on previous volume."); | |
945 | lstrval = -1; | |
946 | return(-1); | |
947 | } | |
948 | cpos = (off_t)0L; | |
949 | } | |
950 | if (lseek(arfd, cpos, SEEK_SET) < 0) { | |
951 | syswarn(1, errno, "Unable to seek archive backwards"); | |
952 | lstrval = -1; | |
953 | return(-1); | |
954 | } | |
955 | break; | |
40bf83fe | 956 | #ifndef __APPLE__ |
44a7a5ab A |
957 | case ISTAPE: |
958 | /* | |
864a4b6e | 959 | * Calculate and move the proper number of PHYSICAL tape |
44a7a5ab A |
960 | * blocks. If the sksz is not an even multiple of the physical |
961 | * tape size, we cannot do the move (this should never happen). | |
864a4b6e | 962 | * (We also cannot handle trailers spread over two vols.) |
44a7a5ab | 963 | * get_phys() also makes sure we are in front of the filemark. |
864a4b6e | 964 | */ |
44a7a5ab A |
965 | if ((phyblk = get_phys()) <= 0) { |
966 | lstrval = -1; | |
967 | return(-1); | |
968 | } | |
969 | ||
970 | /* | |
971 | * make sure future tape reads only go by physical tape block | |
972 | * size (set rdblksz to the real size). | |
973 | */ | |
974 | rdblksz = phyblk; | |
975 | ||
976 | /* | |
977 | * if no movement is required, just return (we must be after | |
978 | * get_phys() so the physical blocksize is properly set) | |
979 | */ | |
980 | if (sksz <= 0) | |
981 | break; | |
982 | ||
983 | /* | |
984 | * ok we have to move. Make sure the tape drive can do it. | |
985 | */ | |
986 | if (sksz % phyblk) { | |
987 | paxwarn(1, | |
988 | "Tape drive unable to backspace requested amount"); | |
989 | lstrval = -1; | |
990 | return(-1); | |
991 | } | |
992 | ||
993 | /* | |
994 | * move backwards the requested number of bytes | |
995 | */ | |
996 | mb.mt_op = MTBSR; | |
997 | mb.mt_count = sksz/phyblk; | |
998 | if (ioctl(arfd, MTIOCTOP, &mb) < 0) { | |
999 | syswarn(1,errno, "Unable to backspace tape %d blocks.", | |
1000 | mb.mt_count); | |
1001 | lstrval = -1; | |
1002 | return(-1); | |
1003 | } | |
1004 | break; | |
40bf83fe | 1005 | #endif /* !__APPLE__ */ |
44a7a5ab A |
1006 | } |
1007 | lstrval = 1; | |
1008 | return(0); | |
1009 | } | |
40bf83fe | 1010 | #ifndef __APPLE__ |
44a7a5ab A |
1011 | /* |
1012 | * get_phys() | |
1013 | * Determine the physical block size on a tape drive. We need the physical | |
864a4b6e | 1014 | * block size so we know how many bytes we skip over when we move with |
44a7a5ab A |
1015 | * mtio commands. We also make sure we are BEFORE THE TAPE FILEMARK when |
1016 | * return. | |
1017 | * This is one really SLOW routine... | |
1018 | * Return: | |
1019 | * physical block size if ok (ok > 0), -1 otherwise | |
1020 | */ | |
1021 | ||
44a7a5ab A |
1022 | static int |
1023 | get_phys(void) | |
44a7a5ab | 1024 | { |
864a4b6e A |
1025 | int padsz = 0; |
1026 | int res; | |
1027 | int phyblk; | |
44a7a5ab A |
1028 | struct mtop mb; |
1029 | char scbuf[MAXBLK]; | |
1030 | ||
1031 | /* | |
1032 | * move to the file mark, and then back up one record and read it. | |
1033 | * this should tell us the physical record size the tape is using. | |
1034 | */ | |
1035 | if (lstrval == 1) { | |
1036 | /* | |
1037 | * we know we are at file mark when we get back a 0 from | |
1038 | * read() | |
1039 | */ | |
1040 | while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0) | |
1041 | padsz += res; | |
1042 | if (res < 0) { | |
1043 | syswarn(1, errno, "Unable to locate tape filemark."); | |
1044 | return(-1); | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | /* | |
1049 | * move backwards over the file mark so we are at the end of the | |
1050 | * last record. | |
1051 | */ | |
1052 | mb.mt_op = MTBSF; | |
1053 | mb.mt_count = 1; | |
1054 | if (ioctl(arfd, MTIOCTOP, &mb) < 0) { | |
1055 | syswarn(1, errno, "Unable to backspace over tape filemark."); | |
1056 | return(-1); | |
1057 | } | |
1058 | ||
1059 | /* | |
1060 | * move backwards so we are in front of the last record and read it to | |
1061 | * get physical tape blocksize. | |
1062 | */ | |
1063 | mb.mt_op = MTBSR; | |
1064 | mb.mt_count = 1; | |
1065 | if (ioctl(arfd, MTIOCTOP, &mb) < 0) { | |
1066 | syswarn(1, errno, "Unable to backspace over last tape block."); | |
1067 | return(-1); | |
1068 | } | |
1069 | if ((phyblk = read(arfd, scbuf, sizeof(scbuf))) <= 0) { | |
1070 | syswarn(1, errno, "Cannot determine archive tape blocksize."); | |
1071 | return(-1); | |
1072 | } | |
1073 | ||
1074 | /* | |
864a4b6e | 1075 | * read forward to the file mark, then back up in front of the filemark |
44a7a5ab A |
1076 | * (this is a bit paranoid, but should be safe to do). |
1077 | */ | |
1078 | while ((res = read(arfd, scbuf, sizeof(scbuf))) > 0) | |
1079 | ; | |
1080 | if (res < 0) { | |
1081 | syswarn(1, errno, "Unable to locate tape filemark."); | |
1082 | return(-1); | |
1083 | } | |
1084 | mb.mt_op = MTBSF; | |
1085 | mb.mt_count = 1; | |
1086 | if (ioctl(arfd, MTIOCTOP, &mb) < 0) { | |
1087 | syswarn(1, errno, "Unable to backspace over tape filemark."); | |
1088 | return(-1); | |
1089 | } | |
1090 | ||
1091 | /* | |
1092 | * set lstrval so we know that the filemark has not been seen | |
1093 | */ | |
1094 | lstrval = 1; | |
1095 | ||
1096 | /* | |
1097 | * return if there was no padding | |
1098 | */ | |
1099 | if (padsz == 0) | |
1100 | return(phyblk); | |
1101 | ||
1102 | /* | |
1103 | * make sure we can move backwards over the padding. (this should | |
1104 | * never fail). | |
1105 | */ | |
1106 | if (padsz % phyblk) { | |
1107 | paxwarn(1, "Tape drive unable to backspace requested amount"); | |
1108 | return(-1); | |
1109 | } | |
1110 | ||
1111 | /* | |
1112 | * move backwards over the padding so the head is where it was when | |
1113 | * we were first called (if required). | |
1114 | */ | |
1115 | mb.mt_op = MTBSR; | |
1116 | mb.mt_count = padsz/phyblk; | |
1117 | if (ioctl(arfd, MTIOCTOP, &mb) < 0) { | |
1118 | syswarn(1,errno,"Unable to backspace tape over %d pad blocks", | |
1119 | mb.mt_count); | |
1120 | return(-1); | |
1121 | } | |
1122 | return(phyblk); | |
1123 | } | |
40bf83fe | 1124 | #endif /* !__APPLE__ */ |
44a7a5ab A |
1125 | /* |
1126 | * ar_next() | |
1127 | * prompts the user for the next volume in this archive. For some devices | |
1128 | * we may allow the media to be changed. Otherwise a new archive is | |
1129 | * prompted for. By pax spec, if there is no controlling tty or an eof is | |
1130 | * read on tty input, we must quit pax. | |
1131 | * Return: | |
1132 | * 0 when ready to continue, -1 when all done | |
1133 | */ | |
1134 | ||
44a7a5ab A |
1135 | int |
1136 | ar_next(void) | |
44a7a5ab A |
1137 | { |
1138 | char buf[PAXPATHLEN+2]; | |
1139 | static int freeit = 0; | |
1140 | sigset_t o_mask; | |
1141 | ||
1142 | /* | |
1143 | * WE MUST CLOSE THE DEVICE. A lot of devices must see last close, (so | |
1144 | * things like writing EOF etc will be done) (Watch out ar_close() can | |
1145 | * also be called via a signal handler, so we must prevent a race. | |
1146 | */ | |
1147 | if (sigprocmask(SIG_BLOCK, &s_mask, &o_mask) < 0) | |
1148 | syswarn(0, errno, "Unable to set signal mask"); | |
1149 | ar_close(); | |
1150 | if (sigprocmask(SIG_SETMASK, &o_mask, NULL) < 0) | |
1151 | syswarn(0, errno, "Unable to restore signal mask"); | |
1152 | ||
864a4b6e | 1153 | if (frmt == NULL || done || !wr_trail || force_one_volume || strcmp(NM_TAR, argv0) == 0 || |
40bf83fe | 1154 | strcmp(NM_PAX, argv0) == 0) |
44a7a5ab A |
1155 | return(-1); |
1156 | ||
1157 | tty_prnt("\nATTENTION! %s archive volume change required.\n", argv0); | |
1158 | ||
1159 | /* | |
1160 | * if i/o is on stdin or stdout, we cannot reopen it (we do not know | |
1161 | * the name), the user will be forced to type it in. | |
1162 | */ | |
1163 | if (strcmp(arcname, STDO) && strcmp(arcname, STDN) && (artyp != ISREG) | |
1164 | && (artyp != ISPIPE)) { | |
1165 | if (artyp == ISTAPE) { | |
1166 | tty_prnt("%s ready for archive tape volume: %d\n", | |
1167 | arcname, arvol); | |
1168 | tty_prnt("Load the NEXT TAPE on the tape drive"); | |
1169 | } else { | |
1170 | tty_prnt("%s ready for archive volume: %d\n", | |
1171 | arcname, arvol); | |
1172 | tty_prnt("Load the NEXT STORAGE MEDIA (if required)"); | |
1173 | } | |
1174 | ||
1175 | if ((act == ARCHIVE) || (act == APPND)) | |
1176 | tty_prnt(" and make sure it is WRITE ENABLED.\n"); | |
1177 | else | |
1178 | tty_prnt("\n"); | |
1179 | ||
864a4b6e | 1180 | for (;;) { |
44a7a5ab A |
1181 | tty_prnt("Type \"y\" to continue, \".\" to quit %s,", |
1182 | argv0); | |
1183 | tty_prnt(" or \"s\" to switch to new device.\nIf you"); | |
1184 | tty_prnt(" cannot change storage media, type \"s\"\n"); | |
1185 | tty_prnt("Is the device ready and online? > "); | |
1186 | ||
1187 | if ((tty_read(buf,sizeof(buf))<0) || !strcmp(buf,".")){ | |
1188 | done = 1; | |
1189 | lstrval = -1; | |
1190 | tty_prnt("Quitting %s!\n", argv0); | |
1191 | vfpart = 0; | |
1192 | return(-1); | |
1193 | } | |
1194 | ||
1195 | if ((buf[0] == '\0') || (buf[1] != '\0')) { | |
1196 | tty_prnt("%s unknown command, try again\n",buf); | |
1197 | continue; | |
1198 | } | |
1199 | ||
1200 | switch (buf[0]) { | |
1201 | case 'y': | |
1202 | case 'Y': | |
1203 | /* | |
1204 | * we are to continue with the same device | |
1205 | */ | |
1206 | if (ar_open(arcname) >= 0) | |
1207 | return(0); | |
1208 | tty_prnt("Cannot re-open %s, try again\n", | |
1209 | arcname); | |
1210 | continue; | |
1211 | case 's': | |
1212 | case 'S': | |
1213 | /* | |
1214 | * user wants to open a different device | |
1215 | */ | |
1216 | tty_prnt("Switching to a different archive\n"); | |
1217 | break; | |
1218 | default: | |
1219 | tty_prnt("%s unknown command, try again\n",buf); | |
1220 | continue; | |
1221 | } | |
1222 | break; | |
1223 | } | |
1224 | } else | |
1225 | tty_prnt("Ready for archive volume: %d\n", arvol); | |
1226 | ||
1227 | /* | |
1228 | * have to go to a different archive | |
1229 | */ | |
1230 | for (;;) { | |
1231 | tty_prnt("Input archive name or \".\" to quit %s.\n", argv0); | |
1232 | tty_prnt("Archive name > "); | |
1233 | ||
1234 | if ((tty_read(buf, sizeof(buf)) < 0) || !strcmp(buf, ".")) { | |
1235 | done = 1; | |
1236 | lstrval = -1; | |
1237 | tty_prnt("Quitting %s!\n", argv0); | |
1238 | vfpart = 0; | |
1239 | return(-1); | |
1240 | } | |
1241 | if (buf[0] == '\0') { | |
1242 | tty_prnt("Empty file name, try again\n"); | |
1243 | continue; | |
1244 | } | |
1245 | if (!strcmp(buf, "..")) { | |
1246 | tty_prnt("Illegal file name: .. try again\n"); | |
1247 | continue; | |
1248 | } | |
1249 | if (strlen(buf) > PAXPATHLEN) { | |
1250 | tty_prnt("File name too long, try again\n"); | |
1251 | continue; | |
1252 | } | |
1253 | ||
1254 | /* | |
1255 | * try to open new archive | |
1256 | */ | |
1257 | if (ar_open(buf) >= 0) { | |
1258 | if (freeit) { | |
864a4b6e | 1259 | (void)free((char *)arcname); |
44a7a5ab A |
1260 | freeit = 0; |
1261 | } | |
1262 | if ((arcname = strdup(buf)) == NULL) { | |
1263 | done = 1; | |
1264 | lstrval = -1; | |
1265 | paxwarn(0, "Cannot save archive name."); | |
1266 | return(-1); | |
1267 | } | |
1268 | freeit = 1; | |
1269 | break; | |
1270 | } | |
1271 | tty_prnt("Cannot open %s, try again\n", buf); | |
1272 | continue; | |
1273 | } | |
1274 | return(0); | |
1275 | } | |
1276 | ||
1277 | /* | |
1278 | * ar_start_gzip() | |
1279 | * starts the gzip compression/decompression process as a child, using magic | |
1280 | * to keep the fd the same in the calling function (parent). | |
1281 | */ | |
1282 | void | |
864a4b6e | 1283 | ar_start_gzip(int fd, const char *gzip_program, int wr) |
44a7a5ab | 1284 | { |
44a7a5ab | 1285 | int fds[2]; |
864a4b6e | 1286 | const char *gzip_flags = NULL; |
44a7a5ab A |
1287 | |
1288 | if (pipe(fds) < 0) | |
1289 | err(1, "could not pipe"); | |
864a4b6e A |
1290 | zpid = fork(); |
1291 | if (zpid < 0) | |
44a7a5ab A |
1292 | err(1, "could not fork"); |
1293 | ||
1294 | /* parent */ | |
864a4b6e A |
1295 | if (zpid) { |
1296 | if (wr) | |
44a7a5ab | 1297 | dup2(fds[1], fd); |
864a4b6e | 1298 | else |
44a7a5ab | 1299 | dup2(fds[0], fd); |
44a7a5ab A |
1300 | close(fds[0]); |
1301 | close(fds[1]); | |
1302 | } else { | |
864a4b6e | 1303 | if (wr) { |
44a7a5ab A |
1304 | dup2(fds[0], STDIN_FILENO); |
1305 | dup2(fd, STDOUT_FILENO); | |
1306 | gzip_flags = "-c"; | |
864a4b6e | 1307 | } else { |
44a7a5ab A |
1308 | dup2(fds[1], STDOUT_FILENO); |
1309 | dup2(fd, STDIN_FILENO); | |
1310 | gzip_flags = "-dc"; | |
44a7a5ab A |
1311 | } |
1312 | close(fds[0]); | |
1313 | close(fds[1]); | |
864a4b6e | 1314 | if (execlp(gzip_program, gzip_program, gzip_flags, (char *)NULL) < 0) |
40bf83fe | 1315 | err(1, "could not exec %s", gzip_program); |
44a7a5ab A |
1316 | /* NOTREACHED */ |
1317 | } | |
1318 | } |