2 * Copyright (c) 1992 Keith Muller.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Keith Muller of the University of California, San Diego.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid
[] = "@(#)cpio.c 8.1 (Berkeley) 5/31/93";
40 #include <sys/types.h>
52 static int rd_nm(ARCHD
*, int);
53 static int rd_ln_nm(ARCHD
*);
54 static int com_rd(ARCHD
*);
57 * Routines which support the different cpio versions
60 static int swp_head
; /* binary cpio header byte swap */
63 * Routines common to all versions of cpio
68 * Fire up the hard link detection code
70 * 0 if ok -1 otherwise (the return values of lnk_start())
81 * Called to determine if a header block is a valid trailer. We are
82 * passed the block, the in_sync flag (which tells us we are in resync
83 * mode; looking for a valid header), and cnt (which starts at zero)
84 * which is used to count the number of empty blocks we have seen so far.
86 * 0 if a valid trailer, -1 if not a valid trailer,
90 cpio_trail(ARCHD
*arcn
)
93 * look for trailer id in file we are about to process
95 if ((strcmp(arcn
->name
, TRAILER
) == 0) && (arcn
->sb
.st_size
== 0))
102 * operations common to all cpio read functions.
112 arcn
->org_name
= arcn
->name
;
113 switch(arcn
->sb
.st_mode
& C_IFMT
) {
115 arcn
->type
= PAX_FIF
;
118 arcn
->type
= PAX_DIR
;
121 arcn
->type
= PAX_BLK
;
124 arcn
->type
= PAX_CHR
;
127 arcn
->type
= PAX_SLK
;
130 arcn
->type
= PAX_SCK
;
136 * we have file data, set up skip (pad is set in the format
139 arcn
->sb
.st_mode
= (arcn
->sb
.st_mode
& 0xfff) | C_ISREG
;
140 arcn
->type
= PAX_REG
;
141 arcn
->skip
= arcn
->sb
.st_size
;
144 if (chk_lnk(arcn
) < 0)
151 * write the special file with the name trailer in the proper format
153 * result of the write of the trailer from the cpio specific write func
162 * create a trailer request and call the proper format write function
164 memset(&last
, 0, sizeof(last
));
165 last
.nlen
= sizeof(TRAILER
) - 1;
167 last
.sb
.st_nlink
= 1;
168 (void)strcpy(last
.name
, TRAILER
);
169 return((*frmt
->wr
)(&last
));
174 * read in the file name which follows the cpio header
176 * 0 if ok, -1 otherwise
180 rd_nm(ARCHD
*arcn
, int nsz
)
183 * do not even try bogus values
185 if ((nsz
== 0) || (nsz
> (int)sizeof(arcn
->name
))) {
186 paxwarn(1, "Cpio file name length %d is out of range", nsz
);
191 * read the name and make sure it is not empty and is \0 terminated
193 if ((rd_wrbuf(arcn
->name
,nsz
) != nsz
) || (arcn
->name
[nsz
-1] != '\0') ||
194 (arcn
->name
[0] == '\0')) {
195 paxwarn(1, "Cpio file name in header is corrupted");
203 * read in the link name for a file with links. The link name is stored
204 * like file data (and is NOT \0 terminated!)
206 * 0 if ok, -1 otherwise
210 rd_ln_nm(ARCHD
*arcn
)
213 * check the length specified for bogus values
215 if ((arcn
->sb
.st_size
== 0) ||
216 ((size_t)arcn
->sb
.st_size
>= sizeof(arcn
->ln_name
))) {
218 paxwarn(1, "Cpio link name length is invalid: %lu",
221 paxwarn(1, "Cpio link name length is invalid: %ju",
222 (uintmax_t)arcn
->sb
.st_size
);
228 * read in the link name and \0 terminate it
230 if (rd_wrbuf(arcn
->ln_name
, (int)arcn
->sb
.st_size
) !=
231 (int)arcn
->sb
.st_size
) {
232 paxwarn(1, "Cpio link name read error");
235 arcn
->ln_nlen
= arcn
->sb
.st_size
;
236 arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
239 * watch out for those empty link names
241 if (arcn
->ln_name
[0] == '\0') {
242 paxwarn(1, "Cpio link name is corrupt");
249 * Routines common to the extended byte oriented cpio format
254 * determine if a block given to us is a valid extended byte oriented
257 * 0 if a valid header, -1 otherwise
261 cpio_id(char *blk
, int size
)
263 if ((size
< (int)sizeof(HD_CPIO
)) ||
264 (strncmp(blk
, AMAGIC
, sizeof(AMAGIC
) - 1) != 0))
271 * determine if a buffer is a byte oriented extended cpio archive entry.
272 * convert and store the values in the ARCHD parameter.
274 * 0 if a valid header, -1 otherwise.
278 cpio_rd(ARCHD
*arcn
, char *buf
)
284 * check that this is a valid header, if not return -1
286 if (cpio_id(buf
, sizeof(HD_CPIO
)) < 0)
291 * byte oriented cpio (posix) does not have padding! extract the octal
292 * ascii fields from the header
295 arcn
->sb
.st_dev
= (dev_t
)asc_ul(hd
->c_dev
, sizeof(hd
->c_dev
), OCT
);
296 arcn
->sb
.st_ino
= (ino_t
)asc_ul(hd
->c_ino
, sizeof(hd
->c_ino
), OCT
);
297 arcn
->sb
.st_mode
= (mode_t
)asc_ul(hd
->c_mode
, sizeof(hd
->c_mode
), OCT
);
298 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->c_uid
, sizeof(hd
->c_uid
), OCT
);
299 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->c_gid
, sizeof(hd
->c_gid
), OCT
);
300 arcn
->sb
.st_nlink
= (nlink_t
)asc_ul(hd
->c_nlink
, sizeof(hd
->c_nlink
),
302 arcn
->sb
.st_rdev
= (dev_t
)asc_ul(hd
->c_rdev
, sizeof(hd
->c_rdev
), OCT
);
304 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->c_mtime
, sizeof(hd
->c_mtime
),
307 arcn
->sb
.st_mtime
= (time_t)asc_uqd(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;
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
< (int)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
< (int)sizeof(HD_VCPIO
)) ||
537 (strncmp(blk
, AVCMAGIC
, (int)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
);
595 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->c_mtime
,sizeof(hd
->c_mtime
),HEX
);
597 arcn
->sb
.st_mtime
= (time_t)asc_uqd(hd
->c_mtime
,sizeof(hd
->c_mtime
),HEX
);
599 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
601 arcn
->sb
.st_size
= (off_t
)asc_ul(hd
->c_filesize
,
602 sizeof(hd
->c_filesize
), HEX
);
604 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->c_filesize
,
605 sizeof(hd
->c_filesize
), HEX
);
607 arcn
->sb
.st_nlink
= (nlink_t
)asc_ul(hd
->c_nlink
, sizeof(hd
->c_nlink
),
609 devmajor
= (dev_t
)asc_ul(hd
->c_maj
, sizeof(hd
->c_maj
), HEX
);
610 devminor
= (dev_t
)asc_ul(hd
->c_min
, sizeof(hd
->c_min
), HEX
);
611 arcn
->sb
.st_dev
= TODEV(devmajor
, devminor
);
612 devmajor
= (dev_t
)asc_ul(hd
->c_rmaj
, sizeof(hd
->c_maj
), HEX
);
613 devminor
= (dev_t
)asc_ul(hd
->c_rmin
, sizeof(hd
->c_min
), HEX
);
614 arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
615 arcn
->crc
= asc_ul(hd
->c_chksum
, sizeof(hd
->c_chksum
), HEX
);
618 * check the length of the file name, if ok read it in, return -1 if
621 if ((nsz
= (int)asc_ul(hd
->c_namesize
,sizeof(hd
->c_namesize
),HEX
)) < 2)
623 arcn
->nlen
= nsz
- 1;
624 if (rd_nm(arcn
, nsz
) < 0)
628 * skip padding. header + filename is aligned to 4 byte boundries
630 if (rd_skip((off_t
)(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
))) < 0)
634 * if not a link (or a file with no data), calculate pad size (for
635 * padding which follows the file data), clear the link name and return
637 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
639 * we have a valid header (not a link)
642 arcn
->ln_name
[0] = '\0';
643 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
644 return(com_rd(arcn
));
648 * read in the link name and skip over the padding
650 if ((rd_ln_nm(arcn
) < 0) ||
651 (rd_skip((off_t
)(VCPIO_PAD(arcn
->sb
.st_size
))) < 0))
655 * we have a valid header (with a link)
657 return(com_rd(arcn
));
662 * no cleanup needed here, just return size of the trailer (for append)
664 * size of trailer header in this format
670 return((off_t
)(sizeof(HD_VCPIO
) + sizeof(TRAILER
) +
671 (VCPIO_PAD(sizeof(HD_VCPIO
) + sizeof(TRAILER
)))));
676 * start up the device mapping table, enable crc file calculation
678 * 0 if ok, -1 otherwise (what dev_start() returns)
690 * copy the data in the ARCHD to buffer in system VR4 cpio
691 * (with/without crc) format.
693 * 0 if file has data to be written after the header, 1 if file has
694 * NO data to write after the header, -1 if archive write failed
698 vcpio_wr(ARCHD
*arcn
)
702 char hdblk
[sizeof(HD_VCPIO
)];
705 * check and repair truncated device and inode fields in the cpio
708 if (map_dev(arcn
, (u_long
)VCPIO_MASK
, (u_long
)VCPIO_MASK
) < 0)
710 nsz
= arcn
->nlen
+ 1;
711 hd
= (HD_VCPIO
*)hdblk
;
712 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
713 arcn
->sb
.st_rdev
= 0;
716 * add the proper magic value depending whether we were asked for
717 * file data crc's, and the crc if needed.
720 if (ul_asc((u_long
)VCMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
),
722 ul_asc((u_long
)arcn
->crc
,hd
->c_chksum
,sizeof(hd
->c_chksum
),
726 if (ul_asc((u_long
)VMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
),
728 ul_asc((u_long
)0L, hd
->c_chksum
, sizeof(hd
->c_chksum
),HEX
))
737 * caller will copy file data to the archive. tell him how
740 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
742 if (ul_asc((u_long
)arcn
->sb
.st_size
, hd
->c_filesize
,
743 sizeof(hd
->c_filesize
), HEX
)) {
745 if (uqd_asc((u_quad_t
)arcn
->sb
.st_size
, hd
->c_filesize
,
746 sizeof(hd
->c_filesize
), HEX
)) {
748 paxwarn(1,"File is too large for sv4cpio format %s",
755 * no file data for the caller to process, the file data has
756 * the size of the link
759 if (ul_asc((u_long
)arcn
->ln_nlen
, hd
->c_filesize
,
760 sizeof(hd
->c_filesize
), HEX
))
765 * no file data for the caller to process
768 if (ul_asc((u_long
)0L, hd
->c_filesize
, sizeof(hd
->c_filesize
),
775 * set the other fields in the header
777 if (ul_asc((u_long
)arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
),
779 ul_asc((u_long
)arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
),
781 ul_asc((u_long
)arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
),
783 ul_asc((u_long
)arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
),
785 ul_asc((u_long
)arcn
->sb
.st_mtime
, hd
->c_mtime
, sizeof(hd
->c_mtime
),
787 ul_asc((u_long
)arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
),
789 ul_asc((u_long
)major(arcn
->sb
.st_dev
),hd
->c_maj
, sizeof(hd
->c_maj
),
791 ul_asc((u_long
)minor(arcn
->sb
.st_dev
),hd
->c_min
, sizeof(hd
->c_min
),
793 ul_asc((u_long
)major(arcn
->sb
.st_rdev
),hd
->c_rmaj
,sizeof(hd
->c_maj
),
795 ul_asc((u_long
)minor(arcn
->sb
.st_rdev
),hd
->c_rmin
,sizeof(hd
->c_min
),
797 ul_asc((u_long
)nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), HEX
))
801 * write the header, the file name and padding as required.
803 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_VCPIO
)) < 0) ||
804 (wr_rdbuf(arcn
->name
, (int)nsz
) < 0) ||
805 (wr_skip((off_t
)(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
))) < 0)) {
806 paxwarn(1,"Could not write sv4cpio header for %s",arcn
->org_name
);
811 * if we have file data, tell the caller we are done, copy the file
813 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
814 (arcn
->type
== PAX_HRG
))
818 * if we are not a link, tell the caller we are done, go to next file
820 if (arcn
->type
!= PAX_SLK
)
824 * write the link name, tell the caller we are done.
826 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
827 (wr_skip((off_t
)(VCPIO_PAD(arcn
->ln_nlen
))) < 0)) {
828 paxwarn(1,"Could not write sv4cpio link name for %s",
836 * header field is out of range
838 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn
->org_name
);
843 * Routines common to the old binary header cpio
848 * determine if a block given to us is an old binary cpio header
849 * (with/without header byte swapping)
851 * 0 if a valid header, -1 otherwise
855 bcpio_id(char *blk
, int size
)
857 if (size
< (int)sizeof(HD_BCPIO
))
861 * check both normal and byte swapped magic cookies
863 if (((u_short
)SHRT_EXT(blk
)) == MAGIC
)
865 if (((u_short
)RSHRT_EXT(blk
)) == MAGIC
) {
875 * determine if a buffer is an old binary archive entry. (It may have byte
876 * swapped header) convert and store the values in the ARCHD parameter.
877 * This is a very old header format and should not really be used.
879 * 0 if a valid header, -1 otherwise.
883 bcpio_rd(ARCHD
*arcn
, char *buf
)
891 if (bcpio_id(buf
, sizeof(HD_BCPIO
)) < 0)
895 hd
= (HD_BCPIO
*)buf
;
898 * header has swapped bytes on 16 bit boundaries
900 arcn
->sb
.st_dev
= (dev_t
)(RSHRT_EXT(hd
->h_dev
));
901 arcn
->sb
.st_ino
= (ino_t
)(RSHRT_EXT(hd
->h_ino
));
902 arcn
->sb
.st_mode
= (mode_t
)(RSHRT_EXT(hd
->h_mode
));
903 arcn
->sb
.st_uid
= (uid_t
)(RSHRT_EXT(hd
->h_uid
));
904 arcn
->sb
.st_gid
= (gid_t
)(RSHRT_EXT(hd
->h_gid
));
905 arcn
->sb
.st_nlink
= (nlink_t
)(RSHRT_EXT(hd
->h_nlink
));
906 arcn
->sb
.st_rdev
= (dev_t
)(RSHRT_EXT(hd
->h_rdev
));
907 arcn
->sb
.st_mtime
= (time_t)(RSHRT_EXT(hd
->h_mtime_1
));
908 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
909 ((time_t)(RSHRT_EXT(hd
->h_mtime_2
)));
910 arcn
->sb
.st_size
= (off_t
)(RSHRT_EXT(hd
->h_filesize_1
));
911 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
912 ((off_t
)(RSHRT_EXT(hd
->h_filesize_2
)));
913 nsz
= (int)(RSHRT_EXT(hd
->h_namesize
));
915 arcn
->sb
.st_dev
= (dev_t
)(SHRT_EXT(hd
->h_dev
));
916 arcn
->sb
.st_ino
= (ino_t
)(SHRT_EXT(hd
->h_ino
));
917 arcn
->sb
.st_mode
= (mode_t
)(SHRT_EXT(hd
->h_mode
));
918 arcn
->sb
.st_uid
= (uid_t
)(SHRT_EXT(hd
->h_uid
));
919 arcn
->sb
.st_gid
= (gid_t
)(SHRT_EXT(hd
->h_gid
));
920 arcn
->sb
.st_nlink
= (nlink_t
)(SHRT_EXT(hd
->h_nlink
));
921 arcn
->sb
.st_rdev
= (dev_t
)(SHRT_EXT(hd
->h_rdev
));
922 arcn
->sb
.st_mtime
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
923 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
924 ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
925 arcn
->sb
.st_size
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
926 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
927 ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
928 nsz
= (int)(SHRT_EXT(hd
->h_namesize
));
930 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
933 * check the file name size, if bogus give up. otherwise read the file
938 arcn
->nlen
= nsz
- 1;
939 if (rd_nm(arcn
, nsz
) < 0)
943 * header + file name are aligned to 2 byte boundries, skip if needed
945 if (rd_skip((off_t
)(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
))) < 0)
949 * if not a link (or a file with no data), calculate pad size (for
950 * padding which follows the file data), clear the link name and return
952 if (((arcn
->sb
.st_mode
& C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)){
954 * we have a valid header (not a link)
957 arcn
->ln_name
[0] = '\0';
958 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
959 return(com_rd(arcn
));
962 if ((rd_ln_nm(arcn
) < 0) ||
963 (rd_skip((off_t
)(BCPIO_PAD(arcn
->sb
.st_size
))) < 0))
967 * we have a valid header (with a link)
969 return(com_rd(arcn
));
974 * no cleanup needed here, just return size of the trailer (for append)
976 * size of trailer header in this format
982 return((off_t
)(sizeof(HD_BCPIO
) + sizeof(TRAILER
) +
983 (BCPIO_PAD(sizeof(HD_BCPIO
) + sizeof(TRAILER
)))));
988 * copy the data in the ARCHD to buffer in old binary cpio format
989 * There is a real chance of field overflow with this critter. So we
990 * always check the conversion is ok. nobody in his their right mind
991 * should write an achive in this format...
993 * 0 if file has data to be written after the header, 1 if file has NO
994 * data to write after the header, -1 if archive write failed
998 bcpio_wr(ARCHD
*arcn
)
1002 char hdblk
[sizeof(HD_BCPIO
)];
1008 * check and repair truncated device and inode fields in the cpio
1011 if (map_dev(arcn
, (u_long
)BCPIO_MASK
, (u_long
)BCPIO_MASK
) < 0)
1014 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
1015 arcn
->sb
.st_rdev
= 0;
1016 hd
= (HD_BCPIO
*)hdblk
;
1018 switch(arcn
->type
) {
1023 * caller will copy file data to the archive. tell him how
1026 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
1027 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->sb
.st_size
);
1028 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->sb
.st_size
);
1029 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->sb
.st_size
);
1030 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->sb
.st_size
);
1031 t_offt
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
1032 t_offt
= (t_offt
<<16) | ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
1033 if (arcn
->sb
.st_size
!= t_offt
) {
1034 paxwarn(1,"File is too large for bcpio format %s",
1041 * no file data for the caller to process, the file data has
1042 * the size of the link
1045 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->ln_nlen
);
1046 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->ln_nlen
);
1047 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->ln_nlen
);
1048 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->ln_nlen
);
1049 t_int
= (int)(SHRT_EXT(hd
->h_filesize_1
));
1050 t_int
= (t_int
<< 16) | ((int)(SHRT_EXT(hd
->h_filesize_2
)));
1051 if (arcn
->ln_nlen
!= t_int
)
1056 * no file data for the caller to process
1059 hd
->h_filesize_1
[0] = (char)0;
1060 hd
->h_filesize_1
[1] = (char)0;
1061 hd
->h_filesize_2
[0] = (char)0;
1062 hd
->h_filesize_2
[1] = (char)0;
1067 * build up the rest of the fields
1069 hd
->h_magic
[0] = CHR_WR_2(MAGIC
);
1070 hd
->h_magic
[1] = CHR_WR_3(MAGIC
);
1071 hd
->h_dev
[0] = CHR_WR_2(arcn
->sb
.st_dev
);
1072 hd
->h_dev
[1] = CHR_WR_3(arcn
->sb
.st_dev
);
1073 if (arcn
->sb
.st_dev
!= (dev_t
)(SHRT_EXT(hd
->h_dev
)))
1075 hd
->h_ino
[0] = CHR_WR_2(arcn
->sb
.st_ino
);
1076 hd
->h_ino
[1] = CHR_WR_3(arcn
->sb
.st_ino
);
1077 if (arcn
->sb
.st_ino
!= (ino_t
)(SHRT_EXT(hd
->h_ino
)))
1079 hd
->h_mode
[0] = CHR_WR_2(arcn
->sb
.st_mode
);
1080 hd
->h_mode
[1] = CHR_WR_3(arcn
->sb
.st_mode
);
1081 if (arcn
->sb
.st_mode
!= (mode_t
)(SHRT_EXT(hd
->h_mode
)))
1083 hd
->h_uid
[0] = CHR_WR_2(arcn
->sb
.st_uid
);
1084 hd
->h_uid
[1] = CHR_WR_3(arcn
->sb
.st_uid
);
1085 if (arcn
->sb
.st_uid
!= (uid_t
)(SHRT_EXT(hd
->h_uid
)))
1087 hd
->h_gid
[0] = CHR_WR_2(arcn
->sb
.st_gid
);
1088 hd
->h_gid
[1] = CHR_WR_3(arcn
->sb
.st_gid
);
1089 if (arcn
->sb
.st_gid
!= (gid_t
)(SHRT_EXT(hd
->h_gid
)))
1091 hd
->h_nlink
[0] = CHR_WR_2(arcn
->sb
.st_nlink
);
1092 hd
->h_nlink
[1] = CHR_WR_3(arcn
->sb
.st_nlink
);
1093 if (arcn
->sb
.st_nlink
!= (nlink_t
)(SHRT_EXT(hd
->h_nlink
)))
1095 hd
->h_rdev
[0] = CHR_WR_2(arcn
->sb
.st_rdev
);
1096 hd
->h_rdev
[1] = CHR_WR_3(arcn
->sb
.st_rdev
);
1097 if (arcn
->sb
.st_rdev
!= (dev_t
)(SHRT_EXT(hd
->h_rdev
)))
1099 hd
->h_mtime_1
[0] = CHR_WR_0(arcn
->sb
.st_mtime
);
1100 hd
->h_mtime_1
[1] = CHR_WR_1(arcn
->sb
.st_mtime
);
1101 hd
->h_mtime_2
[0] = CHR_WR_2(arcn
->sb
.st_mtime
);
1102 hd
->h_mtime_2
[1] = CHR_WR_3(arcn
->sb
.st_mtime
);
1103 t_timet
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
1104 t_timet
= (t_timet
<< 16) | ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
1105 if (arcn
->sb
.st_mtime
!= t_timet
)
1107 nsz
= arcn
->nlen
+ 1;
1108 hd
->h_namesize
[0] = CHR_WR_2(nsz
);
1109 hd
->h_namesize
[1] = CHR_WR_3(nsz
);
1110 if (nsz
!= (int)(SHRT_EXT(hd
->h_namesize
)))
1114 * write the header, the file name and padding as required.
1116 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_BCPIO
)) < 0) ||
1117 (wr_rdbuf(arcn
->name
, nsz
) < 0) ||
1118 (wr_skip((off_t
)(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
))) < 0)) {
1119 paxwarn(1, "Could not write bcpio header for %s", arcn
->org_name
);
1124 * if we have file data, tell the caller we are done
1126 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
1127 (arcn
->type
== PAX_HRG
))
1131 * if we are not a link, tell the caller we are done, go to next file
1133 if (arcn
->type
!= PAX_SLK
)
1137 * write the link name, tell the caller we are done.
1139 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
1140 (wr_skip((off_t
)(BCPIO_PAD(arcn
->ln_nlen
))) < 0)) {
1141 paxwarn(1,"Could not write bcpio link name for %s",arcn
->org_name
);
1148 * header field is out of range
1150 paxwarn(1,"Bcpio header field is too small for file %s", arcn
->org_name
);