1 /* $OpenBSD: cpio.c,v 1.30 2016/08/26 04:11:16 guenther 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
37 #include <sys/types.h>
48 static int rd_nm(ARCHD
*, int);
49 static int rd_ln_nm(ARCHD
*);
50 static int com_rd(ARCHD
*);
53 * Routines which support the different cpio versions
56 static int swp_head
; /* binary cpio header byte swap */
59 * Routines common to all versions of cpio
64 * Fire up the hard link detection code
66 * 0 if ok -1 otherwise (the return values of lnk_start())
77 * Called to determine if a header block is a valid trailer. We are
78 * passed the block, the in_sync flag (which tells us we are in resync
79 * mode; looking for a valid header), and cnt (which starts at zero)
80 * which is used to count the number of empty blocks we have seen so far.
82 * 0 if a valid trailer, -1 if not a valid trailer,
86 cpio_trail(ARCHD
*arcn
, char *notused
, int notused2
, int *notused3
)
89 * look for trailer id in file we are about to process
91 if ((strcmp(arcn
->name
, TRAILER
) == 0) && (arcn
->sb
.st_size
== 0))
98 * operations common to all cpio read functions.
108 arcn
->org_name
= arcn
->name
;
109 switch (arcn
->sb
.st_mode
& C_IFMT
) {
111 arcn
->type
= PAX_FIF
;
114 arcn
->type
= PAX_DIR
;
117 arcn
->type
= PAX_BLK
;
120 arcn
->type
= PAX_CHR
;
123 arcn
->type
= PAX_SLK
;
126 arcn
->type
= PAX_SCK
;
132 * we have file data, set up skip (pad is set in the format
135 arcn
->sb
.st_mode
= (arcn
->sb
.st_mode
& 0xfff) | C_ISREG
;
136 arcn
->type
= PAX_REG
;
137 arcn
->skip
= arcn
->sb
.st_size
;
140 if (chk_lnk(arcn
) < 0)
147 * write the special file with the name trailer in the proper format
149 * result of the write of the trailer from the cpio specific write func
158 * create a trailer request and call the proper format write function
160 memset(&last
, 0, sizeof(last
));
161 last
.nlen
= sizeof(TRAILER
) - 1;
163 last
.sb
.st_nlink
= 1;
164 (void)strlcpy(last
.name
, TRAILER
, sizeof(last
.name
));
165 return((*frmt
->wr
)(&last
));
170 * read in the file name which follows the cpio header
172 * 0 if ok, -1 otherwise
176 rd_nm(ARCHD
*arcn
, int nsz
)
179 * do not even try bogus values
181 if ((nsz
== 0) || (nsz
> sizeof(arcn
->name
))) {
182 paxwarn(1, "Cpio file name length %d is out of range", nsz
);
187 * read the name and make sure it is not empty and is \0 terminated
189 if ((rd_wrbuf(arcn
->name
,nsz
) != nsz
) || (arcn
->name
[nsz
-1] != '\0') ||
190 (arcn
->name
[0] == '\0')) {
191 paxwarn(1, "Cpio file name in header is corrupted");
199 * read in the link name for a file with links. The link name is stored
200 * like file data (and is NOT \0 terminated!)
202 * 0 if ok, -1 otherwise
206 rd_ln_nm(ARCHD
*arcn
)
209 * check the length specified for bogus values
211 if ((arcn
->sb
.st_size
== 0) ||
212 (arcn
->sb
.st_size
>= sizeof(arcn
->ln_name
))) {
213 paxwarn(1, "Cpio link name length is invalid: %llu",
219 * read in the link name and \0 terminate it
221 if (rd_wrbuf(arcn
->ln_name
, (int)arcn
->sb
.st_size
) !=
222 (int)arcn
->sb
.st_size
) {
223 paxwarn(1, "Cpio link name read error");
226 arcn
->ln_nlen
= arcn
->sb
.st_size
;
227 arcn
->ln_name
[arcn
->ln_nlen
] = '\0';
230 * watch out for those empty link names
232 if (arcn
->ln_name
[0] == '\0') {
233 paxwarn(1, "Cpio link name is corrupt");
240 * Routines common to the extended byte oriented cpio format
245 * determine if a block given to us is a valid extended byte oriented
248 * 0 if a valid header, -1 otherwise
252 cpio_id(char *blk
, int size
)
254 if ((size
< sizeof(HD_CPIO
)) ||
255 (strncmp(blk
, AMAGIC
, sizeof(AMAGIC
) - 1) != 0))
262 * determine if a buffer is a byte oriented extended cpio archive entry.
263 * convert and store the values in the ARCHD parameter.
265 * 0 if a valid header, -1 otherwise.
269 cpio_rd(ARCHD
*arcn
, char *buf
)
272 unsigned long long val
;
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 val
= asc_ull(hd
->c_mtime
, sizeof(hd
->c_mtime
), OCT
);
296 if ((time_t)val
< 0 || (time_t)val
!= val
)
297 arcn
->sb
.st_mtime
= INT_MAX
; /* XXX 2038 */
299 arcn
->sb
.st_mtime
= val
;
300 arcn
->sb
.st_mtim
.tv_nsec
= 0;
301 arcn
->sb
.st_ctim
= arcn
->sb
.st_atim
= arcn
->sb
.st_mtim
;
302 arcn
->sb
.st_size
= (off_t
)asc_ull(hd
->c_filesize
,sizeof(hd
->c_filesize
),
306 * check name size and if valid, read in the name of this entry (name
307 * follows header in the archive)
309 if ((nsz
= (int)asc_ul(hd
->c_namesize
,sizeof(hd
->c_namesize
),OCT
)) < 2)
311 arcn
->nlen
= nsz
- 1;
312 if (rd_nm(arcn
, nsz
) < 0)
315 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
317 * no link name to read for this file
320 arcn
->ln_name
[0] = '\0';
321 return(com_rd(arcn
));
325 * check link name size and read in the link name. Link names are
326 * stored like file data.
328 if (rd_ln_nm(arcn
) < 0)
332 * we have a valid header (with a link)
334 return(com_rd(arcn
));
339 * no cleanup needed here, just return size of the trailer (for append)
341 * size of trailer header in this format
347 return sizeof(HD_CPIO
) + sizeof(TRAILER
);
352 * start up the device mapping table
354 * 0 if ok, -1 otherwise (what dev_start() returns)
365 * copy the data in the ARCHD to buffer in extended byte oriented cpio
368 * 0 if file has data to be written after the header, 1 if file has NO
369 * data to write after the header, -1 if archive write failed
377 char hdblk
[sizeof(HD_CPIO
)];
380 * check and repair truncated device and inode fields in the header
382 if (map_dev(arcn
, CPIO_MASK
, CPIO_MASK
) < 0)
386 nsz
= arcn
->nlen
+ 1;
387 hd
= (HD_CPIO
*)hdblk
;
388 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
389 arcn
->sb
.st_rdev
= 0;
391 switch (arcn
->type
) {
396 * set data size for file data
398 if (ull_asc(arcn
->sb
.st_size
, hd
->c_filesize
,
399 sizeof(hd
->c_filesize
), OCT
)) {
400 paxwarn(1,"File is too large for cpio format %s",
407 * set data size to hold link name
409 if (ul_asc(arcn
->ln_nlen
, hd
->c_filesize
,
410 sizeof(hd
->c_filesize
), OCT
))
415 * all other file types have no file data
417 if (ul_asc(0, hd
->c_filesize
, sizeof(hd
->c_filesize
), OCT
))
423 * copy the values to the header using octal ascii
425 if (ul_asc(MAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
), OCT
) ||
426 ul_asc(arcn
->sb
.st_dev
, hd
->c_dev
, sizeof(hd
->c_dev
), OCT
) ||
427 ul_asc(arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
), OCT
) ||
428 ul_asc(arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
), OCT
) ||
429 ul_asc(arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
), OCT
) ||
430 ul_asc(arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
), OCT
) ||
431 ul_asc(arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
), OCT
) ||
432 ul_asc(arcn
->sb
.st_rdev
, hd
->c_rdev
, sizeof(hd
->c_rdev
), OCT
) ||
433 ull_asc(arcn
->sb
.st_mtime
< 0 ? 0 : arcn
->sb
.st_mtime
, hd
->c_mtime
,
434 sizeof(hd
->c_mtime
), OCT
) ||
435 ul_asc(nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), OCT
))
439 * write the file name to the archive
441 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_CPIO
)) < 0) ||
442 (wr_rdbuf(arcn
->name
, nsz
) < 0)) {
443 paxwarn(1, "Unable to write cpio header for %s", arcn
->org_name
);
448 * if this file has data, we are done. The caller will write the file
449 * data, if we are link tell caller we are done, go to next file
451 if (PAX_IS_REG(arcn
->type
) || (arcn
->type
== PAX_HRG
))
453 if (arcn
->type
!= PAX_SLK
)
457 * write the link name to the archive, tell the caller to go to the
458 * next file as we are done.
460 if (wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) {
461 paxwarn(1,"Unable to write cpio link name for %s",arcn
->org_name
);
468 * header field is out of range
470 paxwarn(1, "Cpio header field is too small to store file %s",
476 * Routines common to the system VR4 version of cpio (with/without file CRC)
481 * determine if a block given to us is a valid system VR4 cpio header
482 * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
485 * 0 if a valid header, -1 otherwise
489 vcpio_id(char *blk
, int size
)
491 if ((size
< sizeof(HD_VCPIO
)) ||
492 (strncmp(blk
, AVMAGIC
, sizeof(AVMAGIC
) - 1) != 0))
499 * determine if a block given to us is a valid system VR4 cpio header
500 * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
502 * 0 if a valid header, -1 otherwise
506 crc_id(char *blk
, int size
)
508 if ((size
< sizeof(HD_VCPIO
)) ||
509 (strncmp(blk
, AVCMAGIC
, sizeof(AVCMAGIC
) - 1) != 0))
516 w set file data CRC calculations. Fire up the hard link detection code
518 * 0 if ok -1 otherwise (the return values of lnk_start())
530 * determine if a buffer is a system VR4 archive entry. (with/without CRC)
531 * convert and store the values in the ARCHD parameter.
533 * 0 if a valid header, -1 otherwise.
537 vcpio_rd(ARCHD
*arcn
, char *buf
)
545 * during the id phase it was determined if we were using CRC, use the
549 if (crc_id(buf
, sizeof(HD_VCPIO
)) < 0)
552 if (vcpio_id(buf
, sizeof(HD_VCPIO
)) < 0)
556 hd
= (HD_VCPIO
*)buf
;
560 * extract the hex ascii fields from the header
562 arcn
->sb
.st_ino
= (ino_t
)asc_ul(hd
->c_ino
, sizeof(hd
->c_ino
), HEX
);
563 arcn
->sb
.st_mode
= (mode_t
)asc_ul(hd
->c_mode
, sizeof(hd
->c_mode
), HEX
);
564 arcn
->sb
.st_uid
= (uid_t
)asc_ul(hd
->c_uid
, sizeof(hd
->c_uid
), HEX
);
565 arcn
->sb
.st_gid
= (gid_t
)asc_ul(hd
->c_gid
, sizeof(hd
->c_gid
), HEX
);
566 arcn
->sb
.st_mtime
= (time_t)asc_ul(hd
->c_mtime
,sizeof(hd
->c_mtime
),HEX
);
567 arcn
->sb
.st_mtim
.tv_nsec
= 0;
568 arcn
->sb
.st_ctim
= arcn
->sb
.st_atim
= arcn
->sb
.st_mtim
;
569 arcn
->sb
.st_size
= (off_t
)asc_ull(hd
->c_filesize
,
570 sizeof(hd
->c_filesize
), HEX
);
571 arcn
->sb
.st_nlink
= (nlink_t
)asc_ul(hd
->c_nlink
, sizeof(hd
->c_nlink
),
573 devmajor
= (dev_t
)asc_ul(hd
->c_maj
, sizeof(hd
->c_maj
), HEX
);
574 devminor
= (dev_t
)asc_ul(hd
->c_min
, sizeof(hd
->c_min
), HEX
);
575 arcn
->sb
.st_dev
= TODEV(devmajor
, devminor
);
576 devmajor
= (dev_t
)asc_ul(hd
->c_rmaj
, sizeof(hd
->c_maj
), HEX
);
577 devminor
= (dev_t
)asc_ul(hd
->c_rmin
, sizeof(hd
->c_min
), HEX
);
578 arcn
->sb
.st_rdev
= TODEV(devmajor
, devminor
);
579 arcn
->crc
= asc_ul(hd
->c_chksum
, sizeof(hd
->c_chksum
), HEX
);
582 * check the length of the file name, if ok read it in, return -1 if
585 if ((nsz
= (int)asc_ul(hd
->c_namesize
,sizeof(hd
->c_namesize
),HEX
)) < 2)
587 arcn
->nlen
= nsz
- 1;
588 if (rd_nm(arcn
, nsz
) < 0)
592 * skip padding. header + filename is aligned to 4 byte boundaries
594 if (rd_skip(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
)) < 0)
598 * if not a link (or a file with no data), calculate pad size (for
599 * padding which follows the file data), clear the link name and return
601 if (((arcn
->sb
.st_mode
&C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)) {
603 * we have a valid header (not a link)
606 arcn
->ln_name
[0] = '\0';
607 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
608 return(com_rd(arcn
));
612 * read in the link name and skip over the padding
614 if ((rd_ln_nm(arcn
) < 0) ||
615 (rd_skip(VCPIO_PAD(arcn
->sb
.st_size
)) < 0))
619 * we have a valid header (with a link)
621 return(com_rd(arcn
));
626 * no cleanup needed here, just return size of the trailer (for append)
628 * size of trailer header in this format
634 return sizeof(HD_VCPIO
) + sizeof(TRAILER
) +
635 (VCPIO_PAD(sizeof(HD_VCPIO
) + sizeof(TRAILER
)));
640 * start up the device mapping table, enable crc file calculation
642 * 0 if ok, -1 otherwise (what dev_start() returns)
654 * copy the data in the ARCHD to buffer in system VR4 cpio
655 * (with/without crc) format.
657 * 0 if file has data to be written after the header, 1 if file has
658 * NO data to write after the header, -1 if archive write failed
662 vcpio_wr(ARCHD
*arcn
)
666 char hdblk
[sizeof(HD_VCPIO
)];
669 * check and repair truncated device and inode fields in the cpio
672 if (map_dev(arcn
, VCPIO_MASK
, VCPIO_MASK
) < 0)
674 nsz
= arcn
->nlen
+ 1;
675 hd
= (HD_VCPIO
*)hdblk
;
676 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
677 arcn
->sb
.st_rdev
= 0;
680 * add the proper magic value depending whether we were asked for
681 * file data crc's, and the crc if needed.
684 if (ul_asc(VCMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
), OCT
) ||
685 ul_asc(arcn
->crc
,hd
->c_chksum
,sizeof(hd
->c_chksum
), HEX
))
688 if (ul_asc(VMAGIC
, hd
->c_magic
, sizeof(hd
->c_magic
), OCT
) ||
689 ul_asc(0, hd
->c_chksum
, sizeof(hd
->c_chksum
),HEX
))
693 switch (arcn
->type
) {
698 * caller will copy file data to the archive. tell him how
701 arcn
->pad
= VCPIO_PAD(arcn
->sb
.st_size
);
702 if (ull_asc(arcn
->sb
.st_size
, hd
->c_filesize
,
703 sizeof(hd
->c_filesize
), HEX
)) {
704 paxwarn(1,"File is too large for sv4cpio format %s",
711 * no file data for the caller to process, the file data has
712 * the size of the link
715 if (ul_asc(arcn
->ln_nlen
, hd
->c_filesize
,
716 sizeof(hd
->c_filesize
), HEX
))
721 * no file data for the caller to process
724 if (ul_asc(0, hd
->c_filesize
, sizeof(hd
->c_filesize
), HEX
))
730 * set the other fields in the header
732 if (ul_asc(arcn
->sb
.st_ino
, hd
->c_ino
, sizeof(hd
->c_ino
), HEX
) ||
733 ul_asc(arcn
->sb
.st_mode
, hd
->c_mode
, sizeof(hd
->c_mode
), HEX
) ||
734 ul_asc(arcn
->sb
.st_uid
, hd
->c_uid
, sizeof(hd
->c_uid
), HEX
) ||
735 ul_asc(arcn
->sb
.st_gid
, hd
->c_gid
, sizeof(hd
->c_gid
), HEX
) ||
736 ul_asc(arcn
->sb
.st_mtime
< 0 ? 0 : arcn
->sb
.st_mtime
, hd
->c_mtime
,
737 sizeof(hd
->c_mtime
), HEX
) ||
738 ul_asc(arcn
->sb
.st_nlink
, hd
->c_nlink
, sizeof(hd
->c_nlink
), HEX
) ||
739 ul_asc(MAJOR(arcn
->sb
.st_dev
),hd
->c_maj
, sizeof(hd
->c_maj
), HEX
) ||
740 ul_asc(MINOR(arcn
->sb
.st_dev
),hd
->c_min
, sizeof(hd
->c_min
), HEX
) ||
741 ul_asc(MAJOR(arcn
->sb
.st_rdev
),hd
->c_rmaj
,sizeof(hd
->c_maj
), HEX
) ||
742 ul_asc(MINOR(arcn
->sb
.st_rdev
),hd
->c_rmin
,sizeof(hd
->c_min
), HEX
) ||
743 ul_asc(nsz
, hd
->c_namesize
, sizeof(hd
->c_namesize
), HEX
))
747 * write the header, the file name and padding as required.
749 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_VCPIO
)) < 0) ||
750 (wr_rdbuf(arcn
->name
, (int)nsz
) < 0) ||
751 (wr_skip(VCPIO_PAD(sizeof(HD_VCPIO
) + nsz
)) < 0)) {
752 paxwarn(1,"Could not write sv4cpio header for %s",arcn
->org_name
);
757 * if we have file data, tell the caller we are done, copy the file
759 if (PAX_IS_REG(arcn
->type
) || (arcn
->type
== PAX_HRG
))
763 * if we are not a link, tell the caller we are done, go to next file
765 if (arcn
->type
!= PAX_SLK
)
769 * write the link name, tell the caller we are done.
771 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
772 (wr_skip(VCPIO_PAD(arcn
->ln_nlen
)) < 0)) {
773 paxwarn(1,"Could not write sv4cpio link name for %s",
781 * header field is out of range
783 paxwarn(1,"Sv4cpio header field is too small for file %s",arcn
->org_name
);
788 * Routines common to the old binary header cpio
793 * determine if a block given to us is a old binary cpio header
794 * (with/without header byte swapping)
796 * 0 if a valid header, -1 otherwise
800 bcpio_id(char *blk
, int size
)
802 if (size
< sizeof(HD_BCPIO
))
806 * check both normal and byte swapped magic cookies
808 if (((u_short
)SHRT_EXT(blk
)) == MAGIC
)
810 if (((u_short
)RSHRT_EXT(blk
)) == MAGIC
) {
820 * determine if a buffer is a old binary archive entry. (it may have byte
821 * swapped header) convert and store the values in the ARCHD parameter.
822 * This is a very old header format and should not really be used.
824 * 0 if a valid header, -1 otherwise.
828 bcpio_rd(ARCHD
*arcn
, char *buf
)
836 if (bcpio_id(buf
, sizeof(HD_BCPIO
)) < 0)
840 hd
= (HD_BCPIO
*)buf
;
843 * header has swapped bytes on 16 bit boundaries
845 arcn
->sb
.st_dev
= (dev_t
)(RSHRT_EXT(hd
->h_dev
));
846 arcn
->sb
.st_ino
= (ino_t
)(RSHRT_EXT(hd
->h_ino
));
847 arcn
->sb
.st_mode
= (mode_t
)(RSHRT_EXT(hd
->h_mode
));
848 arcn
->sb
.st_uid
= (uid_t
)(RSHRT_EXT(hd
->h_uid
));
849 arcn
->sb
.st_gid
= (gid_t
)(RSHRT_EXT(hd
->h_gid
));
850 arcn
->sb
.st_nlink
= (nlink_t
)(RSHRT_EXT(hd
->h_nlink
));
851 arcn
->sb
.st_rdev
= (dev_t
)(RSHRT_EXT(hd
->h_rdev
));
852 arcn
->sb
.st_mtime
= (time_t)(RSHRT_EXT(hd
->h_mtime_1
));
853 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
854 ((time_t)(RSHRT_EXT(hd
->h_mtime_2
)));
855 arcn
->sb
.st_size
= (off_t
)(RSHRT_EXT(hd
->h_filesize_1
));
856 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
857 ((off_t
)(RSHRT_EXT(hd
->h_filesize_2
)));
858 nsz
= (int)(RSHRT_EXT(hd
->h_namesize
));
860 arcn
->sb
.st_dev
= (dev_t
)(SHRT_EXT(hd
->h_dev
));
861 arcn
->sb
.st_ino
= (ino_t
)(SHRT_EXT(hd
->h_ino
));
862 arcn
->sb
.st_mode
= (mode_t
)(SHRT_EXT(hd
->h_mode
));
863 arcn
->sb
.st_uid
= (uid_t
)(SHRT_EXT(hd
->h_uid
));
864 arcn
->sb
.st_gid
= (gid_t
)(SHRT_EXT(hd
->h_gid
));
865 arcn
->sb
.st_nlink
= (nlink_t
)(SHRT_EXT(hd
->h_nlink
));
866 arcn
->sb
.st_rdev
= (dev_t
)(SHRT_EXT(hd
->h_rdev
));
867 arcn
->sb
.st_mtime
= (time_t)(SHRT_EXT(hd
->h_mtime_1
));
868 arcn
->sb
.st_mtime
= (arcn
->sb
.st_mtime
<< 16) |
869 ((time_t)(SHRT_EXT(hd
->h_mtime_2
)));
870 arcn
->sb
.st_size
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
871 arcn
->sb
.st_size
= (arcn
->sb
.st_size
<< 16) |
872 ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
873 nsz
= (int)(SHRT_EXT(hd
->h_namesize
));
875 arcn
->sb
.st_mtim
.tv_nsec
= 0;
876 arcn
->sb
.st_ctim
= arcn
->sb
.st_atim
= arcn
->sb
.st_mtim
;
879 * check the file name size, if bogus give up. otherwise read the file
884 arcn
->nlen
= nsz
- 1;
885 if (rd_nm(arcn
, nsz
) < 0)
889 * header + file name are aligned to 2 byte boundaries, skip if needed
891 if (rd_skip(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
)) < 0)
895 * if not a link (or a file with no data), calculate pad size (for
896 * padding which follows the file data), clear the link name and return
898 if (((arcn
->sb
.st_mode
& C_IFMT
) != C_ISLNK
)||(arcn
->sb
.st_size
== 0)){
900 * we have a valid header (not a link)
903 arcn
->ln_name
[0] = '\0';
904 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
905 return(com_rd(arcn
));
908 if ((rd_ln_nm(arcn
) < 0) ||
909 (rd_skip(BCPIO_PAD(arcn
->sb
.st_size
)) < 0))
913 * we have a valid header (with a link)
915 return(com_rd(arcn
));
920 * no cleanup needed here, just return size of the trailer (for append)
922 * size of trailer header in this format
928 return sizeof(HD_BCPIO
) + sizeof(TRAILER
) +
929 (BCPIO_PAD(sizeof(HD_BCPIO
) + sizeof(TRAILER
)));
934 * copy the data in the ARCHD to buffer in old binary cpio format
935 * There is a real chance of field overflow with this critter. So we
936 * always check the conversion is ok. nobody in their right mind
937 * should write an archive in this format...
939 * 0 if file has data to be written after the header, 1 if file has NO
940 * data to write after the header, -1 if archive write failed
944 bcpio_wr(ARCHD
*arcn
)
948 char hdblk
[sizeof(HD_BCPIO
)];
954 * check and repair truncated device and inode fields in the cpio
957 if (map_dev(arcn
, BCPIO_MASK
, BCPIO_MASK
) < 0)
960 if ((arcn
->type
!= PAX_BLK
) && (arcn
->type
!= PAX_CHR
))
961 arcn
->sb
.st_rdev
= 0;
962 hd
= (HD_BCPIO
*)hdblk
;
964 switch (arcn
->type
) {
969 * caller will copy file data to the archive. tell him how
972 arcn
->pad
= BCPIO_PAD(arcn
->sb
.st_size
);
973 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->sb
.st_size
);
974 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->sb
.st_size
);
975 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->sb
.st_size
);
976 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->sb
.st_size
);
977 t_offt
= (off_t
)(SHRT_EXT(hd
->h_filesize_1
));
978 t_offt
= (t_offt
<<16) | ((off_t
)(SHRT_EXT(hd
->h_filesize_2
)));
979 if (arcn
->sb
.st_size
!= t_offt
) {
980 paxwarn(1,"File is too large for bcpio format %s",
987 * no file data for the caller to process, the file data has
988 * the size of the link
991 hd
->h_filesize_1
[0] = CHR_WR_0(arcn
->ln_nlen
);
992 hd
->h_filesize_1
[1] = CHR_WR_1(arcn
->ln_nlen
);
993 hd
->h_filesize_2
[0] = CHR_WR_2(arcn
->ln_nlen
);
994 hd
->h_filesize_2
[1] = CHR_WR_3(arcn
->ln_nlen
);
995 t_int
= (int)(SHRT_EXT(hd
->h_filesize_1
));
996 t_int
= (t_int
<< 16) | ((int)(SHRT_EXT(hd
->h_filesize_2
)));
997 if (arcn
->ln_nlen
!= t_int
)
1002 * no file data for the caller to process
1005 hd
->h_filesize_1
[0] = '\0';
1006 hd
->h_filesize_1
[1] = '\0';
1007 hd
->h_filesize_2
[0] = '\0';
1008 hd
->h_filesize_2
[1] = '\0';
1013 * build up the rest of the fields
1015 hd
->h_magic
[0] = CHR_WR_2(MAGIC
);
1016 hd
->h_magic
[1] = CHR_WR_3(MAGIC
);
1017 hd
->h_dev
[0] = CHR_WR_2(arcn
->sb
.st_dev
);
1018 hd
->h_dev
[1] = CHR_WR_3(arcn
->sb
.st_dev
);
1019 if (arcn
->sb
.st_dev
!= (dev_t
)(SHRT_EXT(hd
->h_dev
)))
1021 hd
->h_ino
[0] = CHR_WR_2(arcn
->sb
.st_ino
);
1022 hd
->h_ino
[1] = CHR_WR_3(arcn
->sb
.st_ino
);
1023 if (arcn
->sb
.st_ino
!= (ino_t
)(SHRT_EXT(hd
->h_ino
)))
1025 hd
->h_mode
[0] = CHR_WR_2(arcn
->sb
.st_mode
);
1026 hd
->h_mode
[1] = CHR_WR_3(arcn
->sb
.st_mode
);
1027 if (arcn
->sb
.st_mode
!= (mode_t
)(SHRT_EXT(hd
->h_mode
)))
1029 hd
->h_uid
[0] = CHR_WR_2(arcn
->sb
.st_uid
);
1030 hd
->h_uid
[1] = CHR_WR_3(arcn
->sb
.st_uid
);
1031 if (arcn
->sb
.st_uid
!= (uid_t
)(SHRT_EXT(hd
->h_uid
)))
1033 hd
->h_gid
[0] = CHR_WR_2(arcn
->sb
.st_gid
);
1034 hd
->h_gid
[1] = CHR_WR_3(arcn
->sb
.st_gid
);
1035 if (arcn
->sb
.st_gid
!= (gid_t
)(SHRT_EXT(hd
->h_gid
)))
1037 hd
->h_nlink
[0] = CHR_WR_2(arcn
->sb
.st_nlink
);
1038 hd
->h_nlink
[1] = CHR_WR_3(arcn
->sb
.st_nlink
);
1039 if (arcn
->sb
.st_nlink
!= (nlink_t
)(SHRT_EXT(hd
->h_nlink
)))
1041 hd
->h_rdev
[0] = CHR_WR_2(arcn
->sb
.st_rdev
);
1042 hd
->h_rdev
[1] = CHR_WR_3(arcn
->sb
.st_rdev
);
1043 if (arcn
->sb
.st_rdev
!= (dev_t
)(SHRT_EXT(hd
->h_rdev
)))
1045 if (arcn
->sb
.st_mtime
> 0) {
1046 hd
->h_mtime_1
[0] = CHR_WR_0(arcn
->sb
.st_mtime
);
1047 hd
->h_mtime_1
[1] = CHR_WR_1(arcn
->sb
.st_mtime
);
1048 hd
->h_mtime_2
[0] = CHR_WR_2(arcn
->sb
.st_mtime
);
1049 hd
->h_mtime_2
[1] = CHR_WR_3(arcn
->sb
.st_mtime
);
1050 t_timet
= (time_t)SHRT_EXT(hd
->h_mtime_1
);
1051 t_timet
= t_timet
<< 16 | (time_t)SHRT_EXT(hd
->h_mtime_2
);
1052 if (arcn
->sb
.st_mtime
!= t_timet
)
1055 hd
->h_mtime_1
[0] = hd
->h_mtime_1
[1] = 0;
1056 hd
->h_mtime_2
[0] = hd
->h_mtime_2
[1] = 0;
1058 nsz
= arcn
->nlen
+ 1;
1059 hd
->h_namesize
[0] = CHR_WR_2(nsz
);
1060 hd
->h_namesize
[1] = CHR_WR_3(nsz
);
1061 if (nsz
!= (int)(SHRT_EXT(hd
->h_namesize
)))
1065 * write the header, the file name and padding as required.
1067 if ((wr_rdbuf(hdblk
, (int)sizeof(HD_BCPIO
)) < 0) ||
1068 (wr_rdbuf(arcn
->name
, nsz
) < 0) ||
1069 (wr_skip(BCPIO_PAD(sizeof(HD_BCPIO
) + nsz
)) < 0)) {
1070 paxwarn(1, "Could not write bcpio header for %s", arcn
->org_name
);
1075 * if we have file data, tell the caller we are done
1077 if (PAX_IS_REG(arcn
->type
) || (arcn
->type
== PAX_HRG
))
1081 * if we are not a link, tell the caller we are done, go to next file
1083 if (arcn
->type
!= PAX_SLK
)
1087 * write the link name, tell the caller we are done.
1089 if ((wr_rdbuf(arcn
->ln_name
, arcn
->ln_nlen
) < 0) ||
1090 (wr_skip(BCPIO_PAD(arcn
->ln_nlen
)) < 0)) {
1091 paxwarn(1,"Could not write bcpio link name for %s",arcn
->org_name
);
1098 * header field is out of range
1100 paxwarn(1,"Bcpio header field is too small for file %s", arcn
->org_name
);