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. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)cpio.c 8.1 (Berkeley) 5/31/93
38 * $FreeBSD: src/bin/pax/cpio.c,v 1.12.2.1 2001/08/01 05:03:11 obrien Exp $
39 * $DragonFly: src/bin/pax/cpio.c,v 1.2 2003/06/17 04:22:50 dillon Exp $
42 #include <sys/types.h>
53 static int rd_nm __P((register ARCHD *, int));
54 static int rd_ln_nm __P((register ARCHD *));
55 static int com_rd __P((register ARCHD *));
58 * Routines which support the different cpio versions
61 static int swp_head; /* binary cpio header byte swap */
64 * Routines common to all versions of cpio
69 * Fire up the hard link detection code
71 * 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,
97 cpio_trail(register ARCHD *arcn)
101 register ARCHD *arcn;
105 * look for trailer id in file we are about to process
107 if ((strcmp(arcn->name, TRAILER) == 0) && (arcn->sb.st_size == 0))
114 * operations common to all cpio read functions.
121 com_rd(register ARCHD *arcn)
125 register ARCHD *arcn;
130 arcn->org_name = arcn->name;
131 switch(arcn->sb.st_mode & C_IFMT) {
133 arcn->type = PAX_FIF;
136 arcn->type = PAX_DIR;
139 arcn->type = PAX_BLK;
142 arcn->type = PAX_CHR;
145 arcn->type = PAX_SLK;
148 arcn->type = PAX_SCK;
154 * we have file data, set up skip (pad is set in the format
157 arcn->sb.st_mode = (arcn->sb.st_mode & 0xfff) | C_ISREG;
158 arcn->type = PAX_REG;
159 arcn->skip = arcn->sb.st_size;
162 if (chk_lnk(arcn) < 0)
169 * write the special file with the name trailer in the proper format
171 * result of the write of the trailer from the cpio specific write func
185 * create a trailer request and call the proper format write function
187 memset(&last, 0, sizeof(last));
188 last.nlen = sizeof(TRAILER) - 1;
190 last.sb.st_nlink = 1;
191 (void)strcpy(last.name, TRAILER);
192 return((*frmt->wr)(&last));
197 * read in the file name which follows the cpio header
199 * 0 if ok, -1 otherwise
204 rd_nm(register ARCHD *arcn, int nsz)
208 register ARCHD *arcn;
213 * do not even try bogus values
215 if ((nsz == 0) || (nsz > sizeof(arcn->name))) {
216 paxwarn(1, "Cpio file name length %d is out of range", nsz);
221 * read the name and make sure it is not empty and is \0 terminated
223 if ((rd_wrbuf(arcn->name,nsz) != nsz) || (arcn->name[nsz-1] != '\0') ||
224 (arcn->name[0] == '\0')) {
225 paxwarn(1, "Cpio file name in header is corrupted");
233 * read in the link name for a file with links. The link name is stored
234 * like file data (and is NOT \0 terminated!)
236 * 0 if ok, -1 otherwise
241 rd_ln_nm(register ARCHD *arcn)
245 register ARCHD *arcn;
249 * check the length specified for bogus values
251 if ((arcn->sb.st_size == 0) ||
252 (arcn->sb.st_size >= sizeof(arcn->ln_name))) {
254 paxwarn(1, "Cpio link name length is invalid: %lu",
257 paxwarn(1, "Cpio link name length is invalid: %qu",
264 * read in the link name and \0 terminate it
266 if (rd_wrbuf(arcn->ln_name, (int)arcn->sb.st_size) !=
267 (int)arcn->sb.st_size) {
268 paxwarn(1, "Cpio link name read error");
271 arcn->ln_nlen = arcn->sb.st_size;
272 arcn->ln_name[arcn->ln_nlen] = '\0';
275 * watch out for those empty link names
277 if (arcn->ln_name[0] == '\0') {
278 paxwarn(1, "Cpio link name is corrupt");
285 * Routines common to the extended byte oriented cpio format
290 * determine if a block given to us is a valid extended byte oriented
293 * 0 if a valid header, -1 otherwise
298 cpio_id(char *blk, int size)
306 if ((size < sizeof(HD_CPIO)) ||
307 (strncmp(blk, AMAGIC, sizeof(AMAGIC) - 1) != 0))
314 * determine if a buffer is a byte oriented extended cpio archive entry.
315 * convert and store the values in the ARCHD parameter.
317 * 0 if a valid header, -1 otherwise.
322 cpio_rd(register ARCHD *arcn, register char *buf)
326 register ARCHD *arcn;
331 register HD_CPIO *hd;
334 * check that this is a valid header, if not return -1
336 if (cpio_id(buf, sizeof(HD_CPIO)) < 0)
341 * byte oriented cpio (posix) does not have padding! extract the octal
342 * ascii fields from the header
345 arcn->sb.st_dev = (dev_t)asc_ul(hd->c_dev, sizeof(hd->c_dev), OCT);
346 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), OCT);
347 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), OCT);
348 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), OCT);
349 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), OCT);
350 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
352 arcn->sb.st_rdev = (dev_t)asc_ul(hd->c_rdev, sizeof(hd->c_rdev), OCT);
353 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime, sizeof(hd->c_mtime),
355 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
357 arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,sizeof(hd->c_filesize),
360 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,sizeof(hd->c_filesize),
365 * check name size and if valid, read in the name of this entry (name
366 * follows header in the archive)
368 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),OCT)) < 2)
370 arcn->nlen = nsz - 1;
371 if (rd_nm(arcn, nsz) < 0)
374 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
376 * no link name to read for this file
379 arcn->ln_name[0] = '\0';
380 return(com_rd(arcn));
384 * check link name size and read in the link name. Link names are
385 * stored like file data.
387 if (rd_ln_nm(arcn) < 0)
391 * we have a valid header (with a link)
393 return(com_rd(arcn));
398 * no cleanup needed here, just return size of the trailer (for append)
400 * size of trailer header in this format
411 return((off_t)(sizeof(HD_CPIO) + sizeof(TRAILER)));
416 * start up the device mapping table
418 * 0 if ok, -1 otherwise (what dev_start() returns)
434 * copy the data in the ARCHD to buffer in extended byte oriented cpio
437 * 0 if file has data to be written after the header, 1 if file has NO
438 * data to write after the header, -1 if archive write failed
443 cpio_wr(register ARCHD *arcn)
447 register ARCHD *arcn;
450 register HD_CPIO *hd;
452 char hdblk[sizeof(HD_CPIO)];
455 * check and repair truncated device and inode fields in the header
457 if (map_dev(arcn, (u_long)CPIO_MASK, (u_long)CPIO_MASK) < 0)
461 nsz = arcn->nlen + 1;
462 hd = (HD_CPIO *)hdblk;
463 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
464 arcn->sb.st_rdev = 0;
471 * set data size for file data
474 if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize,
475 sizeof(hd->c_filesize), OCT)) {
477 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
478 sizeof(hd->c_filesize), OCT)) {
480 paxwarn(1,"File is too large for cpio format %s",
487 * set data size to hold link name
489 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
490 sizeof(hd->c_filesize), OCT))
495 * all other file types have no file data
497 if (ul_asc((u_long)0, hd->c_filesize, sizeof(hd->c_filesize),
504 * copy the values to the header using octal ascii
506 if (ul_asc((u_long)MAGIC, hd->c_magic, sizeof(hd->c_magic), OCT) ||
507 ul_asc((u_long)arcn->sb.st_dev, hd->c_dev, sizeof(hd->c_dev),
509 ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
511 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
513 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
515 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
517 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
519 ul_asc((u_long)arcn->sb.st_rdev, hd->c_rdev, sizeof(hd->c_rdev),
521 ul_asc((u_long)arcn->sb.st_mtime,hd->c_mtime,sizeof(hd->c_mtime),
523 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), OCT))
527 * write the file name to the archive
529 if ((wr_rdbuf(hdblk, (int)sizeof(HD_CPIO)) < 0) ||
530 (wr_rdbuf(arcn->name, nsz) < 0)) {
531 paxwarn(1, "Unable to write cpio header for %s", arcn->org_name);
536 * if this file has data, we are done. The caller will write the file
537 * data, if we are link tell caller we are done, go to next file
539 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
540 (arcn->type == PAX_HRG))
542 if (arcn->type != PAX_SLK)
546 * write the link name to the archive, tell the caller to go to the
547 * next file as we are done.
549 if (wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) {
550 paxwarn(1,"Unable to write cpio link name for %s",arcn->org_name);
557 * header field is out of range
559 paxwarn(1, "Cpio header field is too small to store file %s",
565 * Routines common to the system VR4 version of cpio (with/without file CRC)
570 * determine if a block given to us is a valid system VR4 cpio header
571 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
574 * 0 if a valid header, -1 otherwise
579 vcpio_id(char *blk, int size)
587 if ((size < sizeof(HD_VCPIO)) ||
588 (strncmp(blk, AVMAGIC, sizeof(AVMAGIC) - 1) != 0))
595 * determine if a block given to us is a valid system VR4 cpio header
596 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
598 * 0 if a valid header, -1 otherwise
603 crc_id(char *blk, int size)
611 if ((size < sizeof(HD_VCPIO)) ||
612 (strncmp(blk, AVCMAGIC, sizeof(AVCMAGIC) - 1) != 0))
619 w set file data CRC calculations. Fire up the hard link detection code
621 * 0 if ok -1 otherwise (the return values of lnk_start())
638 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
639 * convert and store the values in the ARCHD parameter.
641 * 0 if a valid header, -1 otherwise.
646 vcpio_rd(register ARCHD *arcn, register char *buf)
650 register ARCHD *arcn;
654 register HD_VCPIO *hd;
660 * during the id phase it was determined if we were using CRC, use the
664 if (crc_id(buf, sizeof(HD_VCPIO)) < 0)
667 if (vcpio_id(buf, sizeof(HD_VCPIO)) < 0)
671 hd = (HD_VCPIO *)buf;
675 * extract the hex ascii fields from the header
677 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), HEX);
678 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), HEX);
679 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), HEX);
680 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), HEX);
681 arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime,sizeof(hd->c_mtime),HEX);
682 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
684 arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,
685 sizeof(hd->c_filesize), HEX);
687 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,
688 sizeof(hd->c_filesize), HEX);
690 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
692 devmajor = (dev_t)asc_ul(hd->c_maj, sizeof(hd->c_maj), HEX);
693 devminor = (dev_t)asc_ul(hd->c_min, sizeof(hd->c_min), HEX);
694 arcn->sb.st_dev = TODEV(devmajor, devminor);
695 devmajor = (dev_t)asc_ul(hd->c_rmaj, sizeof(hd->c_maj), HEX);
696 devminor = (dev_t)asc_ul(hd->c_rmin, sizeof(hd->c_min), HEX);
697 arcn->sb.st_rdev = TODEV(devmajor, devminor);
698 arcn->crc = asc_ul(hd->c_chksum, sizeof(hd->c_chksum), HEX);
701 * check the length of the file name, if ok read it in, return -1 if
704 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),HEX)) < 2)
706 arcn->nlen = nsz - 1;
707 if (rd_nm(arcn, nsz) < 0)
711 * skip padding. header + filename is aligned to 4 byte boundries
713 if (rd_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)
717 * if not a link (or a file with no data), calculate pad size (for
718 * padding which follows the file data), clear the link name and return
720 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
722 * we have a valid header (not a link)
725 arcn->ln_name[0] = '\0';
726 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
727 return(com_rd(arcn));
731 * read in the link name and skip over the padding
733 if ((rd_ln_nm(arcn) < 0) ||
734 (rd_skip((off_t)(VCPIO_PAD(arcn->sb.st_size))) < 0))
738 * we have a valid header (with a link)
740 return(com_rd(arcn));
745 * no cleanup needed here, just return size of the trailer (for append)
747 * size of trailer header in this format
758 return((off_t)(sizeof(HD_VCPIO) + sizeof(TRAILER) +
759 (VCPIO_PAD(sizeof(HD_VCPIO) + sizeof(TRAILER)))));
764 * start up the device mapping table, enable crc file calculation
766 * 0 if ok, -1 otherwise (what dev_start() returns)
783 * copy the data in the ARCHD to buffer in system VR4 cpio
784 * (with/without crc) format.
786 * 0 if file has data to be written after the header, 1 if file has
787 * NO data to write after the header, -1 if archive write failed
792 vcpio_wr(register ARCHD *arcn)
796 register ARCHD *arcn;
799 register HD_VCPIO *hd;
801 char hdblk[sizeof(HD_VCPIO)];
804 * check and repair truncated device and inode fields in the cpio
807 if (map_dev(arcn, (u_long)VCPIO_MASK, (u_long)VCPIO_MASK) < 0)
809 nsz = arcn->nlen + 1;
810 hd = (HD_VCPIO *)hdblk;
811 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
812 arcn->sb.st_rdev = 0;
815 * add the proper magic value depending whether we were asked for
816 * file data crc's, and the crc if needed.
819 if (ul_asc((u_long)VCMAGIC, hd->c_magic, sizeof(hd->c_magic),
821 ul_asc((u_long)arcn->crc,hd->c_chksum,sizeof(hd->c_chksum),
825 if (ul_asc((u_long)VMAGIC, hd->c_magic, sizeof(hd->c_magic),
827 ul_asc((u_long)0L, hd->c_chksum, sizeof(hd->c_chksum),HEX))
836 * caller will copy file data to the archive. tell him how
839 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
841 if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize,
842 sizeof(hd->c_filesize), HEX)) {
844 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
845 sizeof(hd->c_filesize), HEX)) {
847 paxwarn(1,"File is too large for sv4cpio format %s",
854 * no file data for the caller to process, the file data has
855 * the size of the link
858 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
859 sizeof(hd->c_filesize), HEX))
864 * no file data for the caller to process
867 if (ul_asc((u_long)0L, hd->c_filesize, sizeof(hd->c_filesize),
874 * set the other fields in the header
876 if (ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
878 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
880 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
882 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
884 ul_asc((u_long)arcn->sb.st_mtime, hd->c_mtime, sizeof(hd->c_mtime),
886 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
888 ul_asc((u_long)MAJOR(arcn->sb.st_dev),hd->c_maj, sizeof(hd->c_maj),
890 ul_asc((u_long)MINOR(arcn->sb.st_dev),hd->c_min, sizeof(hd->c_min),
892 ul_asc((u_long)MAJOR(arcn->sb.st_rdev),hd->c_rmaj,sizeof(hd->c_maj),
894 ul_asc((u_long)MINOR(arcn->sb.st_rdev),hd->c_rmin,sizeof(hd->c_min),
896 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), HEX))
900 * write the header, the file name and padding as required.
902 if ((wr_rdbuf(hdblk, (int)sizeof(HD_VCPIO)) < 0) ||
903 (wr_rdbuf(arcn->name, (int)nsz) < 0) ||
904 (wr_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)) {
905 paxwarn(1,"Could not write sv4cpio header for %s",arcn->org_name);
910 * if we have file data, tell the caller we are done, copy the file
912 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
913 (arcn->type == PAX_HRG))
917 * if we are not a link, tell the caller we are done, go to next file
919 if (arcn->type != PAX_SLK)
923 * write the link name, tell the caller we are done.
925 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
926 (wr_skip((off_t)(VCPIO_PAD(arcn->ln_nlen))) < 0)) {
927 paxwarn(1,"Could not write sv4cpio link name for %s",
935 * header field is out of range
937 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn->org_name);
942 * Routines common to the old binary header cpio
947 * determine if a block given to us is a old binary cpio header
948 * (with/without header byte swapping)
950 * 0 if a valid header, -1 otherwise
955 bcpio_id(char *blk, int size)
963 if (size < sizeof(HD_BCPIO))
967 * check both normal and byte swapped magic cookies
969 if (((u_short)SHRT_EXT(blk)) == MAGIC)
971 if (((u_short)RSHRT_EXT(blk)) == MAGIC) {
981 * determine if a buffer is a old binary archive entry. (it may have byte
982 * swapped header) convert and store the values in the ARCHD parameter.
983 * This is a very old header format and should not really be used.
985 * 0 if a valid header, -1 otherwise.
990 bcpio_rd(register ARCHD *arcn, register char *buf)
994 register ARCHD *arcn;
998 register HD_BCPIO *hd;
1004 if (bcpio_id(buf, sizeof(HD_BCPIO)) < 0)
1008 hd = (HD_BCPIO *)buf;
1011 * header has swapped bytes on 16 bit boundaries
1013 arcn->sb.st_dev = (dev_t)(RSHRT_EXT(hd->h_dev));
1014 arcn->sb.st_ino = (ino_t)(RSHRT_EXT(hd->h_ino));
1015 arcn->sb.st_mode = (mode_t)(RSHRT_EXT(hd->h_mode));
1016 arcn->sb.st_uid = (uid_t)(RSHRT_EXT(hd->h_uid));
1017 arcn->sb.st_gid = (gid_t)(RSHRT_EXT(hd->h_gid));
1018 arcn->sb.st_nlink = (nlink_t)(RSHRT_EXT(hd->h_nlink));
1019 arcn->sb.st_rdev = (dev_t)(RSHRT_EXT(hd->h_rdev));
1020 arcn->sb.st_mtime = (time_t)(RSHRT_EXT(hd->h_mtime_1));
1021 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
1022 ((time_t)(RSHRT_EXT(hd->h_mtime_2)));
1023 arcn->sb.st_size = (off_t)(RSHRT_EXT(hd->h_filesize_1));
1024 arcn->sb.st_size = (arcn->sb.st_size << 16) |
1025 ((off_t)(RSHRT_EXT(hd->h_filesize_2)));
1026 nsz = (int)(RSHRT_EXT(hd->h_namesize));
1028 arcn->sb.st_dev = (dev_t)(SHRT_EXT(hd->h_dev));
1029 arcn->sb.st_ino = (ino_t)(SHRT_EXT(hd->h_ino));
1030 arcn->sb.st_mode = (mode_t)(SHRT_EXT(hd->h_mode));
1031 arcn->sb.st_uid = (uid_t)(SHRT_EXT(hd->h_uid));
1032 arcn->sb.st_gid = (gid_t)(SHRT_EXT(hd->h_gid));
1033 arcn->sb.st_nlink = (nlink_t)(SHRT_EXT(hd->h_nlink));
1034 arcn->sb.st_rdev = (dev_t)(SHRT_EXT(hd->h_rdev));
1035 arcn->sb.st_mtime = (time_t)(SHRT_EXT(hd->h_mtime_1));
1036 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
1037 ((time_t)(SHRT_EXT(hd->h_mtime_2)));
1038 arcn->sb.st_size = (off_t)(SHRT_EXT(hd->h_filesize_1));
1039 arcn->sb.st_size = (arcn->sb.st_size << 16) |
1040 ((off_t)(SHRT_EXT(hd->h_filesize_2)));
1041 nsz = (int)(SHRT_EXT(hd->h_namesize));
1043 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
1046 * check the file name size, if bogus give up. otherwise read the file
1051 arcn->nlen = nsz - 1;
1052 if (rd_nm(arcn, nsz) < 0)
1056 * header + file name are aligned to 2 byte boundries, skip if needed
1058 if (rd_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)
1062 * if not a link (or a file with no data), calculate pad size (for
1063 * padding which follows the file data), clear the link name and return
1065 if (((arcn->sb.st_mode & C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)){
1067 * we have a valid header (not a link)
1070 arcn->ln_name[0] = '\0';
1071 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
1072 return(com_rd(arcn));
1075 if ((rd_ln_nm(arcn) < 0) ||
1076 (rd_skip((off_t)(BCPIO_PAD(arcn->sb.st_size))) < 0))
1080 * we have a valid header (with a link)
1082 return(com_rd(arcn));
1087 * no cleanup needed here, just return size of the trailer (for append)
1089 * size of trailer header in this format
1100 return((off_t)(sizeof(HD_BCPIO) + sizeof(TRAILER) +
1101 (BCPIO_PAD(sizeof(HD_BCPIO) + sizeof(TRAILER)))));
1106 * copy the data in the ARCHD to buffer in old binary cpio format
1107 * There is a real chance of field overflow with this critter. So we
1108 * always check the conversion is ok. nobody in his their right mind
1109 * should write an achive in this format...
1111 * 0 if file has data to be written after the header, 1 if file has NO
1112 * data to write after the header, -1 if archive write failed
1117 bcpio_wr(register ARCHD *arcn)
1121 register ARCHD *arcn;
1124 register HD_BCPIO *hd;
1126 char hdblk[sizeof(HD_BCPIO)];
1132 * check and repair truncated device and inode fields in the cpio
1135 if (map_dev(arcn, (u_long)BCPIO_MASK, (u_long)BCPIO_MASK) < 0)
1138 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
1139 arcn->sb.st_rdev = 0;
1140 hd = (HD_BCPIO *)hdblk;
1142 switch(arcn->type) {
1147 * caller will copy file data to the archive. tell him how
1150 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
1151 hd->h_filesize_1[0] = CHR_WR_0(arcn->sb.st_size);
1152 hd->h_filesize_1[1] = CHR_WR_1(arcn->sb.st_size);
1153 hd->h_filesize_2[0] = CHR_WR_2(arcn->sb.st_size);
1154 hd->h_filesize_2[1] = CHR_WR_3(arcn->sb.st_size);
1155 t_offt = (off_t)(SHRT_EXT(hd->h_filesize_1));
1156 t_offt = (t_offt<<16) | ((off_t)(SHRT_EXT(hd->h_filesize_2)));
1157 if (arcn->sb.st_size != t_offt) {
1158 paxwarn(1,"File is too large for bcpio format %s",
1165 * no file data for the caller to process, the file data has
1166 * the size of the link
1169 hd->h_filesize_1[0] = CHR_WR_0(arcn->ln_nlen);
1170 hd->h_filesize_1[1] = CHR_WR_1(arcn->ln_nlen);
1171 hd->h_filesize_2[0] = CHR_WR_2(arcn->ln_nlen);
1172 hd->h_filesize_2[1] = CHR_WR_3(arcn->ln_nlen);
1173 t_int = (int)(SHRT_EXT(hd->h_filesize_1));
1174 t_int = (t_int << 16) | ((int)(SHRT_EXT(hd->h_filesize_2)));
1175 if (arcn->ln_nlen != t_int)
1180 * no file data for the caller to process
1183 hd->h_filesize_1[0] = (char)0;
1184 hd->h_filesize_1[1] = (char)0;
1185 hd->h_filesize_2[0] = (char)0;
1186 hd->h_filesize_2[1] = (char)0;
1191 * build up the rest of the fields
1193 hd->h_magic[0] = CHR_WR_2(MAGIC);
1194 hd->h_magic[1] = CHR_WR_3(MAGIC);
1195 hd->h_dev[0] = CHR_WR_2(arcn->sb.st_dev);
1196 hd->h_dev[1] = CHR_WR_3(arcn->sb.st_dev);
1197 if (arcn->sb.st_dev != (dev_t)(SHRT_EXT(hd->h_dev)))
1199 hd->h_ino[0] = CHR_WR_2(arcn->sb.st_ino);
1200 hd->h_ino[1] = CHR_WR_3(arcn->sb.st_ino);
1201 if (arcn->sb.st_ino != (ino_t)(SHRT_EXT(hd->h_ino)))
1203 hd->h_mode[0] = CHR_WR_2(arcn->sb.st_mode);
1204 hd->h_mode[1] = CHR_WR_3(arcn->sb.st_mode);
1205 if (arcn->sb.st_mode != (mode_t)(SHRT_EXT(hd->h_mode)))
1207 hd->h_uid[0] = CHR_WR_2(arcn->sb.st_uid);
1208 hd->h_uid[1] = CHR_WR_3(arcn->sb.st_uid);
1209 if (arcn->sb.st_uid != (uid_t)(SHRT_EXT(hd->h_uid)))
1211 hd->h_gid[0] = CHR_WR_2(arcn->sb.st_gid);
1212 hd->h_gid[1] = CHR_WR_3(arcn->sb.st_gid);
1213 if (arcn->sb.st_gid != (gid_t)(SHRT_EXT(hd->h_gid)))
1215 hd->h_nlink[0] = CHR_WR_2(arcn->sb.st_nlink);
1216 hd->h_nlink[1] = CHR_WR_3(arcn->sb.st_nlink);
1217 if (arcn->sb.st_nlink != (nlink_t)(SHRT_EXT(hd->h_nlink)))
1219 hd->h_rdev[0] = CHR_WR_2(arcn->sb.st_rdev);
1220 hd->h_rdev[1] = CHR_WR_3(arcn->sb.st_rdev);
1221 if (arcn->sb.st_rdev != (dev_t)(SHRT_EXT(hd->h_rdev)))
1223 hd->h_mtime_1[0] = CHR_WR_0(arcn->sb.st_mtime);
1224 hd->h_mtime_1[1] = CHR_WR_1(arcn->sb.st_mtime);
1225 hd->h_mtime_2[0] = CHR_WR_2(arcn->sb.st_mtime);
1226 hd->h_mtime_2[1] = CHR_WR_3(arcn->sb.st_mtime);
1227 t_timet = (time_t)(SHRT_EXT(hd->h_mtime_1));
1228 t_timet = (t_timet << 16) | ((time_t)(SHRT_EXT(hd->h_mtime_2)));
1229 if (arcn->sb.st_mtime != t_timet)
1231 nsz = arcn->nlen + 1;
1232 hd->h_namesize[0] = CHR_WR_2(nsz);
1233 hd->h_namesize[1] = CHR_WR_3(nsz);
1234 if (nsz != (int)(SHRT_EXT(hd->h_namesize)))
1238 * write the header, the file name and padding as required.
1240 if ((wr_rdbuf(hdblk, (int)sizeof(HD_BCPIO)) < 0) ||
1241 (wr_rdbuf(arcn->name, nsz) < 0) ||
1242 (wr_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)) {
1243 paxwarn(1, "Could not write bcpio header for %s", arcn->org_name);
1248 * if we have file data, tell the caller we are done
1250 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
1251 (arcn->type == PAX_HRG))
1255 * if we are not a link, tell the caller we are done, go to next file
1257 if (arcn->type != PAX_SLK)
1261 * write the link name, tell the caller we are done.
1263 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
1264 (wr_skip((off_t)(BCPIO_PAD(arcn->ln_nlen))) < 0)) {
1265 paxwarn(1,"Could not write bcpio link name for %s",arcn->org_name);
1272 * header field is out of range
1274 paxwarn(1,"Bcpio header field is too small for file %s", arcn->org_name);