]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/cpio.c
43feb649a85a45c805c74ca095efa4e05389daec
1 /* $OpenBSD: cpio.c,v 1.5 1997/07/25 18:58:28 mickey 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid
[] = "@(#)cpio.c 8.1 (Berkeley) 5/31/93";
45 static char rcsid
[] __attribute__((__unused__
)) = "$OpenBSD: cpio.c,v 1.5 1997/07/25 18:58:28 mickey Exp $";
49 #include <sys/types.h>
52 #include <sys/param.h>
61 static int rd_nm
__P((register ARCHD
*, int));
62 static int rd_ln_nm
__P((register ARCHD
*));
63 static int com_rd
__P((register ARCHD
*));
66 * Routines which support the different cpio versions
69 static int swp_head
; /* binary cpio header byte swap */
72 * Routines common to all versions of cpio
77 * Fire up the hard link detection code
79 * 0 if ok -1 otherwise (the return values of lnk_start())
95 * Called to determine if a header block is a valid trailer. We are
96 * passed the block, the in_sync flag (which tells us we are in resync
97 * mode; looking for a valid header), and cnt (which starts at zero)
98 * which is used to count the number of empty blocks we have seen so far.
100 * 0 if a valid trailer, -1 if not a valid trailer,
105 cpio_trail(register ARCHD
*arcn
)
109 register ARCHD
*arcn
;
113 * look for trailer id in file we are about to process
115 if ((strcmp(arcn
->name
, TRAILER
) == 0) && (arcn
->sb
.st_size
== 0))
122 * operations common to all cpio read functions.
129 com_rd(register ARCHD
*arcn
)
133 register ARCHD
*arcn
;
138 arcn
->org_name
= arcn
->name
;
139 switch(arcn
->sb
.st_mode
& C_IFMT
) {
141 arcn
->type
= PAX_FIF
;
144 arcn
->type
= PAX_DIR
;
147 arcn
->type
= PAX_BLK
;
150 arcn
->type
= PAX_CHR
;
153 arcn
->type
= PAX_SLK
;
156 arcn
->type
= PAX_SCK
;
162 * we have file data, set up skip (pad is set in the format
165 arcn
->sb
.st_mode
= (arcn
->sb
.st_mode
& 0xfff) | C_ISREG
;
166 arcn
->type
= PAX_REG
;
167 arcn
->skip
= arcn
->sb
.st_size
;
170 if (chk_lnk(arcn
) < 0)
177 * write the special file with the name trailer in the proper format
179 * result of the write of the trailer from the cpio specific write func
193 * create a trailer request and call the proper format write function
195 memset(&last
, 0, sizeof(last
));
196 last
.nlen
= sizeof(TRAILER
) - 1;
198 last
.sb
.st_nlink
= 1;
199 (void)strcpy(last
.name
, TRAILER
);
200 return((*frmt
->wr
)(&last
));
205 * read in the file name which follows the cpio header
207 * 0 if ok, -1 otherwise
212 rd_nm(register ARCHD
*arcn
, int nsz
)
216 register ARCHD
*arcn
;
221 * do not even try bogus values
223 if ((nsz
== 0) || (nsz
> sizeof(arcn
->name
))) {
224 paxwarn(1, "Cpio file name length %d is out of range", nsz
);
229 * read the name and make sure it is not empty and is \0 terminated
231 if ((rd_wrbuf(arcn
->name
,nsz
) != nsz
) || (arcn
->name
[nsz
-1] != '\0') ||
232 (arcn
->name
[0] == '\0')) {
233 paxwarn(1, "Cpio file name in header is corrupted");
241 * read in the link name for a file with links. The link name is stored
242 * like file data (and is NOT \0 terminated!)
244 * 0 if ok, -1 otherwise
249 rd_ln_nm(register ARCHD
*arcn
)
253 register ARCHD
*arcn
;
257 * check the length specified for bogus values
259 if ((arcn
->sb
.st_size
== 0) ||
260 (arcn
->sb
.st_size
>= sizeof(arcn
->ln_name
))) {
262 paxwarn(1, "Cpio link name length is invalid: %lu",
265 paxwarn(1, "Cpio link name length is invalid: %qu",
272 * read in the link name and \0 terminate it
274 if (rd_wrbuf(arcn
->ln_name
, (int)arcn
->sb
.st_size
) !=
275 (int)arcn
->sb
.st_size
) {
276 paxwarn(1, "Cpio link name read error");
279 arcn
->ln_nlen
= arcn
->sb
.st_size
;
280 arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
283 * watch out for those empty link names
285 if (arcn
->ln_name
[0] == '\0') {
286 paxwarn(1, "Cpio link name is corrupt");
293 * Routines common to the extended byte oriented cpio format
298 * determine if a block given to us is a valid extended byte oriented
301 * 0 if a valid header, -1 otherwise
306 cpio_id(char *blk
, int size
)
314 if ((size
< sizeof(HD_CPIO
)) ||
315 (strncmp(blk
, AMAGIC
, sizeof(AMAGIC
) - 1) != 0))
322 * determine if a buffer is a byte oriented extended cpio archive entry.
323 * convert and store the values in the ARCHD parameter.
325 * 0 if a valid header, -1 otherwise.
330 cpio_rd(register ARCHD
*arcn
, register char *buf
)
334 register ARCHD
*arcn
;
339 register HD_CPIO
*hd
;
342 * check that this is a valid header, if not return -1
344 if (cpio_id(buf
, sizeof(HD_CPIO
)) < 0)
349 * byte oriented cpio (posix) does not have padding! extract the octal
350 * ascii fields from the header
353 arcn
->sb
.st_dev
= (dev_t
)asc_ul(hd
->c_dev
, sizeof(hd
->c_dev
), OCT
);
354 arcn
->sb
.st_ino
= (ino_t
)asc_ul(hd
->c_ino
, sizeof(hd
->c_ino
), OCT
);
355 arcn
->sb
.st_mode
= (mode_t
)asc_ul(hd
->c_mode
, sizeof(hd
->c_mode
), OCT
);
356 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->c_uid
, sizeof(hd
->c_uid
), OCT
);
357 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->c_gid
, sizeof(hd
->c_gid
), OCT
);
358 arcn
->sb
.st_nlink
= (nlink_t
)asc_ul(hd
->c_nlink
, sizeof(hd
->c_nlink
),
360 arcn
->sb
.st_rdev
= (dev_t
)asc_ul(hd
->c_rdev
, sizeof(hd
->c_rdev
), OCT
);
361 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->c_mtime
, sizeof(hd
->c_mtime
),
363 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
365 arcn
->sb
.st_size
= (off_t
)asc_ul(hd
->c_filesize
,sizeof(hd
->c_filesize
),
368 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->c_filesize
,sizeof(hd
->c_filesize
),
373 * check name size and if valid, read in the name of this entry (name
374 * follows header in the archive)
376 if ((nsz
= (int)asc_ul(hd
->c_namesize
,sizeof(hd
->c_namesize
),OCT
)) < 2)
378 arcn
->nlen
= nsz
- 1;
379 if (rd_nm(arcn
, nsz
) < 0)
382 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
384 * no link name to read for this file
387 arcn
->ln_name
[0] = '\0';
388 return(com_rd(arcn
));
392 * check link name size and read in the link name. Link names are
393 * stored like file data.
395 if (rd_ln_nm(arcn
) < 0)
399 * we have a valid header (with a link)
401 return(com_rd(arcn
));
406 * no cleanup needed here, just return size of the trailer (for append)
408 * size of trailer header in this format
419 return((off_t
)(sizeof(HD_CPIO
) + sizeof(TRAILER
)));
424 * start up the device mapping table
426 * 0 if ok, -1 otherwise (what dev_start() returns)
442 * copy the data in the ARCHD to buffer in extended byte oriented cpio
445 * 0 if file has data to be written after the header, 1 if file has NO
446 * data to write after the header, -1 if archive write failed
451 cpio_wr(register ARCHD
*arcn
)
455 register ARCHD
*arcn
;
458 register HD_CPIO
*hd
;
460 char hdblk
[sizeof(HD_CPIO
)];
463 * check and repair truncated device and inode fields in the header
465 if (map_dev(arcn
, (u_long
)CPIO_MASK
, (u_long
)CPIO_MASK
) < 0)
469 nsz
= arcn
->nlen
+ 1;
470 hd
= (HD_CPIO
*)hdblk
;
471 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
472 arcn
->sb
.st_rdev
= 0;
479 * set data size for file data
482 if (ul_asc((u_long
)arcn
->sb
.st_size
, hd
->c_filesize
,
483 sizeof(hd
->c_filesize
), OCT
)) {
485 if (uqd_asc((u_quad_t
)arcn
->sb
.st_size
, hd
->c_filesize
,
486 sizeof(hd
->c_filesize
), OCT
)) {
488 paxwarn(1,"File is too large for cpio format %s",
495 * set data size to hold link name
497 if (ul_asc((u_long
)arcn
->ln_nlen
, hd
->c_filesize
,
498 sizeof(hd
->c_filesize
), OCT
))
503 * all other file types have no file data
505 if (ul_asc((u_long
)0, hd
->c_filesize
, sizeof(hd
->c_filesize
),
512 * copy the values to the header using octal ascii
514 if (ul_asc((u_long
)MAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
), OCT
) ||
515 ul_asc((u_long
)arcn
->sb
.st_dev
, hd
->c_dev
, sizeof(hd
->c_dev
),
517 ul_asc((u_long
)arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
),
519 ul_asc((u_long
)arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
),
521 ul_asc((u_long
)arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
),
523 ul_asc((u_long
)arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
),
525 ul_asc((u_long
)arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
),
527 ul_asc((u_long
)arcn
->sb
.st_rdev
, hd
->c_rdev
, sizeof(hd
->c_rdev
),
529 ul_asc((u_long
)arcn
->sb
.st_mtime
,hd
->c_mtime
,sizeof(hd
->c_mtime
),
531 ul_asc((u_long
)nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), OCT
))
535 * write the file name to the archive
537 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_CPIO
)) < 0) ||
538 (wr_rdbuf(arcn
->name
, nsz
) < 0)) {
539 paxwarn(1, "Unable to write cpio header for %s", arcn
->org_name
);
544 * if this file has data, we are done. The caller will write the file
545 * data, if we are link tell caller we are done, go to next file
547 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
548 (arcn
->type
== PAX_HRG
))
550 if (arcn
->type
!= PAX_SLK
)
554 * write the link name to the archive, tell the caller to go to the
555 * next file as we are done.
557 if (wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) {
558 paxwarn(1,"Unable to write cpio link name for %s",arcn
->org_name
);
565 * header field is out of range
567 paxwarn(1, "Cpio header field is too small to store file %s",
573 * Routines common to the system VR4 version of cpio (with/without file CRC)
578 * determine if a block given to us is a valid system VR4 cpio header
579 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
582 * 0 if a valid header, -1 otherwise
587 vcpio_id(char *blk
, int size
)
595 if ((size
< sizeof(HD_VCPIO
)) ||
596 (strncmp(blk
, AVMAGIC
, sizeof(AVMAGIC
) - 1) != 0))
603 * determine if a block given to us is a valid system VR4 cpio header
604 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
606 * 0 if a valid header, -1 otherwise
611 crc_id(char *blk
, int size
)
619 if ((size
< sizeof(HD_VCPIO
)) ||
620 (strncmp(blk
, AVCMAGIC
, sizeof(AVCMAGIC
) - 1) != 0))
627 w set file data CRC calculations. Fire up the hard link detection code
629 * 0 if ok -1 otherwise (the return values of lnk_start())
646 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
647 * convert and store the values in the ARCHD parameter.
649 * 0 if a valid header, -1 otherwise.
654 vcpio_rd(register ARCHD
*arcn
, register char *buf
)
658 register ARCHD
*arcn
;
662 register HD_VCPIO
*hd
;
668 * during the id phase it was determined if we were using CRC, use the
672 if (crc_id(buf
, sizeof(HD_VCPIO
)) < 0)
675 if (vcpio_id(buf
, sizeof(HD_VCPIO
)) < 0)
679 hd
= (HD_VCPIO
*)buf
;
683 * extract the hex ascii fields from the header
685 arcn
->sb
.st_ino
= (ino_t
)asc_ul(hd
->c_ino
, sizeof(hd
->c_ino
), HEX
);
686 arcn
->sb
.st_mode
= (mode_t
)asc_ul(hd
->c_mode
, sizeof(hd
->c_mode
), HEX
);
687 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->c_uid
, sizeof(hd
->c_uid
), HEX
);
688 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->c_gid
, sizeof(hd
->c_gid
), HEX
);
689 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->c_mtime
,sizeof(hd
->c_mtime
),HEX
);
690 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
692 arcn
->sb
.st_size
= (off_t
)asc_ul(hd
->c_filesize
,
693 sizeof(hd
->c_filesize
), HEX
);
695 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->c_filesize
,
696 sizeof(hd
->c_filesize
), HEX
);
698 arcn
->sb
.st_nlink
= (nlink_t
)asc_ul(hd
->c_nlink
, sizeof(hd
->c_nlink
),
700 devmajor
= (dev_t
)asc_ul(hd
->c_maj
, sizeof(hd
->c_maj
), HEX
);
701 devminor
= (dev_t
)asc_ul(hd
->c_min
, sizeof(hd
->c_min
), HEX
);
702 arcn
->sb
.st_dev
= TODEV(devmajor
, devminor
);
703 devmajor
= (dev_t
)asc_ul(hd
->c_rmaj
, sizeof(hd
->c_maj
), HEX
);
704 devminor
= (dev_t
)asc_ul(hd
->c_rmin
, sizeof(hd
->c_min
), HEX
);
705 arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
706 arcn
->crc
= asc_ul(hd
->c_chksum
, sizeof(hd
->c_chksum
), HEX
);
709 * check the length of the file name, if ok read it in, return -1 if
712 if ((nsz
= (int)asc_ul(hd
->c_namesize
,sizeof(hd
->c_namesize
),HEX
)) < 2)
714 arcn
->nlen
= nsz
- 1;
715 if (rd_nm(arcn
, nsz
) < 0)
719 * skip padding. header + filename is aligned to 4 byte boundries
721 if (rd_skip((off_t
)(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
))) < 0)
725 * if not a link (or a file with no data), calculate pad size (for
726 * padding which follows the file data), clear the link name and return
728 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
730 * we have a valid header (not a link)
733 arcn
->ln_name
[0] = '\0';
734 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
735 return(com_rd(arcn
));
739 * read in the link name and skip over the padding
741 if ((rd_ln_nm(arcn
) < 0) ||
742 (rd_skip((off_t
)(VCPIO_PAD(arcn
->sb
.st_size
))) < 0))
746 * we have a valid header (with a link)
748 return(com_rd(arcn
));
753 * no cleanup needed here, just return size of the trailer (for append)
755 * size of trailer header in this format
766 return((off_t
)(sizeof(HD_VCPIO
) + sizeof(TRAILER
) +
767 (VCPIO_PAD(sizeof(HD_VCPIO
) + sizeof(TRAILER
)))));
772 * start up the device mapping table, enable crc file calculation
774 * 0 if ok, -1 otherwise (what dev_start() returns)
791 * copy the data in the ARCHD to buffer in system VR4 cpio
792 * (with/without crc) format.
794 * 0 if file has data to be written after the header, 1 if file has
795 * NO data to write after the header, -1 if archive write failed
800 vcpio_wr(register ARCHD
*arcn
)
804 register ARCHD
*arcn
;
807 register HD_VCPIO
*hd
;
809 char hdblk
[sizeof(HD_VCPIO
)];
812 * check and repair truncated device and inode fields in the cpio
815 if (map_dev(arcn
, (u_long
)VCPIO_MASK
, (u_long
)VCPIO_MASK
) < 0)
817 nsz
= arcn
->nlen
+ 1;
818 hd
= (HD_VCPIO
*)hdblk
;
819 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
820 arcn
->sb
.st_rdev
= 0;
823 * add the proper magic value depending whether we were asked for
824 * file data crc's, and the crc if needed.
827 if (ul_asc((u_long
)VCMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
),
829 ul_asc((u_long
)arcn
->crc
,hd
->c_chksum
,sizeof(hd
->c_chksum
),
833 if (ul_asc((u_long
)VMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
),
835 ul_asc((u_long
)0L, hd
->c_chksum
, sizeof(hd
->c_chksum
),HEX
))
844 * caller will copy file data to the archive. tell him how
847 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
849 if (ul_asc((u_long
)arcn
->sb
.st_size
, hd
->c_filesize
,
850 sizeof(hd
->c_filesize
), HEX
)) {
852 if (uqd_asc((u_quad_t
)arcn
->sb
.st_size
, hd
->c_filesize
,
853 sizeof(hd
->c_filesize
), HEX
)) {
855 paxwarn(1,"File is too large for sv4cpio format %s",
862 * no file data for the caller to process, the file data has
863 * the size of the link
866 if (ul_asc((u_long
)arcn
->ln_nlen
, hd
->c_filesize
,
867 sizeof(hd
->c_filesize
), HEX
))
872 * no file data for the caller to process
875 if (ul_asc((u_long
)0L, hd
->c_filesize
, sizeof(hd
->c_filesize
),
882 * set the other fields in the header
884 if (ul_asc((u_long
)arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
),
886 ul_asc((u_long
)arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
),
888 ul_asc((u_long
)arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
),
890 ul_asc((u_long
)arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
),
892 ul_asc((u_long
)arcn
->sb
.st_mtime
, hd
->c_mtime
, sizeof(hd
->c_mtime
),
894 ul_asc((u_long
)arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
),
896 ul_asc((u_long
)MAJOR(arcn
->sb
.st_dev
),hd
->c_maj
, sizeof(hd
->c_maj
),
898 ul_asc((u_long
)MINOR(arcn
->sb
.st_dev
),hd
->c_min
, sizeof(hd
->c_min
),
900 ul_asc((u_long
)MAJOR(arcn
->sb
.st_rdev
),hd
->c_rmaj
,sizeof(hd
->c_maj
),
902 ul_asc((u_long
)MINOR(arcn
->sb
.st_rdev
),hd
->c_rmin
,sizeof(hd
->c_min
),
904 ul_asc((u_long
)nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), HEX
))
908 * write the header, the file name and padding as required.
910 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_VCPIO
)) < 0) ||
911 (wr_rdbuf(arcn
->name
, (int)nsz
) < 0) ||
912 (wr_skip((off_t
)(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
))) < 0)) {
913 paxwarn(1,"Could not write sv4cpio header for %s",arcn
->org_name
);
918 * if we have file data, tell the caller we are done, copy the file
920 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
921 (arcn
->type
== PAX_HRG
))
925 * if we are not a link, tell the caller we are done, go to next file
927 if (arcn
->type
!= PAX_SLK
)
931 * write the link name, tell the caller we are done.
933 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
934 (wr_skip((off_t
)(VCPIO_PAD(arcn
->ln_nlen
))) < 0)) {
935 paxwarn(1,"Could not write sv4cpio link name for %s",
943 * header field is out of range
945 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn
->org_name
);
950 * Routines common to the old binary header cpio
955 * determine if a block given to us is a old binary cpio header
956 * (with/without header byte swapping)
958 * 0 if a valid header, -1 otherwise
963 bcpio_id(char *blk
, int size
)
971 if (size
< sizeof(HD_BCPIO
))
975 * check both normal and byte swapped magic cookies
977 if (((u_short
)SHRT_EXT(blk
)) == MAGIC
)
979 if (((u_short
)RSHRT_EXT(blk
)) == MAGIC
) {
989 * determine if a buffer is a old binary archive entry. (it may have byte
990 * swapped header) convert and store the values in the ARCHD parameter.
991 * This is a very old header format and should not really be used.
993 * 0 if a valid header, -1 otherwise.
998 bcpio_rd(register ARCHD
*arcn
, register char *buf
)
1002 register ARCHD
*arcn
;
1006 register HD_BCPIO
*hd
;
1012 if (bcpio_id(buf
, sizeof(HD_BCPIO
)) < 0)
1016 hd
= (HD_BCPIO
*)buf
;
1019 * header has swapped bytes on 16 bit boundries
1021 arcn
->sb
.st_dev
= (dev_t
)(RSHRT_EXT(hd
->h_dev
));
1022 arcn
->sb
.st_ino
= (ino_t
)(RSHRT_EXT(hd
->h_ino
));
1023 arcn
->sb
.st_mode
= (mode_t
)(RSHRT_EXT(hd
->h_mode
));
1024 arcn
->sb
.st_uid
= (uid_t
)(RSHRT_EXT(hd
->h_uid
));
1025 arcn
->sb
.st_gid
= (gid_t
)(RSHRT_EXT(hd
->h_gid
));
1026 arcn
->sb
.st_nlink
= (nlink_t
)(RSHRT_EXT(hd
->h_nlink
));
1027 arcn
->sb
.st_rdev
= (dev_t
)(RSHRT_EXT(hd
->h_rdev
));
1028 arcn
->sb
.st_mtime
= (time_t)(RSHRT_EXT(hd
->h_mtime_1
));
1029 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
1030 ((time_t)(RSHRT_EXT(hd
->h_mtime_2
)));
1031 arcn
->sb
.st_size
= (off_t
)(RSHRT_EXT(hd
->h_filesize_1
));
1032 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
1033 ((off_t
)(RSHRT_EXT(hd
->h_filesize_2
)));
1034 nsz
= (int)(RSHRT_EXT(hd
->h_namesize
));
1036 arcn
->sb
.st_dev
= (dev_t
)(SHRT_EXT(hd
->h_dev
));
1037 arcn
->sb
.st_ino
= (ino_t
)(SHRT_EXT(hd
->h_ino
));
1038 arcn
->sb
.st_mode
= (mode_t
)(SHRT_EXT(hd
->h_mode
));
1039 arcn
->sb
.st_uid
= (uid_t
)(SHRT_EXT(hd
->h_uid
));
1040 arcn
->sb
.st_gid
= (gid_t
)(SHRT_EXT(hd
->h_gid
));
1041 arcn
->sb
.st_nlink
= (nlink_t
)(SHRT_EXT(hd
->h_nlink
));
1042 arcn
->sb
.st_rdev
= (dev_t
)(SHRT_EXT(hd
->h_rdev
));
1043 arcn
->sb
.st_mtime
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
1044 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
1045 ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
1046 arcn
->sb
.st_size
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
1047 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
1048 ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
1049 nsz
= (int)(SHRT_EXT(hd
->h_namesize
));
1051 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
1054 * check the file name size, if bogus give up. otherwise read the file
1059 arcn
->nlen
= nsz
- 1;
1060 if (rd_nm(arcn
, nsz
) < 0)
1064 * header + file name are aligned to 2 byte boundries, skip if needed
1066 if (rd_skip((off_t
)(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
))) < 0)
1070 * if not a link (or a file with no data), calculate pad size (for
1071 * padding which follows the file data), clear the link name and return
1073 if (((arcn
->sb
.st_mode
& C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)){
1075 * we have a valid header (not a link)
1078 arcn
->ln_name
[0] = '\0';
1079 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
1080 return(com_rd(arcn
));
1083 if ((rd_ln_nm(arcn
) < 0) ||
1084 (rd_skip((off_t
)(BCPIO_PAD(arcn
->sb
.st_size
))) < 0))
1088 * we have a valid header (with a link)
1090 return(com_rd(arcn
));
1095 * no cleanup needed here, just return size of the trailer (for append)
1097 * size of trailer header in this format
1108 return((off_t
)(sizeof(HD_BCPIO
) + sizeof(TRAILER
) +
1109 (BCPIO_PAD(sizeof(HD_BCPIO
) + sizeof(TRAILER
)))));
1114 * copy the data in the ARCHD to buffer in old binary cpio format
1115 * There is a real chance of field overflow with this critter. So we
1116 * always check the conversion is ok. nobody in his their right mind
1117 * should write an achive in this format...
1119 * 0 if file has data to be written after the header, 1 if file has NO
1120 * data to write after the header, -1 if archive write failed
1125 bcpio_wr(register ARCHD
*arcn
)
1129 register ARCHD
*arcn
;
1132 register HD_BCPIO
*hd
;
1134 char hdblk
[sizeof(HD_BCPIO
)];
1140 * check and repair truncated device and inode fields in the cpio
1143 if (map_dev(arcn
, (u_long
)BCPIO_MASK
, (u_long
)BCPIO_MASK
) < 0)
1146 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
1147 arcn
->sb
.st_rdev
= 0;
1148 hd
= (HD_BCPIO
*)hdblk
;
1150 switch(arcn
->type
) {
1155 * caller will copy file data to the archive. tell him how
1158 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
1159 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->sb
.st_size
);
1160 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->sb
.st_size
);
1161 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->sb
.st_size
);
1162 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->sb
.st_size
);
1163 t_offt
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
1164 t_offt
= (t_offt
<<16) | ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
1165 if (arcn
->sb
.st_size
!= t_offt
) {
1166 paxwarn(1,"File is too large for bcpio format %s",
1173 * no file data for the caller to process, the file data has
1174 * the size of the link
1177 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->ln_nlen
);
1178 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->ln_nlen
);
1179 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->ln_nlen
);
1180 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->ln_nlen
);
1181 t_int
= (int)(SHRT_EXT(hd
->h_filesize_1
));
1182 t_int
= (t_int
<< 16) | ((int)(SHRT_EXT(hd
->h_filesize_2
)));
1183 if (arcn
->ln_nlen
!= t_int
)
1188 * no file data for the caller to process
1191 hd
->h_filesize_1
[0] = (char)0;
1192 hd
->h_filesize_1
[1] = (char)0;
1193 hd
->h_filesize_2
[0] = (char)0;
1194 hd
->h_filesize_2
[1] = (char)0;
1199 * build up the rest of the fields
1201 hd
->h_magic
[0] = CHR_WR_2(MAGIC
);
1202 hd
->h_magic
[1] = CHR_WR_3(MAGIC
);
1203 hd
->h_dev
[0] = CHR_WR_2(arcn
->sb
.st_dev
);
1204 hd
->h_dev
[1] = CHR_WR_3(arcn
->sb
.st_dev
);
1205 if (arcn
->sb
.st_dev
!= (dev_t
)(SHRT_EXT(hd
->h_dev
)))
1207 hd
->h_ino
[0] = CHR_WR_2(arcn
->sb
.st_ino
);
1208 hd
->h_ino
[1] = CHR_WR_3(arcn
->sb
.st_ino
);
1209 if (arcn
->sb
.st_ino
!= (ino_t
)(SHRT_EXT(hd
->h_ino
)))
1211 hd
->h_mode
[0] = CHR_WR_2(arcn
->sb
.st_mode
);
1212 hd
->h_mode
[1] = CHR_WR_3(arcn
->sb
.st_mode
);
1213 if (arcn
->sb
.st_mode
!= (mode_t
)(SHRT_EXT(hd
->h_mode
)))
1215 hd
->h_uid
[0] = CHR_WR_2(arcn
->sb
.st_uid
);
1216 hd
->h_uid
[1] = CHR_WR_3(arcn
->sb
.st_uid
);
1217 if (arcn
->sb
.st_uid
!= (uid_t
)(SHRT_EXT(hd
->h_uid
)))
1219 hd
->h_gid
[0] = CHR_WR_2(arcn
->sb
.st_gid
);
1220 hd
->h_gid
[1] = CHR_WR_3(arcn
->sb
.st_gid
);
1221 if (arcn
->sb
.st_gid
!= (gid_t
)(SHRT_EXT(hd
->h_gid
)))
1223 hd
->h_nlink
[0] = CHR_WR_2(arcn
->sb
.st_nlink
);
1224 hd
->h_nlink
[1] = CHR_WR_3(arcn
->sb
.st_nlink
);
1225 if (arcn
->sb
.st_nlink
!= (nlink_t
)(SHRT_EXT(hd
->h_nlink
)))
1227 hd
->h_rdev
[0] = CHR_WR_2(arcn
->sb
.st_rdev
);
1228 hd
->h_rdev
[1] = CHR_WR_3(arcn
->sb
.st_rdev
);
1229 if (arcn
->sb
.st_rdev
!= (dev_t
)(SHRT_EXT(hd
->h_rdev
)))
1231 hd
->h_mtime_1
[0] = CHR_WR_0(arcn
->sb
.st_mtime
);
1232 hd
->h_mtime_1
[1] = CHR_WR_1(arcn
->sb
.st_mtime
);
1233 hd
->h_mtime_2
[0] = CHR_WR_2(arcn
->sb
.st_mtime
);
1234 hd
->h_mtime_2
[1] = CHR_WR_3(arcn
->sb
.st_mtime
);
1235 t_timet
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
1236 t_timet
= (t_timet
<< 16) | ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
1237 if (arcn
->sb
.st_mtime
!= t_timet
)
1239 nsz
= arcn
->nlen
+ 1;
1240 hd
->h_namesize
[0] = CHR_WR_2(nsz
);
1241 hd
->h_namesize
[1] = CHR_WR_3(nsz
);
1242 if (nsz
!= (int)(SHRT_EXT(hd
->h_namesize
)))
1246 * write the header, the file name and padding as required.
1248 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_BCPIO
)) < 0) ||
1249 (wr_rdbuf(arcn
->name
, nsz
) < 0) ||
1250 (wr_skip((off_t
)(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
))) < 0)) {
1251 paxwarn(1, "Could not write bcpio header for %s", arcn
->org_name
);
1256 * if we have file data, tell the caller we are done
1258 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
1259 (arcn
->type
== PAX_HRG
))
1263 * if we are not a link, tell the caller we are done, go to next file
1265 if (arcn
->type
!= PAX_SLK
)
1269 * write the link name, tell the caller we are done.
1271 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
1272 (wr_skip((off_t
)(BCPIO_PAD(arcn
->ln_nlen
))) < 0)) {
1273 paxwarn(1,"Could not write bcpio link name for %s",arcn
->org_name
);
1280 * header field is out of range
1282 paxwarn(1,"Bcpio header field is too small for file %s", arcn
->org_name
);