]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/cpio.c
71261644769fd92a254e88e418cb41fb491a7815
1 /* $OpenBSD: cpio.c,v 1.18 2008/01/01 16:22:44 tobias Exp $ */
2 /* $NetBSD: cpio.c,v 1.5 1995/03/21 09:07:13 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
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
39 static const char sccsid
[] = "@(#)cpio.c 8.1 (Berkeley) 5/31/93";
41 static const char rcsid
[] = "$OpenBSD: cpio.c,v 1.18 2008/01/01 16:22:44 tobias Exp $";
45 #include <sys/types.h>
48 #include <sys/param.h>
57 static int rd_nm(ARCHD
*, int);
58 static int rd_ln_nm(ARCHD
*);
59 static int com_rd(ARCHD
*);
62 * Routines which support the different cpio versions
65 static int swp_head
; /* binary cpio header byte swap */
68 * Routines common to all versions of cpio
73 * Fire up the hard link detection code
75 * 0 if ok -1 otherwise (the return values of lnk_start())
86 * Called to determine if a header block is a valid trailer. We are
87 * passed the block, the in_sync flag (which tells us we are in resync
88 * mode; looking for a valid header), and cnt (which starts at zero)
89 * which is used to count the number of empty blocks we have seen so far.
91 * 0 if a valid trailer, -1 if not a valid trailer,
95 cpio_trail(ARCHD
*arcn
, char *notused
, int notused2
, int *notused3
)
98 * look for trailer id in file we are about to process
100 if ((strcmp(arcn
->name
, TRAILER
) == 0) && (arcn
->sb
.st_size
== 0))
107 * operations common to all cpio read functions.
117 arcn
->org_name
= arcn
->name
;
118 switch (arcn
->sb
.st_mode
& C_IFMT
) {
120 arcn
->type
= PAX_FIF
;
123 arcn
->type
= PAX_DIR
;
126 arcn
->type
= PAX_BLK
;
129 arcn
->type
= PAX_CHR
;
132 arcn
->type
= PAX_SLK
;
135 arcn
->type
= PAX_SCK
;
141 * we have file data, set up skip (pad is set in the format
144 arcn
->sb
.st_mode
= (arcn
->sb
.st_mode
& 0xfff) | C_ISREG
;
145 arcn
->type
= PAX_REG
;
146 arcn
->skip
= arcn
->sb
.st_size
;
149 if (chk_lnk(arcn
) < 0)
156 * write the special file with the name trailer in the proper format
158 * result of the write of the trailer from the cpio specific write func
167 * create a trailer request and call the proper format write function
169 memset(&last
, 0, sizeof(last
));
170 last
.nlen
= sizeof(TRAILER
) - 1;
172 last
.sb
.st_nlink
= 1;
173 (void)strlcpy(last
.name
, TRAILER
, sizeof(last
.name
));
174 return((*frmt
->wr
)(&last
));
179 * read in the file name which follows the cpio header
181 * 0 if ok, -1 otherwise
185 rd_nm(ARCHD
*arcn
, int nsz
)
188 * do not even try bogus values
190 if ((nsz
== 0) || (nsz
> sizeof(arcn
->name
))) {
191 paxwarn(1, "Cpio file name length %d is out of range", nsz
);
196 * read the name and make sure it is not empty and is \0 terminated
198 if ((rd_wrbuf(arcn
->name
,nsz
) != nsz
) || (arcn
->name
[nsz
-1] != '\0') ||
199 (arcn
->name
[0] == '\0')) {
200 paxwarn(1, "Cpio file name in header is corrupted");
208 * read in the link name for a file with links. The link name is stored
209 * like file data (and is NOT \0 terminated!)
211 * 0 if ok, -1 otherwise
215 rd_ln_nm(ARCHD
*arcn
)
218 * check the length specified for bogus values
220 if ((arcn
->sb
.st_size
== 0) ||
221 (arcn
->sb
.st_size
>= sizeof(arcn
->ln_name
))) {
223 paxwarn(1, "Cpio link name length is invalid: %lu",
226 paxwarn(1, "Cpio link name length is invalid: %qu",
233 * read in the link name and \0 terminate it
235 if (rd_wrbuf(arcn
->ln_name
, (int)arcn
->sb
.st_size
) !=
236 (int)arcn
->sb
.st_size
) {
237 paxwarn(1, "Cpio link name read error");
240 arcn
->ln_nlen
= (int)arcn
->sb
.st_size
;
241 arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
244 * watch out for those empty link names
246 if (arcn
->ln_name
[0] == '\0') {
247 paxwarn(1, "Cpio link name is corrupt");
254 * Routines common to the extended byte oriented cpio format
259 * determine if a block given to us is a valid extended byte oriented
262 * 0 if a valid header, -1 otherwise
266 cpio_id(char *blk
, int size
)
268 if ((size
< sizeof(HD_CPIO
)) ||
269 (strncmp(blk
, AMAGIC
, sizeof(AMAGIC
) - 1) != 0))
276 * determine if a buffer is a byte oriented extended cpio archive entry.
277 * convert and store the values in the ARCHD parameter.
279 * 0 if a valid header, -1 otherwise.
283 cpio_rd(ARCHD
*arcn
, char *buf
)
289 * check that this is a valid header, if not return -1
291 if (cpio_id(buf
, sizeof(HD_CPIO
)) < 0)
296 * byte oriented cpio (posix) does not have padding! extract the octal
297 * ascii fields from the header
300 arcn
->sb
.st_dev
= (dev_t
)asc_ul(hd
->c_dev
, sizeof(hd
->c_dev
), OCT
);
301 arcn
->sb
.st_ino
= (ino_t
)asc_ul(hd
->c_ino
, sizeof(hd
->c_ino
), OCT
);
302 arcn
->sb
.st_mode
= (mode_t
)asc_ul(hd
->c_mode
, sizeof(hd
->c_mode
), OCT
);
303 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->c_uid
, sizeof(hd
->c_uid
), OCT
);
304 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->c_gid
, sizeof(hd
->c_gid
), OCT
);
305 arcn
->sb
.st_nlink
= (nlink_t
)asc_ul(hd
->c_nlink
, sizeof(hd
->c_nlink
),
307 arcn
->sb
.st_rdev
= (dev_t
)asc_ul(hd
->c_rdev
, sizeof(hd
->c_rdev
), OCT
);
308 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->c_mtime
, sizeof(hd
->c_mtime
),
310 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
312 arcn
->sb
.st_size
= (off_t
)asc_ul(hd
->c_filesize
,sizeof(hd
->c_filesize
),
315 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->c_filesize
,sizeof(hd
->c_filesize
),
320 * check name size and if valid, read in the name of this entry (name
321 * follows header in the archive)
323 if ((nsz
= (int)asc_ul(hd
->c_namesize
,sizeof(hd
->c_namesize
),OCT
)) < 2)
325 arcn
->nlen
= nsz
- 1;
326 if (rd_nm(arcn
, nsz
) < 0)
329 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
331 * no link name to read for this file
334 arcn
->ln_name
[0] = '\0';
335 return(com_rd(arcn
));
339 * check link name size and read in the link name. Link names are
340 * stored like file data.
342 if (rd_ln_nm(arcn
) < 0)
346 * we have a valid header (with a link)
348 return(com_rd(arcn
));
353 * no cleanup needed here, just return size of the trailer (for append)
355 * size of trailer header in this format
361 return((off_t
)(sizeof(HD_CPIO
) + sizeof(TRAILER
)));
366 * start up the device mapping table
368 * 0 if ok, -1 otherwise (what dev_start() returns)
379 * copy the data in the ARCHD to buffer in extended byte oriented cpio
382 * 0 if file has data to be written after the header, 1 if file has NO
383 * data to write after the header, -1 if archive write failed
391 char hdblk
[sizeof(HD_CPIO
)];
394 * check and repair truncated device and inode fields in the header
396 if (map_dev(arcn
, (u_long
)CPIO_MASK
, (u_long
)CPIO_MASK
) < 0)
400 nsz
= arcn
->nlen
+ 1;
401 hd
= (HD_CPIO
*)hdblk
;
402 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
403 arcn
->sb
.st_rdev
= 0;
405 switch (arcn
->type
) {
410 * set data size for file data
413 if (ul_asc((u_long
)arcn
->sb
.st_size
, hd
->c_filesize
,
414 sizeof(hd
->c_filesize
), OCT
)) {
416 if (uqd_asc((u_quad_t
)arcn
->sb
.st_size
, hd
->c_filesize
,
417 sizeof(hd
->c_filesize
), OCT
)) {
419 paxwarn(1,"File is too large for cpio format %s",
426 * set data size to hold link name
428 if (ul_asc((u_long
)arcn
->ln_nlen
, hd
->c_filesize
,
429 sizeof(hd
->c_filesize
), OCT
))
434 * all other file types have no file data
436 if (ul_asc((u_long
)0, hd
->c_filesize
, sizeof(hd
->c_filesize
),
443 * copy the values to the header using octal ascii
445 if (ul_asc((u_long
)MAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
), OCT
) ||
446 ul_asc((u_long
)arcn
->sb
.st_dev
, hd
->c_dev
, sizeof(hd
->c_dev
),
448 ul_asc((u_long
)arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
),
450 ul_asc((u_long
)arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
),
452 ul_asc((u_long
)arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
),
454 ul_asc((u_long
)arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
),
456 ul_asc((u_long
)arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
),
458 ul_asc((u_long
)arcn
->sb
.st_rdev
, hd
->c_rdev
, sizeof(hd
->c_rdev
),
460 ul_asc((u_long
)arcn
->sb
.st_mtime
,hd
->c_mtime
,sizeof(hd
->c_mtime
),
462 ul_asc((u_long
)nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), OCT
))
466 * write the file name to the archive
468 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_CPIO
)) < 0) ||
469 (wr_rdbuf(arcn
->name
, nsz
) < 0)) {
470 paxwarn(1, "Unable to write cpio header for %s", arcn
->org_name
);
475 * if this file has data, we are done. The caller will write the file
476 * data, if we are link tell caller we are done, go to next file
478 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
479 (arcn
->type
== PAX_HRG
))
481 if (arcn
->type
!= PAX_SLK
)
485 * write the link name to the archive, tell the caller to go to the
486 * next file as we are done.
488 if (wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) {
489 paxwarn(1,"Unable to write cpio link name for %s",arcn
->org_name
);
496 * header field is out of range
498 paxwarn(1, "Cpio header field is too small to store file %s",
504 * Routines common to the system VR4 version of cpio (with/without file CRC)
509 * determine if a block given to us is a valid system VR4 cpio header
510 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
513 * 0 if a valid header, -1 otherwise
517 vcpio_id(char *blk
, int size
)
519 if ((size
< sizeof(HD_VCPIO
)) ||
520 (strncmp(blk
, AVMAGIC
, sizeof(AVMAGIC
) - 1) != 0))
527 * determine if a block given to us is a valid system VR4 cpio header
528 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
530 * 0 if a valid header, -1 otherwise
534 crc_id(char *blk
, int size
)
536 if ((size
< sizeof(HD_VCPIO
)) ||
537 (strncmp(blk
, AVCMAGIC
, sizeof(AVCMAGIC
) - 1) != 0))
544 w set file data CRC calculations. Fire up the hard link detection code
546 * 0 if ok -1 otherwise (the return values of lnk_start())
558 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
559 * convert and store the values in the ARCHD parameter.
561 * 0 if a valid header, -1 otherwise.
565 vcpio_rd(ARCHD
*arcn
, char *buf
)
573 * during the id phase it was determined if we were using CRC, use the
577 if (crc_id(buf
, sizeof(HD_VCPIO
)) < 0)
580 if (vcpio_id(buf
, sizeof(HD_VCPIO
)) < 0)
584 hd
= (HD_VCPIO
*)buf
;
588 * extract the hex ascii fields from the header
590 arcn
->sb
.st_ino
= (ino_t
)asc_ul(hd
->c_ino
, sizeof(hd
->c_ino
), HEX
);
591 arcn
->sb
.st_mode
= (mode_t
)asc_ul(hd
->c_mode
, sizeof(hd
->c_mode
), HEX
);
592 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->c_uid
, sizeof(hd
->c_uid
), HEX
);
593 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->c_gid
, sizeof(hd
->c_gid
), HEX
);
594 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->c_mtime
,sizeof(hd
->c_mtime
),HEX
);
595 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
597 arcn
->sb
.st_size
= (off_t
)asc_ul(hd
->c_filesize
,
598 sizeof(hd
->c_filesize
), HEX
);
600 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->c_filesize
,
601 sizeof(hd
->c_filesize
), HEX
);
603 arcn
->sb
.st_nlink
= (nlink_t
)asc_ul(hd
->c_nlink
, sizeof(hd
->c_nlink
),
605 devmajor
= (dev_t
)asc_ul(hd
->c_maj
, sizeof(hd
->c_maj
), HEX
);
606 devminor
= (dev_t
)asc_ul(hd
->c_min
, sizeof(hd
->c_min
), HEX
);
607 arcn
->sb
.st_dev
= TODEV(devmajor
, devminor
);
608 devmajor
= (dev_t
)asc_ul(hd
->c_rmaj
, sizeof(hd
->c_maj
), HEX
);
609 devminor
= (dev_t
)asc_ul(hd
->c_rmin
, sizeof(hd
->c_min
), HEX
);
610 arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
611 arcn
->crc
= asc_ul(hd
->c_chksum
, sizeof(hd
->c_chksum
), HEX
);
614 * check the length of the file name, if ok read it in, return -1 if
617 if ((nsz
= (int)asc_ul(hd
->c_namesize
,sizeof(hd
->c_namesize
),HEX
)) < 2)
619 arcn
->nlen
= nsz
- 1;
620 if (rd_nm(arcn
, nsz
) < 0)
624 * skip padding. header + filename is aligned to 4 byte boundaries
626 if (rd_skip((off_t
)(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
))) < 0)
630 * if not a link (or a file with no data), calculate pad size (for
631 * padding which follows the file data), clear the link name and return
633 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
635 * we have a valid header (not a link)
638 arcn
->ln_name
[0] = '\0';
639 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
640 return(com_rd(arcn
));
644 * read in the link name and skip over the padding
646 if ((rd_ln_nm(arcn
) < 0) ||
647 (rd_skip((off_t
)(VCPIO_PAD(arcn
->sb
.st_size
))) < 0))
651 * we have a valid header (with a link)
653 return(com_rd(arcn
));
658 * no cleanup needed here, just return size of the trailer (for append)
660 * size of trailer header in this format
666 return((off_t
)(sizeof(HD_VCPIO
) + sizeof(TRAILER
) +
667 (VCPIO_PAD(sizeof(HD_VCPIO
) + sizeof(TRAILER
)))));
672 * start up the device mapping table, enable crc file calculation
674 * 0 if ok, -1 otherwise (what dev_start() returns)
686 * copy the data in the ARCHD to buffer in system VR4 cpio
687 * (with/without crc) format.
689 * 0 if file has data to be written after the header, 1 if file has
690 * NO data to write after the header, -1 if archive write failed
694 vcpio_wr(ARCHD
*arcn
)
698 char hdblk
[sizeof(HD_VCPIO
)];
701 * check and repair truncated device and inode fields in the cpio
704 if (map_dev(arcn
, (u_long
)VCPIO_MASK
, (u_long
)VCPIO_MASK
) < 0)
706 nsz
= arcn
->nlen
+ 1;
707 hd
= (HD_VCPIO
*)hdblk
;
708 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
709 arcn
->sb
.st_rdev
= 0;
712 * add the proper magic value depending whether we were asked for
713 * file data crc's, and the crc if needed.
716 if (ul_asc((u_long
)VCMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
),
718 ul_asc((u_long
)arcn
->crc
,hd
->c_chksum
,sizeof(hd
->c_chksum
),
722 if (ul_asc((u_long
)VMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
),
724 ul_asc((u_long
)0L, hd
->c_chksum
, sizeof(hd
->c_chksum
),HEX
))
728 switch (arcn
->type
) {
733 * caller will copy file data to the archive. tell him how
736 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
738 if (ul_asc((u_long
)arcn
->sb
.st_size
, hd
->c_filesize
,
739 sizeof(hd
->c_filesize
), HEX
)) {
741 if (uqd_asc((u_quad_t
)arcn
->sb
.st_size
, hd
->c_filesize
,
742 sizeof(hd
->c_filesize
), HEX
)) {
744 paxwarn(1,"File is too large for sv4cpio format %s",
751 * no file data for the caller to process, the file data has
752 * the size of the link
755 if (ul_asc((u_long
)arcn
->ln_nlen
, hd
->c_filesize
,
756 sizeof(hd
->c_filesize
), HEX
))
761 * no file data for the caller to process
764 if (ul_asc((u_long
)0L, hd
->c_filesize
, sizeof(hd
->c_filesize
),
771 * set the other fields in the header
773 if (ul_asc((u_long
)arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
),
775 ul_asc((u_long
)arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
),
777 ul_asc((u_long
)arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
),
779 ul_asc((u_long
)arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
),
781 ul_asc((u_long
)arcn
->sb
.st_mtime
, hd
->c_mtime
, sizeof(hd
->c_mtime
),
783 ul_asc((u_long
)arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
),
785 ul_asc((u_long
)MAJOR(arcn
->sb
.st_dev
),hd
->c_maj
, sizeof(hd
->c_maj
),
787 ul_asc((u_long
)MINOR(arcn
->sb
.st_dev
),hd
->c_min
, sizeof(hd
->c_min
),
789 ul_asc((u_long
)MAJOR(arcn
->sb
.st_rdev
),hd
->c_rmaj
,sizeof(hd
->c_maj
),
791 ul_asc((u_long
)MINOR(arcn
->sb
.st_rdev
),hd
->c_rmin
,sizeof(hd
->c_min
),
793 ul_asc((u_long
)nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), HEX
))
797 * write the header, the file name and padding as required.
799 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_VCPIO
)) < 0) ||
800 (wr_rdbuf(arcn
->name
, (int)nsz
) < 0) ||
801 (wr_skip((off_t
)(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
))) < 0)) {
802 paxwarn(1,"Could not write sv4cpio header for %s",arcn
->org_name
);
807 * if we have file data, tell the caller we are done, copy the file
809 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
810 (arcn
->type
== PAX_HRG
))
814 * if we are not a link, tell the caller we are done, go to next file
816 if (arcn
->type
!= PAX_SLK
)
820 * write the link name, tell the caller we are done.
822 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
823 (wr_skip((off_t
)(VCPIO_PAD(arcn
->ln_nlen
))) < 0)) {
824 paxwarn(1,"Could not write sv4cpio link name for %s",
832 * header field is out of range
834 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn
->org_name
);
839 * Routines common to the old binary header cpio
844 * determine if a block given to us is a old binary cpio header
845 * (with/without header byte swapping)
847 * 0 if a valid header, -1 otherwise
851 bcpio_id(char *blk
, int size
)
853 if (size
< sizeof(HD_BCPIO
))
857 * check both normal and byte swapped magic cookies
859 if (((u_short
)SHRT_EXT(blk
)) == MAGIC
)
861 if (((u_short
)RSHRT_EXT(blk
)) == MAGIC
) {
871 * determine if a buffer is a old binary archive entry. (it may have byte
872 * swapped header) convert and store the values in the ARCHD parameter.
873 * This is a very old header format and should not really be used.
875 * 0 if a valid header, -1 otherwise.
879 bcpio_rd(ARCHD
*arcn
, char *buf
)
887 if (bcpio_id(buf
, sizeof(HD_BCPIO
)) < 0)
891 hd
= (HD_BCPIO
*)buf
;
894 * header has swapped bytes on 16 bit boundaries
896 arcn
->sb
.st_dev
= (dev_t
)(RSHRT_EXT(hd
->h_dev
));
897 arcn
->sb
.st_ino
= (ino_t
)(RSHRT_EXT(hd
->h_ino
));
898 arcn
->sb
.st_mode
= (mode_t
)(RSHRT_EXT(hd
->h_mode
));
899 arcn
->sb
.st_uid
= (uid_t
)(RSHRT_EXT(hd
->h_uid
));
900 arcn
->sb
.st_gid
= (gid_t
)(RSHRT_EXT(hd
->h_gid
));
901 arcn
->sb
.st_nlink
= (nlink_t
)(RSHRT_EXT(hd
->h_nlink
));
902 arcn
->sb
.st_rdev
= (dev_t
)(RSHRT_EXT(hd
->h_rdev
));
903 arcn
->sb
.st_mtime
= (time_t)(RSHRT_EXT(hd
->h_mtime_1
));
904 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
905 ((time_t)(RSHRT_EXT(hd
->h_mtime_2
)));
906 arcn
->sb
.st_size
= (off_t
)(RSHRT_EXT(hd
->h_filesize_1
));
907 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
908 ((off_t
)(RSHRT_EXT(hd
->h_filesize_2
)));
909 nsz
= (int)(RSHRT_EXT(hd
->h_namesize
));
911 arcn
->sb
.st_dev
= (dev_t
)(SHRT_EXT(hd
->h_dev
));
912 arcn
->sb
.st_ino
= (ino_t
)(SHRT_EXT(hd
->h_ino
));
913 arcn
->sb
.st_mode
= (mode_t
)(SHRT_EXT(hd
->h_mode
));
914 arcn
->sb
.st_uid
= (uid_t
)(SHRT_EXT(hd
->h_uid
));
915 arcn
->sb
.st_gid
= (gid_t
)(SHRT_EXT(hd
->h_gid
));
916 arcn
->sb
.st_nlink
= (nlink_t
)(SHRT_EXT(hd
->h_nlink
));
917 arcn
->sb
.st_rdev
= (dev_t
)(SHRT_EXT(hd
->h_rdev
));
918 arcn
->sb
.st_mtime
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
919 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
920 ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
921 arcn
->sb
.st_size
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
922 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
923 ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
924 nsz
= (int)(SHRT_EXT(hd
->h_namesize
));
926 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
929 * check the file name size, if bogus give up. otherwise read the file
934 arcn
->nlen
= nsz
- 1;
935 if (rd_nm(arcn
, nsz
) < 0)
939 * header + file name are aligned to 2 byte boundaries, skip if needed
941 if (rd_skip((off_t
)(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
))) < 0)
945 * if not a link (or a file with no data), calculate pad size (for
946 * padding which follows the file data), clear the link name and return
948 if (((arcn
->sb
.st_mode
& C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)){
950 * we have a valid header (not a link)
953 arcn
->ln_name
[0] = '\0';
954 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
955 return(com_rd(arcn
));
958 if ((rd_ln_nm(arcn
) < 0) ||
959 (rd_skip((off_t
)(BCPIO_PAD(arcn
->sb
.st_size
))) < 0))
963 * we have a valid header (with a link)
965 return(com_rd(arcn
));
970 * no cleanup needed here, just return size of the trailer (for append)
972 * size of trailer header in this format
978 return((off_t
)(sizeof(HD_BCPIO
) + sizeof(TRAILER
) +
979 (BCPIO_PAD(sizeof(HD_BCPIO
) + sizeof(TRAILER
)))));
984 * copy the data in the ARCHD to buffer in old binary cpio format
985 * There is a real chance of field overflow with this critter. So we
986 * always check the conversion is ok. nobody in their right mind
987 * should write an archive in this format...
989 * 0 if file has data to be written after the header, 1 if file has NO
990 * data to write after the header, -1 if archive write failed
994 bcpio_wr(ARCHD
*arcn
)
998 char hdblk
[sizeof(HD_BCPIO
)];
1004 * check and repair truncated device and inode fields in the cpio
1007 if (map_dev(arcn
, (u_long
)BCPIO_MASK
, (u_long
)BCPIO_MASK
) < 0)
1010 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
1011 arcn
->sb
.st_rdev
= 0;
1012 hd
= (HD_BCPIO
*)hdblk
;
1014 switch (arcn
->type
) {
1019 * caller will copy file data to the archive. tell him how
1022 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
1023 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->sb
.st_size
);
1024 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->sb
.st_size
);
1025 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->sb
.st_size
);
1026 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->sb
.st_size
);
1027 t_offt
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
1028 t_offt
= (t_offt
<<16) | ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
1029 if (arcn
->sb
.st_size
!= t_offt
) {
1030 paxwarn(1,"File is too large for bcpio format %s",
1037 * no file data for the caller to process, the file data has
1038 * the size of the link
1041 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->ln_nlen
);
1042 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->ln_nlen
);
1043 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->ln_nlen
);
1044 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->ln_nlen
);
1045 t_int
= (int)(SHRT_EXT(hd
->h_filesize_1
));
1046 t_int
= (t_int
<< 16) | ((int)(SHRT_EXT(hd
->h_filesize_2
)));
1047 if (arcn
->ln_nlen
!= t_int
)
1052 * no file data for the caller to process
1055 hd
->h_filesize_1
[0] = (char)0;
1056 hd
->h_filesize_1
[1] = (char)0;
1057 hd
->h_filesize_2
[0] = (char)0;
1058 hd
->h_filesize_2
[1] = (char)0;
1063 * build up the rest of the fields
1065 hd
->h_magic
[0] = CHR_WR_2(MAGIC
);
1066 hd
->h_magic
[1] = CHR_WR_3(MAGIC
);
1067 hd
->h_dev
[0] = CHR_WR_2(arcn
->sb
.st_dev
);
1068 hd
->h_dev
[1] = CHR_WR_3(arcn
->sb
.st_dev
);
1069 if (arcn
->sb
.st_dev
!= (dev_t
)(SHRT_EXT(hd
->h_dev
)))
1071 hd
->h_ino
[0] = CHR_WR_2(arcn
->sb
.st_ino
);
1072 hd
->h_ino
[1] = CHR_WR_3(arcn
->sb
.st_ino
);
1073 if (arcn
->sb
.st_ino
!= (ino_t
)(SHRT_EXT(hd
->h_ino
)))
1075 hd
->h_mode
[0] = CHR_WR_2(arcn
->sb
.st_mode
);
1076 hd
->h_mode
[1] = CHR_WR_3(arcn
->sb
.st_mode
);
1077 if (arcn
->sb
.st_mode
!= (mode_t
)(SHRT_EXT(hd
->h_mode
)))
1079 hd
->h_uid
[0] = CHR_WR_2(arcn
->sb
.st_uid
);
1080 hd
->h_uid
[1] = CHR_WR_3(arcn
->sb
.st_uid
);
1081 if (arcn
->sb
.st_uid
!= (uid_t
)(SHRT_EXT(hd
->h_uid
)))
1083 hd
->h_gid
[0] = CHR_WR_2(arcn
->sb
.st_gid
);
1084 hd
->h_gid
[1] = CHR_WR_3(arcn
->sb
.st_gid
);
1085 if (arcn
->sb
.st_gid
!= (gid_t
)(SHRT_EXT(hd
->h_gid
)))
1087 hd
->h_nlink
[0] = CHR_WR_2(arcn
->sb
.st_nlink
);
1088 hd
->h_nlink
[1] = CHR_WR_3(arcn
->sb
.st_nlink
);
1089 if (arcn
->sb
.st_nlink
!= (nlink_t
)(SHRT_EXT(hd
->h_nlink
)))
1091 hd
->h_rdev
[0] = CHR_WR_2(arcn
->sb
.st_rdev
);
1092 hd
->h_rdev
[1] = CHR_WR_3(arcn
->sb
.st_rdev
);
1093 if (arcn
->sb
.st_rdev
!= (dev_t
)(SHRT_EXT(hd
->h_rdev
)))
1095 hd
->h_mtime_1
[0] = CHR_WR_0(arcn
->sb
.st_mtime
);
1096 hd
->h_mtime_1
[1] = CHR_WR_1(arcn
->sb
.st_mtime
);
1097 hd
->h_mtime_2
[0] = CHR_WR_2(arcn
->sb
.st_mtime
);
1098 hd
->h_mtime_2
[1] = CHR_WR_3(arcn
->sb
.st_mtime
);
1099 t_timet
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
1100 t_timet
= (t_timet
<< 16) | ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
1101 if (arcn
->sb
.st_mtime
!= t_timet
)
1103 nsz
= arcn
->nlen
+ 1;
1104 hd
->h_namesize
[0] = CHR_WR_2(nsz
);
1105 hd
->h_namesize
[1] = CHR_WR_3(nsz
);
1106 if (nsz
!= (int)(SHRT_EXT(hd
->h_namesize
)))
1110 * write the header, the file name and padding as required.
1112 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_BCPIO
)) < 0) ||
1113 (wr_rdbuf(arcn
->name
, nsz
) < 0) ||
1114 (wr_skip((off_t
)(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
))) < 0)) {
1115 paxwarn(1, "Could not write bcpio header for %s", arcn
->org_name
);
1120 * if we have file data, tell the caller we are done
1122 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
1123 (arcn
->type
== PAX_HRG
))
1127 * if we are not a link, tell the caller we are done, go to next file
1129 if (arcn
->type
!= PAX_SLK
)
1133 * write the link name, tell the caller we are done.
1135 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
1136 (wr_skip((off_t
)(BCPIO_PAD(arcn
->ln_nlen
))) < 0)) {
1137 paxwarn(1,"Could not write bcpio link name for %s",arcn
->org_name
);
1144 * header field is out of range
1146 paxwarn(1,"Bcpio header field is too small for file %s", arcn
->org_name
);