source: freewrt/tools/paxmirabilis/src/cpio.c@ 0bb5b95

freewrt_2_0
Last change on this file since 0bb5b95 was a569125, checked in by Thorsten Glaser <tg@…>, 14 years ago

even FreeWRT 1.0-stable deserves paxmirabilis-20120216 compiled with LTO ☺

git-svn-id: svn://www.freewrt.org/branches/freewrt_1_0@3981 afb5a338-a214-0410-bd46-81f09a774fd1

  • Property mode set to 100644
File size: 31.0 KB
Line 
1/* $OpenBSD: cpio.c,v 1.19 2009/10/27 23:59:22 deraadt Exp $ */
2/* $NetBSD: cpio.c,v 1.5 1995/03/21 09:07:13 cgd Exp $ */
3
4/*-
5 * Copyright (c) 2005, 2012
6 * Thorsten Glaser <tg@mirbsd.org>
7 * Copyright (c) 1992 Keith Muller.
8 * Copyright (c) 1992, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * This code is derived from software contributed to Berkeley by
12 * Keith Muller of the University of California, San Diego.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39#include <sys/param.h>
40#include <sys/time.h>
41#include <sys/stat.h>
42#include <string.h>
43#include <stdio.h>
44#include <unistd.h>
45#include <stdlib.h>
46#include "pax.h"
47#include "cpio.h"
48#include "extern.h"
49#include "options.h"
50
51__RCSID("$MirOS: src/bin/pax/cpio.c,v 1.18 2012/02/16 17:27:31 tg Exp $");
52
53static int rd_nm(ARCHD *, int);
54static int rd_ln_nm(ARCHD *);
55static int com_rd(ARCHD *);
56
57/*
58 * Routines which support the different cpio versions
59 */
60
61static int swp_head; /* binary cpio header byte swap */
62
63/*
64 * Routines common to all versions of cpio
65 */
66
67/*
68 * cpio_strd()
69 * Fire up the hard link detection code
70 * Return:
71 * 0 if ok -1 otherwise (the return values of lnk_start())
72 */
73
74int
75cpio_strd(void)
76{
77 return(lnk_start());
78}
79
80/*
81 * cpio_trail()
82 * Called to determine if a header block is a valid trailer. We are
83 * passed the block, the in_sync flag (which tells us we are in resync
84 * mode; looking for a valid header), and cnt (which starts at zero)
85 * which is used to count the number of empty blocks we have seen so far.
86 * Return:
87 * 0 if a valid trailer, -1 if not a valid trailer,
88 */
89
90int
91cpio_trail(ARCHD *arcn, char *notused __attribute__((__unused__)),
92 int notused2 __attribute__((__unused__)),
93 int *notused3 __attribute__((__unused__)))
94{
95 /*
96 * look for trailer id in file we are about to process
97 */
98 if ((strcmp(arcn->name, TRAILER) == 0) && (arcn->sb.st_size == 0))
99 return(0);
100 return(-1);
101}
102
103/*
104 * com_rd()
105 * operations common to all cpio read functions.
106 * Return:
107 * 0
108 */
109
110static int
111com_rd(ARCHD *arcn)
112{
113 arcn->skip = 0;
114 arcn->pat = NULL;
115 arcn->org_name = arcn->name;
116 switch (arcn->sb.st_mode & C_IFMT) {
117 case C_ISFIFO:
118 arcn->type = PAX_FIF;
119 break;
120 case C_ISDIR:
121 arcn->type = PAX_DIR;
122 break;
123 case C_ISBLK:
124 arcn->type = PAX_BLK;
125 break;
126 case C_ISCHR:
127 arcn->type = PAX_CHR;
128 break;
129 case C_ISLNK:
130 arcn->type = PAX_SLK;
131 break;
132 case C_ISOCK:
133 arcn->type = PAX_SCK;
134 break;
135 case C_ISCTG:
136 case C_ISREG:
137 default:
138 /*
139 * we have file data, set up skip (pad is set in the format
140 * specific sections)
141 */
142 arcn->sb.st_mode = (arcn->sb.st_mode & 0xfff) | C_ISREG;
143 arcn->type = PAX_REG;
144 arcn->skip = arcn->sb.st_size;
145 break;
146 }
147 if (chk_lnk(arcn) < 0)
148 return(-1);
149 return(0);
150}
151
152/*
153 * cpio_endwr()
154 * write the special file with the name trailer in the proper format
155 * Return:
156 * result of the write of the trailer from the cpio specific write func
157 */
158
159int
160cpio_endwr(void)
161{
162 ARCHD last;
163
164 /*
165 * create a trailer request and call the proper format write function
166 */
167 memset(&last, 0, sizeof(last));
168 last.nlen = sizeof(TRAILER) - 1;
169 last.type = PAX_REG;
170 last.sb.st_nlink = 1;
171 (void)strlcpy(last.name, TRAILER, sizeof(last.name));
172 return((*frmt->wr)(&last));
173}
174
175/*
176 * rd_nam()
177 * read in the file name which follows the cpio header
178 * Return:
179 * 0 if ok, -1 otherwise
180 */
181
182static int
183rd_nm(ARCHD *arcn, int nsz)
184{
185 /*
186 * do not even try bogus values
187 */
188 if ((nsz == 0) || ((size_t)nsz > sizeof(arcn->name))) {
189 paxwarn(1, "cpio file name length %d is out of range", nsz);
190 return(-1);
191 }
192
193 /*
194 * read the name and make sure it is not empty and is \0 terminated
195 */
196 if ((rd_wrbuf(arcn->name,nsz) != nsz) || (arcn->name[nsz-1] != '\0') ||
197 (arcn->name[0] == '\0')) {
198 paxwarn(1, "cpio file name in header is corrupted");
199 return(-1);
200 }
201 return(0);
202}
203
204/*
205 * rd_ln_nm()
206 * read in the link name for a file with links. The link name is stored
207 * like file data (and is NOT \0 terminated!)
208 * Return:
209 * 0 if ok, -1 otherwise
210 */
211
212static int
213rd_ln_nm(ARCHD *arcn)
214{
215 /*
216 * check the length specified for bogus values
217 */
218 if ((arcn->sb.st_size == 0) ||
219 ((size_t)arcn->sb.st_size >= sizeof(arcn->ln_name))) {
220 paxwarn(1, "cpio link name length is invalid: %" OT_FMT,
221 (ot_type)arcn->sb.st_size);
222 return (-1);
223 }
224
225 /*
226 * read in the link name and \0 terminate it
227 */
228 if (rd_wrbuf(arcn->ln_name, (int)arcn->sb.st_size) !=
229 (int)arcn->sb.st_size) {
230 paxwarn(1, "cpio link name read error");
231 return(-1);
232 }
233 arcn->ln_nlen = arcn->sb.st_size;
234 arcn->ln_name[arcn->ln_nlen] = '\0';
235
236 /*
237 * watch out for those empty link names
238 */
239 if (arcn->ln_name[0] == '\0') {
240 paxwarn(1, "cpio link name is corrupt");
241 return(-1);
242 }
243 return(0);
244}
245
246/*
247 * Routines common to the extended byte oriented cpio format
248 */
249
250/*
251 * cpio_id()
252 * determine if a block given to us is a valid extended byte oriented
253 * cpio header
254 * Return:
255 * 0 if a valid header, -1 otherwise
256 */
257
258int
259cpio_id(char *blk, int size)
260{
261 if (((size_t)size < sizeof(HD_CPIO)) ||
262 (strncmp(blk, AMAGIC, sizeof(AMAGIC) - 1) != 0))
263 return(-1);
264 return(0);
265}
266
267/*
268 * cpio_rd()
269 * determine if a buffer is a byte oriented extended cpio archive entry.
270 * convert and store the values in the ARCHD parameter.
271 * Return:
272 * 0 if a valid header, -1 otherwise.
273 */
274
275int
276cpio_rd(ARCHD *arcn, char *buf)
277{
278 int nsz;
279 HD_CPIO *hd;
280
281 /*
282 * check that this is a valid header, if not return -1
283 */
284 if (cpio_id(buf, sizeof(HD_CPIO)) < 0)
285 return(-1);
286 hd = (HD_CPIO *)buf;
287
288 /*
289 * byte oriented cpio (posix) does not have padding! extract the octal
290 * ascii fields from the header
291 */
292 arcn->pad = 0L;
293 arcn->sb.st_dev = (dev_t)asc_ul(hd->c_dev, sizeof(hd->c_dev), OCT);
294 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), OCT);
295 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), OCT);
296 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), OCT);
297 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), OCT);
298 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
299 OCT);
300 arcn->sb.st_rdev = (dev_t)asc_ul(hd->c_rdev, sizeof(hd->c_rdev), OCT);
301 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime, sizeof(hd->c_mtime),
302 OCT);
303 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
304 arcn->sb.st_size = (off_t)asc_ot(hd->c_filesize,sizeof(hd->c_filesize),
305 OCT);
306
307 /*
308 * check name size and if valid, read in the name of this entry (name
309 * follows header in the archive)
310 */
311 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),OCT)) < 2)
312 return(-1);
313 arcn->nlen = nsz - 1;
314 if (rd_nm(arcn, nsz) < 0)
315 return(-1);
316
317 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
318 /*
319 * no link name to read for this file
320 */
321 arcn->ln_nlen = 0;
322 arcn->ln_name[0] = '\0';
323 return(com_rd(arcn));
324 }
325
326 /*
327 * check link name size and read in the link name. Link names are
328 * stored like file data.
329 */
330 if (rd_ln_nm(arcn) < 0)
331 return(-1);
332
333 /*
334 * we have a valid header (with a link)
335 */
336 return(com_rd(arcn));
337}
338
339/*
340 * cpio_endrd()
341 * no cleanup needed here, just return size of the trailer (for append)
342 * Return:
343 * size of trailer header in this format
344 */
345
346off_t
347cpio_endrd(void)
348{
349 return((off_t)(sizeof(HD_CPIO) + sizeof(TRAILER)));
350}
351
352/*
353 * cpio_stwr()
354 * start up the device mapping table
355 * Return:
356 * 0 if ok, -1 otherwise (what dev_start() returns)
357 */
358
359int
360cpio_stwr(int is_app __attribute__((__unused__)))
361{
362 if ((anonarch & ANON_INODES) && flnk_start())
363 return (-1);
364 return(dev_start());
365}
366
367int
368dist_stwr(int is_app)
369{
370 anonarch &= ANON_DEBUG | ANON_VERBOSE;
371 anonarch |= ANON_UIDGID | ANON_INODES | ANON_HARDLINKS;
372 return(cpio_stwr(is_app));
373}
374
375/*
376 * cpio_wr()
377 * copy the data in the ARCHD to buffer in extended byte oriented cpio
378 * format.
379 * Return
380 * 0 if file has data to be written after the header, 1 if file has NO
381 * data to write after the header, -1 if archive write failed
382 */
383
384int
385cpio_wr(ARCHD *arcn)
386{
387 HD_CPIO *hd;
388 int nsz;
389 char hdblk[sizeof(HD_CPIO)];
390
391 u_long t_uid, t_gid, t_mtime, t_dev;
392 ino_t t_ino;
393
394 anonarch_init();
395
396 /*
397 * check and repair truncated device and inode fields in the header
398 */
399 if (map_dev(arcn, (u_long)CPIO_MASK, (u_long)CPIO_MASK) < 0)
400 return(-1);
401
402 arcn->pad = 0L;
403 nsz = arcn->nlen + 1;
404 hd = (HD_CPIO *)hdblk;
405 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
406 arcn->sb.st_rdev = 0;
407
408 t_uid = (anonarch & ANON_UIDGID) ? 0UL : (u_long)arcn->sb.st_uid;
409 t_gid = (anonarch & ANON_UIDGID) ? 0UL : (u_long)arcn->sb.st_gid;
410 t_mtime = (anonarch & ANON_MTIME) ? 0UL : (u_long)arcn->sb.st_mtime;
411 t_ino = (anonarch & ANON_INODES) ? (ino_t)chk_flnk(arcn) :
412 arcn->sb.st_ino;
413 t_dev = (anonarch & ANON_INODES) ? 0UL : (u_long)arcn->sb.st_dev;
414 if (!cpio_trail(arcn, NULL, 0, NULL))
415 t_ino = 0UL;
416 if (t_ino == (ino_t)-1) {
417 paxwarn(1, "Invalid inode number for file %s", arcn->org_name);
418 return (1);
419 }
420 if (!(anonarch & ANON_HARDLINKS))
421 arcn->type &= ~PAX_LINKOR;
422
423 switch (arcn->type) {
424 case PAX_CTG:
425 case PAX_REG:
426 case PAX_HRG:
427 /*
428 * set data size for file data
429 */
430 if (ot_asc(arcn->sb.st_size, hd->c_filesize,
431 sizeof(hd->c_filesize), OCT)) {
432 paxwarn(1,"File is too large for cpio format %s",
433 arcn->org_name);
434 return(1);
435 }
436 break;
437 case PAX_SLK:
438 /*
439 * set data size to hold link name
440 */
441 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
442 sizeof(hd->c_filesize), OCT))
443 goto out;
444 break;
445 default:
446 /*
447 * all other file types have no file data
448 */
449 if (ul_asc((u_long)0, hd->c_filesize, sizeof(hd->c_filesize),
450 OCT))
451 goto out;
452 break;
453 }
454
455 if (anonarch & ANON_DEBUG)
456 paxwarn(0, "writing dev %lX inode %10lX mode %8lo user %ld:%ld"
457 "\n\tnlink %3ld mtime %08lX name '%s'", t_dev,
458 (u_long)t_ino, (u_long)arcn->sb.st_mode, t_uid, t_gid,
459 (u_long)arcn->sb.st_nlink, t_mtime, arcn->name);
460
461 /*
462 * copy the values to the header using octal ascii
463 */
464 if (ul_asc((u_long)MAGIC, hd->c_magic, sizeof(hd->c_magic), OCT) ||
465 ul_asc(t_dev, hd->c_dev, sizeof(hd->c_dev),
466 OCT) ||
467 ul_asc((u_long)t_ino, hd->c_ino, sizeof(hd->c_ino),
468 OCT) ||
469 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
470 OCT) ||
471 ul_asc(t_uid, hd->c_uid, sizeof(hd->c_uid),
472 OCT) ||
473 ul_asc(t_gid, hd->c_gid, sizeof(hd->c_gid),
474 OCT) ||
475 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
476 OCT) ||
477 ul_asc((u_long)arcn->sb.st_rdev, hd->c_rdev, sizeof(hd->c_rdev),
478 OCT) ||
479 ul_asc(t_mtime,hd->c_mtime,sizeof(hd->c_mtime),
480 OCT) ||
481 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), OCT))
482 goto out;
483
484 /*
485 * write the file name to the archive
486 */
487 if ((wr_rdbuf(hdblk, (int)sizeof(HD_CPIO)) < 0) ||
488 (wr_rdbuf(arcn->name, nsz) < 0)) {
489 paxwarn(1, "Unable to write cpio header for %s", arcn->org_name);
490 return(-1);
491 }
492
493 /*
494 * if this file has data, we are done. The caller will write the file
495 * data, if we are link tell caller we are done, go to next file
496 */
497 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
498 (arcn->type == PAX_HRG))
499 return(0);
500 if (arcn->type & PAX_LINKOR) {
501 arcn->type &= ~PAX_LINKOR;
502 return (1);
503 }
504 if (arcn->type != PAX_SLK)
505 return(1);
506
507 /*
508 * write the link name to the archive, tell the caller to go to the
509 * next file as we are done.
510 */
511 if (wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) {
512 paxwarn(1,"Unable to write cpio link name for %s",arcn->org_name);
513 return(-1);
514 }
515 return(1);
516
517 out:
518 /*
519 * header field is out of range
520 */
521 paxwarn(1, "cpio header field is too small to store file %s",
522 arcn->org_name);
523 return(1);
524}
525
526/*
527 * Routines common to the system VR4 version of cpio (with/without file CRC)
528 */
529
530/*
531 * vcpio_id()
532 * determine if a block given to us is a valid system VR4 cpio header
533 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
534 * uses HEX
535 * Return:
536 * 0 if a valid header, -1 otherwise
537 */
538
539int
540vcpio_id(char *blk, int size)
541{
542 if (((size_t)size < sizeof(HD_VCPIO)) ||
543 (strncmp(blk, AVMAGIC, sizeof(AVMAGIC) - 1) != 0))
544 return(-1);
545 return(0);
546}
547
548/*
549 * crc_id()
550 * determine if a block given to us is a valid system VR4 cpio header
551 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
552 * Return:
553 * 0 if a valid header, -1 otherwise
554 */
555
556int
557crc_id(char *blk, int size)
558{
559 if (((size_t)size < sizeof(HD_VCPIO)) ||
560 (strncmp(blk, AVCMAGIC, sizeof(AVCMAGIC) - 1) != 0))
561 return(-1);
562 return(0);
563}
564
565/*
566 * crc_strd()
567 w set file data CRC calculations. Fire up the hard link detection code
568 * Return:
569 * 0 if ok -1 otherwise (the return values of lnk_start())
570 */
571
572int
573crc_strd(void)
574{
575 docrc = 1;
576 return(lnk_start());
577}
578
579/*
580 * vcpio_rd()
581 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
582 * convert and store the values in the ARCHD parameter.
583 * Return:
584 * 0 if a valid header, -1 otherwise.
585 */
586
587int
588vcpio_rd(ARCHD *arcn, char *buf)
589{
590 HD_VCPIO *hd;
591 dev_t devminor;
592 dev_t devmajor;
593 int nsz;
594
595 /*
596 * during the id phase it was determined if we were using CRC, use the
597 * proper id routine.
598 */
599 if (docrc) {
600 if (crc_id(buf, sizeof(HD_VCPIO)) < 0)
601 return(-1);
602 } else {
603 if (vcpio_id(buf, sizeof(HD_VCPIO)) < 0)
604 return(-1);
605 }
606
607 hd = (HD_VCPIO *)buf;
608 arcn->pad = 0L;
609
610 /*
611 * extract the hex ascii fields from the header
612 */
613 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), HEX);
614 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), HEX);
615 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), HEX);
616 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), HEX);
617 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime,sizeof(hd->c_mtime),HEX);
618 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
619 arcn->sb.st_size = (off_t)asc_ot(hd->c_filesize,
620 sizeof(hd->c_filesize), HEX);
621 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
622 HEX);
623 devmajor = (dev_t)asc_ul(hd->c_maj, sizeof(hd->c_maj), HEX);
624 devminor = (dev_t)asc_ul(hd->c_min, sizeof(hd->c_min), HEX);
625 arcn->sb.st_dev = TODEV(devmajor, devminor);
626 devmajor = (dev_t)asc_ul(hd->c_rmaj, sizeof(hd->c_rmaj), HEX);
627 devminor = (dev_t)asc_ul(hd->c_rmin, sizeof(hd->c_rmin), HEX);
628 arcn->sb.st_rdev = TODEV(devmajor, devminor);
629 arcn->crc = asc_ul(hd->c_chksum, sizeof(hd->c_chksum), HEX);
630
631 /*
632 * check the length of the file name, if ok read it in, return -1 if
633 * bogus
634 */
635 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),HEX)) < 2)
636 return(-1);
637 arcn->nlen = nsz - 1;
638 if (rd_nm(arcn, nsz) < 0)
639 return(-1);
640
641 /*
642 * skip padding. header + filename is aligned to 4 byte boundaries
643 */
644 if (rd_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)
645 return(-1);
646
647 /*
648 * if not a link (or a file with no data), calculate pad size (for
649 * padding which follows the file data), clear the link name and return
650 */
651 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
652 /*
653 * we have a valid header (not a link)
654 */
655 arcn->ln_nlen = 0;
656 arcn->ln_name[0] = '\0';
657 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
658 return(com_rd(arcn));
659 }
660
661 /*
662 * read in the link name and skip over the padding
663 */
664 if ((rd_ln_nm(arcn) < 0) ||
665 (rd_skip((off_t)(VCPIO_PAD(arcn->sb.st_size))) < 0))
666 return(-1);
667
668 /*
669 * we have a valid header (with a link)
670 */
671 return(com_rd(arcn));
672}
673
674/*
675 * vcpio_endrd()
676 * no cleanup needed here, just return size of the trailer (for append)
677 * Return:
678 * size of trailer header in this format
679 */
680
681off_t
682vcpio_endrd(void)
683{
684 return((off_t)(sizeof(HD_VCPIO) + sizeof(TRAILER) +
685 (VCPIO_PAD(sizeof(HD_VCPIO) + sizeof(TRAILER)))));
686}
687
688/*
689 * crc_stwr()
690 * start up the device mapping table, enable crc file calculation
691 * Return:
692 * 0 if ok, -1 otherwise (what dev_start() returns)
693 */
694
695int
696crc_stwr(int is_app __attribute__((__unused__)))
697{
698 docrc = 1;
699 if ((anonarch & ANON_INODES) && flnk_start())
700 return (-1);
701 return(dev_start());
702}
703
704int
705v4root_stwr(int is_app)
706{
707 anonarch &= ANON_DEBUG | ANON_VERBOSE;
708 anonarch |= ANON_UIDGID | ANON_INODES;
709 return (crc_stwr(is_app));
710}
711
712int
713v4norm_stwr(int is_app)
714{
715 anonarch &= ANON_DEBUG | ANON_VERBOSE;
716 anonarch |= ANON_UIDGID | ANON_INODES | ANON_MTIME | ANON_HARDLINKS;
717 return (crc_stwr(is_app));
718}
719
720/*
721 * vcpio_wr()
722 * copy the data in the ARCHD to buffer in system VR4 cpio
723 * (with/without crc) format.
724 * Return
725 * 0 if file has data to be written after the header, 1 if file has
726 * NO data to write after the header, -1 if archive write failed
727 */
728
729int
730vcpio_wr(ARCHD *arcn)
731{
732 HD_VCPIO *hd;
733 unsigned int nsz;
734 char hdblk[sizeof(HD_VCPIO)];
735
736 u_long t_uid, t_gid, t_mtime, t_major, t_minor;
737 ino_t t_ino;
738
739 anonarch_init();
740
741 /*
742 * check and repair truncated device and inode fields in the cpio
743 * header
744 */
745 if (map_dev(arcn, (u_long)VCPIO_MASK, (u_long)VCPIO_MASK) < 0)
746 return(-1);
747 nsz = arcn->nlen + 1;
748 hd = (HD_VCPIO *)hdblk;
749 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
750 arcn->sb.st_rdev = 0;
751
752 /*
753 * add the proper magic value depending whether we were asked for
754 * file data crc's, and the crc if needed.
755 */
756 if (docrc) {
757 if (ul_asc((u_long)VCMAGIC, hd->c_magic, sizeof(hd->c_magic),
758 OCT) ||
759 ul_asc((u_long)arcn->crc,hd->c_chksum,sizeof(hd->c_chksum),
760 HEX))
761 goto out;
762 } else {
763 if (ul_asc((u_long)VMAGIC, hd->c_magic, sizeof(hd->c_magic),
764 OCT) ||
765 ul_asc((u_long)0L, hd->c_chksum, sizeof(hd->c_chksum),HEX))
766 goto out;
767 }
768
769 t_uid = (anonarch & ANON_UIDGID) ? 0UL : (u_long)arcn->sb.st_uid;
770 t_gid = (anonarch & ANON_UIDGID) ? 0UL : (u_long)arcn->sb.st_gid;
771 t_mtime = (anonarch & ANON_MTIME) ? 0UL : (u_long)arcn->sb.st_mtime;
772 t_ino = (anonarch & ANON_INODES) ? (ino_t)chk_flnk(arcn) :
773 arcn->sb.st_ino;
774 t_major = (anonarch & ANON_INODES) ? 0UL : (u_long)MAJOR(arcn->sb.st_dev);
775 t_minor = (anonarch & ANON_INODES) ? 0UL : (u_long)MINOR(arcn->sb.st_dev);
776 if (!cpio_trail(arcn, NULL, 0, NULL))
777 t_ino = 0UL;
778 if (t_ino == (ino_t)-1) {
779 paxwarn(1, "Invalid inode number for file %s", arcn->org_name);
780 return (1);
781 }
782 if (!(anonarch & ANON_HARDLINKS))
783 arcn->type &= ~PAX_LINKOR;
784
785 switch (arcn->type) {
786 case PAX_CTG:
787 case PAX_REG:
788 case PAX_HRG:
789 /*
790 * caller will copy file data to the archive. tell him how
791 * much to pad.
792 */
793 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
794 if (ot_asc(arcn->sb.st_size, hd->c_filesize,
795 sizeof(hd->c_filesize), HEX)) {
796 paxwarn(1,"File is too large for sv4cpio format %s",
797 arcn->org_name);
798 return(1);
799 }
800 break;
801 case PAX_SLK:
802 /*
803 * no file data for the caller to process, the file data has
804 * the size of the link
805 */
806 arcn->pad = 0L;
807 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
808 sizeof(hd->c_filesize), HEX))
809 goto out;
810 break;
811 default:
812 /*
813 * no file data for the caller to process
814 */
815 arcn->pad = 0L;
816 if (ul_asc((u_long)0L, hd->c_filesize, sizeof(hd->c_filesize),
817 HEX))
818 goto out;
819 break;
820 }
821
822 if (anonarch & ANON_DEBUG)
823 paxwarn(0, "writing dev %lX:%lx inode %10lX mode %8lo user %ld:%ld"
824 "\n\tnlink %3ld mtime %08lX name '%s'", t_major, t_minor,
825 (u_long)t_ino, (u_long)arcn->sb.st_mode, t_uid, t_gid,
826 (u_long)arcn->sb.st_nlink, t_mtime, arcn->name);
827
828 /*
829 * set the other fields in the header
830 */
831 if (ul_asc((u_long)t_ino, hd->c_ino, sizeof(hd->c_ino),
832 HEX) ||
833 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
834 HEX) ||
835 ul_asc(t_uid, hd->c_uid, sizeof(hd->c_uid),
836 HEX) ||
837 ul_asc(t_gid, hd->c_gid, sizeof(hd->c_gid),
838 HEX) ||
839 ul_asc(t_mtime, hd->c_mtime, sizeof(hd->c_mtime),
840 HEX) ||
841 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
842 HEX) ||
843 /* device major:minor of the device the file resides on */
844 ul_asc(t_major, hd->c_maj, sizeof(hd->c_maj), HEX) ||
845 ul_asc(t_minor, hd->c_min, sizeof(hd->c_min), HEX) ||
846 /* device major:minor of the file if it's a device node */
847 ul_asc((u_long)MAJOR(arcn->sb.st_rdev), hd->c_rmaj,
848 sizeof(hd->c_rmaj), HEX) ||
849 ul_asc((u_long)MINOR(arcn->sb.st_rdev), hd->c_rmin,
850 sizeof(hd->c_rmin), HEX) ||
851 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), HEX))
852 goto out;
853
854 /*
855 * write the header, the file name and padding as required.
856 */
857 if ((wr_rdbuf(hdblk, (int)sizeof(HD_VCPIO)) < 0) ||
858 (wr_rdbuf(arcn->name, (int)nsz) < 0) ||
859 (wr_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)) {
860 paxwarn(1,"Could not write sv4cpio header for %s",arcn->org_name);
861 return(-1);
862 }
863
864 /*
865 * if we have file data, tell the caller we are done, copy the file
866 */
867 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
868 (arcn->type == PAX_HRG))
869 return(0);
870
871 /*
872 * if we are a detected hard link, we're done too, but no data written
873 */
874 if (arcn->type & PAX_LINKOR) {
875 arcn->type &= ~PAX_LINKOR;
876 return (1);
877 }
878
879 /*
880 * if we are not a link, tell the caller we are done, go to next file
881 */
882 if (arcn->type != PAX_SLK)
883 return(1);
884
885 /*
886 * write the link name, tell the caller we are done.
887 */
888 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
889 (wr_skip((off_t)(VCPIO_PAD(arcn->ln_nlen))) < 0)) {
890 paxwarn(1,"Could not write sv4cpio link name for %s",
891 arcn->org_name);
892 return(-1);
893 }
894 return(1);
895
896 out:
897 /*
898 * header field is out of range
899 */
900 paxwarn(1,"sv4cpio header field is too small for file %s",arcn->org_name);
901 return(1);
902}
903
904/*
905 * Routines common to the old binary header cpio
906 */
907
908/*
909 * bcpio_id()
910 * determine if a block given to us is a old binary cpio header
911 * (with/without header byte swapping)
912 * Return:
913 * 0 if a valid header, -1 otherwise
914 */
915
916int
917bcpio_id(char *blk, int size)
918{
919 if ((size_t)size < sizeof(HD_BCPIO))
920 return(-1);
921
922 /*
923 * check both normal and byte swapped magic cookies
924 */
925 if (((u_short)SHRT_EXT(blk)) == MAGIC)
926 return(0);
927 if (((u_short)RSHRT_EXT(blk)) == MAGIC) {
928 if (!swp_head)
929 ++swp_head;
930 return(0);
931 }
932 return(-1);
933}
934
935/*
936 * bcpio_rd()
937 * determine if a buffer is a old binary archive entry. (it may have byte
938 * swapped header) convert and store the values in the ARCHD parameter.
939 * This is a very old header format and should not really be used.
940 * Return:
941 * 0 if a valid header, -1 otherwise.
942 */
943
944int
945bcpio_rd(ARCHD *arcn, char *buf)
946{
947 HD_BCPIO *hd;
948 int nsz;
949
950 /*
951 * check the header
952 */
953 if (bcpio_id(buf, sizeof(HD_BCPIO)) < 0)
954 return(-1);
955
956 arcn->pad = 0L;
957 hd = (HD_BCPIO *)buf;
958 if (swp_head) {
959 /*
960 * header has swapped bytes on 16 bit boundaries
961 */
962 arcn->sb.st_dev = (dev_t)(RSHRT_EXT(hd->h_dev));
963 arcn->sb.st_ino = (ino_t)(RSHRT_EXT(hd->h_ino));
964 arcn->sb.st_mode = (mode_t)(RSHRT_EXT(hd->h_mode));
965 arcn->sb.st_uid = (uid_t)(RSHRT_EXT(hd->h_uid));
966 arcn->sb.st_gid = (gid_t)(RSHRT_EXT(hd->h_gid));
967 arcn->sb.st_nlink = (nlink_t)(RSHRT_EXT(hd->h_nlink));
968 arcn->sb.st_rdev = (dev_t)(RSHRT_EXT(hd->h_rdev));
969 arcn->sb.st_mtime = (time_t)(RSHRT_EXT(hd->h_mtime_1));
970 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
971 ((time_t)(RSHRT_EXT(hd->h_mtime_2)));
972 arcn->sb.st_size = (off_t)(RSHRT_EXT(hd->h_filesize_1));
973 arcn->sb.st_size = (arcn->sb.st_size << 16) |
974 ((off_t)(RSHRT_EXT(hd->h_filesize_2)));
975 nsz = (int)(RSHRT_EXT(hd->h_namesize));
976 } else {
977 arcn->sb.st_dev = (dev_t)(SHRT_EXT(hd->h_dev));
978 arcn->sb.st_ino = (ino_t)(SHRT_EXT(hd->h_ino));
979 arcn->sb.st_mode = (mode_t)(SHRT_EXT(hd->h_mode));
980 arcn->sb.st_uid = (uid_t)(SHRT_EXT(hd->h_uid));
981 arcn->sb.st_gid = (gid_t)(SHRT_EXT(hd->h_gid));
982 arcn->sb.st_nlink = (nlink_t)(SHRT_EXT(hd->h_nlink));
983 arcn->sb.st_rdev = (dev_t)(SHRT_EXT(hd->h_rdev));
984 arcn->sb.st_mtime = (time_t)(SHRT_EXT(hd->h_mtime_1));
985 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
986 ((time_t)(SHRT_EXT(hd->h_mtime_2)));
987 arcn->sb.st_size = (off_t)(SHRT_EXT(hd->h_filesize_1));
988 arcn->sb.st_size = (arcn->sb.st_size << 16) |
989 ((off_t)(SHRT_EXT(hd->h_filesize_2)));
990 nsz = (int)(SHRT_EXT(hd->h_namesize));
991 }
992 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
993
994 /*
995 * check the file name size, if bogus give up. otherwise read the file
996 * name
997 */
998 if (nsz < 2)
999 return(-1);
1000 arcn->nlen = nsz - 1;
1001 if (rd_nm(arcn, nsz) < 0)
1002 return(-1);
1003
1004 /*
1005 * header + file name are aligned to 2 byte boundaries, skip if needed
1006 */
1007 if (rd_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)
1008 return(-1);
1009
1010 /*
1011 * if not a link (or a file with no data), calculate pad size (for
1012 * padding which follows the file data), clear the link name and return
1013 */
1014 if (((arcn->sb.st_mode & C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)){
1015 /*
1016 * we have a valid header (not a link)
1017 */
1018 arcn->ln_nlen = 0;
1019 arcn->ln_name[0] = '\0';
1020 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
1021 return(com_rd(arcn));
1022 }
1023
1024 if ((rd_ln_nm(arcn) < 0) ||
1025 (rd_skip((off_t)(BCPIO_PAD(arcn->sb.st_size))) < 0))
1026 return(-1);
1027
1028 /*
1029 * we have a valid header (with a link)
1030 */
1031 return(com_rd(arcn));
1032}
1033
1034/*
1035 * bcpio_endrd()
1036 * no cleanup needed here, just return size of the trailer (for append)
1037 * Return:
1038 * size of trailer header in this format
1039 */
1040
1041off_t
1042bcpio_endrd(void)
1043{
1044 return((off_t)(sizeof(HD_BCPIO) + sizeof(TRAILER) +
1045 (BCPIO_PAD(sizeof(HD_BCPIO) + sizeof(TRAILER)))));
1046}
1047
1048/*
1049 * bcpio_wr()
1050 * copy the data in the ARCHD to buffer in old binary cpio format
1051 * There is a real chance of field overflow with this critter. So we
1052 * always check the conversion is ok. nobody in their right mind
1053 * should write an archive in this format...
1054 * Return
1055 * 0 if file has data to be written after the header, 1 if file has NO
1056 * data to write after the header, -1 if archive write failed
1057 */
1058
1059int
1060bcpio_wr(ARCHD *arcn)
1061{
1062 HD_BCPIO *hd;
1063 int nsz;
1064 char hdblk[sizeof(HD_BCPIO)];
1065 off_t t_offt;
1066 int t_int;
1067 time_t t_timet;
1068
1069 /*
1070 * check and repair truncated device and inode fields in the cpio
1071 * header
1072 */
1073 if (map_dev(arcn, (u_long)BCPIO_MASK, (u_long)BCPIO_MASK) < 0)
1074 return(-1);
1075
1076 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
1077 arcn->sb.st_rdev = 0;
1078 hd = (HD_BCPIO *)hdblk;
1079
1080 switch (arcn->type) {
1081 case PAX_CTG:
1082 case PAX_REG:
1083 case PAX_HRG:
1084 /*
1085 * caller will copy file data to the archive. tell him how
1086 * much to pad.
1087 */
1088 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
1089 hd->h_filesize_1[0] = CHR_WR_0(arcn->sb.st_size);
1090 hd->h_filesize_1[1] = CHR_WR_1(arcn->sb.st_size);
1091 hd->h_filesize_2[0] = CHR_WR_2(arcn->sb.st_size);
1092 hd->h_filesize_2[1] = CHR_WR_3(arcn->sb.st_size);
1093 t_offt = (off_t)(SHRT_EXT(hd->h_filesize_1));
1094 t_offt = (t_offt<<16) | ((off_t)(SHRT_EXT(hd->h_filesize_2)));
1095 if (arcn->sb.st_size != t_offt) {
1096 paxwarn(1,"File is too large for bcpio format %s",
1097 arcn->org_name);
1098 return(1);
1099 }
1100 break;
1101 case PAX_SLK:
1102 /*
1103 * no file data for the caller to process, the file data has
1104 * the size of the link
1105 */
1106 arcn->pad = 0L;
1107 hd->h_filesize_1[0] = CHR_WR_0(arcn->ln_nlen);
1108 hd->h_filesize_1[1] = CHR_WR_1(arcn->ln_nlen);
1109 hd->h_filesize_2[0] = CHR_WR_2(arcn->ln_nlen);
1110 hd->h_filesize_2[1] = CHR_WR_3(arcn->ln_nlen);
1111 t_int = (int)(SHRT_EXT(hd->h_filesize_1));
1112 t_int = (t_int << 16) | ((int)(SHRT_EXT(hd->h_filesize_2)));
1113 if (arcn->ln_nlen != t_int)
1114 goto out;
1115 break;
1116 default:
1117 /*
1118 * no file data for the caller to process
1119 */
1120 arcn->pad = 0L;
1121 hd->h_filesize_1[0] = (char)0;
1122 hd->h_filesize_1[1] = (char)0;
1123 hd->h_filesize_2[0] = (char)0;
1124 hd->h_filesize_2[1] = (char)0;
1125 break;
1126 }
1127
1128 /*
1129 * build up the rest of the fields
1130 */
1131 hd->h_magic[0] = CHR_WR_2(MAGIC);
1132 hd->h_magic[1] = CHR_WR_3(MAGIC);
1133 hd->h_dev[0] = CHR_WR_2(arcn->sb.st_dev);
1134 hd->h_dev[1] = CHR_WR_3(arcn->sb.st_dev);
1135 if (arcn->sb.st_dev != (dev_t)(SHRT_EXT(hd->h_dev)))
1136 goto out;
1137 hd->h_ino[0] = CHR_WR_2(arcn->sb.st_ino);
1138 hd->h_ino[1] = CHR_WR_3(arcn->sb.st_ino);
1139 if (arcn->sb.st_ino != (ino_t)(SHRT_EXT(hd->h_ino)))
1140 goto out;
1141 hd->h_mode[0] = CHR_WR_2(arcn->sb.st_mode);
1142 hd->h_mode[1] = CHR_WR_3(arcn->sb.st_mode);
1143 if (arcn->sb.st_mode != (mode_t)(SHRT_EXT(hd->h_mode)))
1144 goto out;
1145 hd->h_uid[0] = CHR_WR_2(arcn->sb.st_uid);
1146 hd->h_uid[1] = CHR_WR_3(arcn->sb.st_uid);
1147 if (arcn->sb.st_uid != (uid_t)(SHRT_EXT(hd->h_uid)))
1148 goto out;
1149 hd->h_gid[0] = CHR_WR_2(arcn->sb.st_gid);
1150 hd->h_gid[1] = CHR_WR_3(arcn->sb.st_gid);
1151 if (arcn->sb.st_gid != (gid_t)(SHRT_EXT(hd->h_gid)))
1152 goto out;
1153 hd->h_nlink[0] = CHR_WR_2(arcn->sb.st_nlink);
1154 hd->h_nlink[1] = CHR_WR_3(arcn->sb.st_nlink);
1155 if (arcn->sb.st_nlink != (nlink_t)(SHRT_EXT(hd->h_nlink)))
1156 goto out;
1157 hd->h_rdev[0] = CHR_WR_2(arcn->sb.st_rdev);
1158 hd->h_rdev[1] = CHR_WR_3(arcn->sb.st_rdev);
1159 if (arcn->sb.st_rdev != (dev_t)(SHRT_EXT(hd->h_rdev)))
1160 goto out;
1161 hd->h_mtime_1[0] = CHR_WR_0(arcn->sb.st_mtime);
1162 hd->h_mtime_1[1] = CHR_WR_1(arcn->sb.st_mtime);
1163 hd->h_mtime_2[0] = CHR_WR_2(arcn->sb.st_mtime);
1164 hd->h_mtime_2[1] = CHR_WR_3(arcn->sb.st_mtime);
1165 t_timet = (time_t)(SHRT_EXT(hd->h_mtime_1));
1166 t_timet = (t_timet << 16) | ((time_t)(SHRT_EXT(hd->h_mtime_2)));
1167 if (arcn->sb.st_mtime != t_timet)
1168 goto out;
1169 nsz = arcn->nlen + 1;
1170 hd->h_namesize[0] = CHR_WR_2(nsz);
1171 hd->h_namesize[1] = CHR_WR_3(nsz);
1172 if (nsz != (int)(SHRT_EXT(hd->h_namesize)))
1173 goto out;
1174
1175 /*
1176 * write the header, the file name and padding as required.
1177 */
1178 if ((wr_rdbuf(hdblk, (int)sizeof(HD_BCPIO)) < 0) ||
1179 (wr_rdbuf(arcn->name, nsz) < 0) ||
1180 (wr_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)) {
1181 paxwarn(1, "Could not write bcpio header for %s", arcn->org_name);
1182 return(-1);
1183 }
1184
1185 /*
1186 * if we have file data, tell the caller we are done
1187 */
1188 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
1189 (arcn->type == PAX_HRG))
1190 return(0);
1191
1192 /*
1193 * if we are not a link, tell the caller we are done, go to next file
1194 */
1195 if (arcn->type != PAX_SLK)
1196 return(1);
1197
1198 /*
1199 * write the link name, tell the caller we are done.
1200 */
1201 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
1202 (wr_skip((off_t)(BCPIO_PAD(arcn->ln_nlen))) < 0)) {
1203 paxwarn(1,"Could not write bcpio link name for %s",arcn->org_name);
1204 return(-1);
1205 }
1206 return(1);
1207
1208 out:
1209 /*
1210 * header field is out of range
1211 */
1212 paxwarn(1,"Bcpio header field is too small for file %s", arcn->org_name);
1213 return(1);
1214}
Note: See TracBrowser for help on using the repository browser.