cpio.c revision 1.4 1 1.1 jtc /*-
2 1.1 jtc * Copyright (c) 1992 Keith Muller.
3 1.1 jtc * Copyright (c) 1992, 1993
4 1.1 jtc * The Regents of the University of California. All rights reserved.
5 1.1 jtc *
6 1.1 jtc * This code is derived from software contributed to Berkeley by
7 1.1 jtc * Keith Muller of the University of California, San Diego.
8 1.1 jtc *
9 1.1 jtc * Redistribution and use in source and binary forms, with or without
10 1.1 jtc * modification, are permitted provided that the following conditions
11 1.1 jtc * are met:
12 1.1 jtc * 1. Redistributions of source code must retain the above copyright
13 1.1 jtc * notice, this list of conditions and the following disclaimer.
14 1.1 jtc * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 jtc * notice, this list of conditions and the following disclaimer in the
16 1.1 jtc * documentation and/or other materials provided with the distribution.
17 1.1 jtc * 3. All advertising materials mentioning features or use of this software
18 1.1 jtc * must display the following acknowledgement:
19 1.1 jtc * This product includes software developed by the University of
20 1.1 jtc * California, Berkeley and its contributors.
21 1.1 jtc * 4. Neither the name of the University nor the names of its contributors
22 1.1 jtc * may be used to endorse or promote products derived from this software
23 1.1 jtc * without specific prior written permission.
24 1.1 jtc *
25 1.1 jtc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 jtc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 jtc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 jtc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 jtc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 jtc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 jtc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 jtc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 jtc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 jtc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 jtc * SUCH DAMAGE.
36 1.1 jtc */
37 1.1 jtc
38 1.1 jtc #ifndef lint
39 1.2 jtc /*static char sccsid[] = "from: @(#)cpio.c 8.1 (Berkeley) 5/31/93";*/
40 1.4 mycroft static char *rcsid = "$Id: cpio.c,v 1.4 1994/09/23 11:35:09 mycroft Exp $";
41 1.1 jtc #endif /* not lint */
42 1.1 jtc
43 1.1 jtc #include <sys/types.h>
44 1.1 jtc #include <sys/time.h>
45 1.1 jtc #include <sys/stat.h>
46 1.1 jtc #include <sys/param.h>
47 1.1 jtc #include <string.h>
48 1.1 jtc #include <ctype.h>
49 1.1 jtc #include <stdio.h>
50 1.1 jtc #include <unistd.h>
51 1.1 jtc #include <stdlib.h>
52 1.1 jtc #include "pax.h"
53 1.1 jtc #include "cpio.h"
54 1.1 jtc #include "extern.h"
55 1.1 jtc
56 1.1 jtc static int rd_nm __P((register ARCHD *, int));
57 1.1 jtc static int rd_ln_nm __P((register ARCHD *));
58 1.1 jtc static int com_rd __P((register ARCHD *));
59 1.1 jtc
60 1.1 jtc /*
61 1.1 jtc * Routines which support the different cpio versions
62 1.1 jtc */
63 1.1 jtc
64 1.1 jtc static int swp_head; /* binary cpio header byte swap */
65 1.1 jtc
66 1.1 jtc /*
67 1.1 jtc * Routines common to all versions of cpio
68 1.1 jtc */
69 1.1 jtc
70 1.1 jtc /*
71 1.1 jtc * cpio_strd()
72 1.1 jtc * Fire up the hard link detection code
73 1.1 jtc * Return:
74 1.1 jtc * 0 if ok -1 otherwise (the return values of lnk_start())
75 1.1 jtc */
76 1.1 jtc
77 1.1 jtc #if __STDC__
78 1.1 jtc int
79 1.1 jtc cpio_strd(void)
80 1.1 jtc #else
81 1.1 jtc int
82 1.1 jtc cpio_strd()
83 1.1 jtc #endif
84 1.1 jtc {
85 1.1 jtc return(lnk_start());
86 1.1 jtc }
87 1.1 jtc
88 1.1 jtc /*
89 1.1 jtc * cpio_trail()
90 1.1 jtc * Called to determine if a header block is a valid trailer. We are
91 1.1 jtc * passed the block, the in_sync flag (which tells us we are in resync
92 1.1 jtc * mode; looking for a valid header), and cnt (which starts at zero)
93 1.1 jtc * which is used to count the number of empty blocks we have seen so far.
94 1.1 jtc * Return:
95 1.1 jtc * 0 if a valid trailer, -1 if not a valid trailer,
96 1.1 jtc */
97 1.1 jtc
98 1.1 jtc #if __STDC__
99 1.1 jtc int
100 1.1 jtc cpio_trail(register ARCHD *arcn)
101 1.1 jtc #else
102 1.1 jtc int
103 1.1 jtc cpio_trail(arcn)
104 1.1 jtc register ARCHD *arcn;
105 1.1 jtc #endif
106 1.1 jtc {
107 1.1 jtc /*
108 1.1 jtc * look for trailer id in file we are about to process
109 1.1 jtc */
110 1.1 jtc if ((strcmp(arcn->name, TRAILER) == 0) && (arcn->sb.st_size == 0))
111 1.1 jtc return(0);
112 1.1 jtc return(-1);
113 1.1 jtc }
114 1.1 jtc
115 1.1 jtc /*
116 1.1 jtc * com_rd()
117 1.1 jtc * operations common to all cpio read functions.
118 1.1 jtc * Return:
119 1.1 jtc * 0
120 1.1 jtc */
121 1.1 jtc
122 1.1 jtc #if __STDC__
123 1.1 jtc static int
124 1.1 jtc com_rd(register ARCHD *arcn)
125 1.1 jtc #else
126 1.1 jtc static int
127 1.1 jtc com_rd(arcn)
128 1.1 jtc register ARCHD *arcn;
129 1.1 jtc #endif
130 1.1 jtc {
131 1.1 jtc arcn->skip = 0;
132 1.1 jtc arcn->pat = NULL;
133 1.1 jtc arcn->org_name = arcn->name;
134 1.1 jtc switch(arcn->sb.st_mode & C_IFMT) {
135 1.1 jtc case C_ISFIFO:
136 1.1 jtc arcn->type = PAX_FIF;
137 1.1 jtc break;
138 1.1 jtc case C_ISDIR:
139 1.1 jtc arcn->type = PAX_DIR;
140 1.1 jtc break;
141 1.1 jtc case C_ISBLK:
142 1.1 jtc arcn->type = PAX_BLK;
143 1.1 jtc break;
144 1.1 jtc case C_ISCHR:
145 1.1 jtc arcn->type = PAX_CHR;
146 1.1 jtc break;
147 1.1 jtc case C_ISLNK:
148 1.1 jtc arcn->type = PAX_SLK;
149 1.1 jtc break;
150 1.1 jtc case C_ISOCK:
151 1.1 jtc arcn->type = PAX_SCK;
152 1.1 jtc break;
153 1.1 jtc case C_ISCTG:
154 1.1 jtc case C_ISREG:
155 1.1 jtc default:
156 1.1 jtc /*
157 1.1 jtc * we have file data, set up skip (pad is set in the format
158 1.1 jtc * specific sections)
159 1.1 jtc */
160 1.1 jtc arcn->sb.st_mode = (arcn->sb.st_mode & 0xfff) | C_ISREG;
161 1.1 jtc arcn->type = PAX_REG;
162 1.1 jtc arcn->skip = arcn->sb.st_size;
163 1.1 jtc break;
164 1.1 jtc }
165 1.1 jtc if (chk_lnk(arcn) < 0)
166 1.1 jtc return(-1);
167 1.1 jtc return(0);
168 1.1 jtc }
169 1.1 jtc
170 1.1 jtc /*
171 1.1 jtc * cpio_end_wr()
172 1.1 jtc * write the special file with the name trailer in the proper format
173 1.1 jtc * Return:
174 1.1 jtc * result of the write of the trailer from the cpio specific write func
175 1.1 jtc */
176 1.1 jtc
177 1.1 jtc #if __STDC__
178 1.1 jtc int
179 1.1 jtc cpio_endwr(void)
180 1.1 jtc #else
181 1.1 jtc int
182 1.1 jtc cpio_endwr()
183 1.1 jtc #endif
184 1.1 jtc {
185 1.1 jtc ARCHD last;
186 1.1 jtc
187 1.1 jtc /*
188 1.1 jtc * create a trailer request and call the proper format write function
189 1.1 jtc */
190 1.4 mycroft memset(&last, 0, sizeof(last));
191 1.1 jtc last.nlen = sizeof(TRAILER) - 1;
192 1.1 jtc last.type = PAX_REG;
193 1.1 jtc last.sb.st_nlink = 1;
194 1.1 jtc (void)strcpy(last.name, TRAILER);
195 1.1 jtc return((*frmt->wr)(&last));
196 1.1 jtc }
197 1.1 jtc
198 1.1 jtc /*
199 1.1 jtc * rd_nam()
200 1.1 jtc * read in the file name which follows the cpio header
201 1.1 jtc * Return:
202 1.1 jtc * 0 if ok, -1 otherwise
203 1.1 jtc */
204 1.1 jtc
205 1.1 jtc #if __STDC__
206 1.1 jtc static int
207 1.1 jtc rd_nm(register ARCHD *arcn, int nsz)
208 1.1 jtc #else
209 1.1 jtc static int
210 1.1 jtc rd_nm(arcn, nsz)
211 1.1 jtc register ARCHD *arcn;
212 1.1 jtc int nsz;
213 1.1 jtc #endif
214 1.1 jtc {
215 1.1 jtc /*
216 1.1 jtc * do not even try bogus values
217 1.1 jtc */
218 1.1 jtc if ((nsz == 0) || (nsz > sizeof(arcn->name))) {
219 1.1 jtc warn(1, "Cpio file name length %d is out of range", nsz);
220 1.1 jtc return(-1);
221 1.1 jtc }
222 1.1 jtc
223 1.1 jtc /*
224 1.1 jtc * read the name and make sure it is not empty and is \0 terminated
225 1.1 jtc */
226 1.1 jtc if ((rd_wrbuf(arcn->name,nsz) != nsz) || (arcn->name[nsz-1] != '\0') ||
227 1.1 jtc (arcn->name[0] == '\0')) {
228 1.1 jtc warn(1, "Cpio file name in header is corrupted");
229 1.1 jtc return(-1);
230 1.1 jtc }
231 1.1 jtc return(0);
232 1.1 jtc }
233 1.1 jtc
234 1.1 jtc /*
235 1.1 jtc * rd_ln_nm()
236 1.1 jtc * read in the link name for a file with links. The link name is stored
237 1.1 jtc * like file data (and is NOT \0 terminated!)
238 1.1 jtc * Return:
239 1.1 jtc * 0 if ok, -1 otherwise
240 1.1 jtc */
241 1.1 jtc
242 1.1 jtc #if __STDC__
243 1.1 jtc static int
244 1.1 jtc rd_ln_nm(register ARCHD *arcn)
245 1.1 jtc #else
246 1.1 jtc static int
247 1.1 jtc rd_ln_nm(arcn)
248 1.1 jtc register ARCHD *arcn;
249 1.1 jtc #endif
250 1.1 jtc {
251 1.1 jtc /*
252 1.1 jtc * check the length specified for bogus values
253 1.1 jtc */
254 1.1 jtc if ((arcn->sb.st_size == 0) ||
255 1.1 jtc (arcn->sb.st_size >= sizeof(arcn->ln_name))) {
256 1.1 jtc # ifdef NET2_STAT
257 1.1 jtc warn(1, "Cpio link name length is invalid: %lu",
258 1.1 jtc arcn->sb.st_size);
259 1.1 jtc # else
260 1.1 jtc warn(1, "Cpio link name length is invalid: %qu",
261 1.1 jtc arcn->sb.st_size);
262 1.1 jtc # endif
263 1.1 jtc return(-1);
264 1.1 jtc }
265 1.1 jtc
266 1.1 jtc /*
267 1.1 jtc * read in the link name and \0 terminate it
268 1.1 jtc */
269 1.1 jtc if (rd_wrbuf(arcn->ln_name, (int)arcn->sb.st_size) !=
270 1.1 jtc (int)arcn->sb.st_size) {
271 1.1 jtc warn(1, "Cpio link name read error");
272 1.1 jtc return(-1);
273 1.1 jtc }
274 1.1 jtc arcn->ln_nlen = arcn->sb.st_size;
275 1.1 jtc arcn->ln_name[arcn->ln_nlen] = '\0';
276 1.1 jtc
277 1.1 jtc /*
278 1.1 jtc * watch out for those empty link names
279 1.1 jtc */
280 1.1 jtc if (arcn->ln_name[0] == '\0') {
281 1.1 jtc warn(1, "Cpio link name is corrupt");
282 1.1 jtc return(-1);
283 1.1 jtc }
284 1.1 jtc return(0);
285 1.1 jtc }
286 1.1 jtc
287 1.1 jtc /*
288 1.1 jtc * Routines common to the extended byte oriented cpio format
289 1.1 jtc */
290 1.1 jtc
291 1.1 jtc /*
292 1.1 jtc * cpio_id()
293 1.1 jtc * determine if a block given to us is a valid extended byte oriented
294 1.1 jtc * cpio header
295 1.1 jtc * Return:
296 1.1 jtc * 0 if a valid header, -1 otherwise
297 1.1 jtc */
298 1.1 jtc
299 1.1 jtc #if __STDC__
300 1.1 jtc int
301 1.1 jtc cpio_id(char *blk, int size)
302 1.1 jtc #else
303 1.1 jtc int
304 1.1 jtc cpio_id(blk, size)
305 1.1 jtc char *blk;
306 1.1 jtc int size;
307 1.1 jtc #endif
308 1.1 jtc {
309 1.1 jtc if ((size < sizeof(HD_CPIO)) ||
310 1.1 jtc (strncmp(blk, AMAGIC, sizeof(AMAGIC) - 1) != 0))
311 1.1 jtc return(-1);
312 1.1 jtc return(0);
313 1.1 jtc }
314 1.1 jtc
315 1.1 jtc /*
316 1.1 jtc * cpio_rd()
317 1.1 jtc * determine if a buffer is a byte oriented extended cpio archive entry.
318 1.1 jtc * convert and store the values in the ARCHD parameter.
319 1.1 jtc * Return:
320 1.1 jtc * 0 if a valid header, -1 otherwise.
321 1.1 jtc */
322 1.1 jtc
323 1.1 jtc #if __STDC__
324 1.1 jtc int
325 1.1 jtc cpio_rd(register ARCHD *arcn, register char *buf)
326 1.1 jtc #else
327 1.1 jtc int
328 1.1 jtc cpio_rd(arcn, buf)
329 1.1 jtc register ARCHD *arcn;
330 1.1 jtc register char *buf;
331 1.1 jtc #endif
332 1.1 jtc {
333 1.1 jtc register int nsz;
334 1.1 jtc register HD_CPIO *hd;
335 1.1 jtc
336 1.1 jtc /*
337 1.1 jtc * check that this is a valid header, if not return -1
338 1.1 jtc */
339 1.1 jtc if (cpio_id(buf, sizeof(HD_CPIO)) < 0)
340 1.1 jtc return(-1);
341 1.1 jtc hd = (HD_CPIO *)buf;
342 1.1 jtc
343 1.1 jtc /*
344 1.1 jtc * byte oriented cpio (posix) does not have padding! extract the octal
345 1.1 jtc * ascii fields from the header
346 1.1 jtc */
347 1.1 jtc arcn->pad = 0L;
348 1.1 jtc arcn->sb.st_dev = (dev_t)asc_ul(hd->c_dev, sizeof(hd->c_dev), OCT);
349 1.1 jtc arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), OCT);
350 1.1 jtc arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), OCT);
351 1.1 jtc arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), OCT);
352 1.1 jtc arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), OCT);
353 1.1 jtc arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
354 1.1 jtc OCT);
355 1.1 jtc arcn->sb.st_rdev = (dev_t)asc_ul(hd->c_rdev, sizeof(hd->c_rdev), OCT);
356 1.1 jtc arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime, sizeof(hd->c_mtime),
357 1.1 jtc OCT);
358 1.1 jtc arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
359 1.1 jtc # ifdef NET2_STAT
360 1.1 jtc arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,sizeof(hd->c_filesize),
361 1.1 jtc OCT);
362 1.1 jtc # else
363 1.1 jtc arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,sizeof(hd->c_filesize),
364 1.1 jtc OCT);
365 1.1 jtc # endif
366 1.1 jtc
367 1.1 jtc /*
368 1.1 jtc * check name size and if valid, read in the name of this entry (name
369 1.1 jtc * follows header in the archive)
370 1.1 jtc */
371 1.1 jtc if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),OCT)) < 2)
372 1.1 jtc return(-1);
373 1.1 jtc arcn->nlen = nsz - 1;
374 1.1 jtc if (rd_nm(arcn, nsz) < 0)
375 1.1 jtc return(-1);
376 1.1 jtc
377 1.1 jtc if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
378 1.1 jtc /*
379 1.1 jtc * no link name to read for this file
380 1.1 jtc */
381 1.1 jtc arcn->ln_nlen = 0;
382 1.1 jtc arcn->ln_name[0] = '\0';
383 1.1 jtc return(com_rd(arcn));
384 1.1 jtc }
385 1.1 jtc
386 1.1 jtc /*
387 1.1 jtc * check link name size and read in the link name. Link names are
388 1.1 jtc * stored like file data.
389 1.1 jtc */
390 1.1 jtc if (rd_ln_nm(arcn) < 0)
391 1.1 jtc return(-1);
392 1.1 jtc
393 1.1 jtc /*
394 1.1 jtc * we have a valid header (with a link)
395 1.1 jtc */
396 1.1 jtc return(com_rd(arcn));
397 1.1 jtc }
398 1.1 jtc
399 1.1 jtc /*
400 1.1 jtc * cpio_endrd()
401 1.1 jtc * no cleanup needed here, just return size of the trailer (for append)
402 1.1 jtc * Return:
403 1.1 jtc * size of trailer header in this format
404 1.1 jtc */
405 1.1 jtc
406 1.1 jtc #if __STDC__
407 1.1 jtc off_t
408 1.1 jtc cpio_endrd(void)
409 1.1 jtc #else
410 1.1 jtc off_t
411 1.1 jtc cpio_endrd()
412 1.1 jtc #endif
413 1.1 jtc {
414 1.1 jtc return((off_t)(sizeof(HD_CPIO) + sizeof(TRAILER)));
415 1.1 jtc }
416 1.1 jtc
417 1.1 jtc /*
418 1.1 jtc * cpio_stwr()
419 1.1 jtc * start up the device mapping table
420 1.1 jtc * Return:
421 1.1 jtc * 0 if ok, -1 otherwise (what dev_start() returns)
422 1.1 jtc */
423 1.1 jtc
424 1.1 jtc #if __STDC__
425 1.1 jtc int
426 1.1 jtc cpio_stwr(void)
427 1.1 jtc #else
428 1.1 jtc int
429 1.1 jtc cpio_stwr()
430 1.1 jtc #endif
431 1.1 jtc {
432 1.1 jtc return(dev_start());
433 1.1 jtc }
434 1.1 jtc
435 1.1 jtc /*
436 1.1 jtc * cpio_wr()
437 1.1 jtc * copy the data in the ARCHD to buffer in extended byte oriented cpio
438 1.1 jtc * format.
439 1.1 jtc * Return
440 1.1 jtc * 0 if file has data to be written after the header, 1 if file has NO
441 1.1 jtc * data to write after the header, -1 if archive write failed
442 1.1 jtc */
443 1.1 jtc
444 1.1 jtc #if __STDC__
445 1.1 jtc int
446 1.1 jtc cpio_wr(register ARCHD *arcn)
447 1.1 jtc #else
448 1.1 jtc int
449 1.1 jtc cpio_wr(arcn)
450 1.1 jtc register ARCHD *arcn;
451 1.1 jtc #endif
452 1.1 jtc {
453 1.1 jtc register HD_CPIO *hd;
454 1.1 jtc register int nsz;
455 1.1 jtc char hdblk[sizeof(HD_CPIO)];
456 1.1 jtc
457 1.1 jtc /*
458 1.1 jtc * check and repair truncated device and inode fields in the header
459 1.1 jtc */
460 1.1 jtc if (map_dev(arcn, (u_long)CPIO_MASK, (u_long)CPIO_MASK) < 0)
461 1.1 jtc return(-1);
462 1.1 jtc
463 1.1 jtc arcn->pad = 0L;
464 1.1 jtc nsz = arcn->nlen + 1;
465 1.1 jtc hd = (HD_CPIO *)hdblk;
466 1.1 jtc if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
467 1.1 jtc arcn->sb.st_rdev = 0;
468 1.1 jtc
469 1.1 jtc switch(arcn->type) {
470 1.1 jtc case PAX_CTG:
471 1.1 jtc case PAX_REG:
472 1.1 jtc case PAX_HRG:
473 1.1 jtc /*
474 1.1 jtc * set data size for file data
475 1.1 jtc */
476 1.1 jtc # ifdef NET2_STAT
477 1.1 jtc if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize,
478 1.1 jtc sizeof(hd->c_filesize), OCT)) {
479 1.1 jtc # else
480 1.1 jtc if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
481 1.1 jtc sizeof(hd->c_filesize), OCT)) {
482 1.1 jtc # endif
483 1.1 jtc warn(1,"File is too large for cpio format %s",
484 1.1 jtc arcn->org_name);
485 1.1 jtc return(1);
486 1.1 jtc }
487 1.1 jtc break;
488 1.1 jtc case PAX_SLK:
489 1.1 jtc /*
490 1.1 jtc * set data size to hold link name
491 1.1 jtc */
492 1.1 jtc if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
493 1.1 jtc sizeof(hd->c_filesize), OCT))
494 1.1 jtc goto out;
495 1.1 jtc break;
496 1.1 jtc default:
497 1.1 jtc /*
498 1.1 jtc * all other file types have no file data
499 1.1 jtc */
500 1.1 jtc if (ul_asc((u_long)0, hd->c_filesize, sizeof(hd->c_filesize),
501 1.1 jtc OCT))
502 1.1 jtc goto out;
503 1.1 jtc break;
504 1.1 jtc }
505 1.1 jtc
506 1.1 jtc /*
507 1.1 jtc * copy the values to the header using octal ascii
508 1.1 jtc */
509 1.1 jtc if (ul_asc((u_long)MAGIC, hd->c_magic, sizeof(hd->c_magic), OCT) ||
510 1.1 jtc ul_asc((u_long)arcn->sb.st_dev, hd->c_dev, sizeof(hd->c_dev),
511 1.1 jtc OCT) ||
512 1.1 jtc ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
513 1.1 jtc OCT) ||
514 1.1 jtc ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
515 1.1 jtc OCT) ||
516 1.1 jtc ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
517 1.1 jtc OCT) ||
518 1.1 jtc ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
519 1.1 jtc OCT) ||
520 1.1 jtc ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
521 1.1 jtc OCT) ||
522 1.1 jtc ul_asc((u_long)arcn->sb.st_rdev, hd->c_rdev, sizeof(hd->c_rdev),
523 1.1 jtc OCT) ||
524 1.1 jtc ul_asc((u_long)arcn->sb.st_mtime,hd->c_mtime,sizeof(hd->c_mtime),
525 1.1 jtc OCT) ||
526 1.1 jtc ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), OCT))
527 1.1 jtc goto out;
528 1.1 jtc
529 1.1 jtc /*
530 1.1 jtc * write the file name to the archive
531 1.1 jtc */
532 1.1 jtc if ((wr_rdbuf(hdblk, (int)sizeof(HD_CPIO)) < 0) ||
533 1.1 jtc (wr_rdbuf(arcn->name, nsz) < 0)) {
534 1.1 jtc warn(1, "Unable to write cpio header for %s", arcn->org_name);
535 1.1 jtc return(-1);
536 1.1 jtc }
537 1.1 jtc
538 1.1 jtc /*
539 1.1 jtc * if this file has data, we are done. The caller will write the file
540 1.1 jtc * data, if we are link tell caller we are done, go to next file
541 1.1 jtc */
542 1.1 jtc if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
543 1.1 jtc (arcn->type == PAX_HRG))
544 1.1 jtc return(0);
545 1.1 jtc if (arcn->type != PAX_SLK)
546 1.1 jtc return(1);
547 1.1 jtc
548 1.1 jtc /*
549 1.1 jtc * write the link name to the archive, tell the caller to go to the
550 1.1 jtc * next file as we are done.
551 1.1 jtc */
552 1.1 jtc if (wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) {
553 1.1 jtc warn(1,"Unable to write cpio link name for %s",arcn->org_name);
554 1.1 jtc return(-1);
555 1.1 jtc }
556 1.1 jtc return(1);
557 1.1 jtc
558 1.1 jtc out:
559 1.1 jtc /*
560 1.1 jtc * header field is out of range
561 1.1 jtc */
562 1.1 jtc warn(1, "Cpio header field is too small to store file %s",
563 1.1 jtc arcn->org_name);
564 1.1 jtc return(1);
565 1.1 jtc }
566 1.1 jtc
567 1.1 jtc /*
568 1.1 jtc * Routines common to the system VR4 version of cpio (with/without file CRC)
569 1.1 jtc */
570 1.1 jtc
571 1.1 jtc /*
572 1.1 jtc * vcpio_id()
573 1.1 jtc * determine if a block given to us is a valid system VR4 cpio header
574 1.1 jtc * WITHOUT crc. WATCH it the magic cookies are in OCTAL, the header
575 1.1 jtc * uses HEX
576 1.1 jtc * Return:
577 1.1 jtc * 0 if a valid header, -1 otherwise
578 1.1 jtc */
579 1.1 jtc
580 1.1 jtc #if __STDC__
581 1.1 jtc int
582 1.1 jtc vcpio_id(char *blk, int size)
583 1.1 jtc #else
584 1.1 jtc int
585 1.1 jtc vcpio_id(blk, size)
586 1.1 jtc char *blk;
587 1.1 jtc int size;
588 1.1 jtc #endif
589 1.1 jtc {
590 1.1 jtc if ((size < sizeof(HD_VCPIO)) ||
591 1.1 jtc (strncmp(blk, AVMAGIC, sizeof(AVMAGIC) - 1) != 0))
592 1.1 jtc return(-1);
593 1.1 jtc return(0);
594 1.1 jtc }
595 1.1 jtc
596 1.1 jtc /*
597 1.1 jtc * crc_id()
598 1.1 jtc * determine if a block given to us is a valid system VR4 cpio header
599 1.1 jtc * WITH crc. WATCH it the magic cookies are in OCTAL the header uses HEX
600 1.1 jtc * Return:
601 1.1 jtc * 0 if a valid header, -1 otherwise
602 1.1 jtc */
603 1.1 jtc
604 1.1 jtc #if __STDC__
605 1.1 jtc int
606 1.1 jtc crc_id(char *blk, int size)
607 1.1 jtc #else
608 1.1 jtc int
609 1.1 jtc crc_id(blk, size)
610 1.1 jtc char *blk;
611 1.1 jtc int size;
612 1.1 jtc #endif
613 1.1 jtc {
614 1.1 jtc if ((size < sizeof(HD_VCPIO)) ||
615 1.1 jtc (strncmp(blk, AVCMAGIC, sizeof(AVCMAGIC) - 1) != 0))
616 1.1 jtc return(-1);
617 1.1 jtc return(0);
618 1.1 jtc }
619 1.1 jtc
620 1.1 jtc /*
621 1.1 jtc * crc_strd()
622 1.1 jtc w set file data CRC calculations. Fire up the hard link detection code
623 1.1 jtc * Return:
624 1.1 jtc * 0 if ok -1 otherwise (the return values of lnk_start())
625 1.1 jtc */
626 1.1 jtc
627 1.1 jtc #if __STDC__
628 1.1 jtc int
629 1.1 jtc crc_strd(void)
630 1.1 jtc #else
631 1.1 jtc int
632 1.1 jtc crc_strd()
633 1.1 jtc #endif
634 1.1 jtc {
635 1.1 jtc docrc = 1;
636 1.1 jtc return(lnk_start());
637 1.1 jtc }
638 1.1 jtc
639 1.1 jtc /*
640 1.1 jtc * vcpio_rd()
641 1.1 jtc * determine if a buffer is a system VR4 archive entry. (with/without CRC)
642 1.1 jtc * convert and store the values in the ARCHD parameter.
643 1.1 jtc * Return:
644 1.1 jtc * 0 if a valid header, -1 otherwise.
645 1.1 jtc */
646 1.1 jtc
647 1.1 jtc #if __STDC__
648 1.1 jtc int
649 1.1 jtc vcpio_rd(register ARCHD *arcn, register char *buf)
650 1.1 jtc #else
651 1.1 jtc int
652 1.1 jtc vcpio_rd(arcn, buf)
653 1.1 jtc register ARCHD *arcn;
654 1.1 jtc register char *buf;
655 1.1 jtc #endif
656 1.1 jtc {
657 1.1 jtc register HD_VCPIO *hd;
658 1.1 jtc dev_t devminor;
659 1.1 jtc dev_t devmajor;
660 1.1 jtc register int nsz;
661 1.1 jtc
662 1.1 jtc /*
663 1.1 jtc * during the id phase it was determined if we were using CRC, use the
664 1.1 jtc * proper id routine.
665 1.1 jtc */
666 1.1 jtc if (docrc) {
667 1.1 jtc if (crc_id(buf, sizeof(HD_VCPIO)) < 0)
668 1.1 jtc return(-1);
669 1.1 jtc } else {
670 1.1 jtc if (vcpio_id(buf, sizeof(HD_VCPIO)) < 0)
671 1.1 jtc return(-1);
672 1.1 jtc }
673 1.1 jtc
674 1.1 jtc hd = (HD_VCPIO *)buf;
675 1.1 jtc arcn->pad = 0L;
676 1.1 jtc
677 1.1 jtc /*
678 1.1 jtc * extract the hex ascii fields from the header
679 1.1 jtc */
680 1.1 jtc arcn->sb.st_ino = (ino_t)asc_ul(hd->c_ino, sizeof(hd->c_ino), HEX);
681 1.1 jtc arcn->sb.st_mode = (mode_t)asc_ul(hd->c_mode, sizeof(hd->c_mode), HEX);
682 1.1 jtc arcn->sb.st_uid = (uid_t)asc_ul(hd->c_uid, sizeof(hd->c_uid), HEX);
683 1.1 jtc arcn->sb.st_gid = (gid_t)asc_ul(hd->c_gid, sizeof(hd->c_gid), HEX);
684 1.1 jtc arcn->sb.st_mtime = (time_t)asc_ul(hd->c_mtime,sizeof(hd->c_mtime),HEX);
685 1.1 jtc arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
686 1.1 jtc # ifdef NET2_STAT
687 1.1 jtc arcn->sb.st_size = (off_t)asc_ul(hd->c_filesize,
688 1.1 jtc sizeof(hd->c_filesize), HEX);
689 1.1 jtc # else
690 1.1 jtc arcn->sb.st_size = (off_t)asc_uqd(hd->c_filesize,
691 1.1 jtc sizeof(hd->c_filesize), HEX);
692 1.1 jtc # endif
693 1.1 jtc arcn->sb.st_nlink = (nlink_t)asc_ul(hd->c_nlink, sizeof(hd->c_nlink),
694 1.1 jtc HEX);
695 1.1 jtc devmajor = (dev_t)asc_ul(hd->c_maj, sizeof(hd->c_maj), HEX);
696 1.1 jtc devminor = (dev_t)asc_ul(hd->c_min, sizeof(hd->c_min), HEX);
697 1.1 jtc arcn->sb.st_dev = TODEV(devmajor, devminor);
698 1.1 jtc devmajor = (dev_t)asc_ul(hd->c_rmaj, sizeof(hd->c_maj), HEX);
699 1.1 jtc devminor = (dev_t)asc_ul(hd->c_rmin, sizeof(hd->c_min), HEX);
700 1.1 jtc arcn->sb.st_rdev = TODEV(devmajor, devminor);
701 1.1 jtc arcn->crc = asc_ul(hd->c_chksum, sizeof(hd->c_chksum), HEX);
702 1.1 jtc
703 1.1 jtc /*
704 1.1 jtc * check the length of the file name, if ok read it in, return -1 if
705 1.1 jtc * bogus
706 1.1 jtc */
707 1.1 jtc if ((nsz = (int)asc_ul(hd->c_namesize,sizeof(hd->c_namesize),HEX)) < 2)
708 1.1 jtc return(-1);
709 1.1 jtc arcn->nlen = nsz - 1;
710 1.1 jtc if (rd_nm(arcn, nsz) < 0)
711 1.1 jtc return(-1);
712 1.1 jtc
713 1.1 jtc /*
714 1.1 jtc * skip padding. header + filename is aligned to 4 byte boundries
715 1.1 jtc */
716 1.1 jtc if (rd_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)
717 1.1 jtc return(-1);
718 1.1 jtc
719 1.1 jtc /*
720 1.1 jtc * if not a link (or a file with no data), calculate pad size (for
721 1.1 jtc * padding which follows the file data), clear the link name and return
722 1.1 jtc */
723 1.1 jtc if (((arcn->sb.st_mode&C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)) {
724 1.1 jtc /*
725 1.1 jtc * we have a valid header (not a link)
726 1.1 jtc */
727 1.1 jtc arcn->ln_nlen = 0;
728 1.1 jtc arcn->ln_name[0] = '\0';
729 1.1 jtc arcn->pad = VCPIO_PAD(arcn->sb.st_size);
730 1.1 jtc return(com_rd(arcn));
731 1.1 jtc }
732 1.1 jtc
733 1.1 jtc /*
734 1.1 jtc * read in the link name and skip over the padding
735 1.1 jtc */
736 1.1 jtc if ((rd_ln_nm(arcn) < 0) ||
737 1.1 jtc (rd_skip((off_t)(VCPIO_PAD(arcn->sb.st_size))) < 0))
738 1.1 jtc return(-1);
739 1.1 jtc
740 1.1 jtc /*
741 1.1 jtc * we have a valid header (with a link)
742 1.1 jtc */
743 1.1 jtc return(com_rd(arcn));
744 1.1 jtc }
745 1.1 jtc
746 1.1 jtc /*
747 1.1 jtc * vcpio_endrd()
748 1.1 jtc * no cleanup needed here, just return size of the trailer (for append)
749 1.1 jtc * Return:
750 1.1 jtc * size of trailer header in this format
751 1.1 jtc */
752 1.1 jtc
753 1.1 jtc #if __STDC__
754 1.1 jtc off_t
755 1.1 jtc vcpio_endrd(void)
756 1.1 jtc #else
757 1.1 jtc off_t
758 1.1 jtc vcpio_endrd()
759 1.1 jtc #endif
760 1.1 jtc {
761 1.1 jtc return((off_t)(sizeof(HD_VCPIO) + sizeof(TRAILER) +
762 1.1 jtc (VCPIO_PAD(sizeof(HD_VCPIO) + sizeof(TRAILER)))));
763 1.1 jtc }
764 1.1 jtc
765 1.1 jtc /*
766 1.1 jtc * crc_stwr()
767 1.1 jtc * start up the device mapping table, enable crc file calculation
768 1.1 jtc * Return:
769 1.1 jtc * 0 if ok, -1 otherwise (what dev_start() returns)
770 1.1 jtc */
771 1.1 jtc
772 1.1 jtc #if __STDC__
773 1.1 jtc int
774 1.1 jtc crc_stwr(void)
775 1.1 jtc #else
776 1.1 jtc int
777 1.1 jtc crc_stwr()
778 1.1 jtc #endif
779 1.1 jtc {
780 1.1 jtc docrc = 1;
781 1.1 jtc return(dev_start());
782 1.1 jtc }
783 1.1 jtc
784 1.1 jtc /*
785 1.1 jtc * vcpio_wr()
786 1.1 jtc * copy the data in the ARCHD to buffer in system VR4 cpio
787 1.1 jtc * (with/without crc) format.
788 1.1 jtc * Return
789 1.1 jtc * 0 if file has data to be written after the header, 1 if file has
790 1.1 jtc * NO data to write after the header, -1 if archive write failed
791 1.1 jtc */
792 1.1 jtc
793 1.1 jtc #if __STDC__
794 1.1 jtc int
795 1.1 jtc vcpio_wr(register ARCHD *arcn)
796 1.1 jtc #else
797 1.1 jtc int
798 1.1 jtc vcpio_wr(arcn)
799 1.1 jtc register ARCHD *arcn;
800 1.1 jtc #endif
801 1.1 jtc {
802 1.1 jtc register HD_VCPIO *hd;
803 1.1 jtc unsigned int nsz;
804 1.1 jtc char hdblk[sizeof(HD_VCPIO)];
805 1.1 jtc
806 1.1 jtc /*
807 1.1 jtc * check and repair truncated device and inode fields in the cpio
808 1.1 jtc * header
809 1.1 jtc */
810 1.1 jtc if (map_dev(arcn, (u_long)VCPIO_MASK, (u_long)VCPIO_MASK) < 0)
811 1.1 jtc return(-1);
812 1.1 jtc nsz = arcn->nlen + 1;
813 1.1 jtc hd = (HD_VCPIO *)hdblk;
814 1.1 jtc if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
815 1.1 jtc arcn->sb.st_rdev = 0;
816 1.1 jtc
817 1.1 jtc /*
818 1.1 jtc * add the proper magic value depending whether we were asked for
819 1.1 jtc * file data crc's, and the crc if needed.
820 1.1 jtc */
821 1.1 jtc if (docrc) {
822 1.1 jtc if (ul_asc((u_long)VCMAGIC, hd->c_magic, sizeof(hd->c_magic),
823 1.1 jtc OCT) ||
824 1.1 jtc ul_asc((u_long)arcn->crc,hd->c_chksum,sizeof(hd->c_chksum),
825 1.1 jtc HEX))
826 1.1 jtc goto out;
827 1.1 jtc } else {
828 1.1 jtc if (ul_asc((u_long)VMAGIC, hd->c_magic, sizeof(hd->c_magic),
829 1.1 jtc OCT) ||
830 1.1 jtc ul_asc((u_long)0L, hd->c_chksum, sizeof(hd->c_chksum),HEX))
831 1.1 jtc goto out;
832 1.1 jtc }
833 1.1 jtc
834 1.1 jtc switch(arcn->type) {
835 1.1 jtc case PAX_CTG:
836 1.1 jtc case PAX_REG:
837 1.1 jtc case PAX_HRG:
838 1.1 jtc /*
839 1.1 jtc * caller will copy file data to the archive. tell him how
840 1.1 jtc * much to pad.
841 1.1 jtc */
842 1.1 jtc arcn->pad = VCPIO_PAD(arcn->sb.st_size);
843 1.1 jtc # ifdef NET2_STAT
844 1.1 jtc if (ul_asc((u_long)arcn->sb.st_size, hd->c_filesize,
845 1.1 jtc sizeof(hd->c_filesize), HEX)) {
846 1.1 jtc # else
847 1.1 jtc if (uqd_asc((u_quad_t)arcn->sb.st_size, hd->c_filesize,
848 1.1 jtc sizeof(hd->c_filesize), HEX)) {
849 1.1 jtc # endif
850 1.1 jtc warn(1,"File is too large for sv4cpio format %s",
851 1.1 jtc arcn->org_name);
852 1.1 jtc return(1);
853 1.1 jtc }
854 1.1 jtc break;
855 1.1 jtc case PAX_SLK:
856 1.1 jtc /*
857 1.1 jtc * no file data for the caller to process, the file data has
858 1.1 jtc * the size of the link
859 1.1 jtc */
860 1.1 jtc arcn->pad = 0L;
861 1.1 jtc if (ul_asc((u_long)arcn->ln_nlen, hd->c_filesize,
862 1.1 jtc sizeof(hd->c_filesize), HEX))
863 1.1 jtc goto out;
864 1.1 jtc break;
865 1.1 jtc default:
866 1.1 jtc /*
867 1.1 jtc * no file data for the caller to process
868 1.1 jtc */
869 1.1 jtc arcn->pad = 0L;
870 1.1 jtc if (ul_asc((u_long)0L, hd->c_filesize, sizeof(hd->c_filesize),
871 1.1 jtc HEX))
872 1.1 jtc goto out;
873 1.1 jtc break;
874 1.1 jtc }
875 1.1 jtc
876 1.1 jtc /*
877 1.1 jtc * set the other fields in the header
878 1.1 jtc */
879 1.1 jtc if (ul_asc((u_long)arcn->sb.st_ino, hd->c_ino, sizeof(hd->c_ino),
880 1.1 jtc HEX) ||
881 1.1 jtc ul_asc((u_long)arcn->sb.st_mode, hd->c_mode, sizeof(hd->c_mode),
882 1.1 jtc HEX) ||
883 1.1 jtc ul_asc((u_long)arcn->sb.st_uid, hd->c_uid, sizeof(hd->c_uid),
884 1.1 jtc HEX) ||
885 1.1 jtc ul_asc((u_long)arcn->sb.st_gid, hd->c_gid, sizeof(hd->c_gid),
886 1.1 jtc HEX) ||
887 1.1 jtc ul_asc((u_long)arcn->sb.st_mtime, hd->c_mtime, sizeof(hd->c_mtime),
888 1.1 jtc HEX) ||
889 1.1 jtc ul_asc((u_long)arcn->sb.st_nlink, hd->c_nlink, sizeof(hd->c_nlink),
890 1.1 jtc HEX) ||
891 1.1 jtc ul_asc((u_long)MAJOR(arcn->sb.st_dev),hd->c_maj, sizeof(hd->c_maj),
892 1.1 jtc HEX) ||
893 1.1 jtc ul_asc((u_long)MINOR(arcn->sb.st_dev),hd->c_min, sizeof(hd->c_min),
894 1.1 jtc HEX) ||
895 1.1 jtc ul_asc((u_long)MAJOR(arcn->sb.st_rdev),hd->c_rmaj,sizeof(hd->c_maj),
896 1.1 jtc HEX) ||
897 1.1 jtc ul_asc((u_long)MINOR(arcn->sb.st_rdev),hd->c_rmin,sizeof(hd->c_min),
898 1.1 jtc HEX) ||
899 1.1 jtc ul_asc((u_long)nsz, hd->c_namesize, sizeof(hd->c_namesize), HEX))
900 1.1 jtc goto out;
901 1.1 jtc
902 1.1 jtc /*
903 1.1 jtc * write the header, the file name and padding as required.
904 1.1 jtc */
905 1.1 jtc if ((wr_rdbuf(hdblk, (int)sizeof(HD_VCPIO)) < 0) ||
906 1.1 jtc (wr_rdbuf(arcn->name, (int)nsz) < 0) ||
907 1.1 jtc (wr_skip((off_t)(VCPIO_PAD(sizeof(HD_VCPIO) + nsz))) < 0)) {
908 1.1 jtc warn(1,"Could not write sv4cpio header for %s",arcn->org_name);
909 1.1 jtc return(-1);
910 1.1 jtc }
911 1.1 jtc
912 1.1 jtc /*
913 1.1 jtc * if we have file data, tell the caller we are done, copy the file
914 1.1 jtc */
915 1.1 jtc if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
916 1.1 jtc (arcn->type == PAX_HRG))
917 1.1 jtc return(0);
918 1.1 jtc
919 1.1 jtc /*
920 1.1 jtc * if we are not a link, tell the caller we are done, go to next file
921 1.1 jtc */
922 1.1 jtc if (arcn->type != PAX_SLK)
923 1.1 jtc return(1);
924 1.1 jtc
925 1.1 jtc /*
926 1.1 jtc * write the link name, tell the caller we are done.
927 1.1 jtc */
928 1.1 jtc if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
929 1.1 jtc (wr_skip((off_t)(VCPIO_PAD(arcn->ln_nlen))) < 0)) {
930 1.1 jtc warn(1,"Could not write sv4cpio link name for %s",
931 1.1 jtc arcn->org_name);
932 1.1 jtc return(-1);
933 1.1 jtc }
934 1.1 jtc return(1);
935 1.1 jtc
936 1.1 jtc out:
937 1.1 jtc /*
938 1.1 jtc * header field is out of range
939 1.1 jtc */
940 1.1 jtc warn(1,"Sv4cpio header field is too small for file %s",arcn->org_name);
941 1.1 jtc return(1);
942 1.1 jtc }
943 1.1 jtc
944 1.1 jtc /*
945 1.1 jtc * Routines common to the old binary header cpio
946 1.1 jtc */
947 1.1 jtc
948 1.1 jtc /*
949 1.1 jtc * bcpio_id()
950 1.1 jtc * determine if a block given to us is a old binary cpio header
951 1.1 jtc * (with/without header byte swapping)
952 1.1 jtc * Return:
953 1.1 jtc * 0 if a valid header, -1 otherwise
954 1.1 jtc */
955 1.1 jtc
956 1.1 jtc #if __STDC__
957 1.1 jtc int
958 1.1 jtc bcpio_id(char *blk, int size)
959 1.1 jtc #else
960 1.1 jtc int
961 1.1 jtc bcpio_id(blk, size)
962 1.1 jtc char *blk;
963 1.1 jtc int size;
964 1.1 jtc #endif
965 1.1 jtc {
966 1.1 jtc if (size < sizeof(HD_BCPIO))
967 1.1 jtc return(-1);
968 1.1 jtc
969 1.1 jtc /*
970 1.1 jtc * check both normal and byte swapped magic cookies
971 1.1 jtc */
972 1.1 jtc if (((u_short)SHRT_EXT(blk)) == MAGIC)
973 1.1 jtc return(0);
974 1.1 jtc if (((u_short)RSHRT_EXT(blk)) == MAGIC) {
975 1.1 jtc if (!swp_head)
976 1.1 jtc ++swp_head;
977 1.1 jtc return(0);
978 1.1 jtc }
979 1.1 jtc return(-1);
980 1.1 jtc }
981 1.1 jtc
982 1.1 jtc /*
983 1.1 jtc * bcpio_rd()
984 1.1 jtc * determine if a buffer is a old binary archive entry. (it may have byte
985 1.1 jtc * swapped header) convert and store the values in the ARCHD parameter.
986 1.1 jtc * This is a very old header format and should not really be used.
987 1.1 jtc * Return:
988 1.1 jtc * 0 if a valid header, -1 otherwise.
989 1.1 jtc */
990 1.1 jtc
991 1.1 jtc #if __STDC__
992 1.1 jtc int
993 1.1 jtc bcpio_rd(register ARCHD *arcn, register char *buf)
994 1.1 jtc #else
995 1.1 jtc int
996 1.1 jtc bcpio_rd(arcn, buf)
997 1.1 jtc register ARCHD *arcn;
998 1.1 jtc register char *buf;
999 1.1 jtc #endif
1000 1.1 jtc {
1001 1.1 jtc register HD_BCPIO *hd;
1002 1.1 jtc register int nsz;
1003 1.1 jtc
1004 1.1 jtc /*
1005 1.1 jtc * check the header
1006 1.1 jtc */
1007 1.1 jtc if (bcpio_id(buf, sizeof(HD_BCPIO)) < 0)
1008 1.1 jtc return(-1);
1009 1.1 jtc
1010 1.1 jtc arcn->pad = 0L;
1011 1.1 jtc hd = (HD_BCPIO *)buf;
1012 1.1 jtc if (swp_head) {
1013 1.1 jtc /*
1014 1.1 jtc * header has swapped bytes on 16 bit boundries
1015 1.1 jtc */
1016 1.1 jtc arcn->sb.st_dev = (dev_t)(RSHRT_EXT(hd->h_dev));
1017 1.1 jtc arcn->sb.st_ino = (ino_t)(RSHRT_EXT(hd->h_ino));
1018 1.1 jtc arcn->sb.st_mode = (mode_t)(RSHRT_EXT(hd->h_mode));
1019 1.1 jtc arcn->sb.st_uid = (uid_t)(RSHRT_EXT(hd->h_uid));
1020 1.1 jtc arcn->sb.st_gid = (gid_t)(RSHRT_EXT(hd->h_gid));
1021 1.1 jtc arcn->sb.st_nlink = (nlink_t)(RSHRT_EXT(hd->h_nlink));
1022 1.1 jtc arcn->sb.st_rdev = (dev_t)(RSHRT_EXT(hd->h_rdev));
1023 1.1 jtc arcn->sb.st_mtime = (time_t)(RSHRT_EXT(hd->h_mtime_1));
1024 1.1 jtc arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
1025 1.1 jtc ((time_t)(RSHRT_EXT(hd->h_mtime_2)));
1026 1.1 jtc arcn->sb.st_size = (off_t)(RSHRT_EXT(hd->h_filesize_1));
1027 1.1 jtc arcn->sb.st_size = (arcn->sb.st_size << 16) |
1028 1.1 jtc ((off_t)(RSHRT_EXT(hd->h_filesize_2)));
1029 1.1 jtc nsz = (int)(RSHRT_EXT(hd->h_namesize));
1030 1.1 jtc } else {
1031 1.1 jtc arcn->sb.st_dev = (dev_t)(SHRT_EXT(hd->h_dev));
1032 1.1 jtc arcn->sb.st_ino = (ino_t)(SHRT_EXT(hd->h_ino));
1033 1.1 jtc arcn->sb.st_mode = (mode_t)(SHRT_EXT(hd->h_mode));
1034 1.1 jtc arcn->sb.st_uid = (uid_t)(SHRT_EXT(hd->h_uid));
1035 1.1 jtc arcn->sb.st_gid = (gid_t)(SHRT_EXT(hd->h_gid));
1036 1.1 jtc arcn->sb.st_nlink = (nlink_t)(SHRT_EXT(hd->h_nlink));
1037 1.1 jtc arcn->sb.st_rdev = (dev_t)(SHRT_EXT(hd->h_rdev));
1038 1.1 jtc arcn->sb.st_mtime = (time_t)(SHRT_EXT(hd->h_mtime_1));
1039 1.1 jtc arcn->sb.st_mtime = (arcn->sb.st_mtime << 16) |
1040 1.1 jtc ((time_t)(SHRT_EXT(hd->h_mtime_2)));
1041 1.1 jtc arcn->sb.st_size = (off_t)(SHRT_EXT(hd->h_filesize_1));
1042 1.1 jtc arcn->sb.st_size = (arcn->sb.st_size << 16) |
1043 1.1 jtc ((off_t)(SHRT_EXT(hd->h_filesize_2)));
1044 1.1 jtc nsz = (int)(SHRT_EXT(hd->h_namesize));
1045 1.1 jtc }
1046 1.1 jtc arcn->sb.st_ctime = arcn->sb.st_atime = arcn->sb.st_mtime;
1047 1.1 jtc
1048 1.1 jtc /*
1049 1.1 jtc * check the file name size, if bogus give up. otherwise read the file
1050 1.1 jtc * name
1051 1.1 jtc */
1052 1.1 jtc if (nsz < 2)
1053 1.1 jtc return(-1);
1054 1.1 jtc arcn->nlen = nsz - 1;
1055 1.1 jtc if (rd_nm(arcn, nsz) < 0)
1056 1.1 jtc return(-1);
1057 1.1 jtc
1058 1.1 jtc /*
1059 1.1 jtc * header + file name are aligned to 2 byte boundries, skip if needed
1060 1.1 jtc */
1061 1.1 jtc if (rd_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)
1062 1.1 jtc return(-1);
1063 1.1 jtc
1064 1.1 jtc /*
1065 1.1 jtc * if not a link (or a file with no data), calculate pad size (for
1066 1.1 jtc * padding which follows the file data), clear the link name and return
1067 1.1 jtc */
1068 1.1 jtc if (((arcn->sb.st_mode & C_IFMT) != C_ISLNK)||(arcn->sb.st_size == 0)){
1069 1.1 jtc /*
1070 1.1 jtc * we have a valid header (not a link)
1071 1.1 jtc */
1072 1.1 jtc arcn->ln_nlen = 0;
1073 1.1 jtc arcn->ln_name[0] = '\0';
1074 1.1 jtc arcn->pad = BCPIO_PAD(arcn->sb.st_size);
1075 1.1 jtc return(com_rd(arcn));
1076 1.1 jtc }
1077 1.1 jtc
1078 1.1 jtc if ((rd_ln_nm(arcn) < 0) ||
1079 1.1 jtc (rd_skip((off_t)(BCPIO_PAD(arcn->sb.st_size))) < 0))
1080 1.1 jtc return(-1);
1081 1.1 jtc
1082 1.1 jtc /*
1083 1.1 jtc * we have a valid header (with a link)
1084 1.1 jtc */
1085 1.1 jtc return(com_rd(arcn));
1086 1.1 jtc }
1087 1.1 jtc
1088 1.1 jtc /*
1089 1.1 jtc * bcpio_endrd()
1090 1.1 jtc * no cleanup needed here, just return size of the trailer (for append)
1091 1.1 jtc * Return:
1092 1.1 jtc * size of trailer header in this format
1093 1.1 jtc */
1094 1.1 jtc
1095 1.1 jtc #if __STDC__
1096 1.1 jtc off_t
1097 1.1 jtc bcpio_endrd(void)
1098 1.1 jtc #else
1099 1.1 jtc off_t
1100 1.1 jtc bcpio_endrd()
1101 1.1 jtc #endif
1102 1.1 jtc {
1103 1.1 jtc return((off_t)(sizeof(HD_BCPIO) + sizeof(TRAILER) +
1104 1.1 jtc (BCPIO_PAD(sizeof(HD_BCPIO) + sizeof(TRAILER)))));
1105 1.1 jtc }
1106 1.1 jtc
1107 1.1 jtc /*
1108 1.1 jtc * bcpio_wr()
1109 1.1 jtc * copy the data in the ARCHD to buffer in old binary cpio format
1110 1.1 jtc * There is a real chance of field overflow with this critter. So we
1111 1.1 jtc * always check the conversion is ok. nobody in his their right mind
1112 1.1 jtc * should write an achive in this format...
1113 1.1 jtc * Return
1114 1.1 jtc * 0 if file has data to be written after the header, 1 if file has NO
1115 1.1 jtc * data to write after the header, -1 if archive write failed
1116 1.1 jtc */
1117 1.1 jtc
1118 1.1 jtc #if __STDC__
1119 1.1 jtc int
1120 1.1 jtc bcpio_wr(register ARCHD *arcn)
1121 1.1 jtc #else
1122 1.1 jtc int
1123 1.1 jtc bcpio_wr(arcn)
1124 1.1 jtc register ARCHD *arcn;
1125 1.1 jtc #endif
1126 1.1 jtc {
1127 1.1 jtc register HD_BCPIO *hd;
1128 1.1 jtc register int nsz;
1129 1.1 jtc char hdblk[sizeof(HD_BCPIO)];
1130 1.1 jtc off_t t_offt;
1131 1.1 jtc int t_int;
1132 1.1 jtc time_t t_timet;
1133 1.1 jtc
1134 1.1 jtc /*
1135 1.1 jtc * check and repair truncated device and inode fields in the cpio
1136 1.1 jtc * header
1137 1.1 jtc */
1138 1.1 jtc if (map_dev(arcn, (u_long)BCPIO_MASK, (u_long)BCPIO_MASK) < 0)
1139 1.1 jtc return(-1);
1140 1.1 jtc
1141 1.1 jtc if ((arcn->type != PAX_BLK) && (arcn->type != PAX_CHR))
1142 1.1 jtc arcn->sb.st_rdev = 0;
1143 1.1 jtc hd = (HD_BCPIO *)hdblk;
1144 1.1 jtc
1145 1.1 jtc switch(arcn->type) {
1146 1.1 jtc case PAX_CTG:
1147 1.1 jtc case PAX_REG:
1148 1.1 jtc case PAX_HRG:
1149 1.1 jtc /*
1150 1.1 jtc * caller will copy file data to the archive. tell him how
1151 1.1 jtc * much to pad.
1152 1.1 jtc */
1153 1.1 jtc arcn->pad = BCPIO_PAD(arcn->sb.st_size);
1154 1.1 jtc hd->h_filesize_1[0] = CHR_WR_0(arcn->sb.st_size);
1155 1.1 jtc hd->h_filesize_1[1] = CHR_WR_1(arcn->sb.st_size);
1156 1.1 jtc hd->h_filesize_2[0] = CHR_WR_2(arcn->sb.st_size);
1157 1.1 jtc hd->h_filesize_2[1] = CHR_WR_3(arcn->sb.st_size);
1158 1.1 jtc t_offt = (off_t)(SHRT_EXT(hd->h_filesize_1));
1159 1.1 jtc t_offt = (t_offt<<16) | ((off_t)(SHRT_EXT(hd->h_filesize_2)));
1160 1.1 jtc if (arcn->sb.st_size != t_offt) {
1161 1.1 jtc warn(1,"File is too large for bcpio format %s",
1162 1.1 jtc arcn->org_name);
1163 1.1 jtc return(1);
1164 1.1 jtc }
1165 1.1 jtc break;
1166 1.1 jtc case PAX_SLK:
1167 1.1 jtc /*
1168 1.1 jtc * no file data for the caller to process, the file data has
1169 1.1 jtc * the size of the link
1170 1.1 jtc */
1171 1.1 jtc arcn->pad = 0L;
1172 1.1 jtc hd->h_filesize_1[0] = CHR_WR_0(arcn->ln_nlen);
1173 1.1 jtc hd->h_filesize_1[1] = CHR_WR_1(arcn->ln_nlen);
1174 1.1 jtc hd->h_filesize_2[0] = CHR_WR_2(arcn->ln_nlen);
1175 1.1 jtc hd->h_filesize_2[1] = CHR_WR_3(arcn->ln_nlen);
1176 1.1 jtc t_int = (int)(SHRT_EXT(hd->h_filesize_1));
1177 1.1 jtc t_int = (t_int << 16) | ((int)(SHRT_EXT(hd->h_filesize_2)));
1178 1.1 jtc if (arcn->ln_nlen != t_int)
1179 1.1 jtc goto out;
1180 1.1 jtc break;
1181 1.1 jtc default:
1182 1.1 jtc /*
1183 1.1 jtc * no file data for the caller to process
1184 1.1 jtc */
1185 1.1 jtc arcn->pad = 0L;
1186 1.1 jtc hd->h_filesize_1[0] = (char)0;
1187 1.1 jtc hd->h_filesize_1[1] = (char)0;
1188 1.1 jtc hd->h_filesize_2[0] = (char)0;
1189 1.1 jtc hd->h_filesize_2[1] = (char)0;
1190 1.1 jtc break;
1191 1.1 jtc }
1192 1.1 jtc
1193 1.1 jtc /*
1194 1.1 jtc * build up the rest of the fields
1195 1.1 jtc */
1196 1.1 jtc hd->h_magic[0] = CHR_WR_2(MAGIC);
1197 1.1 jtc hd->h_magic[1] = CHR_WR_3(MAGIC);
1198 1.1 jtc hd->h_dev[0] = CHR_WR_2(arcn->sb.st_dev);
1199 1.1 jtc hd->h_dev[1] = CHR_WR_3(arcn->sb.st_dev);
1200 1.1 jtc if (arcn->sb.st_dev != (dev_t)(SHRT_EXT(hd->h_dev)))
1201 1.1 jtc goto out;
1202 1.1 jtc hd->h_ino[0] = CHR_WR_2(arcn->sb.st_ino);
1203 1.1 jtc hd->h_ino[1] = CHR_WR_3(arcn->sb.st_ino);
1204 1.1 jtc if (arcn->sb.st_ino != (ino_t)(SHRT_EXT(hd->h_ino)))
1205 1.1 jtc goto out;
1206 1.1 jtc hd->h_mode[0] = CHR_WR_2(arcn->sb.st_mode);
1207 1.1 jtc hd->h_mode[1] = CHR_WR_3(arcn->sb.st_mode);
1208 1.1 jtc if (arcn->sb.st_mode != (mode_t)(SHRT_EXT(hd->h_mode)))
1209 1.1 jtc goto out;
1210 1.1 jtc hd->h_uid[0] = CHR_WR_2(arcn->sb.st_uid);
1211 1.1 jtc hd->h_uid[1] = CHR_WR_3(arcn->sb.st_uid);
1212 1.1 jtc if (arcn->sb.st_uid != (uid_t)(SHRT_EXT(hd->h_uid)))
1213 1.1 jtc goto out;
1214 1.1 jtc hd->h_gid[0] = CHR_WR_2(arcn->sb.st_gid);
1215 1.1 jtc hd->h_gid[1] = CHR_WR_3(arcn->sb.st_gid);
1216 1.1 jtc if (arcn->sb.st_gid != (gid_t)(SHRT_EXT(hd->h_gid)))
1217 1.1 jtc goto out;
1218 1.1 jtc hd->h_nlink[0] = CHR_WR_2(arcn->sb.st_nlink);
1219 1.1 jtc hd->h_nlink[1] = CHR_WR_3(arcn->sb.st_nlink);
1220 1.1 jtc if (arcn->sb.st_nlink != (nlink_t)(SHRT_EXT(hd->h_nlink)))
1221 1.1 jtc goto out;
1222 1.1 jtc hd->h_rdev[0] = CHR_WR_2(arcn->sb.st_rdev);
1223 1.1 jtc hd->h_rdev[1] = CHR_WR_3(arcn->sb.st_rdev);
1224 1.1 jtc if (arcn->sb.st_rdev != (dev_t)(SHRT_EXT(hd->h_rdev)))
1225 1.1 jtc goto out;
1226 1.1 jtc hd->h_mtime_1[0] = CHR_WR_0(arcn->sb.st_mtime);
1227 1.1 jtc hd->h_mtime_1[1] = CHR_WR_1(arcn->sb.st_mtime);
1228 1.1 jtc hd->h_mtime_2[0] = CHR_WR_2(arcn->sb.st_mtime);
1229 1.1 jtc hd->h_mtime_2[1] = CHR_WR_3(arcn->sb.st_mtime);
1230 1.1 jtc t_timet = (time_t)(SHRT_EXT(hd->h_mtime_1));
1231 1.1 jtc t_timet = (t_timet << 16) | ((time_t)(SHRT_EXT(hd->h_mtime_2)));
1232 1.1 jtc if (arcn->sb.st_mtime != t_timet)
1233 1.1 jtc goto out;
1234 1.1 jtc nsz = arcn->nlen + 1;
1235 1.1 jtc hd->h_namesize[0] = CHR_WR_2(nsz);
1236 1.1 jtc hd->h_namesize[1] = CHR_WR_3(nsz);
1237 1.1 jtc if (nsz != (int)(SHRT_EXT(hd->h_namesize)))
1238 1.1 jtc goto out;
1239 1.1 jtc
1240 1.1 jtc /*
1241 1.1 jtc * write the header, the file name and padding as required.
1242 1.1 jtc */
1243 1.1 jtc if ((wr_rdbuf(hdblk, (int)sizeof(HD_BCPIO)) < 0) ||
1244 1.1 jtc (wr_rdbuf(arcn->name, nsz) < 0) ||
1245 1.1 jtc (wr_skip((off_t)(BCPIO_PAD(sizeof(HD_BCPIO) + nsz))) < 0)) {
1246 1.1 jtc warn(1, "Could not write bcpio header for %s", arcn->org_name);
1247 1.1 jtc return(-1);
1248 1.1 jtc }
1249 1.1 jtc
1250 1.1 jtc /*
1251 1.1 jtc * if we have file data, tell the caller we are done
1252 1.1 jtc */
1253 1.1 jtc if ((arcn->type == PAX_CTG) || (arcn->type == PAX_REG) ||
1254 1.1 jtc (arcn->type == PAX_HRG))
1255 1.1 jtc return(0);
1256 1.1 jtc
1257 1.1 jtc /*
1258 1.1 jtc * if we are not a link, tell the caller we are done, go to next file
1259 1.1 jtc */
1260 1.1 jtc if (arcn->type != PAX_SLK)
1261 1.1 jtc return(1);
1262 1.1 jtc
1263 1.1 jtc /*
1264 1.1 jtc * write the link name, tell the caller we are done.
1265 1.1 jtc */
1266 1.1 jtc if ((wr_rdbuf(arcn->ln_name, arcn->ln_nlen) < 0) ||
1267 1.1 jtc (wr_skip((off_t)(BCPIO_PAD(arcn->ln_nlen))) < 0)) {
1268 1.1 jtc warn(1,"Could not write bcpio link name for %s",arcn->org_name);
1269 1.1 jtc return(-1);
1270 1.1 jtc }
1271 1.1 jtc return(1);
1272 1.1 jtc
1273 1.1 jtc out:
1274 1.1 jtc /*
1275 1.1 jtc * header field is out of range
1276 1.1 jtc */
1277 1.1 jtc warn(1,"Bcpio header field is too small for file %s", arcn->org_name);
1278 1.1 jtc return(1);
1279 1.1 jtc }
1280