2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/types.h>
46 static int rd_nm(ARCHD *, int);
47 static int rd_ln_nm(ARCHD *);
48 static int com_rd(ARCHD *);
51 * Routines which support the different cpio versions
54 static int swp_head; /* binary cpio header byte swap */
57 * Routines common to all versions of cpio
62 * Fire up the hard link detection code
64 * 0 if ok -1 otherwise (the return values of lnk_start())
75 * Called to determine if a header block is a valid trailer. We are
76 * passed the block, the in_sync flag (which tells us we are in resync
77 * mode; looking for a valid header), and cnt (which starts at zero)
78 * which is used to count the number of empty blocks we have seen so far.
80 * 0 if a valid trailer, -1 if not a valid trailer,
84 cpio_trail(ARCHD *arcn)
87 * look for trailer id in file we are about to process
89 if ((strcmp(arcn->name, TRAILER) == 0) && (arcn->sb.st_size == 0))
96 * operations common to all cpio read functions.
106 arcn->org_name = arcn->name;
107 switch(arcn->sb.st_mode & C_IFMT) {
109 arcn->type = PAX_FIF;
112 arcn->type = PAX_DIR;
115 arcn->type = PAX_BLK;
118 arcn->type = PAX_CHR;
121 arcn->type = PAX_SLK;
124 arcn->type = PAX_SCK;
130 * we have file data, set up skip (pad is set in the format
133 arcn->sb.st_mode = (arcn->sb.st_mode & 0xfff) | C_ISREG;
134 arcn->type = PAX_REG;
135 arcn->skip = arcn->sb.st_size;
138 if (chk_lnk(arcn) < 0)
145 * write the special file with the name trailer in the proper format
147 * result of the write of the trailer from the cpio specific write func
156 * create a trailer request and call the proper format write function
158 memset(&last, 0, sizeof(last));
159 last.nlen = sizeof(TRAILER) - 1;
161 last.sb.st_nlink = 1;
162 (void)strcpy(last.name, TRAILER);
163 return((*frmt->wr)(&last));
168 * read in the file name which follows the cpio header
170 * 0 if ok, -1 otherwise
174 rd_nm(ARCHD *arcn, int nsz)
177 * do not even try bogus values
179 if ((nsz == 0) || (nsz > (int)sizeof(arcn->name))) {
180 paxwarn(1, "Cpio file name length %d is out of range", nsz);
185 * read the name and make sure it is not empty and is \0 terminated
187 if ((rd_wrbuf(arcn->name,nsz) != nsz) || (arcn->name[nsz-1] != '\0') ||
188 (arcn->name[0] == '\0')) {
189 paxwarn(1, "Cpio file name in header is corrupted");
197 * read in the link name for a file with links. The link name is stored
198 * like file data (and is NOT \0 terminated!)
200 * 0 if ok, -1 otherwise
204 rd_ln_nm(ARCHD *arcn)
207 * check the length specified for bogus values
209 if ((arcn->sb.st_size == 0) ||
210 ((size_t)arcn->sb.st_size >= sizeof(arcn->ln_name))) {
211 paxwarn(1, "Cpio link name length is invalid: %ju",
212 (uintmax_t)arcn->sb.st_size);
217 * read in the link name and \0 terminate it
219 if (rd_wrbuf(arcn->ln_name, (int)arcn->sb.st_size) !=
220 (int)arcn->sb.st_size) {
221 paxwarn(1, "Cpio link name read error");
224 arcn->ln_nlen = arcn->sb.st_size;
225 arcn->ln_name[arcn->ln_nlen] = '\0';
228 * watch out for those empty link names
230 if (arcn->ln_name[0] == '\0') {
231 paxwarn(1, "Cpio link name is corrupt");
238 * Routines common to the extended byte oriented cpio format
243 * determine if a block given to us is a valid extended byte oriented
246 * 0 if a valid header, -1 otherwise
250 cpio_id(char *blk, int size)
252 if ((size < (int)sizeof(HD_CPIO)) ||
253 (strncmp(blk, AMAGIC, sizeof(AMAGIC) - 1) != 0))
260 * determine if a buffer is a byte oriented extended cpio archive entry.
261 * convert and store the values in the ARCHD parameter.
263 * 0 if a valid header, -1 otherwise.
267 cpio_rd(ARCHD *arcn, char *buf)
273 * check that this is a valid header, if not return -1
275 if (cpio_id(buf, sizeof(HD_CPIO)) < 0)
280 * byte oriented cpio (posix) does not have padding! extract the octal
281 * ascii fields from the header
284 arcn->sb.st_dev = (dev_t)asc_ul(hd->c_dev, sizeof(hd->c_dev), OCT);
285 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), OCT);
286 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), OCT);
287 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), OCT);
288 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), OCT);
289 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
291 arcn->sb.st_rdev = (dev_t)asc_ul(hd->c_rdev, sizeof(hd->c_rdev), OCT);
292 arcn->sb.st_mtime = (time_t)asc_uqd(hd->c_mtime, sizeof(hd->c_mtime),
294 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
295 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,sizeof(hd->c_filesize),
299 * check name size and if valid, read in the name of this entry (name
300 * follows header in the archive)
302 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),OCT)) < 2)
304 arcn->nlen = nsz - 1;
305 if (rd_nm(arcn, nsz) < 0)
308 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
310 * no link name to read for this file
313 arcn->ln_name[0] = '\0';
314 return(com_rd(arcn));
318 * check link name size and read in the link name. Link names are
319 * stored like file data.
321 if (rd_ln_nm(arcn) < 0)
325 * we have a valid header (with a link)
327 return(com_rd(arcn));
332 * no cleanup needed here, just return size of the trailer (for append)
334 * size of trailer header in this format
340 return((off_t)(sizeof(HD_CPIO) + sizeof(TRAILER)));
345 * start up the device mapping table
347 * 0 if ok, -1 otherwise (what dev_start() returns)
358 * copy the data in the ARCHD to buffer in extended byte oriented cpio
361 * 0 if file has data to be written after the header, 1 if file has NO
362 * data to write after the header, -1 if archive write failed
373 * check and repair truncated device and inode fields in the header
375 if (map_dev(arcn, (u_long)CPIO_MASK, (u_long)CPIO_MASK) < 0)
379 nsz = arcn->nlen + 1;
381 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
382 arcn->sb.st_rdev = 0;
389 * set data size for file data
391 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
392 sizeof(hd->c_filesize), OCT)) {
393 paxwarn(1,"File is too large for cpio format %s",
400 * set data size to hold link name
402 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
403 sizeof(hd->c_filesize), OCT))
408 * all other file types have no file data
410 if (ul_asc((u_long)0, hd->c_filesize, sizeof(hd->c_filesize),
417 * copy the values to the header using octal ascii
419 if (ul_asc((u_long)MAGIC, hd->c_magic, sizeof(hd->c_magic), OCT) ||
420 ul_asc((u_long)arcn->sb.st_dev, hd->c_dev, sizeof(hd->c_dev),
422 ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
424 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
426 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
428 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
430 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
432 ul_asc((u_long)arcn->sb.st_rdev, hd->c_rdev, sizeof(hd->c_rdev),
434 ul_asc((u_long)arcn->sb.st_mtime,hd->c_mtime,sizeof(hd->c_mtime),
436 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), OCT))
440 * write the file name to the archive
442 if ((wr_rdbuf((char *)&hdblk, (int)sizeof(HD_CPIO)) < 0) ||
443 (wr_rdbuf(arcn->name, nsz) < 0)) {
444 paxwarn(1, "Unable to write cpio header for %s", arcn->org_name);
449 * if this file has data, we are done. The caller will write the file
450 * data, if we are link tell caller we are done, go to next file
452 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
453 (arcn->type == PAX_HRG))
455 if (arcn->type != PAX_SLK)
459 * write the link name to the archive, tell the caller to go to the
460 * next file as we are done.
462 if (wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) {
463 paxwarn(1,"Unable to write cpio link name for %s",arcn->org_name);
470 * header field is out of range
472 paxwarn(1, "Cpio header field is too small to store file %s",
478 * Routines common to the system VR4 version of cpio (with/without file CRC)
483 * determine if a block given to us is a valid system VR4 cpio header
484 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
487 * 0 if a valid header, -1 otherwise
491 vcpio_id(char *blk, int size)
493 if ((size < (int)sizeof(HD_VCPIO)) ||
494 (strncmp(blk, AVMAGIC, sizeof(AVMAGIC) - 1) != 0))
501 * determine if a block given to us is a valid system VR4 cpio header
502 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
504 * 0 if a valid header, -1 otherwise
508 crc_id(char *blk, int size)
510 if ((size < (int)sizeof(HD_VCPIO)) ||
511 (strncmp(blk, AVCMAGIC, (int)sizeof(AVCMAGIC) - 1) != 0))
518 w set file data CRC calculations. Fire up the hard link detection code
520 * 0 if ok -1 otherwise (the return values of lnk_start())
532 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
533 * convert and store the values in the ARCHD parameter.
535 * 0 if a valid header, -1 otherwise.
539 vcpio_rd(ARCHD *arcn, char *buf)
547 * during the id phase it was determined if we were using CRC, use the
551 if (crc_id(buf, sizeof(HD_VCPIO)) < 0)
554 if (vcpio_id(buf, sizeof(HD_VCPIO)) < 0)
558 hd = (HD_VCPIO *)buf;
562 * extract the hex ascii fields from the header
564 arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), HEX);
565 arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), HEX);
566 arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), HEX);
567 arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), HEX);
568 arcn->sb.st_mtime = (time_t)asc_uqd(hd->c_mtime,sizeof(hd->c_mtime),HEX);
569 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
570 arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,
571 sizeof(hd->c_filesize), HEX);
572 arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
574 devmajor = (dev_t)asc_ul(hd->c_maj, sizeof(hd->c_maj), HEX);
575 devminor = (dev_t)asc_ul(hd->c_min, sizeof(hd->c_min), HEX);
576 arcn->sb.st_dev = TODEV(devmajor, devminor);
577 devmajor = (dev_t)asc_ul(hd->c_rmaj, sizeof(hd->c_maj), HEX);
578 devminor = (dev_t)asc_ul(hd->c_rmin, sizeof(hd->c_min), HEX);
579 arcn->sb.st_rdev = TODEV(devmajor, devminor);
580 arcn->crc = asc_ul(hd->c_chksum, sizeof(hd->c_chksum), HEX);
583 * check the length of the file name, if ok read it in, return -1 if
586 if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),HEX)) < 2)
588 arcn->nlen = nsz - 1;
589 if (rd_nm(arcn, nsz) < 0)
593 * skip padding. header + filename is aligned to 4 byte boundaries
595 if (rd_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)
599 * if not a link (or a file with no data), calculate pad size (for
600 * padding which follows the file data), clear the link name and return
602 if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
604 * we have a valid header (not a link)
607 arcn->ln_name[0] = '\0';
608 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
609 return(com_rd(arcn));
613 * read in the link name and skip over the padding
615 if ((rd_ln_nm(arcn) < 0) ||
616 (rd_skip((off_t)(VCPIO_PAD(arcn->sb.st_size))) < 0))
620 * we have a valid header (with a link)
622 return(com_rd(arcn));
627 * no cleanup needed here, just return size of the trailer (for append)
629 * size of trailer header in this format
635 return((off_t)(sizeof(HD_VCPIO) + sizeof(TRAILER) +
636 (VCPIO_PAD(sizeof(HD_VCPIO) + sizeof(TRAILER)))));
641 * start up the device mapping table, enable crc file calculation
643 * 0 if ok, -1 otherwise (what dev_start() returns)
655 * copy the data in the ARCHD to buffer in system VR4 cpio
656 * (with/without crc) format.
658 * 0 if file has data to be written after the header, 1 if file has
659 * NO data to write after the header, -1 if archive write failed
663 vcpio_wr(ARCHD *arcn)
670 * check and repair truncated device and inode fields in the cpio
673 if (map_dev(arcn, (u_long)VCPIO_MASK, (u_long)VCPIO_MASK) < 0)
675 nsz = arcn->nlen + 1;
677 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
678 arcn->sb.st_rdev = 0;
681 * add the proper magic value depending whether we were asked for
682 * file data crc's, and the crc if needed.
685 if (ul_asc((u_long)VCMAGIC, hd->c_magic, sizeof(hd->c_magic),
687 ul_asc((u_long)arcn->crc,hd->c_chksum,sizeof(hd->c_chksum),
691 if (ul_asc((u_long)VMAGIC, hd->c_magic, sizeof(hd->c_magic),
693 ul_asc((u_long)0L, hd->c_chksum, sizeof(hd->c_chksum),HEX))
702 * caller will copy file data to the archive. tell him how
705 arcn->pad = VCPIO_PAD(arcn->sb.st_size);
706 if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
707 sizeof(hd->c_filesize), HEX)) {
708 paxwarn(1,"File is too large for sv4cpio format %s",
715 * no file data for the caller to process, the file data has
716 * the size of the link
719 if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
720 sizeof(hd->c_filesize), HEX))
725 * no file data for the caller to process
728 if (ul_asc((u_long)0L, hd->c_filesize, sizeof(hd->c_filesize),
735 * set the other fields in the header
737 if (ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
739 ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
741 ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
743 ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
745 ul_asc((u_long)arcn->sb.st_mtime, hd->c_mtime, sizeof(hd->c_mtime),
747 ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
749 ul_asc((u_long)MAJOR(arcn->sb.st_dev),hd->c_maj, sizeof(hd->c_maj),
751 ul_asc((u_long)MINOR(arcn->sb.st_dev),hd->c_min, sizeof(hd->c_min),
753 ul_asc((u_long)MAJOR(arcn->sb.st_rdev),hd->c_rmaj,sizeof(hd->c_maj),
755 ul_asc((u_long)MINOR(arcn->sb.st_rdev),hd->c_rmin,sizeof(hd->c_min),
757 ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), HEX))
761 * write the header, the file name and padding as required.
763 if ((wr_rdbuf((char *)&hdblk, (int)sizeof(HD_VCPIO)) < 0) ||
764 (wr_rdbuf(arcn->name, (int)nsz) < 0) ||
765 (wr_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)) {
766 paxwarn(1,"Could not write sv4cpio header for %s",arcn->org_name);
771 * if we have file data, tell the caller we are done, copy the file
773 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
774 (arcn->type == PAX_HRG))
778 * if we are not a link, tell the caller we are done, go to next file
780 if (arcn->type != PAX_SLK)
784 * write the link name, tell the caller we are done.
786 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
787 (wr_skip((off_t)(VCPIO_PAD(arcn->ln_nlen))) < 0)) {
788 paxwarn(1,"Could not write sv4cpio link name for %s",
796 * header field is out of range
798 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn->org_name);
803 * Routines common to the old binary header cpio
808 * determine if a block given to us is an old binary cpio header
809 * (with/without header byte swapping)
811 * 0 if a valid header, -1 otherwise
815 bcpio_id(char *blk, int size)
817 if (size < (int)sizeof(HD_BCPIO))
821 * check both normal and byte swapped magic cookies
823 if (((u_short)SHRT_EXT(blk)) == MAGIC)
825 if (((u_short)RSHRT_EXT(blk)) == MAGIC) {
835 * determine if a buffer is an old binary archive entry. (It may have byte
836 * swapped header) convert and store the values in the ARCHD parameter.
837 * This is a very old header format and should not really be used.
839 * 0 if a valid header, -1 otherwise.
843 bcpio_rd(ARCHD *arcn, char *buf)
851 if (bcpio_id(buf, sizeof(HD_BCPIO)) < 0)
855 hd = (HD_BCPIO *)buf;
858 * header has swapped bytes on 16 bit boundaries
860 arcn->sb.st_dev = (dev_t)(RSHRT_EXT(hd->h_dev));
861 arcn->sb.st_ino = (ino_t)(RSHRT_EXT(hd->h_ino));
862 arcn->sb.st_mode = (mode_t)(RSHRT_EXT(hd->h_mode));
863 arcn->sb.st_uid = (uid_t)(RSHRT_EXT(hd->h_uid));
864 arcn->sb.st_gid = (gid_t)(RSHRT_EXT(hd->h_gid));
865 arcn->sb.st_nlink = (nlink_t)(RSHRT_EXT(hd->h_nlink));
866 arcn->sb.st_rdev = (dev_t)(RSHRT_EXT(hd->h_rdev));
867 arcn->sb.st_mtime = (time_t)(RSHRT_EXT(hd->h_mtime_1));
868 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
869 ((time_t)(RSHRT_EXT(hd->h_mtime_2)));
870 arcn->sb.st_size = (off_t)(RSHRT_EXT(hd->h_filesize_1));
871 arcn->sb.st_size = (arcn->sb.st_size << 16) |
872 ((off_t)(RSHRT_EXT(hd->h_filesize_2)));
873 nsz = (int)(RSHRT_EXT(hd->h_namesize));
875 arcn->sb.st_dev = (dev_t)(SHRT_EXT(hd->h_dev));
876 arcn->sb.st_ino = (ino_t)(SHRT_EXT(hd->h_ino));
877 arcn->sb.st_mode = (mode_t)(SHRT_EXT(hd->h_mode));
878 arcn->sb.st_uid = (uid_t)(SHRT_EXT(hd->h_uid));
879 arcn->sb.st_gid = (gid_t)(SHRT_EXT(hd->h_gid));
880 arcn->sb.st_nlink = (nlink_t)(SHRT_EXT(hd->h_nlink));
881 arcn->sb.st_rdev = (dev_t)(SHRT_EXT(hd->h_rdev));
882 arcn->sb.st_mtime = (time_t)(SHRT_EXT(hd->h_mtime_1));
883 arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
884 ((time_t)(SHRT_EXT(hd->h_mtime_2)));
885 arcn->sb.st_size = (off_t)(SHRT_EXT(hd->h_filesize_1));
886 arcn->sb.st_size = (arcn->sb.st_size << 16) |
887 ((off_t)(SHRT_EXT(hd->h_filesize_2)));
888 nsz = (int)(SHRT_EXT(hd->h_namesize));
890 arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
893 * check the file name size, if bogus give up. otherwise read the file
898 arcn->nlen = nsz - 1;
899 if (rd_nm(arcn, nsz) < 0)
903 * header + file name are aligned to 2 byte boundaries, skip if needed
905 if (rd_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)
909 * if not a link (or a file with no data), calculate pad size (for
910 * padding which follows the file data), clear the link name and return
912 if (((arcn->sb.st_mode & C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)){
914 * we have a valid header (not a link)
917 arcn->ln_name[0] = '\0';
918 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
919 return(com_rd(arcn));
922 if ((rd_ln_nm(arcn) < 0) ||
923 (rd_skip((off_t)(BCPIO_PAD(arcn->sb.st_size))) < 0))
927 * we have a valid header (with a link)
929 return(com_rd(arcn));
934 * no cleanup needed here, just return size of the trailer (for append)
936 * size of trailer header in this format
942 return((off_t)(sizeof(HD_BCPIO) + sizeof(TRAILER) +
943 (BCPIO_PAD(sizeof(HD_BCPIO) + sizeof(TRAILER)))));
948 * copy the data in the ARCHD to buffer in old binary cpio format
949 * There is a real chance of field overflow with this critter. So we
950 * always check that the conversion is ok. nobody in their right mind
951 * should write an archive in this format...
953 * 0 if file has data to be written after the header, 1 if file has NO
954 * data to write after the header, -1 if archive write failed
958 bcpio_wr(ARCHD *arcn)
968 * check and repair truncated device and inode fields in the cpio
971 if (map_dev(arcn, (u_long)BCPIO_MASK, (u_long)BCPIO_MASK) < 0)
974 if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
975 arcn->sb.st_rdev = 0;
983 * caller will copy file data to the archive. tell him how
986 arcn->pad = BCPIO_PAD(arcn->sb.st_size);
987 hd->h_filesize_1[0] = CHR_WR_0(arcn->sb.st_size);
988 hd->h_filesize_1[1] = CHR_WR_1(arcn->sb.st_size);
989 hd->h_filesize_2[0] = CHR_WR_2(arcn->sb.st_size);
990 hd->h_filesize_2[1] = CHR_WR_3(arcn->sb.st_size);
991 t_offt = (off_t)(SHRT_EXT(hd->h_filesize_1));
992 t_offt = (t_offt<<16) | ((off_t)(SHRT_EXT(hd->h_filesize_2)));
993 if (arcn->sb.st_size != t_offt) {
994 paxwarn(1,"File is too large for bcpio format %s",
1001 * no file data for the caller to process, the file data has
1002 * the size of the link
1005 hd->h_filesize_1[0] = CHR_WR_0(arcn->ln_nlen);
1006 hd->h_filesize_1[1] = CHR_WR_1(arcn->ln_nlen);
1007 hd->h_filesize_2[0] = CHR_WR_2(arcn->ln_nlen);
1008 hd->h_filesize_2[1] = CHR_WR_3(arcn->ln_nlen);
1009 t_int = (int)(SHRT_EXT(hd->h_filesize_1));
1010 t_int = (t_int << 16) | ((int)(SHRT_EXT(hd->h_filesize_2)));
1011 if (arcn->ln_nlen != t_int)
1016 * no file data for the caller to process
1019 hd->h_filesize_1[0] = (char)0;
1020 hd->h_filesize_1[1] = (char)0;
1021 hd->h_filesize_2[0] = (char)0;
1022 hd->h_filesize_2[1] = (char)0;
1027 * build up the rest of the fields
1029 hd->h_magic[0] = CHR_WR_2(MAGIC);
1030 hd->h_magic[1] = CHR_WR_3(MAGIC);
1031 hd->h_dev[0] = CHR_WR_2(arcn->sb.st_dev);
1032 hd->h_dev[1] = CHR_WR_3(arcn->sb.st_dev);
1033 if (arcn->sb.st_dev != (dev_t)(SHRT_EXT(hd->h_dev)))
1035 hd->h_ino[0] = CHR_WR_2(arcn->sb.st_ino);
1036 hd->h_ino[1] = CHR_WR_3(arcn->sb.st_ino);
1037 if (arcn->sb.st_ino != (ino_t)(SHRT_EXT(hd->h_ino)))
1039 hd->h_mode[0] = CHR_WR_2(arcn->sb.st_mode);
1040 hd->h_mode[1] = CHR_WR_3(arcn->sb.st_mode);
1041 if (arcn->sb.st_mode != (mode_t)(SHRT_EXT(hd->h_mode)))
1043 hd->h_uid[0] = CHR_WR_2(arcn->sb.st_uid);
1044 hd->h_uid[1] = CHR_WR_3(arcn->sb.st_uid);
1045 if (arcn->sb.st_uid != (uid_t)(SHRT_EXT(hd->h_uid)))
1047 hd->h_gid[0] = CHR_WR_2(arcn->sb.st_gid);
1048 hd->h_gid[1] = CHR_WR_3(arcn->sb.st_gid);
1049 if (arcn->sb.st_gid != (gid_t)(SHRT_EXT(hd->h_gid)))
1051 hd->h_nlink[0] = CHR_WR_2(arcn->sb.st_nlink);
1052 hd->h_nlink[1] = CHR_WR_3(arcn->sb.st_nlink);
1053 if (arcn->sb.st_nlink != (nlink_t)(SHRT_EXT(hd->h_nlink)))
1055 hd->h_rdev[0] = CHR_WR_2(arcn->sb.st_rdev);
1056 hd->h_rdev[1] = CHR_WR_3(arcn->sb.st_rdev);
1057 if (arcn->sb.st_rdev != (dev_t)(SHRT_EXT(hd->h_rdev)))
1059 hd->h_mtime_1[0] = CHR_WR_0(arcn->sb.st_mtime);
1060 hd->h_mtime_1[1] = CHR_WR_1(arcn->sb.st_mtime);
1061 hd->h_mtime_2[0] = CHR_WR_2(arcn->sb.st_mtime);
1062 hd->h_mtime_2[1] = CHR_WR_3(arcn->sb.st_mtime);
1063 t_timet = (time_t)(SHRT_EXT(hd->h_mtime_1));
1064 t_timet = (t_timet << 16) | ((time_t)(SHRT_EXT(hd->h_mtime_2)));
1065 if (arcn->sb.st_mtime != t_timet)
1067 nsz = arcn->nlen + 1;
1068 hd->h_namesize[0] = CHR_WR_2(nsz);
1069 hd->h_namesize[1] = CHR_WR_3(nsz);
1070 if (nsz != (int)(SHRT_EXT(hd->h_namesize)))
1074 * write the header, the file name and padding as required.
1076 if ((wr_rdbuf((char *)&hdblk, (int)sizeof(HD_BCPIO)) < 0) ||
1077 (wr_rdbuf(arcn->name, nsz) < 0) ||
1078 (wr_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)) {
1079 paxwarn(1, "Could not write bcpio header for %s", arcn->org_name);
1084 * if we have file data, tell the caller we are done
1086 if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
1087 (arcn->type == PAX_HRG))
1091 * if we are not a link, tell the caller we are done, go to next file
1093 if (arcn->type != PAX_SLK)
1097 * write the link name, tell the caller we are done.
1099 if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
1100 (wr_skip((off_t)(BCPIO_PAD(arcn->ln_nlen))) < 0)) {
1101 paxwarn(1,"Could not write bcpio link name for %s",arcn->org_name);
1108 * header field is out of range
1110 paxwarn(1,"Bcpio header field is too small for file %s", arcn->org_name);