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 * 3. 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
33 * @(#)cpio.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/bin/pax/cpio.c,v 1.12.2.1 2001/08/01 05:03:11 obrien Exp $
35 * $DragonFly: src/bin/pax/cpio.c,v 1.8 2006/09/27 21:58:08 pavalos Exp $
38 #include <sys/types.h>
49 static int rd_nm (ARCHD
*, int);
50 static int rd_ln_nm (ARCHD
*);
51 static int com_rd (ARCHD
*);
54 * Routines which support the different cpio versions
57 static int swp_head
; /* binary cpio header byte swap */
60 * Routines common to all versions of cpio
65 * Fire up the hard link detection code
67 * 0 if ok -1 otherwise (the return values of lnk_start())
78 * Called to determine if a header block is a valid trailer. We are
79 * passed the block, the in_sync flag (which tells us we are in resync
80 * mode; looking for a valid header), and cnt (which starts at zero)
81 * which is used to count the number of empty blocks we have seen so far.
83 * 0 if a valid trailer, -1 if not a valid trailer,
87 cpio_trail(ARCHD
*arcn
)
90 * look for trailer id in file we are about to process
92 if ((strcmp(arcn
->name
, TRAILER
) == 0) && (arcn
->sb
.st_size
== 0))
99 * operations common to all cpio read functions.
109 arcn
->org_name
= arcn
->name
;
110 switch(arcn
->sb
.st_mode
& C_IFMT
) {
112 arcn
->type
= PAX_FIF
;
115 arcn
->type
= PAX_DIR
;
118 arcn
->type
= PAX_BLK
;
121 arcn
->type
= PAX_CHR
;
124 arcn
->type
= PAX_SLK
;
127 arcn
->type
= PAX_SCK
;
133 * we have file data, set up skip (pad is set in the format
136 arcn
->sb
.st_mode
= (arcn
->sb
.st_mode
& 0xfff) | C_ISREG
;
137 arcn
->type
= PAX_REG
;
138 arcn
->skip
= arcn
->sb
.st_size
;
141 if (chk_lnk(arcn
) < 0)
148 * write the special file with the name trailer in the proper format
150 * result of the write of the trailer from the cpio specific write func
159 * create a trailer request and call the proper format write function
161 memset(&last
, 0, sizeof(last
));
162 last
.nlen
= sizeof(TRAILER
) - 1;
164 last
.sb
.st_nlink
= 1;
165 strcpy(last
.name
, TRAILER
);
166 return((*frmt
->wr
)(&last
));
171 * read in the file name which follows the cpio header
173 * 0 if ok, -1 otherwise
177 rd_nm(ARCHD
*arcn
, int nsz
)
180 * do not even try bogus values
182 if ((nsz
== 0) || (nsz
> sizeof(arcn
->name
))) {
183 paxwarn(1, "Cpio file name length %d is out of range", nsz
);
188 * read the name and make sure it is not empty and is \0 terminated
190 if ((rd_wrbuf(arcn
->name
,nsz
) != nsz
) || (arcn
->name
[nsz
-1] != '\0') ||
191 (arcn
->name
[0] == '\0')) {
192 paxwarn(1, "Cpio file name in header is corrupted");
200 * read in the link name for a file with links. The link name is stored
201 * like file data (and is NOT \0 terminated!)
203 * 0 if ok, -1 otherwise
207 rd_ln_nm(ARCHD
*arcn
)
210 * check the length specified for bogus values
212 if ((arcn
->sb
.st_size
== 0) ||
213 (arcn
->sb
.st_size
>= sizeof(arcn
->ln_name
))) {
214 paxwarn(1, "Cpio link name length is invalid: %jd",
215 (intmax_t)arcn
->sb
.st_size
);
220 * read in the link name and \0 terminate it
222 if (rd_wrbuf(arcn
->ln_name
, (int)arcn
->sb
.st_size
) !=
223 (int)arcn
->sb
.st_size
) {
224 paxwarn(1, "Cpio link name read error");
227 arcn
->ln_nlen
= arcn
->sb
.st_size
;
228 arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
231 * watch out for those empty link names
233 if (arcn
->ln_name
[0] == '\0') {
234 paxwarn(1, "Cpio link name is corrupt");
241 * Routines common to the extended byte oriented cpio format
246 * determine if a block given to us is a valid extended byte oriented
249 * 0 if a valid header, -1 otherwise
253 cpio_id(char *blk
, int size
)
255 if ((size
< sizeof(HD_CPIO
)) ||
256 (strncmp(blk
, AMAGIC
, sizeof(AMAGIC
) - 1) != 0))
263 * determine if a buffer is a byte oriented extended cpio archive entry.
264 * convert and store the values in the ARCHD parameter.
266 * 0 if a valid header, -1 otherwise.
270 cpio_rd(ARCHD
*arcn
, char *buf
)
276 * check that this is a valid header, if not return -1
278 if (cpio_id(buf
, sizeof(HD_CPIO
)) < 0)
283 * byte oriented cpio (posix) does not have padding! extract the octal
284 * ascii fields from the header
287 arcn
->sb
.st_dev
= (dev_t
)asc_ul(hd
->c_dev
, sizeof(hd
->c_dev
), OCT
);
288 arcn
->sb
.st_ino
= (ino_t
)asc_ul(hd
->c_ino
, sizeof(hd
->c_ino
), OCT
);
289 arcn
->sb
.st_mode
= (mode_t
)asc_ul(hd
->c_mode
, sizeof(hd
->c_mode
), OCT
);
290 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->c_uid
, sizeof(hd
->c_uid
), OCT
);
291 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->c_gid
, sizeof(hd
->c_gid
), OCT
);
292 arcn
->sb
.st_nlink
= (nlink_t
)asc_ul(hd
->c_nlink
, sizeof(hd
->c_nlink
),
294 arcn
->sb
.st_rdev
= (dev_t
)asc_ul(hd
->c_rdev
, sizeof(hd
->c_rdev
), OCT
);
295 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->c_mtime
, sizeof(hd
->c_mtime
),
297 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
298 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->c_filesize
,sizeof(hd
->c_filesize
),
302 * check name size and if valid, read in the name of this entry (name
303 * follows header in the archive)
305 if ((nsz
= (int)asc_ul(hd
->c_namesize
,sizeof(hd
->c_namesize
),OCT
)) < 2)
307 arcn
->nlen
= nsz
- 1;
308 if (rd_nm(arcn
, nsz
) < 0)
311 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
313 * no link name to read for this file
316 arcn
->ln_name
[0] = '\0';
317 return(com_rd(arcn
));
321 * check link name size and read in the link name. Link names are
322 * stored like file data.
324 if (rd_ln_nm(arcn
) < 0)
328 * we have a valid header (with a link)
330 return(com_rd(arcn
));
335 * no cleanup needed here, just return size of the trailer (for append)
337 * size of trailer header in this format
343 return((off_t
)(sizeof(HD_CPIO
) + sizeof(TRAILER
)));
348 * start up the device mapping table
350 * 0 if ok, -1 otherwise (what dev_start() returns)
361 * copy the data in the ARCHD to buffer in extended byte oriented cpio
364 * 0 if file has data to be written after the header, 1 if file has NO
365 * data to write after the header, -1 if archive write failed
373 char hdblk
[sizeof(HD_CPIO
)];
376 * check and repair truncated device and inode fields in the header
378 if (map_dev(arcn
, (u_long
)CPIO_MASK
, (u_long
)CPIO_MASK
) < 0)
382 nsz
= arcn
->nlen
+ 1;
383 hd
= (HD_CPIO
*)hdblk
;
384 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
385 arcn
->sb
.st_rdev
= 0;
392 * set data size for file data
394 if (uqd_asc((u_quad_t
)arcn
->sb
.st_size
, hd
->c_filesize
,
395 sizeof(hd
->c_filesize
), OCT
)) {
396 paxwarn(1,"File is too large for cpio format %s",
403 * set data size to hold link name
405 if (ul_asc((u_long
)arcn
->ln_nlen
, hd
->c_filesize
,
406 sizeof(hd
->c_filesize
), OCT
))
411 * all other file types have no file data
413 if (ul_asc((u_long
)0, hd
->c_filesize
, sizeof(hd
->c_filesize
),
420 * copy the values to the header using octal ascii
422 if (ul_asc((u_long
)MAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
), OCT
) ||
423 ul_asc((u_long
)arcn
->sb
.st_dev
, hd
->c_dev
, sizeof(hd
->c_dev
),
425 ul_asc((u_long
)arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
),
427 ul_asc((u_long
)arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
),
429 ul_asc((u_long
)arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
),
431 ul_asc((u_long
)arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
),
433 ul_asc((u_long
)arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
),
435 ul_asc((u_long
)arcn
->sb
.st_rdev
, hd
->c_rdev
, sizeof(hd
->c_rdev
),
437 ul_asc((u_long
)arcn
->sb
.st_mtime
,hd
->c_mtime
,sizeof(hd
->c_mtime
),
439 ul_asc((u_long
)nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), OCT
))
443 * write the file name to the archive
445 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_CPIO
)) < 0) ||
446 (wr_rdbuf(arcn
->name
, nsz
) < 0)) {
447 paxwarn(1, "Unable to write cpio header for %s", arcn
->org_name
);
452 * if this file has data, we are done. The caller will write the file
453 * data, if we are link tell caller we are done, go to next file
455 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
456 (arcn
->type
== PAX_HRG
))
458 if (arcn
->type
!= PAX_SLK
)
462 * write the link name to the archive, tell the caller to go to the
463 * next file as we are done.
465 if (wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) {
466 paxwarn(1,"Unable to write cpio link name for %s",arcn
->org_name
);
473 * header field is out of range
475 paxwarn(1, "Cpio header field is too small to store file %s",
481 * Routines common to the system VR4 version of cpio (with/without file CRC)
486 * determine if a block given to us is a valid system VR4 cpio header
487 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
490 * 0 if a valid header, -1 otherwise
494 vcpio_id(char *blk
, int size
)
496 if ((size
< sizeof(HD_VCPIO
)) ||
497 (strncmp(blk
, AVMAGIC
, sizeof(AVMAGIC
) - 1) != 0))
504 * determine if a block given to us is a valid system VR4 cpio header
505 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
507 * 0 if a valid header, -1 otherwise
511 crc_id(char *blk
, int size
)
513 if ((size
< sizeof(HD_VCPIO
)) ||
514 (strncmp(blk
, AVCMAGIC
, sizeof(AVCMAGIC
) - 1) != 0))
521 w set file data CRC calculations. Fire up the hard link detection code
523 * 0 if ok -1 otherwise (the return values of lnk_start())
535 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
536 * convert and store the values in the ARCHD parameter.
538 * 0 if a valid header, -1 otherwise.
542 vcpio_rd(ARCHD
*arcn
, char *buf
)
550 * during the id phase it was determined if we were using CRC, use the
554 if (crc_id(buf
, sizeof(HD_VCPIO
)) < 0)
557 if (vcpio_id(buf
, sizeof(HD_VCPIO
)) < 0)
561 hd
= (HD_VCPIO
*)buf
;
565 * extract the hex ascii fields from the header
567 arcn
->sb
.st_ino
= (ino_t
)asc_ul(hd
->c_ino
, sizeof(hd
->c_ino
), HEX
);
568 arcn
->sb
.st_mode
= (mode_t
)asc_ul(hd
->c_mode
, sizeof(hd
->c_mode
), HEX
);
569 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->c_uid
, sizeof(hd
->c_uid
), HEX
);
570 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->c_gid
, sizeof(hd
->c_gid
), HEX
);
571 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->c_mtime
,sizeof(hd
->c_mtime
),HEX
);
572 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
573 arcn
->sb
.st_size
= (off_t
)asc_uqd(hd
->c_filesize
,
574 sizeof(hd
->c_filesize
), HEX
);
575 arcn
->sb
.st_nlink
= (nlink_t
)asc_ul(hd
->c_nlink
, sizeof(hd
->c_nlink
),
577 devmajor
= (dev_t
)asc_ul(hd
->c_maj
, sizeof(hd
->c_maj
), HEX
);
578 devminor
= (dev_t
)asc_ul(hd
->c_min
, sizeof(hd
->c_min
), HEX
);
579 arcn
->sb
.st_dev
= TODEV(devmajor
, devminor
);
580 devmajor
= (dev_t
)asc_ul(hd
->c_rmaj
, sizeof(hd
->c_maj
), HEX
);
581 devminor
= (dev_t
)asc_ul(hd
->c_rmin
, sizeof(hd
->c_min
), HEX
);
582 arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
583 arcn
->crc
= asc_ul(hd
->c_chksum
, sizeof(hd
->c_chksum
), HEX
);
586 * check the length of the file name, if ok read it in, return -1 if
589 if ((nsz
= (int)asc_ul(hd
->c_namesize
,sizeof(hd
->c_namesize
),HEX
)) < 2)
591 arcn
->nlen
= nsz
- 1;
592 if (rd_nm(arcn
, nsz
) < 0)
596 * skip padding. header + filename is aligned to 4 byte boundaries
598 if (rd_skip((off_t
)(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
))) < 0)
602 * if not a link (or a file with no data), calculate pad size (for
603 * padding which follows the file data), clear the link name and return
605 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
607 * we have a valid header (not a link)
610 arcn
->ln_name
[0] = '\0';
611 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
612 return(com_rd(arcn
));
616 * read in the link name and skip over the padding
618 if ((rd_ln_nm(arcn
) < 0) ||
619 (rd_skip((off_t
)(VCPIO_PAD(arcn
->sb
.st_size
))) < 0))
623 * we have a valid header (with a link)
625 return(com_rd(arcn
));
630 * no cleanup needed here, just return size of the trailer (for append)
632 * size of trailer header in this format
638 return((off_t
)(sizeof(HD_VCPIO
) + sizeof(TRAILER
) +
639 (VCPIO_PAD(sizeof(HD_VCPIO
) + sizeof(TRAILER
)))));
644 * start up the device mapping table, enable crc file calculation
646 * 0 if ok, -1 otherwise (what dev_start() returns)
658 * copy the data in the ARCHD to buffer in system VR4 cpio
659 * (with/without crc) format.
661 * 0 if file has data to be written after the header, 1 if file has
662 * NO data to write after the header, -1 if archive write failed
666 vcpio_wr(ARCHD
*arcn
)
670 char hdblk
[sizeof(HD_VCPIO
)];
673 * check and repair truncated device and inode fields in the cpio
676 if (map_dev(arcn
, (u_long
)VCPIO_MASK
, (u_long
)VCPIO_MASK
) < 0)
678 nsz
= arcn
->nlen
+ 1;
679 hd
= (HD_VCPIO
*)hdblk
;
680 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
681 arcn
->sb
.st_rdev
= 0;
684 * add the proper magic value depending whether we were asked for
685 * file data crc's, and the crc if needed.
688 if (ul_asc((u_long
)VCMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
),
690 ul_asc((u_long
)arcn
->crc
,hd
->c_chksum
,sizeof(hd
->c_chksum
),
694 if (ul_asc((u_long
)VMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
),
696 ul_asc((u_long
)0L, hd
->c_chksum
, sizeof(hd
->c_chksum
),HEX
))
705 * caller will copy file data to the archive. tell him how
708 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
709 if (uqd_asc((u_quad_t
)arcn
->sb
.st_size
, hd
->c_filesize
,
710 sizeof(hd
->c_filesize
), HEX
)) {
711 paxwarn(1,"File is too large for sv4cpio format %s",
718 * no file data for the caller to process, the file data has
719 * the size of the link
722 if (ul_asc((u_long
)arcn
->ln_nlen
, hd
->c_filesize
,
723 sizeof(hd
->c_filesize
), HEX
))
728 * no file data for the caller to process
731 if (ul_asc((u_long
)0L, hd
->c_filesize
, sizeof(hd
->c_filesize
),
738 * set the other fields in the header
740 if (ul_asc((u_long
)arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
),
742 ul_asc((u_long
)arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
),
744 ul_asc((u_long
)arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
),
746 ul_asc((u_long
)arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
),
748 ul_asc((u_long
)arcn
->sb
.st_mtime
, hd
->c_mtime
, sizeof(hd
->c_mtime
),
750 ul_asc((u_long
)arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
),
752 ul_asc((u_long
)MAJOR(arcn
->sb
.st_dev
),hd
->c_maj
, sizeof(hd
->c_maj
),
754 ul_asc((u_long
)MINOR(arcn
->sb
.st_dev
),hd
->c_min
, sizeof(hd
->c_min
),
756 ul_asc((u_long
)MAJOR(arcn
->sb
.st_rdev
),hd
->c_rmaj
,sizeof(hd
->c_maj
),
758 ul_asc((u_long
)MINOR(arcn
->sb
.st_rdev
),hd
->c_rmin
,sizeof(hd
->c_min
),
760 ul_asc((u_long
)nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), HEX
))
764 * write the header, the file name and padding as required.
766 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_VCPIO
)) < 0) ||
767 (wr_rdbuf(arcn
->name
, (int)nsz
) < 0) ||
768 (wr_skip((off_t
)(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
))) < 0)) {
769 paxwarn(1,"Could not write sv4cpio header for %s",arcn
->org_name
);
774 * if we have file data, tell the caller we are done, copy the file
776 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
777 (arcn
->type
== PAX_HRG
))
781 * if we are not a link, tell the caller we are done, go to next file
783 if (arcn
->type
!= PAX_SLK
)
787 * write the link name, tell the caller we are done.
789 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
790 (wr_skip((off_t
)(VCPIO_PAD(arcn
->ln_nlen
))) < 0)) {
791 paxwarn(1,"Could not write sv4cpio link name for %s",
799 * header field is out of range
801 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn
->org_name
);
806 * Routines common to the old binary header cpio
811 * determine if a block given to us is a old binary cpio header
812 * (with/without header byte swapping)
814 * 0 if a valid header, -1 otherwise
818 bcpio_id(char *blk
, int size
)
820 if (size
< sizeof(HD_BCPIO
))
824 * check both normal and byte swapped magic cookies
826 if (((u_short
)SHRT_EXT(blk
)) == MAGIC
)
828 if (((u_short
)RSHRT_EXT(blk
)) == MAGIC
) {
838 * determine if a buffer is a old binary archive entry. (it may have byte
839 * swapped header) convert and store the values in the ARCHD parameter.
840 * This is a very old header format and should not really be used.
842 * 0 if a valid header, -1 otherwise.
846 bcpio_rd(ARCHD
*arcn
, char *buf
)
854 if (bcpio_id(buf
, sizeof(HD_BCPIO
)) < 0)
858 hd
= (HD_BCPIO
*)buf
;
861 * header has swapped bytes on 16 bit boundaries
863 arcn
->sb
.st_dev
= (dev_t
)(RSHRT_EXT(hd
->h_dev
));
864 arcn
->sb
.st_ino
= (ino_t
)(RSHRT_EXT(hd
->h_ino
));
865 arcn
->sb
.st_mode
= (mode_t
)(RSHRT_EXT(hd
->h_mode
));
866 arcn
->sb
.st_uid
= (uid_t
)(RSHRT_EXT(hd
->h_uid
));
867 arcn
->sb
.st_gid
= (gid_t
)(RSHRT_EXT(hd
->h_gid
));
868 arcn
->sb
.st_nlink
= (nlink_t
)(RSHRT_EXT(hd
->h_nlink
));
869 arcn
->sb
.st_rdev
= (dev_t
)(RSHRT_EXT(hd
->h_rdev
));
870 arcn
->sb
.st_mtime
= (time_t)(RSHRT_EXT(hd
->h_mtime_1
));
871 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
872 ((time_t)(RSHRT_EXT(hd
->h_mtime_2
)));
873 arcn
->sb
.st_size
= (off_t
)(RSHRT_EXT(hd
->h_filesize_1
));
874 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
875 ((off_t
)(RSHRT_EXT(hd
->h_filesize_2
)));
876 nsz
= (int)(RSHRT_EXT(hd
->h_namesize
));
878 arcn
->sb
.st_dev
= (dev_t
)(SHRT_EXT(hd
->h_dev
));
879 arcn
->sb
.st_ino
= (ino_t
)(SHRT_EXT(hd
->h_ino
));
880 arcn
->sb
.st_mode
= (mode_t
)(SHRT_EXT(hd
->h_mode
));
881 arcn
->sb
.st_uid
= (uid_t
)(SHRT_EXT(hd
->h_uid
));
882 arcn
->sb
.st_gid
= (gid_t
)(SHRT_EXT(hd
->h_gid
));
883 arcn
->sb
.st_nlink
= (nlink_t
)(SHRT_EXT(hd
->h_nlink
));
884 arcn
->sb
.st_rdev
= (dev_t
)(SHRT_EXT(hd
->h_rdev
));
885 arcn
->sb
.st_mtime
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
886 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
887 ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
888 arcn
->sb
.st_size
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
889 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
890 ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
891 nsz
= (int)(SHRT_EXT(hd
->h_namesize
));
893 arcn
->sb
.st_ctime
= arcn
->sb
.st_atime
= arcn
->sb
.st_mtime
;
896 * check the file name size, if bogus give up. otherwise read the file
901 arcn
->nlen
= nsz
- 1;
902 if (rd_nm(arcn
, nsz
) < 0)
906 * header + file name are aligned to 2 byte boundaries, skip if needed
908 if (rd_skip((off_t
)(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
))) < 0)
912 * if not a link (or a file with no data), calculate pad size (for
913 * padding which follows the file data), clear the link name and return
915 if (((arcn
->sb
.st_mode
& C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)){
917 * we have a valid header (not a link)
920 arcn
->ln_name
[0] = '\0';
921 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
922 return(com_rd(arcn
));
925 if ((rd_ln_nm(arcn
) < 0) ||
926 (rd_skip((off_t
)(BCPIO_PAD(arcn
->sb
.st_size
))) < 0))
930 * we have a valid header (with a link)
932 return(com_rd(arcn
));
937 * no cleanup needed here, just return size of the trailer (for append)
939 * size of trailer header in this format
945 return((off_t
)(sizeof(HD_BCPIO
) + sizeof(TRAILER
) +
946 (BCPIO_PAD(sizeof(HD_BCPIO
) + sizeof(TRAILER
)))));
951 * copy the data in the ARCHD to buffer in old binary cpio format
952 * There is a real chance of field overflow with this critter. So we
953 * always check the conversion is ok. nobody in their right mind
954 * should write an archive in this format...
956 * 0 if file has data to be written after the header, 1 if file has NO
957 * data to write after the header, -1 if archive write failed
961 bcpio_wr(ARCHD
*arcn
)
965 char hdblk
[sizeof(HD_BCPIO
)];
971 * check and repair truncated device and inode fields in the cpio
974 if (map_dev(arcn
, (u_long
)BCPIO_MASK
, (u_long
)BCPIO_MASK
) < 0)
977 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
978 arcn
->sb
.st_rdev
= 0;
979 hd
= (HD_BCPIO
*)hdblk
;
986 * caller will copy file data to the archive. tell him how
989 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
990 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->sb
.st_size
);
991 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->sb
.st_size
);
992 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->sb
.st_size
);
993 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->sb
.st_size
);
994 t_offt
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
995 t_offt
= (t_offt
<<16) | ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
996 if (arcn
->sb
.st_size
!= t_offt
) {
997 paxwarn(1,"File is too large for bcpio format %s",
1004 * no file data for the caller to process, the file data has
1005 * the size of the link
1008 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->ln_nlen
);
1009 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->ln_nlen
);
1010 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->ln_nlen
);
1011 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->ln_nlen
);
1012 t_int
= (int)(SHRT_EXT(hd
->h_filesize_1
));
1013 t_int
= (t_int
<< 16) | ((int)(SHRT_EXT(hd
->h_filesize_2
)));
1014 if (arcn
->ln_nlen
!= t_int
)
1019 * no file data for the caller to process
1022 hd
->h_filesize_1
[0] = (char)0;
1023 hd
->h_filesize_1
[1] = (char)0;
1024 hd
->h_filesize_2
[0] = (char)0;
1025 hd
->h_filesize_2
[1] = (char)0;
1030 * build up the rest of the fields
1032 hd
->h_magic
[0] = CHR_WR_2(MAGIC
);
1033 hd
->h_magic
[1] = CHR_WR_3(MAGIC
);
1034 hd
->h_dev
[0] = CHR_WR_2(arcn
->sb
.st_dev
);
1035 hd
->h_dev
[1] = CHR_WR_3(arcn
->sb
.st_dev
);
1036 if (arcn
->sb
.st_dev
!= (dev_t
)(SHRT_EXT(hd
->h_dev
)))
1038 hd
->h_ino
[0] = CHR_WR_2(arcn
->sb
.st_ino
);
1039 hd
->h_ino
[1] = CHR_WR_3(arcn
->sb
.st_ino
);
1040 if (arcn
->sb
.st_ino
!= (ino_t
)(SHRT_EXT(hd
->h_ino
)))
1042 hd
->h_mode
[0] = CHR_WR_2(arcn
->sb
.st_mode
);
1043 hd
->h_mode
[1] = CHR_WR_3(arcn
->sb
.st_mode
);
1044 if (arcn
->sb
.st_mode
!= (mode_t
)(SHRT_EXT(hd
->h_mode
)))
1046 hd
->h_uid
[0] = CHR_WR_2(arcn
->sb
.st_uid
);
1047 hd
->h_uid
[1] = CHR_WR_3(arcn
->sb
.st_uid
);
1048 if (arcn
->sb
.st_uid
!= (uid_t
)(SHRT_EXT(hd
->h_uid
)))
1050 hd
->h_gid
[0] = CHR_WR_2(arcn
->sb
.st_gid
);
1051 hd
->h_gid
[1] = CHR_WR_3(arcn
->sb
.st_gid
);
1052 if (arcn
->sb
.st_gid
!= (gid_t
)(SHRT_EXT(hd
->h_gid
)))
1054 hd
->h_nlink
[0] = CHR_WR_2(arcn
->sb
.st_nlink
);
1055 hd
->h_nlink
[1] = CHR_WR_3(arcn
->sb
.st_nlink
);
1056 if (arcn
->sb
.st_nlink
!= (nlink_t
)(SHRT_EXT(hd
->h_nlink
)))
1058 hd
->h_rdev
[0] = CHR_WR_2(arcn
->sb
.st_rdev
);
1059 hd
->h_rdev
[1] = CHR_WR_3(arcn
->sb
.st_rdev
);
1060 if (arcn
->sb
.st_rdev
!= (dev_t
)(SHRT_EXT(hd
->h_rdev
)))
1062 hd
->h_mtime_1
[0] = CHR_WR_0(arcn
->sb
.st_mtime
);
1063 hd
->h_mtime_1
[1] = CHR_WR_1(arcn
->sb
.st_mtime
);
1064 hd
->h_mtime_2
[0] = CHR_WR_2(arcn
->sb
.st_mtime
);
1065 hd
->h_mtime_2
[1] = CHR_WR_3(arcn
->sb
.st_mtime
);
1066 t_timet
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
1067 t_timet
= (t_timet
<< 16) | ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
1068 if (arcn
->sb
.st_mtime
!= t_timet
)
1070 nsz
= arcn
->nlen
+ 1;
1071 hd
->h_namesize
[0] = CHR_WR_2(nsz
);
1072 hd
->h_namesize
[1] = CHR_WR_3(nsz
);
1073 if (nsz
!= (int)(SHRT_EXT(hd
->h_namesize
)))
1077 * write the header, the file name and padding as required.
1079 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_BCPIO
)) < 0) ||
1080 (wr_rdbuf(arcn
->name
, nsz
) < 0) ||
1081 (wr_skip((off_t
)(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
))) < 0)) {
1082 paxwarn(1, "Could not write bcpio header for %s", arcn
->org_name
);
1087 * if we have file data, tell the caller we are done
1089 if ((arcn
->type
== PAX_CTG
) || (arcn
->type
== PAX_REG
) ||
1090 (arcn
->type
== PAX_HRG
))
1094 * if we are not a link, tell the caller we are done, go to next file
1096 if (arcn
->type
!= PAX_SLK
)
1100 * write the link name, tell the caller we are done.
1102 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
1103 (wr_skip((off_t
)(BCPIO_PAD(arcn
->ln_nlen
))) < 0)) {
1104 paxwarn(1,"Could not write bcpio link name for %s",arcn
->org_name
);
1111 * header field is out of range
1113 paxwarn(1,"Bcpio header field is too small for file %s", arcn
->org_name
);