1 /* $OpenBSD: cpio.c,v 1.18 2008/01/01 16:22:44 tobias Exp $ */
2 /* $NetBSD: cpio.c,v 1.5 1995/03/21 09:07:13 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char sccsid[] = "@(#)cpio.c 8.1 (Berkeley) 5/31/93";
41 static const char rcsid[] = "$OpenBSD: cpio.c,v 1.18 2008/01/01 16:22:44 tobias Exp $";
45 #include <sys/types.h>
46 #include <sys/sysmacros.h>
49 #include <sys/param.h>
58 static int rd_nm(ARCHD *, int);
59 static int rd_ln_nm(ARCHD *);
60 static int com_rd(ARCHD *);
63 * Routines which support the different cpio versions
66 static int swp_head; /* binary cpio header byte swap */
69 * Routines common to all versions of cpio
74 * Fire up the hard link detection code
76 * 0 if ok -1 otherwise (the return values of lnk_start())
87 * Called to determine if a header block is a valid trailer. We are
88 * passed the block, the in_sync flag (which tells us we are in resync
89 * mode; looking for a valid header), and cnt (which starts at zero)
90 * which is used to count the number of empty blocks we have seen so far.
92 * 0 if a valid trailer, -1 if not a valid trailer,
96 cpio_trail(ARCHD *arcn, char *notused, int notused2, int *notused3)
99 * look for trailer id in file we are about to process
101 if ((strcmp(arcn->name, TRAILER) == 0) && (arcn->sb.st_size == 0))
108 * operations common to all cpio read functions.
118 arcn->org_name = arcn->name;
119 switch (arcn->sb.st_mode & C_IFMT) {
121 arcn->type = PAX_FIF;
124 arcn->type = PAX_DIR;
127 arcn->type = PAX_BLK;
130 arcn->type = PAX_CHR;
133 arcn->type = PAX_SLK;
136 arcn->type = PAX_SCK;
142 * we have file data, set up skip (pad is set in the format
145 arcn->sb.st_mode = (arcn->sb.st_mode & 0xfff) | C_ISREG;
146 arcn->type = PAX_REG;
147 arcn->skip = arcn->sb.st_size;
150 if (chk_lnk(arcn) < 0)
157 * write the special file with the name trailer in the proper format
159 * result of the write of the trailer from the cpio specific write func
168 * create a trailer request and call the proper format write function
170 memset(&last, 0, sizeof(last));
171 last.nlen = sizeof(TRAILER) - 1;
173 last.sb.st_nlink = 1;
174 (void)strlcpy(last.name, TRAILER, sizeof(last.name));
175 return((*frmt->wr)(&last));
180 * read in the file name which follows the cpio header
182 * 0 if ok, -1 otherwise
186 rd_nm(ARCHD *arcn, int nsz)
189 * do not even try bogus values
191 if ((nsz == 0) || (nsz > sizeof(arcn->name))) {
192 paxwarn(1, "Cpio file name length %d is out of range", nsz);
197 * read the name and make sure it is not empty and is \0 terminated
199 if ((rd_wrbuf(arcn->name,nsz) != nsz) || (arcn->name[nsz-1] != '\0') ||
200 (arcn->name[0] == '\0')) {
201 paxwarn(1, "Cpio file name in header is corrupted");
209 * read in the link name for a file with links. The link name is stored
210 * like file data (and is NOT \0 terminated!)
212 * 0 if ok, -1 otherwise
216 rd_ln_nm(ARCHD *arcn)
219 * check the length specified for bogus values
221 if ((arcn->sb.st_size == 0) ||
222 (arcn->sb.st_size >= sizeof(arcn->ln_name))) {
224 paxwarn(1, "Cpio link name length is invalid: %lu",
227 paxwarn(1, "Cpio link name length is invalid: %qu",
234 * read in the link name and \0 terminate it
236 if (rd_wrbuf(arcn->ln_name, (int)arcn->sb.st_size) !=
237 (int)arcn->sb.st_size) {
238 paxwarn(1, "Cpio link name read error");
241 arcn->ln_nlen = arcn->sb.st_size;
242 arcn->ln_name[arcn->ln_nlen] = '\0';
245 * watch out for those empty link names
247 if (arcn->ln_name[0] == '\0') {
248 paxwarn(1, "Cpio link name is corrupt");
255 * Routines common to the extended byte oriented cpio format
260 * determine if a block given to us is a valid extended byte oriented
263 * 0 if a valid header, -1 otherwise
267 cpio_id(char *blk, int size)
269 if ((size < sizeof(HD_CPIO)) ||
270 (strncmp(blk, AMAGIC, sizeof(AMAGIC) - 1) != 0))
277 * determine if a buffer is a byte oriented extended cpio archive entry.
278 * convert and store the values in the ARCHD parameter.
280 * 0 if a valid header, -1 otherwise.
284 cpio_rd(ARCHD *arcn, char *buf)
290 * check that this is a valid header, if not return -1
292 if (cpio_id(buf, sizeof(HD_CPIO)) < 0)
297 * byte oriented cpio (posix) does not have padding! extract the octal
298 * ascii fields from the header
301 arcn->sb.st_dev = (dev_t)asc_ul(hd->c_dev, sizeof(hd->c_dev), OCT);
302 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), OCT);
303 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), OCT);
304 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), OCT);
305 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), OCT);
306 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
308 arcn->sb.st_rdev = (dev_t)asc_ul(hd->c_rdev, sizeof(hd->c_rdev), OCT);
309 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime, sizeof(hd->c_mtime),
311 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
313 arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,sizeof(hd->c_filesize),
316 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,sizeof(hd->c_filesize),
321 * check name size and if valid, read in the name of this entry (name
322 * follows header in the archive)
324 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),OCT)) < 2)
326 arcn->nlen = nsz - 1;
327 if (rd_nm(arcn, nsz) < 0)
330 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
332 * no link name to read for this file
335 arcn->ln_name[0] = '\0';
336 return(com_rd(arcn));
340 * check link name size and read in the link name. Link names are
341 * stored like file data.
343 if (rd_ln_nm(arcn) < 0)
347 * we have a valid header (with a link)
349 return(com_rd(arcn));
354 * no cleanup needed here, just return size of the trailer (for append)
356 * size of trailer header in this format
362 return((off_t)(sizeof(HD_CPIO) + sizeof(TRAILER)));
367 * start up the device mapping table
369 * 0 if ok, -1 otherwise (what dev_start() returns)
380 * copy the data in the ARCHD to buffer in extended byte oriented cpio
383 * 0 if file has data to be written after the header, 1 if file has NO
384 * data to write after the header, -1 if archive write failed
392 char hdblk[sizeof(HD_CPIO)];
395 * check and repair truncated device and inode fields in the header
397 if (map_dev(arcn, (u_long)CPIO_MASK, (u_long)CPIO_MASK) < 0)
401 nsz = arcn->nlen + 1;
402 hd = (HD_CPIO *)hdblk;
403 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
404 arcn->sb.st_rdev = 0;
406 switch (arcn->type) {
411 * set data size for file data
414 if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize,
415 sizeof(hd->c_filesize), OCT)) {
417 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
418 sizeof(hd->c_filesize), OCT)) {
420 paxwarn(1,"File is too large for cpio format %s",
427 * set data size to hold link name
429 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
430 sizeof(hd->c_filesize), OCT))
435 * all other file types have no file data
437 if (ul_asc((u_long)0, hd->c_filesize, sizeof(hd->c_filesize),
444 * copy the values to the header using octal ascii
446 if (ul_asc((u_long)MAGIC, hd->c_magic, sizeof(hd->c_magic), OCT) ||
447 ul_asc((u_long)arcn->sb.st_dev, hd->c_dev, sizeof(hd->c_dev),
449 ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
451 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
453 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
455 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
457 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
459 ul_asc((u_long)arcn->sb.st_rdev, hd->c_rdev, sizeof(hd->c_rdev),
461 ul_asc((u_long)arcn->sb.st_mtime,hd->c_mtime,sizeof(hd->c_mtime),
463 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), OCT))
467 * write the file name to the archive
469 if ((wr_rdbuf(hdblk, (int)sizeof(HD_CPIO)) < 0) ||
470 (wr_rdbuf(arcn->name, nsz) < 0)) {
471 paxwarn(1, "Unable to write cpio header for %s", arcn->org_name);
476 * if this file has data, we are done. The caller will write the file
477 * data, if we are link tell caller we are done, go to next file
479 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
480 (arcn->type == PAX_HRG))
482 if (arcn->type != PAX_SLK)
486 * write the link name to the archive, tell the caller to go to the
487 * next file as we are done.
489 if (wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) {
490 paxwarn(1,"Unable to write cpio link name for %s",arcn->org_name);
497 * header field is out of range
499 paxwarn(1, "Cpio header field is too small to store file %s",
505 * Routines common to the system VR4 version of cpio (with/without file CRC)
510 * determine if a block given to us is a valid system VR4 cpio header
511 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
514 * 0 if a valid header, -1 otherwise
518 vcpio_id(char *blk, int size)
520 if ((size < sizeof(HD_VCPIO)) ||
521 (strncmp(blk, AVMAGIC, sizeof(AVMAGIC) - 1) != 0))
528 * determine if a block given to us is a valid system VR4 cpio header
529 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
531 * 0 if a valid header, -1 otherwise
535 crc_id(char *blk, int size)
537 if ((size < sizeof(HD_VCPIO)) ||
538 (strncmp(blk, AVCMAGIC, sizeof(AVCMAGIC) - 1) != 0))
545 w set file data CRC calculations. Fire up the hard link detection code
547 * 0 if ok -1 otherwise (the return values of lnk_start())
559 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
560 * convert and store the values in the ARCHD parameter.
562 * 0 if a valid header, -1 otherwise.
566 vcpio_rd(ARCHD *arcn, char *buf)
574 * during the id phase it was determined if we were using CRC, use the
578 if (crc_id(buf, sizeof(HD_VCPIO)) < 0)
581 if (vcpio_id(buf, sizeof(HD_VCPIO)) < 0)
585 hd = (HD_VCPIO *)buf;
589 * extract the hex ascii fields from the header
591 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), HEX);
592 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), HEX);
593 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), HEX);
594 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);
596 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
598 arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,
599 sizeof(hd->c_filesize), HEX);
601 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,
602 sizeof(hd->c_filesize), HEX);
604 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
606 devmajor = (dev_t)asc_ul(hd->c_maj, sizeof(hd->c_maj), HEX);
607 devminor = (dev_t)asc_ul(hd->c_min, sizeof(hd->c_min), HEX);
608 arcn->sb.st_dev = TODEV(devmajor, devminor);
609 devmajor = (dev_t)asc_ul(hd->c_rmaj, sizeof(hd->c_maj), HEX);
610 devminor = (dev_t)asc_ul(hd->c_rmin, sizeof(hd->c_min), HEX);
611 arcn->sb.st_rdev = TODEV(devmajor, devminor);
612 arcn->crc = asc_ul(hd->c_chksum, sizeof(hd->c_chksum), HEX);
615 * check the length of the file name, if ok read it in, return -1 if
618 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),HEX)) < 2)
620 arcn->nlen = nsz - 1;
621 if (rd_nm(arcn, nsz) < 0)
625 * skip padding. header + filename is aligned to 4 byte boundaries
627 if (rd_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)
631 * if not a link (or a file with no data), calculate pad size (for
632 * padding which follows the file data), clear the link name and return
634 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
636 * we have a valid header (not a link)
639 arcn->ln_name[0] = '\0';
640 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
641 return(com_rd(arcn));
645 * read in the link name and skip over the padding
647 if ((rd_ln_nm(arcn) < 0) ||
648 (rd_skip((off_t)(VCPIO_PAD(arcn->sb.st_size))) < 0))
652 * we have a valid header (with a link)
654 return(com_rd(arcn));
659 * no cleanup needed here, just return size of the trailer (for append)
661 * size of trailer header in this format
667 return((off_t)(sizeof(HD_VCPIO) + sizeof(TRAILER) +
668 (VCPIO_PAD(sizeof(HD_VCPIO) + sizeof(TRAILER)))));
673 * start up the device mapping table, enable crc file calculation
675 * 0 if ok, -1 otherwise (what dev_start() returns)
687 * copy the data in the ARCHD to buffer in system VR4 cpio
688 * (with/without crc) format.
690 * 0 if file has data to be written after the header, 1 if file has
691 * NO data to write after the header, -1 if archive write failed
695 vcpio_wr(ARCHD *arcn)
699 char hdblk[sizeof(HD_VCPIO)];
702 * check and repair truncated device and inode fields in the cpio
705 if (map_dev(arcn, (u_long)VCPIO_MASK, (u_long)VCPIO_MASK) < 0)
707 nsz = arcn->nlen + 1;
708 hd = (HD_VCPIO *)hdblk;
709 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
710 arcn->sb.st_rdev = 0;
713 * add the proper magic value depending whether we were asked for
714 * file data crc's, and the crc if needed.
717 if (ul_asc((u_long)VCMAGIC, hd->c_magic, sizeof(hd->c_magic),
719 ul_asc((u_long)arcn->crc,hd->c_chksum,sizeof(hd->c_chksum),
723 if (ul_asc((u_long)VMAGIC, hd->c_magic, sizeof(hd->c_magic),
725 ul_asc((u_long)0L, hd->c_chksum, sizeof(hd->c_chksum),HEX))
729 switch (arcn->type) {
734 * caller will copy file data to the archive. tell him how
737 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
739 if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize,
740 sizeof(hd->c_filesize), HEX)) {
742 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
743 sizeof(hd->c_filesize), HEX)) {
745 paxwarn(1,"File is too large for sv4cpio format %s",
752 * no file data for the caller to process, the file data has
753 * the size of the link
756 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
757 sizeof(hd->c_filesize), HEX))
762 * no file data for the caller to process
765 if (ul_asc((u_long)0L, hd->c_filesize, sizeof(hd->c_filesize),
772 * set the other fields in the header
774 if (ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
776 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
778 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
780 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
782 ul_asc((u_long)arcn->sb.st_mtime, hd->c_mtime, sizeof(hd->c_mtime),
784 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
786 ul_asc((u_long)MAJOR(arcn->sb.st_dev),hd->c_maj, sizeof(hd->c_maj),
788 ul_asc((u_long)MINOR(arcn->sb.st_dev),hd->c_min, sizeof(hd->c_min),
790 ul_asc((u_long)MAJOR(arcn->sb.st_rdev),hd->c_rmaj,sizeof(hd->c_maj),
792 ul_asc((u_long)MINOR(arcn->sb.st_rdev),hd->c_rmin,sizeof(hd->c_min),
794 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), HEX))
798 * write the header, the file name and padding as required.
800 if ((wr_rdbuf(hdblk, (int)sizeof(HD_VCPIO)) < 0) ||
801 (wr_rdbuf(arcn->name, (int)nsz) < 0) ||
802 (wr_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)) {
803 paxwarn(1,"Could not write sv4cpio header for %s",arcn->org_name);
808 * if we have file data, tell the caller we are done, copy the file
810 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
811 (arcn->type == PAX_HRG))
815 * if we are not a link, tell the caller we are done, go to next file
817 if (arcn->type != PAX_SLK)
821 * write the link name, tell the caller we are done.
823 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
824 (wr_skip((off_t)(VCPIO_PAD(arcn->ln_nlen))) < 0)) {
825 paxwarn(1,"Could not write sv4cpio link name for %s",
833 * header field is out of range
835 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn->org_name);
840 * Routines common to the old binary header cpio
845 * determine if a block given to us is a old binary cpio header
846 * (with/without header byte swapping)
848 * 0 if a valid header, -1 otherwise
852 bcpio_id(char *blk, int size)
854 if (size < sizeof(HD_BCPIO))
858 * check both normal and byte swapped magic cookies
860 if (((u_short)SHRT_EXT(blk)) == MAGIC)
862 if (((u_short)RSHRT_EXT(blk)) == MAGIC) {
872 * determine if a buffer is a old binary archive entry. (it may have byte
873 * swapped header) convert and store the values in the ARCHD parameter.
874 * This is a very old header format and should not really be used.
876 * 0 if a valid header, -1 otherwise.
880 bcpio_rd(ARCHD *arcn, char *buf)
888 if (bcpio_id(buf, sizeof(HD_BCPIO)) < 0)
892 hd = (HD_BCPIO *)buf;
895 * header has swapped bytes on 16 bit boundaries
897 arcn->sb.st_dev = (dev_t)(RSHRT_EXT(hd->h_dev));
898 arcn->sb.st_ino = (ino_t)(RSHRT_EXT(hd->h_ino));
899 arcn->sb.st_mode = (mode_t)(RSHRT_EXT(hd->h_mode));
900 arcn->sb.st_uid = (uid_t)(RSHRT_EXT(hd->h_uid));
901 arcn->sb.st_gid = (gid_t)(RSHRT_EXT(hd->h_gid));
902 arcn->sb.st_nlink = (nlink_t)(RSHRT_EXT(hd->h_nlink));
903 arcn->sb.st_rdev = (dev_t)(RSHRT_EXT(hd->h_rdev));
904 arcn->sb.st_mtime = (time_t)(RSHRT_EXT(hd->h_mtime_1));
905 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
906 ((time_t)(RSHRT_EXT(hd->h_mtime_2)));
907 arcn->sb.st_size = (off_t)(RSHRT_EXT(hd->h_filesize_1));
908 arcn->sb.st_size = (arcn->sb.st_size << 16) |
909 ((off_t)(RSHRT_EXT(hd->h_filesize_2)));
910 nsz = (int)(RSHRT_EXT(hd->h_namesize));
912 arcn->sb.st_dev = (dev_t)(SHRT_EXT(hd->h_dev));
913 arcn->sb.st_ino = (ino_t)(SHRT_EXT(hd->h_ino));
914 arcn->sb.st_mode = (mode_t)(SHRT_EXT(hd->h_mode));
915 arcn->sb.st_uid = (uid_t)(SHRT_EXT(hd->h_uid));
916 arcn->sb.st_gid = (gid_t)(SHRT_EXT(hd->h_gid));
917 arcn->sb.st_nlink = (nlink_t)(SHRT_EXT(hd->h_nlink));
918 arcn->sb.st_rdev = (dev_t)(SHRT_EXT(hd->h_rdev));
919 arcn->sb.st_mtime = (time_t)(SHRT_EXT(hd->h_mtime_1));
920 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
921 ((time_t)(SHRT_EXT(hd->h_mtime_2)));
922 arcn->sb.st_size = (off_t)(SHRT_EXT(hd->h_filesize_1));
923 arcn->sb.st_size = (arcn->sb.st_size << 16) |
924 ((off_t)(SHRT_EXT(hd->h_filesize_2)));
925 nsz = (int)(SHRT_EXT(hd->h_namesize));
927 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
930 * check the file name size, if bogus give up. otherwise read the file
935 arcn->nlen = nsz - 1;
936 if (rd_nm(arcn, nsz) < 0)
940 * header + file name are aligned to 2 byte boundaries, skip if needed
942 if (rd_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)
946 * if not a link (or a file with no data), calculate pad size (for
947 * padding which follows the file data), clear the link name and return
949 if (((arcn->sb.st_mode & C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)){
951 * we have a valid header (not a link)
954 arcn->ln_name[0] = '\0';
955 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
956 return(com_rd(arcn));
959 if ((rd_ln_nm(arcn) < 0) ||
960 (rd_skip((off_t)(BCPIO_PAD(arcn->sb.st_size))) < 0))
964 * we have a valid header (with a link)
966 return(com_rd(arcn));
971 * no cleanup needed here, just return size of the trailer (for append)
973 * size of trailer header in this format
979 return((off_t)(sizeof(HD_BCPIO) + sizeof(TRAILER) +
980 (BCPIO_PAD(sizeof(HD_BCPIO) + sizeof(TRAILER)))));
985 * copy the data in the ARCHD to buffer in old binary cpio format
986 * There is a real chance of field overflow with this critter. So we
987 * always check the conversion is ok. nobody in their right mind
988 * should write an archive in this format...
990 * 0 if file has data to be written after the header, 1 if file has NO
991 * data to write after the header, -1 if archive write failed
995 bcpio_wr(ARCHD *arcn)
999 char hdblk[sizeof(HD_BCPIO)];
1005 * check and repair truncated device and inode fields in the cpio
1008 if (map_dev(arcn, (u_long)BCPIO_MASK, (u_long)BCPIO_MASK) < 0)
1011 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
1012 arcn->sb.st_rdev = 0;
1013 hd = (HD_BCPIO *)hdblk;
1015 switch (arcn->type) {
1020 * caller will copy file data to the archive. tell him how
1023 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
1024 hd->h_filesize_1[0] = CHR_WR_0(arcn->sb.st_size);
1025 hd->h_filesize_1[1] = CHR_WR_1(arcn->sb.st_size);
1026 hd->h_filesize_2[0] = CHR_WR_2(arcn->sb.st_size);
1027 hd->h_filesize_2[1] = CHR_WR_3(arcn->sb.st_size);
1028 t_offt = (off_t)(SHRT_EXT(hd->h_filesize_1));
1029 t_offt = (t_offt<<16) | ((off_t)(SHRT_EXT(hd->h_filesize_2)));
1030 if (arcn->sb.st_size != t_offt) {
1031 paxwarn(1,"File is too large for bcpio format %s",
1038 * no file data for the caller to process, the file data has
1039 * the size of the link
1042 hd->h_filesize_1[0] = CHR_WR_0(arcn->ln_nlen);
1043 hd->h_filesize_1[1] = CHR_WR_1(arcn->ln_nlen);
1044 hd->h_filesize_2[0] = CHR_WR_2(arcn->ln_nlen);
1045 hd->h_filesize_2[1] = CHR_WR_3(arcn->ln_nlen);
1046 t_int = (int)(SHRT_EXT(hd->h_filesize_1));
1047 t_int = (t_int << 16) | ((int)(SHRT_EXT(hd->h_filesize_2)));
1048 if (arcn->ln_nlen != t_int)
1053 * no file data for the caller to process
1056 hd->h_filesize_1[0] = (char)0;
1057 hd->h_filesize_1[1] = (char)0;
1058 hd->h_filesize_2[0] = (char)0;
1059 hd->h_filesize_2[1] = (char)0;
1064 * build up the rest of the fields
1066 hd->h_magic[0] = CHR_WR_2(MAGIC);
1067 hd->h_magic[1] = CHR_WR_3(MAGIC);
1068 hd->h_dev[0] = CHR_WR_2(arcn->sb.st_dev);
1069 hd->h_dev[1] = CHR_WR_3(arcn->sb.st_dev);
1070 if (arcn->sb.st_dev != (dev_t)(SHRT_EXT(hd->h_dev)))
1072 hd->h_ino[0] = CHR_WR_2(arcn->sb.st_ino);
1073 hd->h_ino[1] = CHR_WR_3(arcn->sb.st_ino);
1074 if (arcn->sb.st_ino != (ino_t)(SHRT_EXT(hd->h_ino)))
1076 hd->h_mode[0] = CHR_WR_2(arcn->sb.st_mode);
1077 hd->h_mode[1] = CHR_WR_3(arcn->sb.st_mode);
1078 if (arcn->sb.st_mode != (mode_t)(SHRT_EXT(hd->h_mode)))
1080 hd->h_uid[0] = CHR_WR_2(arcn->sb.st_uid);
1081 hd->h_uid[1] = CHR_WR_3(arcn->sb.st_uid);
1082 if (arcn->sb.st_uid != (uid_t)(SHRT_EXT(hd->h_uid)))
1084 hd->h_gid[0] = CHR_WR_2(arcn->sb.st_gid);
1085 hd->h_gid[1] = CHR_WR_3(arcn->sb.st_gid);
1086 if (arcn->sb.st_gid != (gid_t)(SHRT_EXT(hd->h_gid)))
1088 hd->h_nlink[0] = CHR_WR_2(arcn->sb.st_nlink);
1089 hd->h_nlink[1] = CHR_WR_3(arcn->sb.st_nlink);
1090 if (arcn->sb.st_nlink != (nlink_t)(SHRT_EXT(hd->h_nlink)))
1092 hd->h_rdev[0] = CHR_WR_2(arcn->sb.st_rdev);
1093 hd->h_rdev[1] = CHR_WR_3(arcn->sb.st_rdev);
1094 if (arcn->sb.st_rdev != (dev_t)(SHRT_EXT(hd->h_rdev)))
1096 hd->h_mtime_1[0] = CHR_WR_0(arcn->sb.st_mtime);
1097 hd->h_mtime_1[1] = CHR_WR_1(arcn->sb.st_mtime);
1098 hd->h_mtime_2[0] = CHR_WR_2(arcn->sb.st_mtime);
1099 hd->h_mtime_2[1] = CHR_WR_3(arcn->sb.st_mtime);
1100 t_timet = (time_t)(SHRT_EXT(hd->h_mtime_1));
1101 t_timet = (t_timet << 16) | ((time_t)(SHRT_EXT(hd->h_mtime_2)));
1102 if (arcn->sb.st_mtime != t_timet)
1104 nsz = arcn->nlen + 1;
1105 hd->h_namesize[0] = CHR_WR_2(nsz);
1106 hd->h_namesize[1] = CHR_WR_3(nsz);
1107 if (nsz != (int)(SHRT_EXT(hd->h_namesize)))
1111 * write the header, the file name and padding as required.
1113 if ((wr_rdbuf(hdblk, (int)sizeof(HD_BCPIO)) < 0) ||
1114 (wr_rdbuf(arcn->name, nsz) < 0) ||
1115 (wr_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)) {
1116 paxwarn(1, "Could not write bcpio header for %s", arcn->org_name);
1121 * if we have file data, tell the caller we are done
1123 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
1124 (arcn->type == PAX_HRG))
1128 * if we are not a link, tell the caller we are done, go to next file
1130 if (arcn->type != PAX_SLK)
1134 * write the link name, tell the caller we are done.
1136 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
1137 (wr_skip((off_t)(BCPIO_PAD(arcn->ln_nlen))) < 0)) {
1138 paxwarn(1,"Could not write bcpio link name for %s",arcn->org_name);
1145 * header field is out of range
1147 paxwarn(1,"Bcpio header field is too small for file %s", arcn->org_name);