1 $NetBSD: patch-ap,v 1.1 2006/02/12 01:44:28 seb Exp $
3 --- src/copyout.c.orig 2004-10-14 09:14:03.000000000 +0000
5 @@ -159,7 +159,7 @@ add_link_defer (struct new_cpio_header *
8 /* We are about to put a file into a newc or crc archive that is
9 - multiply linked. We have already seen and defered all of the
10 + multiply linked. We have already seen and deferred all of the
11 other links to the file but haven't written them into the archive.
12 Write the other links into the archive, and remove them from the
14 @@ -231,8 +231,10 @@ writeout_defered_file (struct new_cpio_h
18 - write_out_header (&file_hdr, out_file_des);
19 - copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize, header->c_name);
20 + if (write_out_header (&file_hdr, out_file_des))
22 + copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize,
24 warn_if_file_changed(header->c_name, file_hdr.c_filesize, file_hdr.c_mtime);
26 if (archive_format == arf_tar || archive_format == arf_ustar)
27 @@ -288,153 +290,313 @@ writeout_final_defers (int out_des)
32 -/* Write out header FILE_HDR, including the file name, to file
33 - descriptor OUT_DES. */
34 +/* FIXME: These two defines should be defined in paxutils */
38 +/* FIXME: to_ascii could be used instead of to_oct() and to_octal() from tar,
39 + so it should be moved to paxutils too.
40 + Allowed values for logbase are: 1 (binary), 2, 3 (octal), 4 (hex) */
42 +to_ascii (char *where, uintmax_t v, size_t digits, unsigned logbase)
44 + static char codetab[] = "0123456789ABCDEF";
49 + where[--i] = codetab[(v & ((1 << logbase) - 1))];
58 +field_width_error (const char *filename, const char *fieldname)
60 + error (0, 0, _("%s: field width not sufficient for storing %s"),
61 + filename, fieldname);
65 +field_width_warning (const char *filename, const char *fieldname)
67 + if (warn_option & CPIO_WARN_TRUNCATE)
68 + error (0, 0, _("%s: truncating %s"), filename, fieldname);
72 -write_out_header (struct new_cpio_header *file_hdr, int out_des)
73 +to_ascii_or_warn (char *where, uintmax_t n, size_t digits,
75 + const char *filename, const char *fieldname)
77 - if (archive_format == arf_newascii || archive_format == arf_crcascii)
78 + if (to_ascii (where, n, digits, logbase))
79 + field_width_warning (filename, fieldname);
83 +to_ascii_or_error (char *where, uintmax_t n, size_t digits,
85 + const char *filename, const char *fieldname)
87 + if (to_ascii (where, n, digits, logbase))
89 - char ascii_header[112];
91 + field_width_error (filename, fieldname);
97 - if (archive_format == arf_crcascii)
98 - magic_string = "070702";
100 - magic_string = "070701";
101 - sprintf (ascii_header,
102 - "%6s%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx%08lx",
104 - file_hdr->c_ino, file_hdr->c_mode, file_hdr->c_uid,
105 - file_hdr->c_gid, file_hdr->c_nlink, file_hdr->c_mtime,
106 - file_hdr->c_filesize, file_hdr->c_dev_maj, file_hdr->c_dev_min,
107 - file_hdr->c_rdev_maj, file_hdr->c_rdev_min, file_hdr->c_namesize,
108 - file_hdr->c_chksum);
109 - tape_buffered_write (ascii_header, out_des, 110L);
111 - /* Write file name to output. */
112 - tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
113 - tape_pad_output (out_des, file_hdr->c_namesize + 110);
115 - else if (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
117 - char ascii_header[78];
121 +write_out_new_ascii_header (const char *magic_string,
122 + struct new_cpio_header *file_hdr, int out_des)
124 + char ascii_header[110];
127 - if (archive_format == arf_oldascii)
129 - dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
130 - rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
134 - /* HP/UX cpio creates archives that look just like ordinary archives,
135 - but for devices it sets major = 0, minor = 1, and puts the
136 - actual major/minor number in the filesize field. */
137 - switch (file_hdr->c_mode & CP_IFMT)
141 + (void)strncpy(ascii_header, magic_string, sizeof(ascii_header) - 1);
142 + ascii_header[sizeof(ascii_header) -1] = '\0';
143 + p = ascii_header + strlen(ascii_header);
144 + to_ascii_or_warn (p, file_hdr->c_ino, 8, LG_16,
145 + file_hdr->c_name, _("inode number"));
147 + to_ascii_or_warn (p, file_hdr->c_mode, 8, LG_16, file_hdr->c_name,
150 + to_ascii_or_warn (p, file_hdr->c_uid, 8, LG_16, file_hdr->c_name,
153 + to_ascii_or_warn (p, file_hdr->c_gid, 8, LG_16, file_hdr->c_name,
156 + to_ascii_or_warn (p, file_hdr->c_nlink, 8, LG_16, file_hdr->c_name,
157 + _("number of links"));
159 + to_ascii_or_warn (p, file_hdr->c_mtime, 8, LG_16, file_hdr->c_name,
160 + _("modification time"));
162 + if (to_ascii_or_error (p, file_hdr->c_filesize, 8, LG_16, file_hdr->c_name,
166 + if (to_ascii_or_error (p, file_hdr->c_dev_maj, 8, LG_16, file_hdr->c_name,
167 + _("device major number")))
170 + if (to_ascii_or_error (p, file_hdr->c_dev_min, 8, LG_16, file_hdr->c_name,
171 + _("device minor number")))
174 + if (to_ascii_or_error (p, file_hdr->c_rdev_maj, 8, LG_16, file_hdr->c_name,
178 + if (to_ascii_or_error (p, file_hdr->c_rdev_min, 8, LG_16, file_hdr->c_name,
182 + if (to_ascii_or_error (p, file_hdr->c_namesize, 8, LG_16, file_hdr->c_name,
186 + to_ascii (p, file_hdr->c_chksum & 0xffffffff, 8, LG_16);
188 + tape_buffered_write (ascii_header, out_des, sizeof ascii_header);
190 + /* Write file name to output. */
191 + tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
192 + tape_pad_output (out_des, file_hdr->c_namesize + sizeof ascii_header);
197 +write_out_old_ascii_header (dev_t dev, dev_t rdev,
198 + struct new_cpio_header *file_hdr, int out_des)
200 + char ascii_header[76];
201 + char *p = ascii_header;
203 + to_ascii (p, file_hdr->c_magic, 6, LG_8);
205 + to_ascii_or_warn (p, dev, 6, LG_8, file_hdr->c_name, _("device number"));
207 + to_ascii_or_warn (p, file_hdr->c_ino, 6, LG_8, file_hdr->c_name,
208 + _("inode number"));
210 + to_ascii_or_warn (p, file_hdr->c_mode, 6, LG_8, file_hdr->c_name,
213 + to_ascii_or_warn (p, file_hdr->c_uid, 6, LG_8, file_hdr->c_name, _("uid"));
215 + to_ascii_or_warn (p, file_hdr->c_gid, 6, LG_8, file_hdr->c_name, _("gid"));
217 + to_ascii_or_warn (p, file_hdr->c_nlink, 6, LG_8, file_hdr->c_name,
218 + _("number of links"));
220 + to_ascii_or_warn (p, rdev, 6, LG_8, file_hdr->c_name, _("rdev"));
222 + to_ascii_or_warn (p, file_hdr->c_mtime, 11, LG_8, file_hdr->c_name,
223 + _("modification time"));
225 + if (to_ascii_or_error (p, file_hdr->c_namesize, 6, LG_8, file_hdr->c_name,
229 + if (to_ascii_or_error (p, file_hdr->c_filesize, 11, LG_8, file_hdr->c_name,
233 + tape_buffered_write (ascii_header, out_des, sizeof ascii_header);
235 + /* Write file name to output. */
236 + tape_buffered_write (file_hdr->c_name, out_des, file_hdr->c_namesize);
241 +hp_compute_dev (struct new_cpio_header *file_hdr, dev_t *pdev, dev_t *prdev)
243 + /* HP/UX cpio creates archives that look just like ordinary archives,
244 + but for devices it sets major = 0, minor = 1, and puts the
245 + actual major/minor number in the filesize field. */
246 + switch (file_hdr->c_mode & CP_IFMT)
258 - file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj,
259 - file_hdr->c_rdev_min);
263 - dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
264 - rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
268 + file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj,
269 + file_hdr->c_rdev_min);
270 + *pdev = *prdev = makedev (0, 1);
274 + *pdev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
275 + *prdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
280 - if ((warn_option & CPIO_WARN_TRUNCATE) && (file_hdr->c_ino >> 16) != 0)
281 - error (0, 0, _("%s: truncating inode number"), file_hdr->c_name);
283 +write_out_binary_header (dev_t rdev,
284 + struct new_cpio_header *file_hdr, int out_des)
286 + struct old_cpio_header short_hdr;
288 - /* Debian hack: The type of dev_t has changed in glibc. Fixed output
289 - to ensure that a long int is passed to sprintf. This has been
290 - reported to "bug-gnu-utils@prep.ai.mit.edu". (1998/5/26) -BEM */
291 - sprintf (ascii_header,
292 - "%06ho%06lo%06lo%06lo%06lo%06lo%06lo%06lo%011lo%06lo%011lo",
293 - file_hdr->c_magic & 0xFFFF, (long) dev & 0xFFFF,
294 - file_hdr->c_ino & 0xFFFF, file_hdr->c_mode & 0xFFFF,
295 - file_hdr->c_uid & 0xFFFF, file_hdr->c_gid & 0xFFFF,
296 - file_hdr->c_nlink & 0xFFFF, (long) rdev & 0xFFFF,
297 - file_hdr->c_mtime, file_hdr->c_namesize & 0xFFFF,
298 - file_hdr->c_filesize);
299 - tape_buffered_write (ascii_header, out_des, 76L);
300 + short_hdr.c_magic = 070707;
301 + short_hdr.c_dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
303 - /* Write file name to output. */
304 - tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
306 - else if (archive_format == arf_tar || archive_format == arf_ustar)
308 - write_out_tar_header (file_hdr, out_des);
312 - struct old_cpio_header short_hdr;
313 + if ((warn_option & CPIO_WARN_TRUNCATE) && (file_hdr->c_ino >> 16) != 0)
314 + error (0, 0, _("%s: truncating inode number"), file_hdr->c_name);
316 - short_hdr.c_magic = 070707;
317 - short_hdr.c_dev = makedev (file_hdr->c_dev_maj, file_hdr->c_dev_min);
318 + short_hdr.c_ino = file_hdr->c_ino & 0xFFFF;
319 + if (short_hdr.c_ino != file_hdr->c_ino)
320 + field_width_warning (file_hdr->c_name, _("inode number"));
322 + short_hdr.c_mode = file_hdr->c_mode & 0xFFFF;
323 + if (short_hdr.c_mode != file_hdr->c_mode)
324 + field_width_warning (file_hdr->c_name, _("file mode"));
326 + short_hdr.c_uid = file_hdr->c_uid & 0xFFFF;
327 + if (short_hdr.c_uid != file_hdr->c_uid)
328 + field_width_warning (file_hdr->c_name, _("uid"));
330 + short_hdr.c_gid = file_hdr->c_gid & 0xFFFF;
331 + if (short_hdr.c_gid != file_hdr->c_gid)
332 + field_width_warning (file_hdr->c_name, _("gid"));
334 + short_hdr.c_nlink = file_hdr->c_nlink & 0xFFFF;
335 + if (short_hdr.c_nlink != file_hdr->c_nlink)
336 + field_width_warning (file_hdr->c_name, _("number of links"));
338 + short_hdr.c_rdev = rdev;
339 + short_hdr.c_mtimes[0] = file_hdr->c_mtime >> 16;
340 + short_hdr.c_mtimes[1] = file_hdr->c_mtime & 0xFFFF;
342 + short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF;
343 + if (short_hdr.c_namesize != file_hdr->c_namesize)
345 + field_width_error (file_hdr->c_name, _("name size"));
349 + short_hdr.c_filesize = file_hdr->c_filesize;
350 + if (short_hdr.c_filesize != file_hdr->c_filesize)
352 + field_width_error (file_hdr->c_name, _("file size"));
356 + short_hdr.c_filesizes[0] = file_hdr->c_filesize >> 16;
357 + short_hdr.c_filesizes[1] = file_hdr->c_filesize & 0xFFFF;
359 - if ((warn_option & CPIO_WARN_TRUNCATE) && (file_hdr->c_ino >> 16) != 0)
360 - error (0, 0, _("%s: truncating inode number"), file_hdr->c_name);
361 + /* Output the file header. */
362 + tape_buffered_write ((char *) &short_hdr, out_des, 26);
364 - short_hdr.c_ino = file_hdr->c_ino & 0xFFFF;
365 - short_hdr.c_mode = file_hdr->c_mode & 0xFFFF;
366 - short_hdr.c_uid = file_hdr->c_uid & 0xFFFF;
367 - short_hdr.c_gid = file_hdr->c_gid & 0xFFFF;
368 - short_hdr.c_nlink = file_hdr->c_nlink & 0xFFFF;
369 - if (archive_format != arf_hpbinary)
370 - short_hdr.c_rdev = makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min);
373 - switch (file_hdr->c_mode & CP_IFMT)
375 - /* HP/UX cpio creates archives that look just like ordinary
376 - archives, but for devices it sets major = 0, minor = 1, and
377 - puts the actual major/minor number in the filesize field. */
386 - file_hdr->c_filesize = makedev (file_hdr->c_rdev_maj,
387 - file_hdr->c_rdev_min);
388 - short_hdr.c_rdev = makedev (0, 1);
391 - short_hdr.c_rdev = makedev (file_hdr->c_rdev_maj,
392 - file_hdr->c_rdev_min);
396 - short_hdr.c_mtimes[0] = file_hdr->c_mtime >> 16;
397 - short_hdr.c_mtimes[1] = file_hdr->c_mtime & 0xFFFF;
398 + /* Write file name to output. */
399 + tape_buffered_write (file_hdr->c_name, out_des, file_hdr->c_namesize);
401 - short_hdr.c_namesize = file_hdr->c_namesize & 0xFFFF;
402 + tape_pad_output (out_des, file_hdr->c_namesize + 26);
406 - short_hdr.c_filesizes[0] = file_hdr->c_filesize >> 16;
407 - short_hdr.c_filesizes[1] = file_hdr->c_filesize & 0xFFFF;
409 +/* Write out header FILE_HDR, including the file name, to file
410 + descriptor OUT_DES. */
412 - /* Output the file header. */
413 - tape_buffered_write ((char *) &short_hdr, out_des, 26L);
415 +write_out_header (struct new_cpio_header *file_hdr, int out_des)
420 + switch (archive_format)
423 + return write_out_new_ascii_header ("070701", file_hdr, out_des);
426 + return write_out_new_ascii_header ("070702", file_hdr, out_des);
429 + return write_out_old_ascii_header (makedev (file_hdr->c_dev_maj,
430 + file_hdr->c_dev_min),
431 + makedev (file_hdr->c_rdev_maj,
432 + file_hdr->c_rdev_min),
433 + file_hdr, out_des);
435 + case arf_hpoldascii:
436 + hp_compute_dev (file_hdr, &dev, &rdev);
437 + return write_out_old_ascii_header (dev, rdev, file_hdr, out_des);
441 + if (is_tar_filename_too_long (file_hdr->c_name))
443 + error (0, 0, _("%s: file name too long"), file_hdr->c_name);
446 + write_out_tar_header (file_hdr, out_des); /* FIXME: No error checking */
449 - /* Write file name to output. */
450 - tape_buffered_write (file_hdr->c_name, out_des, (long) file_hdr->c_namesize);
452 + return write_out_binary_header (makedev (file_hdr->c_rdev_maj,
453 + file_hdr->c_rdev_min),
454 + file_hdr, out_des);
457 + hp_compute_dev (file_hdr, &dev, &rdev);
458 + /* FIXME: dev ignored. Should it be? */
459 + return write_out_binary_header (rdev, file_hdr, out_des);
461 - tape_pad_output (out_des, file_hdr->c_namesize + 26);
467 @@ -593,14 +755,7 @@ process_copy_out ()
468 file_hdr.c_namesize = strlen (p) + 1;
471 - if ((archive_format == arf_tar || archive_format == arf_ustar)
472 - && is_tar_filename_too_long (file_hdr.c_name))
474 - error (0, 0, _("%s: file name too long"),
480 /* Copy the named file to the output. */
481 switch (file_hdr.c_mode & CP_IFMT)
483 @@ -613,8 +768,8 @@ process_copy_out ()
484 file_hdr.c_dev_min)))
486 file_hdr.c_tar_linkname = otherfile;
487 - write_out_header (&file_hdr, out_file_des);
489 + if (write_out_header (&file_hdr, out_file_des))
493 if ( (archive_format == arf_newascii || archive_format == arf_crcascii)
494 @@ -643,7 +798,8 @@ process_copy_out ()
496 input_name.ds_string);
498 - write_out_header (&file_hdr, out_file_des);
499 + if (write_out_header (&file_hdr, out_file_des))
501 copy_files_disk_to_tape (in_file_des, out_file_des, file_hdr.c_filesize, input_name.ds_string);
502 warn_if_file_changed(input_name.ds_string, file_hdr.c_filesize,
504 @@ -673,7 +829,8 @@ process_copy_out ()
507 file_hdr.c_filesize = 0;
508 - write_out_header (&file_hdr, out_file_des);
509 + if (write_out_header (&file_hdr, out_file_des))
514 @@ -702,14 +859,16 @@ process_copy_out ()
515 file_hdr.c_mode = (file_stat.st_mode & 07777);
516 file_hdr.c_mode |= CP_IFREG;
517 file_hdr.c_tar_linkname = otherfile;
518 - write_out_header (&file_hdr, out_file_des);
519 + if (write_out_header (&file_hdr, out_file_des))
523 add_inode (file_hdr.c_ino, file_hdr.c_name,
524 file_hdr.c_dev_maj, file_hdr.c_dev_min);
526 file_hdr.c_filesize = 0;
527 - write_out_header (&file_hdr, out_file_des);
528 + if (write_out_header (&file_hdr, out_file_des))
533 @@ -738,12 +897,14 @@ process_copy_out ()
535 link_name[link_size] = '\0';
536 file_hdr.c_tar_linkname = link_name;
537 - write_out_header (&file_hdr, out_file_des);
538 + if (write_out_header (&file_hdr, out_file_des))
544 - write_out_header (&file_hdr, out_file_des);
545 + if (write_out_header (&file_hdr, out_file_des))
547 tape_buffered_write (link_name, out_file_des, link_size);
548 tape_pad_output (out_file_des, link_size);