1 /* $NetBSD: cpio.c,v 1.22 2012/08/09 08:09:21 christos Exp $ */
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
40 #include <sys/cdefs.h>
43 static char sccsid
[] = "@(#)cpio.c 8.1 (Berkeley) 5/31/93";
45 __RCSID("$NetBSD: cpio.c,v 1.22 2012/08/09 08:09:21 christos Exp $");
49 #include <sys/types.h>
52 #include <sys/param.h>
62 static int rd_nm(ARCHD
*, int);
63 static int rd_ln_nm(ARCHD
*);
64 static int com_rd(ARCHD
*);
67 * Routines which support the different cpio versions
70 int cpio_swp_head
; /* binary cpio header byte swap */
73 * Routines common to all versions of cpio
78 * Fire up the hard link detection code
80 * 0 if ok -1 otherwise (the return values of lnk_start())
91 * Called to determine if a header block is a valid trailer. We are
92 * passed the block, the in_sync flag (which tells us we are in resync
93 * mode; looking for a valid header), and cnt (which starts at zero)
94 * which is used to count the number of empty blocks we have seen so far.
96 * 0 if a valid trailer, -1 if not a valid trailer,
100 cpio_subtrail(ARCHD
*arcn
)
103 * look for trailer id in file we are about to process
105 if ((strcmp(arcn
->name
, TRAILER
) == 0) && (arcn
->sb
.st_size
== 0))
112 * operations common to all cpio read functions.
122 arcn
->org_name
= arcn
->name
;
123 switch(arcn
->sb
.st_mode
& C_IFMT
) {
125 arcn
->type
= PAX_FIF
;
128 arcn
->type
= PAX_DIR
;
131 arcn
->type
= PAX_BLK
;
134 arcn
->type
= PAX_CHR
;
137 arcn
->type
= PAX_SLK
;
140 arcn
->type
= PAX_SCK
;
146 * we have file data, set up skip (pad is set in the format
149 arcn
->sb
.st_mode
= (arcn
->sb
.st_mode
& 0xfff) | C_ISREG
;
150 arcn
->type
= PAX_REG
;
151 arcn
->skip
= arcn
->sb
.st_size
;
154 if (chk_lnk(arcn
) < 0)
161 * write the special file with the name trailer in the proper format
163 * result of the write of the trailer from the cpio specific write func
172 * create a trailer request and call the proper format write function
174 memset(&last
, 0, sizeof(last
));
175 last
.nlen
= sizeof(TRAILER
) - 1;
177 last
.sb
.st_nlink
= 1;
178 (void)strcpy(last
.name
, TRAILER
);
179 return (*frmt
->wr
)(&last
);
184 * read in the file name which follows the cpio header
186 * 0 if ok, -1 otherwise
190 rd_nm(ARCHD
*arcn
, int nsz
)
193 * do not even try bogus values
195 if ((nsz
<= 0) || (nsz
> (int)sizeof(arcn
->name
))) {
196 tty_warn(1, "Cpio file name length %d is out of range", nsz
);
201 * read the name and make sure it is not empty and is \0 terminated
203 if ((rd_wrbuf(arcn
->name
,nsz
) != nsz
) || (arcn
->name
[nsz
-1] != '\0') ||
204 (arcn
->name
[0] == '\0')) {
205 tty_warn(1, "Cpio file name in header is corrupted");
213 * read in the link name for a file with links. The link name is stored
214 * like file data (and is NOT \0 terminated!)
216 * 0 if ok, -1 otherwise
220 rd_ln_nm(ARCHD
*arcn
)
223 * check the length specified for bogus values
225 if ((arcn
->sb
.st_size
== 0) ||
226 (arcn
->sb
.st_size
>= (off_t
)sizeof(arcn
->ln_name
))) {
227 tty_warn(1, "Cpio link name length is invalid: " OFFT_F
,
228 (OFFT_T
) arcn
->sb
.st_size
);
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 tty_warn(1, "Cpio link name read error");
240 arcn
->ln_nlen
= 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 tty_warn(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
< (int)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_u32(hd
->c_dev
, sizeof(hd
->c_dev
), OCT
);
301 arcn
->sb
.st_ino
= (ino_t
)asc_u32(hd
->c_ino
, sizeof(hd
->c_ino
), OCT
);
302 arcn
->sb
.st_mode
= (mode_t
)asc_u32(hd
->c_mode
, sizeof(hd
->c_mode
), OCT
);
303 arcn
->sb
.st_uid
= (uid_t
)asc_u32(hd
->c_uid
, sizeof(hd
->c_uid
), OCT
);
304 arcn
->sb
.st_gid
= (gid_t
)asc_u32(hd
->c_gid
, sizeof(hd
->c_gid
), OCT
);
305 arcn
->sb
.st_nlink
= (nlink_t
)asc_u32(hd
->c_nlink
, sizeof(hd
->c_nlink
),
307 arcn
->sb
.st_rdev
= (dev_t
)asc_u32(hd
->c_rdev
, sizeof(hd
->c_rdev
), OCT
);
308 arcn
->sb
.st_mtime
= (time_t)(int32_t)asc_u32(hd
->c_mtime
, sizeof(hd
->c_mtime
),
310 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
311 arcn
->sb
.st_size
= (off_t
)ASC_OFFT(hd
->c_filesize
,
312 sizeof(hd
->c_filesize
), OCT
);
315 * check name size and if valid, read in the name of this entry (name
316 * follows header in the archive)
318 if ((nsz
= (int)asc_u32(hd
->c_namesize
,sizeof(hd
->c_namesize
),OCT
)) < 2)
320 arcn
->nlen
= nsz
- 1;
321 if (rd_nm(arcn
, nsz
) < 0)
324 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
326 * no link name to read for this file
329 arcn
->ln_name
[0] = '\0';
334 * check link name size and read in the link name. Link names are
335 * stored like file data.
337 if (rd_ln_nm(arcn
) < 0)
341 * we have a valid header (with a link)
348 * no cleanup needed here, just return size of the trailer (for append)
350 * size of trailer header in this format
356 return (off_t
)(sizeof(HD_CPIO
) + sizeof(TRAILER
));
361 * start up the device mapping table
363 * 0 if ok, -1 otherwise (what dev_start() returns)
374 * copy the data in the ARCHD to buffer in extended byte oriented cpio
377 * 0 if file has data to be written after the header, 1 if file has NO
378 * data to write after the header, -1 if archive write failed
386 char hdblk
[sizeof(HD_CPIO
)];
389 * check and repair truncated device and inode fields in the header
391 if (map_dev(arcn
, (u_long
)CPIO_MASK
, (u_long
)CPIO_MASK
) < 0)
395 nsz
= arcn
->nlen
+ 1;
396 hd
= (HD_CPIO
*)hdblk
;
397 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
398 arcn
->sb
.st_rdev
= 0;
405 * set data size for file data
407 if (OFFT_ASC(arcn
->sb
.st_size
, hd
->c_filesize
,
408 sizeof(hd
->c_filesize
), OCT
)) {
409 tty_warn(1,"File is too large for cpio format %s",
416 * set data size to hold link name
418 if (u32_asc((uintmax_t)arcn
->ln_nlen
, hd
->c_filesize
,
419 sizeof(hd
->c_filesize
), OCT
))
424 * all other file types have no file data
426 if (u32_asc((uintmax_t)0, hd
->c_filesize
, sizeof(hd
->c_filesize
),
433 * copy the values to the header using octal ascii
435 if (u32_asc((uintmax_t)MAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
), OCT
) ||
436 u32_asc((uintmax_t)arcn
->sb
.st_dev
, hd
->c_dev
, sizeof(hd
->c_dev
),
438 u32_asc((uintmax_t)arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
),
440 u32_asc((uintmax_t)arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
),
442 u32_asc((uintmax_t)arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
),
444 u32_asc((uintmax_t)arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
),
446 u32_asc((uintmax_t)arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
),
448 u32_asc((uintmax_t)arcn
->sb
.st_rdev
, hd
->c_rdev
, sizeof(hd
->c_rdev
),
450 u32_asc((uintmax_t)arcn
->sb
.st_mtime
,hd
->c_mtime
,sizeof(hd
->c_mtime
),
452 u32_asc((uintmax_t)nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), OCT
))
456 * write the file name to the archive
458 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_CPIO
)) < 0) ||
459 (wr_rdbuf(arcn
->name
, nsz
) < 0)) {
460 tty_warn(1, "Unable to write cpio header for %s",
466 * if this file has data, we are done. The caller will write the file
467 * data, if we are link tell caller we are done, go to next file
469 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
))
471 if (arcn
->type
!= PAX_SLK
)
475 * write the link name to the archive, tell the caller to go to the
476 * next file as we are done.
478 if (wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) {
479 tty_warn(1,"Unable to write cpio link name for %s",
487 * header field is out of range
489 tty_warn(1, "Cpio header field is too small to store file %s",
495 * Routines common to the system VR4 version of cpio (with/without file CRC)
500 * determine if a block given to us is a valid system VR4 cpio header
501 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
504 * 0 if a valid header, -1 otherwise
508 vcpio_id(char *blk
, int size
)
510 if ((size
< (int)sizeof(HD_VCPIO
)) ||
511 (strncmp(blk
, AVMAGIC
, sizeof(AVMAGIC
) - 1) != 0))
518 * determine if a block given to us is a valid system VR4 cpio header
519 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
521 * 0 if a valid header, -1 otherwise
525 crc_id(char *blk
, int size
)
527 if ((size
< (int)sizeof(HD_VCPIO
)) ||
528 (strncmp(blk
, AVCMAGIC
, sizeof(AVCMAGIC
) - 1) != 0))
535 * set file data CRC calculations. Fire up the hard link detection code
537 * 0 if ok -1 otherwise (the return values of lnk_start())
549 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
550 * convert and store the values in the ARCHD parameter.
552 * 0 if a valid header, -1 otherwise.
556 vcpio_rd(ARCHD
*arcn
, char *buf
)
564 * during the id phase it was determined if we were using CRC, use the
568 if (crc_id(buf
, sizeof(HD_VCPIO
)) < 0)
571 if (vcpio_id(buf
, sizeof(HD_VCPIO
)) < 0)
575 hd
= (HD_VCPIO
*)buf
;
579 * extract the hex ascii fields from the header
581 arcn
->sb
.st_ino
= (ino_t
)asc_u32(hd
->c_ino
, sizeof(hd
->c_ino
), HEX
);
582 arcn
->sb
.st_mode
= (mode_t
)asc_u32(hd
->c_mode
, sizeof(hd
->c_mode
), HEX
);
583 arcn
->sb
.st_uid
= (uid_t
)asc_u32(hd
->c_uid
, sizeof(hd
->c_uid
), HEX
);
584 arcn
->sb
.st_gid
= (gid_t
)asc_u32(hd
->c_gid
, sizeof(hd
->c_gid
), HEX
);
585 arcn
->sb
.st_mtime
= (time_t)(int32_t)asc_u32(hd
->c_mtime
,sizeof(hd
->c_mtime
),HEX
);
586 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
587 arcn
->sb
.st_size
= (off_t
)ASC_OFFT(hd
->c_filesize
,
588 sizeof(hd
->c_filesize
), HEX
);
589 arcn
->sb
.st_nlink
= (nlink_t
)asc_u32(hd
->c_nlink
, sizeof(hd
->c_nlink
),
591 devmajor
= (dev_t
)asc_u32(hd
->c_maj
, sizeof(hd
->c_maj
), HEX
);
592 devminor
= (dev_t
)asc_u32(hd
->c_min
, sizeof(hd
->c_min
), HEX
);
593 arcn
->sb
.st_dev
= TODEV(devmajor
, devminor
);
594 devmajor
= (dev_t
)asc_u32(hd
->c_rmaj
, sizeof(hd
->c_maj
), HEX
);
595 devminor
= (dev_t
)asc_u32(hd
->c_rmin
, sizeof(hd
->c_min
), HEX
);
596 arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
597 arcn
->crc
= asc_u32(hd
->c_chksum
, sizeof(hd
->c_chksum
), HEX
);
600 * check the length of the file name, if ok read it in, return -1 if
603 if ((nsz
= (int)asc_u32(hd
->c_namesize
,sizeof(hd
->c_namesize
),HEX
)) < 2)
605 arcn
->nlen
= nsz
- 1;
606 if (rd_nm(arcn
, nsz
) < 0)
610 * skip padding. header + filename is aligned to 4 byte boundaries
612 if (rd_skip((off_t
)(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
))) < 0)
616 * if not a link (or a file with no data), calculate pad size (for
617 * padding which follows the file data), clear the link name and return
619 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
621 * we have a valid header (not a link)
624 arcn
->ln_name
[0] = '\0';
625 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
630 * read in the link name and skip over the padding
632 if ((rd_ln_nm(arcn
) < 0) ||
633 (rd_skip((off_t
)(VCPIO_PAD(arcn
->sb
.st_size
))) < 0))
637 * we have a valid header (with a link)
644 * no cleanup needed here, just return size of the trailer (for append)
646 * size of trailer header in this format
652 return (off_t
)(sizeof(HD_VCPIO
) + sizeof(TRAILER
) +
653 (VCPIO_PAD(sizeof(HD_VCPIO
) + sizeof(TRAILER
))));
658 * start up the device mapping table, enable crc file calculation
660 * 0 if ok, -1 otherwise (what dev_start() returns)
672 * copy the data in the ARCHD to buffer in system VR4 cpio
673 * (with/without crc) format.
675 * 0 if file has data to be written after the header, 1 if file has
676 * NO data to write after the header, -1 if archive write failed
680 vcpio_wr(ARCHD
*arcn
)
684 char hdblk
[sizeof(HD_VCPIO
)];
687 * check and repair truncated device and inode fields in the cpio
690 if (map_dev(arcn
, (u_long
)VCPIO_MASK
, (u_long
)VCPIO_MASK
) < 0)
692 nsz
= arcn
->nlen
+ 1;
693 hd
= (HD_VCPIO
*)hdblk
;
694 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
695 arcn
->sb
.st_rdev
= 0;
698 * add the proper magic value depending whether we were asked for
699 * file data crc's, and the crc if needed.
702 if (u32_asc((uintmax_t)VCMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
),
704 u32_asc((uintmax_t)arcn
->crc
,hd
->c_chksum
,sizeof(hd
->c_chksum
),
708 if (u32_asc((uintmax_t)VMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
),
710 u32_asc((uintmax_t)0, hd
->c_chksum
, sizeof(hd
->c_chksum
),HEX
))
719 * caller will copy file data to the archive. tell him how
722 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
723 if (OFFT_ASC(arcn
->sb
.st_size
, hd
->c_filesize
,
724 sizeof(hd
->c_filesize
), HEX
)) {
725 tty_warn(1,"File is too large for sv4cpio format %s",
732 * no file data for the caller to process, the file data has
733 * the size of the link
736 if (u32_asc((uintmax_t)arcn
->ln_nlen
, hd
->c_filesize
,
737 sizeof(hd
->c_filesize
), HEX
))
742 * no file data for the caller to process
745 if (u32_asc((uintmax_t)0, hd
->c_filesize
, sizeof(hd
->c_filesize
),
752 * set the other fields in the header
754 if (u32_asc((uintmax_t)arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
),
756 u32_asc((uintmax_t)arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
),
758 u32_asc((uintmax_t)arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
),
760 u32_asc((uintmax_t)arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
),
762 u32_asc((uintmax_t)arcn
->sb
.st_mtime
, hd
->c_mtime
, sizeof(hd
->c_mtime
),
764 u32_asc((uintmax_t)arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
),
766 u32_asc((uintmax_t)MAJOR(arcn
->sb
.st_dev
),hd
->c_maj
, sizeof(hd
->c_maj
),
768 u32_asc((uintmax_t)MINOR(arcn
->sb
.st_dev
),hd
->c_min
, sizeof(hd
->c_min
),
770 u32_asc((uintmax_t)MAJOR(arcn
->sb
.st_rdev
),hd
->c_rmaj
,sizeof(hd
->c_maj
),
772 u32_asc((uintmax_t)MINOR(arcn
->sb
.st_rdev
),hd
->c_rmin
,sizeof(hd
->c_min
),
774 u32_asc((uintmax_t)nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), HEX
))
778 * write the header, the file name and padding as required.
780 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_VCPIO
)) < 0) ||
781 (wr_rdbuf(arcn
->name
, (int)nsz
) < 0) ||
782 (wr_skip((off_t
)(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
))) < 0)) {
783 tty_warn(1,"Could not write sv4cpio header for %s",
789 * if we have file data, tell the caller we are done, copy the file
791 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
792 (arcn
->type
== PAX_HRG
))
796 * if we are not a link, tell the caller we are done, go to next file
798 if (arcn
->type
!= PAX_SLK
)
802 * write the link name, tell the caller we are done.
804 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
805 (wr_skip((off_t
)(VCPIO_PAD(arcn
->ln_nlen
))) < 0)) {
806 tty_warn(1,"Could not write sv4cpio link name for %s",
814 * header field is out of range
816 tty_warn(1,"Sv4cpio header field is too small for file %s",
822 * Routines common to the old binary header cpio
827 * determine if a block given to us is a old binary cpio header
828 * (with/without header byte swapping)
830 * 0 if a valid header, -1 otherwise
834 bcpio_id(char *blk
, int size
)
836 if (size
< (int)sizeof(HD_BCPIO
))
840 * check both normal and byte swapped magic cookies
842 if (((u_short
)SHRT_EXT(blk
)) == MAGIC
)
844 if (((u_short
)RSHRT_EXT(blk
)) == MAGIC
) {
854 * determine if a buffer is a old binary archive entry. (it may have byte
855 * swapped header) convert and store the values in the ARCHD parameter.
856 * This is a very old header format and should not really be used.
858 * 0 if a valid header, -1 otherwise.
862 bcpio_rd(ARCHD
*arcn
, char *buf
)
870 if (bcpio_id(buf
, sizeof(HD_BCPIO
)) < 0)
874 hd
= (HD_BCPIO
*)buf
;
877 * header has swapped bytes on 16 bit boundaries
879 arcn
->sb
.st_dev
= (dev_t
)(RSHRT_EXT(hd
->h_dev
));
880 arcn
->sb
.st_ino
= (ino_t
)(RSHRT_EXT(hd
->h_ino
));
881 arcn
->sb
.st_mode
= (mode_t
)(RSHRT_EXT(hd
->h_mode
));
882 arcn
->sb
.st_uid
= (uid_t
)(RSHRT_EXT(hd
->h_uid
));
883 arcn
->sb
.st_gid
= (gid_t
)(RSHRT_EXT(hd
->h_gid
));
884 arcn
->sb
.st_nlink
= (nlink_t
)(RSHRT_EXT(hd
->h_nlink
));
885 arcn
->sb
.st_rdev
= (dev_t
)(RSHRT_EXT(hd
->h_rdev
));
886 arcn
->sb
.st_mtime
= (time_t)(RSHRT_EXT(hd
->h_mtime_1
));
887 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
888 ((time_t)(RSHRT_EXT(hd
->h_mtime_2
)));
889 arcn
->sb
.st_size
= (off_t
)(RSHRT_EXT(hd
->h_filesize_1
));
890 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
891 ((off_t
)(RSHRT_EXT(hd
->h_filesize_2
)));
892 nsz
= (int)(RSHRT_EXT(hd
->h_namesize
));
894 arcn
->sb
.st_dev
= (dev_t
)(SHRT_EXT(hd
->h_dev
));
895 arcn
->sb
.st_ino
= (ino_t
)(SHRT_EXT(hd
->h_ino
));
896 arcn
->sb
.st_mode
= (mode_t
)(SHRT_EXT(hd
->h_mode
));
897 arcn
->sb
.st_uid
= (uid_t
)(SHRT_EXT(hd
->h_uid
));
898 arcn
->sb
.st_gid
= (gid_t
)(SHRT_EXT(hd
->h_gid
));
899 arcn
->sb
.st_nlink
= (nlink_t
)(SHRT_EXT(hd
->h_nlink
));
900 arcn
->sb
.st_rdev
= (dev_t
)(SHRT_EXT(hd
->h_rdev
));
901 arcn
->sb
.st_mtime
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
902 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
903 ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
904 arcn
->sb
.st_size
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
905 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
906 ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
907 nsz
= (int)(SHRT_EXT(hd
->h_namesize
));
909 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
912 * check the file name size, if bogus give up. otherwise read the file
917 arcn
->nlen
= nsz
- 1;
918 if (rd_nm(arcn
, nsz
) < 0)
922 * header + file name are aligned to 2 byte boundaries, skip if needed
924 if (rd_skip((off_t
)(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
))) < 0)
928 * if not a link (or a file with no data), calculate pad size (for
929 * padding which follows the file data), clear the link name and return
931 if (((arcn
->sb
.st_mode
& C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)){
933 * we have a valid header (not a link)
936 arcn
->ln_name
[0] = '\0';
937 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
941 if ((rd_ln_nm(arcn
) < 0) ||
942 (rd_skip((off_t
)(BCPIO_PAD(arcn
->sb
.st_size
))) < 0))
946 * we have a valid header (with a link)
953 * no cleanup needed here, just return size of the trailer (for append)
955 * size of trailer header in this format
961 return (off_t
)(sizeof(HD_BCPIO
) + sizeof(TRAILER
) +
962 (BCPIO_PAD(sizeof(HD_BCPIO
) + sizeof(TRAILER
))));
967 * copy the data in the ARCHD to buffer in old binary cpio format
968 * There is a real chance of field overflow with this critter. So we
969 * always check the conversion is ok. nobody in their right mind
970 * should write an archive in this format...
972 * 0 if file has data to be written after the header, 1 if file has NO
973 * data to write after the header, -1 if archive write failed
977 bcpio_wr(ARCHD
*arcn
)
981 char hdblk
[sizeof(HD_BCPIO
)];
987 * check and repair truncated device and inode fields in the cpio
990 if (map_dev(arcn
, (u_long
)BCPIO_MASK
, (u_long
)BCPIO_MASK
) < 0)
993 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
994 arcn
->sb
.st_rdev
= 0;
995 hd
= (HD_BCPIO
*)hdblk
;
1002 * caller will copy file data to the archive. tell him how
1005 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
1006 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->sb
.st_size
);
1007 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->sb
.st_size
);
1008 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->sb
.st_size
);
1009 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->sb
.st_size
);
1010 t_offt
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
1011 t_offt
= (t_offt
<<16) | ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
1012 if (arcn
->sb
.st_size
!= t_offt
) {
1013 tty_warn(1,"File is too large for bcpio format %s",
1020 * no file data for the caller to process, the file data has
1021 * the size of the link
1024 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->ln_nlen
);
1025 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->ln_nlen
);
1026 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->ln_nlen
);
1027 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->ln_nlen
);
1028 t_int
= (int)(SHRT_EXT(hd
->h_filesize_1
));
1029 t_int
= (t_int
<< 16) | ((int)(SHRT_EXT(hd
->h_filesize_2
)));
1030 if (arcn
->ln_nlen
!= t_int
)
1035 * no file data for the caller to process
1038 hd
->h_filesize_1
[0] = (char)0;
1039 hd
->h_filesize_1
[1] = (char)0;
1040 hd
->h_filesize_2
[0] = (char)0;
1041 hd
->h_filesize_2
[1] = (char)0;
1046 * build up the rest of the fields
1048 hd
->h_magic
[0] = CHR_WR_2(MAGIC
);
1049 hd
->h_magic
[1] = CHR_WR_3(MAGIC
);
1050 hd
->h_dev
[0] = CHR_WR_2(arcn
->sb
.st_dev
);
1051 hd
->h_dev
[1] = CHR_WR_3(arcn
->sb
.st_dev
);
1052 if (arcn
->sb
.st_dev
!= (dev_t
)(SHRT_EXT(hd
->h_dev
)))
1054 hd
->h_ino
[0] = CHR_WR_2(arcn
->sb
.st_ino
);
1055 hd
->h_ino
[1] = CHR_WR_3(arcn
->sb
.st_ino
);
1056 if (arcn
->sb
.st_ino
!= (ino_t
)(SHRT_EXT(hd
->h_ino
)))
1058 hd
->h_mode
[0] = CHR_WR_2(arcn
->sb
.st_mode
);
1059 hd
->h_mode
[1] = CHR_WR_3(arcn
->sb
.st_mode
);
1060 if (arcn
->sb
.st_mode
!= (mode_t
)(SHRT_EXT(hd
->h_mode
)))
1062 hd
->h_uid
[0] = CHR_WR_2(arcn
->sb
.st_uid
);
1063 hd
->h_uid
[1] = CHR_WR_3(arcn
->sb
.st_uid
);
1064 if (arcn
->sb
.st_uid
!= (uid_t
)(SHRT_EXT(hd
->h_uid
)))
1066 hd
->h_gid
[0] = CHR_WR_2(arcn
->sb
.st_gid
);
1067 hd
->h_gid
[1] = CHR_WR_3(arcn
->sb
.st_gid
);
1068 if (arcn
->sb
.st_gid
!= (gid_t
)(SHRT_EXT(hd
->h_gid
)))
1070 hd
->h_nlink
[0] = CHR_WR_2(arcn
->sb
.st_nlink
);
1071 hd
->h_nlink
[1] = CHR_WR_3(arcn
->sb
.st_nlink
);
1072 if (arcn
->sb
.st_nlink
!= (nlink_t
)(SHRT_EXT(hd
->h_nlink
)))
1074 hd
->h_rdev
[0] = CHR_WR_2(arcn
->sb
.st_rdev
);
1075 hd
->h_rdev
[1] = CHR_WR_3(arcn
->sb
.st_rdev
);
1076 if (arcn
->sb
.st_rdev
!= (dev_t
)(SHRT_EXT(hd
->h_rdev
)))
1078 hd
->h_mtime_1
[0] = CHR_WR_0(arcn
->sb
.st_mtime
);
1079 hd
->h_mtime_1
[1] = CHR_WR_1(arcn
->sb
.st_mtime
);
1080 hd
->h_mtime_2
[0] = CHR_WR_2(arcn
->sb
.st_mtime
);
1081 hd
->h_mtime_2
[1] = CHR_WR_3(arcn
->sb
.st_mtime
);
1082 t_timet
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
1083 t_timet
= (t_timet
<< 16) | ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
1084 if (arcn
->sb
.st_mtime
!= t_timet
)
1086 nsz
= arcn
->nlen
+ 1;
1087 hd
->h_namesize
[0] = CHR_WR_2(nsz
);
1088 hd
->h_namesize
[1] = CHR_WR_3(nsz
);
1089 if (nsz
!= (int)(SHRT_EXT(hd
->h_namesize
)))
1093 * write the header, the file name and padding as required.
1095 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_BCPIO
)) < 0) ||
1096 (wr_rdbuf(arcn
->name
, nsz
) < 0) ||
1097 (wr_skip((off_t
)(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
))) < 0)) {
1098 tty_warn(1, "Could not write bcpio header for %s",
1104 * if we have file data, tell the caller we are done
1106 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
1107 (arcn
->type
== PAX_HRG
))
1111 * if we are not a link, tell the caller we are done, go to next file
1113 if (arcn
->type
!= PAX_SLK
)
1117 * write the link name, tell the caller we are done.
1119 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
1120 (wr_skip((off_t
)(BCPIO_PAD(arcn
->ln_nlen
))) < 0)) {
1121 tty_warn(1,"Could not write bcpio link name for %s",
1129 * header field is out of range
1131 tty_warn(1,"Bcpio header field is too small for file %s",