ar_subs.c revision 1.4 1 1.1 jtc /*-
2 1.1 jtc * Copyright (c) 1992 Keith Muller.
3 1.1 jtc * Copyright (c) 1992, 1993
4 1.1 jtc * The Regents of the University of California. All rights reserved.
5 1.1 jtc *
6 1.1 jtc * This code is derived from software contributed to Berkeley by
7 1.1 jtc * Keith Muller of the University of California, San Diego.
8 1.1 jtc *
9 1.1 jtc * Redistribution and use in source and binary forms, with or without
10 1.1 jtc * modification, are permitted provided that the following conditions
11 1.1 jtc * are met:
12 1.1 jtc * 1. Redistributions of source code must retain the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer.
14 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jtc * notice, this list of conditions and the following disclaimer in the
16 1.1 jtc * documentation and/or other materials provided with the distribution.
17 1.1 jtc * 3. All advertising materials mentioning features or use of this software
18 1.1 jtc * must display the following acknowledgement:
19 1.1 jtc * This product includes software developed by the University of
20 1.1 jtc * California, Berkeley and its contributors.
21 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
22 1.1 jtc * may be used to endorse or promote products derived from this software
23 1.1 jtc * without specific prior written permission.
24 1.1 jtc *
25 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 jtc * SUCH DAMAGE.
36 1.1 jtc */
37 1.1 jtc
38 1.1 jtc #ifndef lint
39 1.2 jtc /*static char sccsid[] = "from: @(#)ar_subs.c 8.2 (Berkeley) 4/18/94";*/
40 1.4 mycroft static char *rcsid = "$Id: ar_subs.c,v 1.4 1994/09/23 11:35:05 mycroft Exp $";
41 1.1 jtc #endif /* not lint */
42 1.1 jtc
43 1.1 jtc #include <sys/types.h>
44 1.1 jtc #include <sys/time.h>
45 1.1 jtc #include <sys/stat.h>
46 1.1 jtc #include <sys/param.h>
47 1.1 jtc #include <signal.h>
48 1.1 jtc #include <string.h>
49 1.1 jtc #include <stdio.h>
50 1.1 jtc #include <ctype.h>
51 1.1 jtc #include <fcntl.h>
52 1.1 jtc #include <errno.h>
53 1.1 jtc #include <unistd.h>
54 1.1 jtc #include <stdlib.h>
55 1.1 jtc #include "pax.h"
56 1.1 jtc #include "extern.h"
57 1.1 jtc
58 1.1 jtc static void wr_archive __P((register ARCHD *, int is_app));
59 1.1 jtc static int get_arc __P((void));
60 1.1 jtc static int next_head __P((register ARCHD *));
61 1.1 jtc extern sigset_t s_mask;
62 1.1 jtc
63 1.1 jtc /*
64 1.1 jtc * Routines which control the overall operation modes of pax as specified by
65 1.1 jtc * the user: list, append, read ...
66 1.1 jtc */
67 1.1 jtc
68 1.1 jtc static char hdbuf[BLKMULT]; /* space for archive header on read */
69 1.1 jtc u_long flcnt; /* number of files processed */
70 1.1 jtc
71 1.1 jtc /*
72 1.1 jtc * list()
73 1.1 jtc * list the contents of an archive which match user supplied pattern(s)
74 1.1 jtc * (no pattern matches all).
75 1.1 jtc */
76 1.1 jtc
77 1.1 jtc #if __STDC__
78 1.1 jtc void
79 1.1 jtc list(void)
80 1.1 jtc #else
81 1.1 jtc void
82 1.1 jtc list()
83 1.1 jtc #endif
84 1.1 jtc {
85 1.1 jtc register ARCHD *arcn;
86 1.1 jtc register int res;
87 1.1 jtc ARCHD archd;
88 1.1 jtc time_t now;
89 1.1 jtc
90 1.1 jtc arcn = &archd;
91 1.1 jtc /*
92 1.1 jtc * figure out archive type; pass any format specific options to the
93 1.1 jtc * archive option processing routine; call the format init routine. We
94 1.1 jtc * also save current time for ls_list() so we do not make a system
95 1.1 jtc * call for each file we need to print. If verbose (vflag) start up
96 1.1 jtc * the name and group caches.
97 1.1 jtc */
98 1.1 jtc if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
99 1.1 jtc ((*frmt->st_rd)() < 0))
100 1.1 jtc return;
101 1.1 jtc
102 1.1 jtc if (vflag && ((uidtb_start() < 0) || (gidtb_start() < 0)))
103 1.1 jtc return;
104 1.1 jtc
105 1.1 jtc now = time((time_t *)NULL);
106 1.1 jtc
107 1.1 jtc /*
108 1.1 jtc * step through the archive until the format says it is done
109 1.1 jtc */
110 1.1 jtc while (next_head(arcn) == 0) {
111 1.1 jtc /*
112 1.1 jtc * check for pattern, and user specified options match.
113 1.1 jtc * When all patterns are matched we are done.
114 1.1 jtc */
115 1.1 jtc if ((res = pat_match(arcn)) < 0)
116 1.1 jtc break;
117 1.1 jtc
118 1.1 jtc if ((res == 0) && (sel_chk(arcn) == 0)) {
119 1.1 jtc /*
120 1.1 jtc * pattern resulted in a selected file
121 1.1 jtc */
122 1.1 jtc if (pat_sel(arcn) < 0)
123 1.1 jtc break;
124 1.1 jtc
125 1.1 jtc /*
126 1.1 jtc * modify the name as requested by the user if name
127 1.1 jtc * survives modification, do a listing of the file
128 1.1 jtc */
129 1.1 jtc if ((res = mod_name(arcn)) < 0)
130 1.1 jtc break;
131 1.1 jtc if (res == 0)
132 1.1 jtc ls_list(arcn, now);
133 1.1 jtc }
134 1.1 jtc
135 1.1 jtc /*
136 1.1 jtc * skip to next archive format header using values calculated
137 1.1 jtc * by the format header read routine
138 1.1 jtc */
139 1.1 jtc if (rd_skip(arcn->skip + arcn->pad) == 1)
140 1.1 jtc break;
141 1.1 jtc }
142 1.1 jtc
143 1.1 jtc /*
144 1.1 jtc * all done, let format have a chance to cleanup, and make sure that
145 1.1 jtc * the patterns supplied by the user were all matched
146 1.1 jtc */
147 1.1 jtc (void)(*frmt->end_rd)();
148 1.1 jtc (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
149 1.1 jtc ar_close();
150 1.1 jtc pat_chk();
151 1.1 jtc }
152 1.1 jtc
153 1.1 jtc /*
154 1.1 jtc * extract()
155 1.1 jtc * extract the member(s) of an archive as specified by user supplied
156 1.1 jtc * pattern(s) (no patterns extracts all members)
157 1.1 jtc */
158 1.1 jtc
159 1.1 jtc #if __STDC__
160 1.1 jtc void
161 1.1 jtc extract(void)
162 1.1 jtc #else
163 1.1 jtc void
164 1.1 jtc extract()
165 1.1 jtc #endif
166 1.1 jtc {
167 1.1 jtc register ARCHD *arcn;
168 1.1 jtc register int res;
169 1.1 jtc off_t cnt;
170 1.1 jtc ARCHD archd;
171 1.1 jtc struct stat sb;
172 1.1 jtc int fd;
173 1.1 jtc
174 1.1 jtc arcn = &archd;
175 1.1 jtc /*
176 1.1 jtc * figure out archive type; pass any format specific options to the
177 1.1 jtc * archive option processing routine; call the format init routine;
178 1.1 jtc * start up the directory modification time and access mode database
179 1.1 jtc */
180 1.1 jtc if ((get_arc() < 0) || ((*frmt->options)() < 0) ||
181 1.1 jtc ((*frmt->st_rd)() < 0) || (dir_start() < 0))
182 1.1 jtc return;
183 1.1 jtc
184 1.1 jtc /*
185 1.1 jtc * When we are doing interactive rename, we store the mapping of names
186 1.1 jtc * so we can fix up hard links files later in the archive.
187 1.1 jtc */
188 1.1 jtc if (iflag && (name_start() < 0))
189 1.1 jtc return;
190 1.1 jtc
191 1.1 jtc /*
192 1.1 jtc * step through each entry on the archive until the format read routine
193 1.1 jtc * says it is done
194 1.1 jtc */
195 1.1 jtc while (next_head(arcn) == 0) {
196 1.1 jtc
197 1.1 jtc /*
198 1.1 jtc * check for pattern, and user specified options match. When
199 1.1 jtc * all the patterns are matched we are done
200 1.1 jtc */
201 1.1 jtc if ((res = pat_match(arcn)) < 0)
202 1.1 jtc break;
203 1.1 jtc
204 1.1 jtc if ((res > 0) || (sel_chk(arcn) != 0)) {
205 1.1 jtc /*
206 1.1 jtc * file is not selected. skip past any file data and
207 1.1 jtc * padding and go back for the next archive member
208 1.1 jtc */
209 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
210 1.1 jtc continue;
211 1.1 jtc }
212 1.1 jtc
213 1.1 jtc /*
214 1.1 jtc * with -u or -D only extract when the archive member is newer
215 1.1 jtc * than the file with the same name in the file system (nos
216 1.1 jtc * test of being the same type is required).
217 1.1 jtc * NOTE: this test is done BEFORE name modifications as
218 1.1 jtc * specified by pax. this operation can be confusing to the
219 1.1 jtc * user who might expect the test to be done on an existing
220 1.1 jtc * file AFTER the name mod. In honesty the pax spec is probably
221 1.1 jtc * flawed in this respect.
222 1.1 jtc */
223 1.1 jtc if ((uflag || Dflag) && ((lstat(arcn->name, &sb) == 0))) {
224 1.1 jtc if (uflag && Dflag) {
225 1.1 jtc if ((arcn->sb.st_mtime <= sb.st_mtime) &&
226 1.1 jtc (arcn->sb.st_ctime <= sb.st_ctime)) {
227 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
228 1.1 jtc continue;
229 1.1 jtc }
230 1.1 jtc } else if (Dflag) {
231 1.1 jtc if (arcn->sb.st_ctime <= sb.st_ctime) {
232 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
233 1.1 jtc continue;
234 1.1 jtc }
235 1.1 jtc } else if (arcn->sb.st_mtime <= sb.st_mtime) {
236 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
237 1.1 jtc continue;
238 1.1 jtc }
239 1.1 jtc }
240 1.1 jtc
241 1.1 jtc /*
242 1.1 jtc * this archive member is now been selected. modify the name.
243 1.1 jtc */
244 1.1 jtc if ((pat_sel(arcn) < 0) || ((res = mod_name(arcn)) < 0))
245 1.1 jtc break;
246 1.1 jtc if (res > 0) {
247 1.1 jtc /*
248 1.1 jtc * a bad name mod, skip and purge name from link table
249 1.1 jtc */
250 1.1 jtc purg_lnk(arcn);
251 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
252 1.1 jtc continue;
253 1.1 jtc }
254 1.1 jtc
255 1.1 jtc /*
256 1.1 jtc * Non standard -Y and -Z flag. When the exisiting file is
257 1.1 jtc * same age or newer skip
258 1.1 jtc */
259 1.1 jtc if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
260 1.1 jtc if (Yflag && Zflag) {
261 1.1 jtc if ((arcn->sb.st_mtime <= sb.st_mtime) &&
262 1.1 jtc (arcn->sb.st_ctime <= sb.st_ctime)) {
263 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
264 1.1 jtc continue;
265 1.1 jtc }
266 1.1 jtc } else if (Yflag) {
267 1.1 jtc if (arcn->sb.st_ctime <= sb.st_ctime) {
268 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
269 1.1 jtc continue;
270 1.1 jtc }
271 1.1 jtc } else if (arcn->sb.st_mtime <= sb.st_mtime) {
272 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
273 1.1 jtc continue;
274 1.1 jtc }
275 1.1 jtc }
276 1.1 jtc
277 1.1 jtc if (vflag) {
278 1.1 jtc (void)fputs(arcn->name, stderr);
279 1.1 jtc vfpart = 1;
280 1.1 jtc }
281 1.1 jtc
282 1.1 jtc /*
283 1.1 jtc * all ok, extract this member based on type
284 1.1 jtc */
285 1.1 jtc if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
286 1.1 jtc /*
287 1.1 jtc * process archive members that are not regular files.
288 1.1 jtc * throw out padding and any data that might follow the
289 1.1 jtc * header (as determined by the format).
290 1.1 jtc */
291 1.1 jtc if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
292 1.1 jtc res = lnk_creat(arcn);
293 1.1 jtc else
294 1.1 jtc res = node_creat(arcn);
295 1.1 jtc
296 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
297 1.1 jtc if (res < 0)
298 1.1 jtc purg_lnk(arcn);
299 1.1 jtc
300 1.1 jtc if (vflag && vfpart) {
301 1.1 jtc (void)putc('\n', stderr);
302 1.1 jtc vfpart = 0;
303 1.1 jtc }
304 1.1 jtc continue;
305 1.1 jtc }
306 1.1 jtc /*
307 1.1 jtc * we have a file with data here. If we can not create it, skip
308 1.1 jtc * over the data and purge the name from hard link table
309 1.1 jtc */
310 1.1 jtc if ((fd = file_creat(arcn)) < 0) {
311 1.1 jtc (void)rd_skip(arcn->skip + arcn->pad);
312 1.1 jtc purg_lnk(arcn);
313 1.1 jtc continue;
314 1.1 jtc }
315 1.1 jtc /*
316 1.1 jtc * extract the file from the archive and skip over padding and
317 1.1 jtc * any unprocessed data
318 1.1 jtc */
319 1.1 jtc res = (*frmt->rd_data)(arcn, fd, &cnt);
320 1.1 jtc file_close(arcn, fd);
321 1.1 jtc if (vflag && vfpart) {
322 1.1 jtc (void)putc('\n', stderr);
323 1.1 jtc vfpart = 0;
324 1.1 jtc }
325 1.1 jtc if (!res)
326 1.1 jtc (void)rd_skip(cnt + arcn->pad);
327 1.1 jtc }
328 1.1 jtc
329 1.1 jtc /*
330 1.1 jtc * all done, restore directory modes and times as required; make sure
331 1.1 jtc * all patterns supplied by the user were matched; block off signals
332 1.1 jtc * to avoid chance for multiple entry into the cleanup code.
333 1.1 jtc */
334 1.1 jtc (void)(*frmt->end_rd)();
335 1.1 jtc (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
336 1.1 jtc ar_close();
337 1.1 jtc proc_dir();
338 1.1 jtc pat_chk();
339 1.1 jtc }
340 1.1 jtc
341 1.1 jtc /*
342 1.1 jtc * wr_archive()
343 1.1 jtc * Write an archive. used in both creating a new archive and appends on
344 1.1 jtc * previously written archive.
345 1.1 jtc */
346 1.1 jtc
347 1.1 jtc #if __STDC__
348 1.1 jtc static void
349 1.1 jtc wr_archive(register ARCHD *arcn, int is_app)
350 1.1 jtc #else
351 1.1 jtc static void
352 1.1 jtc wr_archive(arcn, is_app)
353 1.1 jtc register ARCHD *arcn;
354 1.1 jtc int is_app;
355 1.1 jtc #endif
356 1.1 jtc {
357 1.1 jtc register int res;
358 1.1 jtc register int hlk;
359 1.1 jtc register int wr_one;
360 1.1 jtc off_t cnt;
361 1.1 jtc int (*wrf)();
362 1.1 jtc int fd = -1;
363 1.1 jtc
364 1.1 jtc /*
365 1.1 jtc * if this format supports hard link storage, start up the database
366 1.1 jtc * that detects them.
367 1.1 jtc */
368 1.1 jtc if (((hlk = frmt->hlk) == 1) && (lnk_start() < 0))
369 1.1 jtc return;
370 1.1 jtc
371 1.1 jtc /*
372 1.1 jtc * start up the file traversal code and format specific write
373 1.1 jtc */
374 1.1 jtc if ((ftree_start() < 0) || ((*frmt->st_wr)() < 0))
375 1.1 jtc return;
376 1.1 jtc wrf = frmt->wr;
377 1.1 jtc
378 1.1 jtc /*
379 1.1 jtc * When we are doing interactive rename, we store the mapping of names
380 1.1 jtc * so we can fix up hard links files later in the archive.
381 1.1 jtc */
382 1.1 jtc if (iflag && (name_start() < 0))
383 1.1 jtc return;
384 1.1 jtc
385 1.1 jtc /*
386 1.1 jtc * if this not append, and there are no files, we do no write a trailer
387 1.1 jtc */
388 1.1 jtc wr_one = is_app;
389 1.1 jtc
390 1.1 jtc /*
391 1.1 jtc * while there are files to archive, process them one at at time
392 1.1 jtc */
393 1.1 jtc while (next_file(arcn) == 0) {
394 1.1 jtc /*
395 1.1 jtc * check if this file meets user specified options match.
396 1.1 jtc */
397 1.1 jtc if (sel_chk(arcn) != 0)
398 1.1 jtc continue;
399 1.1 jtc fd = -1;
400 1.1 jtc if (uflag) {
401 1.1 jtc /*
402 1.1 jtc * only archive if this file is newer than a file with
403 1.1 jtc * the same name that is already stored on the archive
404 1.1 jtc */
405 1.1 jtc if ((res = chk_ftime(arcn)) < 0)
406 1.1 jtc break;
407 1.1 jtc if (res > 0)
408 1.1 jtc continue;
409 1.1 jtc }
410 1.1 jtc
411 1.1 jtc /*
412 1.1 jtc * this file is considered selected now. see if this is a hard
413 1.1 jtc * link to a file already stored
414 1.1 jtc */
415 1.1 jtc ftree_sel(arcn);
416 1.1 jtc if (hlk && (chk_lnk(arcn) < 0))
417 1.1 jtc break;
418 1.1 jtc
419 1.1 jtc if ((arcn->type == PAX_REG) || (arcn->type == PAX_HRG) ||
420 1.1 jtc (arcn->type == PAX_CTG)) {
421 1.1 jtc /*
422 1.1 jtc * we will have to read this file. by opening it now we
423 1.1 jtc * can avoid writing a header to the archive for a file
424 1.1 jtc * we were later unable to read (we also purge it from
425 1.1 jtc * the link table).
426 1.1 jtc */
427 1.1 jtc if ((fd = open(arcn->org_name, O_RDONLY, 0)) < 0) {
428 1.1 jtc syswarn(1,errno, "Unable to open %s to read",
429 1.1 jtc arcn->org_name);
430 1.1 jtc purg_lnk(arcn);
431 1.1 jtc continue;
432 1.1 jtc }
433 1.1 jtc }
434 1.1 jtc
435 1.1 jtc /*
436 1.1 jtc * Now modify the name as requested by the user
437 1.1 jtc */
438 1.1 jtc if ((res = mod_name(arcn)) < 0) {
439 1.1 jtc /*
440 1.1 jtc * name modification says to skip this file, close the
441 1.1 jtc * file and purge link table entry
442 1.1 jtc */
443 1.1 jtc rdfile_close(arcn, &fd);
444 1.1 jtc purg_lnk(arcn);
445 1.1 jtc break;
446 1.1 jtc }
447 1.1 jtc
448 1.1 jtc if ((res > 0) || (docrc && (set_crc(arcn, fd) < 0))) {
449 1.1 jtc /*
450 1.1 jtc * unable to obtain the crc we need, close the file,
451 1.1 jtc * purge link table entry
452 1.1 jtc */
453 1.1 jtc rdfile_close(arcn, &fd);
454 1.1 jtc purg_lnk(arcn);
455 1.1 jtc continue;
456 1.1 jtc }
457 1.1 jtc
458 1.1 jtc if (vflag) {
459 1.1 jtc (void)fputs(arcn->name, stderr);
460 1.1 jtc vfpart = 1;
461 1.1 jtc }
462 1.1 jtc ++flcnt;
463 1.1 jtc
464 1.1 jtc /*
465 1.1 jtc * looks safe to store the file, have the format specific
466 1.1 jtc * routine write routine store the file header on the archive
467 1.1 jtc */
468 1.1 jtc if ((res = (*wrf)(arcn)) < 0) {
469 1.1 jtc rdfile_close(arcn, &fd);
470 1.1 jtc break;
471 1.1 jtc }
472 1.1 jtc wr_one = 1;
473 1.1 jtc if (res > 0) {
474 1.1 jtc /*
475 1.1 jtc * format write says no file data needs to be stored
476 1.1 jtc * so we are done messing with this file
477 1.1 jtc */
478 1.1 jtc if (vflag && vfpart) {
479 1.1 jtc (void)putc('\n', stderr);
480 1.1 jtc vfpart = 0;
481 1.1 jtc }
482 1.1 jtc rdfile_close(arcn, &fd);
483 1.1 jtc continue;
484 1.1 jtc }
485 1.1 jtc
486 1.1 jtc /*
487 1.1 jtc * Add file data to the archive, quit on write error. if we
488 1.1 jtc * cannot write the entire file contents to the archive we
489 1.1 jtc * must pad the archive to replace the missing file data
490 1.1 jtc * (otherwise during an extract the file header for the file
491 1.1 jtc * which FOLLOWS this one will not be where we expect it to
492 1.1 jtc * be).
493 1.1 jtc */
494 1.1 jtc res = (*frmt->wr_data)(arcn, fd, &cnt);
495 1.1 jtc rdfile_close(arcn, &fd);
496 1.1 jtc if (vflag && vfpart) {
497 1.1 jtc (void)putc('\n', stderr);
498 1.1 jtc vfpart = 0;
499 1.1 jtc }
500 1.1 jtc if (res < 0)
501 1.1 jtc break;
502 1.1 jtc
503 1.1 jtc /*
504 1.1 jtc * pad as required, cnt is number of bytes not written
505 1.1 jtc */
506 1.1 jtc if (((cnt > 0) && (wr_skip(cnt) < 0)) ||
507 1.1 jtc ((arcn->pad > 0) && (wr_skip(arcn->pad) < 0)))
508 1.1 jtc break;
509 1.1 jtc }
510 1.1 jtc
511 1.1 jtc /*
512 1.1 jtc * tell format to write trailer; pad to block boundry; reset directory
513 1.1 jtc * mode/access times, and check if all patterns supplied by the user
514 1.1 jtc * were matched. block off signals to avoid chance for multiple entry
515 1.1 jtc * into the cleanup code
516 1.1 jtc */
517 1.1 jtc if (wr_one) {
518 1.1 jtc (*frmt->end_wr)();
519 1.1 jtc wr_fin();
520 1.1 jtc }
521 1.1 jtc (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
522 1.1 jtc ar_close();
523 1.1 jtc if (tflag)
524 1.1 jtc proc_dir();
525 1.1 jtc ftree_chk();
526 1.1 jtc }
527 1.1 jtc
528 1.1 jtc /*
529 1.1 jtc * append()
530 1.1 jtc * Add file to previously written archive. Archive format specified by the
531 1.1 jtc * user must agree with archive. The archive is read first to collect
532 1.1 jtc * modification times (if -u) and locate the archive trailer. The archive
533 1.1 jtc * is positioned in front of the record with the trailer and wr_archive()
534 1.1 jtc * is called to add the new members.
535 1.1 jtc * PAX IMPLEMENTATION DETAIL NOTE:
536 1.1 jtc * -u is implemented by adding the new members to the end of the archive.
537 1.1 jtc * Care is taken so that these do not end up as links to the older
538 1.1 jtc * version of the same file already stored in the archive. It is expected
539 1.1 jtc * when extraction occurs these newer versions will over-write the older
540 1.1 jtc * ones stored "earlier" in the archive (this may be a bad assumption as
541 1.1 jtc * it depends on the implementation of the program doing the extraction).
542 1.1 jtc * It is really difficult to splice in members without either re-writing
543 1.1 jtc * the entire archive (from the point were the old version was), or having
544 1.1 jtc * assistance of the format specification in terms of a special update
545 1.1 jtc * header that invalidates a previous archive record. The posix spec left
546 1.1 jtc * the method used to implement -u unspecified. This pax is able to
547 1.1 jtc * over write existing files that it creates.
548 1.1 jtc */
549 1.1 jtc
550 1.1 jtc #if __STDC__
551 1.1 jtc void
552 1.1 jtc append(void)
553 1.1 jtc #else
554 1.1 jtc void
555 1.1 jtc append()
556 1.1 jtc #endif
557 1.1 jtc {
558 1.1 jtc register ARCHD *arcn;
559 1.1 jtc register int res;
560 1.1 jtc ARCHD archd;
561 1.1 jtc FSUB *orgfrmt;
562 1.1 jtc int udev;
563 1.1 jtc off_t tlen;
564 1.1 jtc
565 1.1 jtc arcn = &archd;
566 1.1 jtc orgfrmt = frmt;
567 1.1 jtc
568 1.1 jtc /*
569 1.1 jtc * Do not allow an append operation if the actual archive is of a
570 1.1 jtc * different format than the user specified foramt.
571 1.1 jtc */
572 1.1 jtc if (get_arc() < 0)
573 1.1 jtc return;
574 1.1 jtc if ((orgfrmt != NULL) && (orgfrmt != frmt)) {
575 1.1 jtc warn(1, "Cannot mix current archive format %s with %s",
576 1.1 jtc frmt->name, orgfrmt->name);
577 1.1 jtc return;
578 1.1 jtc }
579 1.1 jtc
580 1.1 jtc /*
581 1.1 jtc * pass the format any options and start up format
582 1.1 jtc */
583 1.1 jtc if (((*frmt->options)() < 0) || ((*frmt->st_rd)() < 0))
584 1.1 jtc return;
585 1.1 jtc
586 1.1 jtc /*
587 1.1 jtc * if we only are adding members that are newer, we need to save the
588 1.1 jtc * mod times for all files we see.
589 1.1 jtc */
590 1.1 jtc if (uflag && (ftime_start() < 0))
591 1.1 jtc return;
592 1.1 jtc
593 1.1 jtc /*
594 1.1 jtc * some archive formats encode hard links by recording the device and
595 1.1 jtc * file serial number (inode) but copy the file anyway (multiple times)
596 1.1 jtc * to the archive. When we append, we run the risk that newly added
597 1.1 jtc * files may have the same device and inode numbers as those recorded
598 1.1 jtc * on the archive but during a previous run. If this happens, when the
599 1.1 jtc * archive is extracted we get INCORRECT hard links. We avoid this by
600 1.1 jtc * remapping the device numbers so that newly added files will never
601 1.1 jtc * use the same device number as one found on the archive. remapping
602 1.1 jtc * allows new members to safely have links among themselves. remapping
603 1.1 jtc * also avoids problems with file inode (serial number) truncations
604 1.1 jtc * when the inode number is larger than storage space in the archive
605 1.1 jtc * header. See the remap routines for more details.
606 1.1 jtc */
607 1.1 jtc if ((udev = frmt->udev) && (dev_start() < 0))
608 1.1 jtc return;
609 1.1 jtc
610 1.1 jtc /*
611 1.1 jtc * reading the archive may take a long time. If verbose tell the user
612 1.1 jtc */
613 1.1 jtc if (vflag) {
614 1.1 jtc (void)fprintf(stderr,
615 1.1 jtc "%s: Reading archive to position at the end...", argv0);
616 1.1 jtc vfpart = 1;
617 1.1 jtc }
618 1.1 jtc
619 1.1 jtc /*
620 1.1 jtc * step through the archive until the format says it is done
621 1.1 jtc */
622 1.1 jtc while (next_head(arcn) == 0) {
623 1.1 jtc /*
624 1.1 jtc * check if this file meets user specified options.
625 1.1 jtc */
626 1.1 jtc if (sel_chk(arcn) != 0) {
627 1.1 jtc if (rd_skip(arcn->skip + arcn->pad) == 1)
628 1.1 jtc break;
629 1.1 jtc continue;
630 1.1 jtc }
631 1.1 jtc
632 1.1 jtc if (uflag) {
633 1.1 jtc /*
634 1.1 jtc * see if this is the newest version of this file has
635 1.1 jtc * already been seen, if so skip.
636 1.1 jtc */
637 1.1 jtc if ((res = chk_ftime(arcn)) < 0)
638 1.1 jtc break;
639 1.1 jtc if (res > 0) {
640 1.1 jtc if (rd_skip(arcn->skip + arcn->pad) == 1)
641 1.1 jtc break;
642 1.1 jtc continue;
643 1.1 jtc }
644 1.1 jtc }
645 1.1 jtc
646 1.1 jtc /*
647 1.1 jtc * Store this device number. Device numbers seen during the
648 1.1 jtc * read phase of append will cause newly appended files with a
649 1.1 jtc * device number seen in the old part of the archive to be
650 1.1 jtc * remapped to an unused device number.
651 1.1 jtc */
652 1.1 jtc if ((udev && (add_dev(arcn) < 0)) ||
653 1.1 jtc (rd_skip(arcn->skip + arcn->pad) == 1))
654 1.1 jtc break;
655 1.1 jtc }
656 1.1 jtc
657 1.1 jtc /*
658 1.1 jtc * done, finish up read and get the number of bytes to back up so we
659 1.1 jtc * can add new members. The format might have used the hard link table,
660 1.1 jtc * purge it.
661 1.1 jtc */
662 1.1 jtc tlen = (*frmt->end_rd)();
663 1.1 jtc lnk_end();
664 1.1 jtc
665 1.1 jtc /*
666 1.1 jtc * try to postion for write, if this fails quit. if any error occurs,
667 1.1 jtc * we will refuse to write
668 1.1 jtc */
669 1.1 jtc if (appnd_start(tlen) < 0)
670 1.1 jtc return;
671 1.1 jtc
672 1.1 jtc /*
673 1.1 jtc * tell the user we are done reading.
674 1.1 jtc */
675 1.1 jtc if (vflag && vfpart) {
676 1.1 jtc (void)fputs("done.\n", stderr);
677 1.1 jtc vfpart = 0;
678 1.1 jtc }
679 1.1 jtc
680 1.1 jtc /*
681 1.1 jtc * go to the writing phase to add the new members
682 1.1 jtc */
683 1.1 jtc wr_archive(arcn, 1);
684 1.1 jtc }
685 1.1 jtc
686 1.1 jtc /*
687 1.1 jtc * archive()
688 1.1 jtc * write a new archive
689 1.1 jtc */
690 1.1 jtc
691 1.1 jtc #if __STDC__
692 1.1 jtc void
693 1.1 jtc archive(void)
694 1.1 jtc #else
695 1.1 jtc void
696 1.1 jtc archive()
697 1.1 jtc #endif
698 1.1 jtc {
699 1.1 jtc ARCHD archd;
700 1.1 jtc
701 1.1 jtc /*
702 1.1 jtc * if we only are adding members that are newer, we need to save the
703 1.1 jtc * mod times for all files; set up for writing; pass the format any
704 1.1 jtc * options write the archive
705 1.1 jtc */
706 1.1 jtc if ((uflag && (ftime_start() < 0)) || (wr_start() < 0))
707 1.1 jtc return;
708 1.1 jtc if ((*frmt->options)() < 0)
709 1.1 jtc return;
710 1.1 jtc
711 1.1 jtc wr_archive(&archd, 0);
712 1.1 jtc }
713 1.1 jtc
714 1.1 jtc /*
715 1.1 jtc * copy()
716 1.1 jtc * copy files from one part of the file system to another. this does not
717 1.1 jtc * use any archive storage. The EFFECT OF THE COPY IS THE SAME as if an
718 1.1 jtc * archive was written and then extracted in the destination directory
719 1.1 jtc * (except the files are forced to be under the destination directory).
720 1.1 jtc */
721 1.1 jtc
722 1.1 jtc #if __STDC__
723 1.1 jtc void
724 1.1 jtc copy(void)
725 1.1 jtc #else
726 1.1 jtc void
727 1.1 jtc copy()
728 1.1 jtc #endif
729 1.1 jtc {
730 1.1 jtc register ARCHD *arcn;
731 1.1 jtc register int res;
732 1.1 jtc register int fddest;
733 1.1 jtc register char *dest_pt;
734 1.1 jtc register int dlen;
735 1.1 jtc register int drem;
736 1.1 jtc int fdsrc = -1;
737 1.1 jtc struct stat sb;
738 1.1 jtc ARCHD archd;
739 1.1 jtc char dirbuf[PAXPATHLEN+1];
740 1.1 jtc
741 1.1 jtc arcn = &archd;
742 1.1 jtc /*
743 1.1 jtc * set up the destination dir path and make sure it is a directory. We
744 1.1 jtc * make sure we have a trailing / on the destination
745 1.1 jtc */
746 1.1 jtc dlen = l_strncpy(dirbuf, dirptr, PAXPATHLEN);
747 1.1 jtc dest_pt = dirbuf + dlen;
748 1.1 jtc if (*(dest_pt-1) != '/') {
749 1.1 jtc *dest_pt++ = '/';
750 1.1 jtc ++dlen;
751 1.1 jtc }
752 1.1 jtc *dest_pt = '\0';
753 1.1 jtc drem = PAXPATHLEN - dlen;
754 1.1 jtc
755 1.1 jtc if (stat(dirptr, &sb) < 0) {
756 1.1 jtc syswarn(1, errno, "Cannot access destination directory %s",
757 1.1 jtc dirptr);
758 1.1 jtc return;
759 1.1 jtc }
760 1.1 jtc if (!S_ISDIR(sb.st_mode)) {
761 1.1 jtc warn(1, "Destination is not a directory %s", dirptr);
762 1.1 jtc return;
763 1.1 jtc }
764 1.1 jtc
765 1.1 jtc /*
766 1.1 jtc * start up the hard link table; file traversal routines and the
767 1.1 jtc * modification time and access mode database
768 1.1 jtc */
769 1.1 jtc if ((lnk_start() < 0) || (ftree_start() < 0) || (dir_start() < 0))
770 1.1 jtc return;
771 1.1 jtc
772 1.1 jtc /*
773 1.1 jtc * When we are doing interactive rename, we store the mapping of names
774 1.1 jtc * so we can fix up hard links files later in the archive.
775 1.1 jtc */
776 1.1 jtc if (iflag && (name_start() < 0))
777 1.1 jtc return;
778 1.1 jtc
779 1.1 jtc /*
780 1.1 jtc * set up to cp file trees
781 1.1 jtc */
782 1.1 jtc cp_start();
783 1.1 jtc
784 1.1 jtc /*
785 1.1 jtc * while there are files to archive, process them
786 1.1 jtc */
787 1.1 jtc while (next_file(arcn) == 0) {
788 1.1 jtc fdsrc = -1;
789 1.1 jtc
790 1.1 jtc /*
791 1.1 jtc * check if this file meets user specified options
792 1.1 jtc */
793 1.1 jtc if (sel_chk(arcn) != 0)
794 1.1 jtc continue;
795 1.1 jtc
796 1.1 jtc /*
797 1.1 jtc * if there is already a file in the destination directory with
798 1.1 jtc * the same name and it is newer, skip the one stored on the
799 1.1 jtc * archive.
800 1.1 jtc * NOTE: this test is done BEFORE name modifications as
801 1.1 jtc * specified by pax. this can be confusing to the user who
802 1.1 jtc * might expect the test to be done on an existing file AFTER
803 1.1 jtc * the name mod. In honesty the pax spec is probably flawed in
804 1.1 jtc * this respect
805 1.1 jtc */
806 1.1 jtc if (uflag || Dflag) {
807 1.1 jtc /*
808 1.1 jtc * create the destination name
809 1.1 jtc */
810 1.1 jtc if (*(arcn->name) == '/')
811 1.1 jtc res = 1;
812 1.1 jtc else
813 1.1 jtc res = 0;
814 1.1 jtc if ((arcn->nlen - res) > drem) {
815 1.1 jtc warn(1, "Destination pathname too long %s",
816 1.1 jtc arcn->name);
817 1.1 jtc continue;
818 1.1 jtc }
819 1.1 jtc (void)strncpy(dest_pt, arcn->name + res, drem);
820 1.1 jtc dirbuf[PAXPATHLEN] = '\0';
821 1.1 jtc
822 1.1 jtc /*
823 1.1 jtc * if existing file is same age or newer skip
824 1.1 jtc */
825 1.1 jtc res = lstat(dirbuf, &sb);
826 1.1 jtc *dest_pt = '\0';
827 1.1 jtc
828 1.1 jtc if (res == 0) {
829 1.1 jtc if (uflag && Dflag) {
830 1.1 jtc if ((arcn->sb.st_mtime<=sb.st_mtime) &&
831 1.1 jtc (arcn->sb.st_ctime<=sb.st_ctime))
832 1.1 jtc continue;
833 1.1 jtc } else if (Dflag) {
834 1.1 jtc if (arcn->sb.st_ctime <= sb.st_ctime)
835 1.1 jtc continue;
836 1.1 jtc } else if (arcn->sb.st_mtime <= sb.st_mtime)
837 1.1 jtc continue;
838 1.1 jtc }
839 1.1 jtc }
840 1.1 jtc
841 1.1 jtc /*
842 1.1 jtc * this file is considered selected. See if this is a hard link
843 1.1 jtc * to a previous file; modify the name as requested by the
844 1.1 jtc * user; set the final destination.
845 1.1 jtc */
846 1.1 jtc ftree_sel(arcn);
847 1.1 jtc if ((chk_lnk(arcn) < 0) || ((res = mod_name(arcn)) < 0))
848 1.1 jtc break;
849 1.1 jtc if ((res > 0) || (set_dest(arcn, dirbuf, dlen) < 0)) {
850 1.1 jtc /*
851 1.1 jtc * skip file, purge from link table
852 1.1 jtc */
853 1.1 jtc purg_lnk(arcn);
854 1.1 jtc continue;
855 1.1 jtc }
856 1.1 jtc
857 1.1 jtc /*
858 1.1 jtc * Non standard -Y and -Z flag. When the exisiting file is
859 1.1 jtc * same age or newer skip
860 1.1 jtc */
861 1.1 jtc if ((Yflag || Zflag) && ((lstat(arcn->name, &sb) == 0))) {
862 1.1 jtc if (Yflag && Zflag) {
863 1.1 jtc if ((arcn->sb.st_mtime <= sb.st_mtime) &&
864 1.1 jtc (arcn->sb.st_ctime <= sb.st_ctime))
865 1.1 jtc continue;
866 1.1 jtc } else if (Yflag) {
867 1.1 jtc if (arcn->sb.st_ctime <= sb.st_ctime)
868 1.1 jtc continue;
869 1.1 jtc } else if (arcn->sb.st_mtime <= sb.st_mtime)
870 1.1 jtc continue;
871 1.1 jtc }
872 1.1 jtc
873 1.1 jtc if (vflag) {
874 1.1 jtc (void)fputs(arcn->name, stderr);
875 1.1 jtc vfpart = 1;
876 1.1 jtc }
877 1.1 jtc ++flcnt;
878 1.1 jtc
879 1.1 jtc /*
880 1.1 jtc * try to create a hard link to the src file if requested
881 1.1 jtc * but make sure we are not trying to overwrite ourselves.
882 1.1 jtc */
883 1.1 jtc if (lflag)
884 1.1 jtc res = cross_lnk(arcn);
885 1.1 jtc else
886 1.1 jtc res = chk_same(arcn);
887 1.1 jtc if (res <= 0) {
888 1.1 jtc if (vflag && vfpart) {
889 1.1 jtc (void)putc('\n', stderr);
890 1.1 jtc vfpart = 0;
891 1.1 jtc }
892 1.1 jtc continue;
893 1.1 jtc }
894 1.1 jtc
895 1.1 jtc /*
896 1.1 jtc * have to create a new file
897 1.1 jtc */
898 1.1 jtc if ((arcn->type != PAX_REG) && (arcn->type != PAX_CTG)) {
899 1.1 jtc /*
900 1.1 jtc * create a link or special file
901 1.1 jtc */
902 1.1 jtc if ((arcn->type == PAX_HLK) || (arcn->type == PAX_HRG))
903 1.1 jtc res = lnk_creat(arcn);
904 1.1 jtc else
905 1.1 jtc res = node_creat(arcn);
906 1.1 jtc if (res < 0)
907 1.1 jtc purg_lnk(arcn);
908 1.1 jtc if (vflag && vfpart) {
909 1.1 jtc (void)putc('\n', stderr);
910 1.1 jtc vfpart = 0;
911 1.1 jtc }
912 1.1 jtc continue;
913 1.1 jtc }
914 1.1 jtc
915 1.1 jtc /*
916 1.1 jtc * have to copy a regular file to the destination directory.
917 1.1 jtc * first open source file and then create the destination file
918 1.1 jtc */
919 1.1 jtc if ((fdsrc = open(arcn->org_name, O_RDONLY, 0)) < 0) {
920 1.1 jtc syswarn(1, errno, "Unable to open %s to read",
921 1.1 jtc arcn->org_name);
922 1.1 jtc purg_lnk(arcn);
923 1.1 jtc continue;
924 1.1 jtc }
925 1.1 jtc if ((fddest = file_creat(arcn)) < 0) {
926 1.1 jtc rdfile_close(arcn, &fdsrc);
927 1.1 jtc purg_lnk(arcn);
928 1.1 jtc continue;
929 1.1 jtc }
930 1.1 jtc
931 1.1 jtc /*
932 1.1 jtc * copy source file data to the destination file
933 1.1 jtc */
934 1.1 jtc cp_file(arcn, fdsrc, fddest);
935 1.1 jtc file_close(arcn, fddest);
936 1.1 jtc rdfile_close(arcn, &fdsrc);
937 1.1 jtc
938 1.1 jtc if (vflag && vfpart) {
939 1.1 jtc (void)putc('\n', stderr);
940 1.1 jtc vfpart = 0;
941 1.1 jtc }
942 1.1 jtc }
943 1.1 jtc
944 1.1 jtc /*
945 1.1 jtc * restore directory modes and times as required; make sure all
946 1.1 jtc * patterns were selected block off signals to avoid chance for
947 1.1 jtc * multiple entry into the cleanup code.
948 1.1 jtc */
949 1.1 jtc (void)sigprocmask(SIG_BLOCK, &s_mask, (sigset_t *)NULL);
950 1.1 jtc ar_close();
951 1.1 jtc proc_dir();
952 1.1 jtc ftree_chk();
953 1.1 jtc }
954 1.1 jtc
955 1.1 jtc /*
956 1.1 jtc * next_head()
957 1.1 jtc * try to find a valid header in the archive. Uses format specific
958 1.1 jtc * routines to extract the header and id the trailer. Trailers may be
959 1.1 jtc * located within a valid header or in an invalid header (the location
960 1.1 jtc * is format specific. The inhead field from the option table tells us
961 1.1 jtc * where to look for the trailer).
962 1.1 jtc * We keep reading (and resyncing) until we get enough contiguous data
963 1.1 jtc * to check for a header. If we cannot find one, we shift by a byte
964 1.1 jtc * add a new byte from the archive to the end of the buffer and try again.
965 1.1 jtc * If we get a read error, we throw out what we have (as we must have
966 1.1 jtc * contiguous data) and start over again.
967 1.1 jtc * ASSUMED: headers fit within a BLKMULT header.
968 1.1 jtc * Return:
969 1.1 jtc * 0 if we got a header, -1 if we are unable to ever find another one
970 1.1 jtc * (we reached the end of input, or we reached the limit on retries. see
971 1.1 jtc * the specs for rd_wrbuf() for more details)
972 1.1 jtc */
973 1.1 jtc
974 1.1 jtc #if __STDC__
975 1.1 jtc static int
976 1.1 jtc next_head(register ARCHD *arcn)
977 1.1 jtc #else
978 1.1 jtc static int
979 1.1 jtc next_head(arcn)
980 1.1 jtc register ARCHD *arcn;
981 1.1 jtc #endif
982 1.1 jtc {
983 1.1 jtc register int ret;
984 1.1 jtc register char *hdend;
985 1.1 jtc register int res;
986 1.1 jtc register int shftsz;
987 1.1 jtc register int hsz;
988 1.1 jtc register int in_resync = 0; /* set when we are in resync mode */
989 1.1 jtc int cnt = 0; /* counter for trailer function */
990 1.1 jtc
991 1.1 jtc /*
992 1.1 jtc * set up initial conditions, we want a whole frmt->hsz block as we
993 1.1 jtc * have no data yet.
994 1.1 jtc */
995 1.1 jtc res = hsz = frmt->hsz;
996 1.1 jtc hdend = hdbuf;
997 1.1 jtc shftsz = hsz - 1;
998 1.1 jtc for(;;) {
999 1.1 jtc /*
1000 1.1 jtc * keep looping until we get a contiguous FULL buffer
1001 1.1 jtc * (frmt->hsz is the proper size)
1002 1.1 jtc */
1003 1.1 jtc for (;;) {
1004 1.1 jtc if ((ret = rd_wrbuf(hdend, res)) == res)
1005 1.1 jtc break;
1006 1.1 jtc
1007 1.1 jtc /*
1008 1.1 jtc * some kind of archive read problem, try to resync the
1009 1.1 jtc * storage device, better give the user the bad news.
1010 1.1 jtc */
1011 1.1 jtc if ((ret == 0) || (rd_sync() < 0)) {
1012 1.1 jtc warn(1,"Premature end of file on archive read");
1013 1.1 jtc return(-1);
1014 1.1 jtc }
1015 1.1 jtc if (!in_resync) {
1016 1.1 jtc if (act == APPND) {
1017 1.1 jtc warn(1,
1018 1.1 jtc "Archive I/O error, cannot continue");
1019 1.1 jtc return(-1);
1020 1.1 jtc }
1021 1.1 jtc warn(1,"Archive I/O error. Trying to recover.");
1022 1.1 jtc ++in_resync;
1023 1.1 jtc }
1024 1.1 jtc
1025 1.1 jtc /*
1026 1.1 jtc * oh well, throw it all out and start over
1027 1.1 jtc */
1028 1.1 jtc res = hsz;
1029 1.1 jtc hdend = hdbuf;
1030 1.1 jtc }
1031 1.1 jtc
1032 1.1 jtc /*
1033 1.1 jtc * ok we have a contiguous buffer of the right size. Call the
1034 1.1 jtc * format read routine. If this was not a valid header and this
1035 1.1 jtc * format stores trailers outside of the header, call the
1036 1.1 jtc * format specific trailer routine to check for a trailer. We
1037 1.1 jtc * have to watch out that we do not mis-identify file data or
1038 1.1 jtc * block padding as a header or trailer. Format specific
1039 1.1 jtc * trailer functions must NOT check for the trailer while we
1040 1.1 jtc * are running in resync mode. Some trailer functions may tell
1041 1.1 jtc * us that this block cannot contain a valid header either, so
1042 1.1 jtc * we then throw out the entire block and start over.
1043 1.1 jtc */
1044 1.1 jtc if ((*frmt->rd)(arcn, hdbuf) == 0)
1045 1.1 jtc break;
1046 1.1 jtc
1047 1.1 jtc if (!frmt->inhead) {
1048 1.1 jtc /*
1049 1.1 jtc * this format has trailers outside of valid headers
1050 1.1 jtc */
1051 1.1 jtc if ((ret = (*frmt->trail)(hdbuf,in_resync,&cnt)) == 0){
1052 1.1 jtc /*
1053 1.1 jtc * valid trailer found, drain input as required
1054 1.1 jtc */
1055 1.1 jtc ar_drain();
1056 1.1 jtc return(-1);
1057 1.1 jtc }
1058 1.1 jtc
1059 1.1 jtc if (ret == 1) {
1060 1.1 jtc /*
1061 1.1 jtc * we are in resync and we were told to throw
1062 1.1 jtc * the whole block out because none of the
1063 1.1 jtc * bytes in this block can be used to form a
1064 1.1 jtc * valid header
1065 1.1 jtc */
1066 1.1 jtc res = hsz;
1067 1.1 jtc hdend = hdbuf;
1068 1.1 jtc continue;
1069 1.1 jtc }
1070 1.1 jtc }
1071 1.1 jtc
1072 1.1 jtc /*
1073 1.1 jtc * Brute force section.
1074 1.1 jtc * not a valid header. We may be able to find a header yet. So
1075 1.1 jtc * we shift over by one byte, and set up to read one byte at a
1076 1.1 jtc * time from the archive and place it at the end of the buffer.
1077 1.1 jtc * We will keep moving byte at a time until we find a header or
1078 1.1 jtc * get a read error and have to start over.
1079 1.1 jtc */
1080 1.1 jtc if (!in_resync) {
1081 1.1 jtc if (act == APPND) {
1082 1.1 jtc warn(1,"Unable to append, archive header flaw");
1083 1.1 jtc return(-1);
1084 1.1 jtc }
1085 1.1 jtc warn(1,"Invalid header, starting valid header search.");
1086 1.1 jtc ++in_resync;
1087 1.1 jtc }
1088 1.4 mycroft memmove(hdbuf, hdbuf+1, shftsz);
1089 1.1 jtc res = 1;
1090 1.1 jtc hdend = hdbuf + shftsz;
1091 1.1 jtc }
1092 1.1 jtc
1093 1.1 jtc /*
1094 1.1 jtc * ok got a valid header, check for trailer if format encodes it in the
1095 1.1 jtc * the header. NOTE: the parameters are different than trailer routines
1096 1.1 jtc * which encode trailers outside of the header!
1097 1.1 jtc */
1098 1.1 jtc if (frmt->inhead && ((*frmt->trail)(arcn) == 0)) {
1099 1.1 jtc /*
1100 1.1 jtc * valid trailer found, drain input as required
1101 1.1 jtc */
1102 1.1 jtc ar_drain();
1103 1.1 jtc return(-1);
1104 1.1 jtc }
1105 1.1 jtc
1106 1.1 jtc ++flcnt;
1107 1.1 jtc return(0);
1108 1.1 jtc }
1109 1.1 jtc
1110 1.1 jtc /*
1111 1.1 jtc * get_arc()
1112 1.1 jtc * Figure out what format an archive is. Handles archive with flaws by
1113 1.1 jtc * brute force searches for a legal header in any supported format. The
1114 1.1 jtc * format id routines have to be careful to NOT mis-identify a format.
1115 1.1 jtc * ASSUMED: headers fit within a BLKMULT header.
1116 1.1 jtc * Return:
1117 1.1 jtc * 0 if archive found -1 otherwise
1118 1.1 jtc */
1119 1.1 jtc
1120 1.1 jtc #if __STDC__
1121 1.1 jtc static int
1122 1.1 jtc get_arc(void)
1123 1.1 jtc #else
1124 1.1 jtc static int
1125 1.1 jtc get_arc()
1126 1.1 jtc #endif
1127 1.1 jtc {
1128 1.1 jtc register int i;
1129 1.1 jtc register int hdsz = 0;
1130 1.1 jtc register int res;
1131 1.1 jtc register int minhd = BLKMULT;
1132 1.1 jtc char *hdend;
1133 1.1 jtc int notice = 0;
1134 1.1 jtc
1135 1.1 jtc /*
1136 1.1 jtc * find the smallest header size in all archive formats and then set up
1137 1.1 jtc * to read the archive.
1138 1.1 jtc */
1139 1.1 jtc for (i = 0; ford[i] >= 0; ++i) {
1140 1.1 jtc if (fsub[ford[i]].hsz < minhd)
1141 1.1 jtc minhd = fsub[ford[i]].hsz;
1142 1.1 jtc }
1143 1.1 jtc if (rd_start() < 0)
1144 1.1 jtc return(-1);
1145 1.1 jtc res = BLKMULT;
1146 1.1 jtc hdsz = 0;
1147 1.1 jtc hdend = hdbuf;
1148 1.1 jtc for(;;) {
1149 1.1 jtc for (;;) {
1150 1.1 jtc /*
1151 1.1 jtc * fill the buffer with at least the smallest header
1152 1.1 jtc */
1153 1.1 jtc i = rd_wrbuf(hdend, res);
1154 1.1 jtc if (i > 0)
1155 1.1 jtc hdsz += i;
1156 1.1 jtc if (hdsz >= minhd)
1157 1.1 jtc break;
1158 1.1 jtc
1159 1.1 jtc /*
1160 1.1 jtc * if we cannot recover from a read error quit
1161 1.1 jtc */
1162 1.1 jtc if ((i == 0) || (rd_sync() < 0))
1163 1.1 jtc goto out;
1164 1.1 jtc
1165 1.1 jtc /*
1166 1.1 jtc * when we get an error none of the data we already
1167 1.1 jtc * have can be used to create a legal header (we just
1168 1.1 jtc * got an error in the middle), so we throw it all out
1169 1.1 jtc * and refill the buffer with fresh data.
1170 1.1 jtc */
1171 1.1 jtc res = BLKMULT;
1172 1.1 jtc hdsz = 0;
1173 1.1 jtc hdend = hdbuf;
1174 1.1 jtc if (!notice) {
1175 1.1 jtc if (act == APPND)
1176 1.1 jtc return(-1);
1177 1.1 jtc warn(1,"Cannot identify format. Searching...");
1178 1.1 jtc ++notice;
1179 1.1 jtc }
1180 1.1 jtc }
1181 1.1 jtc
1182 1.1 jtc /*
1183 1.1 jtc * we have at least the size of the smallest header in any
1184 1.1 jtc * archive format. Look to see if we have a match. The array
1185 1.1 jtc * ford[] is used to specify the header id order to reduce the
1186 1.1 jtc * chance of incorrectly id'ing a valid header (some formats
1187 1.1 jtc * may be subsets of each other and the order would then be
1188 1.1 jtc * important).
1189 1.1 jtc */
1190 1.1 jtc for (i = 0; ford[i] >= 0; ++i) {
1191 1.1 jtc if ((*fsub[ford[i]].id)(hdbuf, hdsz) < 0)
1192 1.1 jtc continue;
1193 1.1 jtc frmt = &(fsub[ford[i]]);
1194 1.1 jtc /*
1195 1.1 jtc * yuck, to avoid slow special case code in the extract
1196 1.1 jtc * routines, just push this header back as if it was
1197 1.1 jtc * not seen. We have left extra space at start of the
1198 1.1 jtc * buffer for this purpose. This is a bit ugly, but
1199 1.1 jtc * adding all the special case code is far worse.
1200 1.1 jtc */
1201 1.1 jtc pback(hdbuf, hdsz);
1202 1.1 jtc return(0);
1203 1.1 jtc }
1204 1.1 jtc
1205 1.1 jtc /*
1206 1.1 jtc * We have a flawed archive, no match. we start searching, but
1207 1.1 jtc * we never allow additions to flawed archives
1208 1.1 jtc */
1209 1.1 jtc if (!notice) {
1210 1.1 jtc if (act == APPND)
1211 1.1 jtc return(-1);
1212 1.1 jtc warn(1, "Cannot identify format. Searching...");
1213 1.1 jtc ++notice;
1214 1.1 jtc }
1215 1.1 jtc
1216 1.1 jtc /*
1217 1.1 jtc * brute force search for a header that we can id.
1218 1.1 jtc * we shift through byte at a time. this is slow, but we cannot
1219 1.1 jtc * determine the nature of the flaw in the archive in a
1220 1.1 jtc * portable manner
1221 1.1 jtc */
1222 1.1 jtc if (--hdsz > 0) {
1223 1.4 mycroft memmove(hdbuf, hdbuf+1, hdsz);
1224 1.1 jtc res = BLKMULT - hdsz;
1225 1.1 jtc hdend = hdbuf + hdsz;
1226 1.1 jtc } else {
1227 1.1 jtc res = BLKMULT;
1228 1.1 jtc hdend = hdbuf;
1229 1.1 jtc hdsz = 0;
1230 1.1 jtc }
1231 1.1 jtc }
1232 1.1 jtc
1233 1.1 jtc out:
1234 1.1 jtc /*
1235 1.1 jtc * we cannot find a header, bow, apologize and quit
1236 1.1 jtc */
1237 1.1 jtc warn(1, "Sorry, unable to determine archive format.");
1238 1.1 jtc return(-1);
1239 1.1 jtc }
1240