| 1 | /* copyin.c - extract or list a cpio archive
|
|---|
| 2 | Copyright (C) 1990,1991,1992,2001,2002,2003,2004,
|
|---|
| 3 | 2005, 2006 Free Software Foundation, Inc.
|
|---|
| 4 |
|
|---|
| 5 | This program is free software; you can redistribute it and/or modify
|
|---|
| 6 | it under the terms of the GNU General Public License as published by
|
|---|
| 7 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 8 | any later version.
|
|---|
| 9 |
|
|---|
| 10 | This program is distributed in the hope that it will be useful,
|
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 13 | GNU General Public License for more details.
|
|---|
| 14 |
|
|---|
| 15 | You should have received a copy of the GNU General Public
|
|---|
| 16 | License along with this program; if not, write to the Free
|
|---|
| 17 | Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|---|
| 18 | Boston, MA 02110-1301 USA. */
|
|---|
| 19 |
|
|---|
| 20 | #include <system.h>
|
|---|
| 21 |
|
|---|
| 22 | #include <stdio.h>
|
|---|
| 23 | #include <sys/types.h>
|
|---|
| 24 | #include <sys/stat.h>
|
|---|
| 25 | #include "filetypes.h"
|
|---|
| 26 | #include "cpiohdr.h"
|
|---|
| 27 | #include "dstring.h"
|
|---|
| 28 | #include "extern.h"
|
|---|
| 29 | #include "defer.h"
|
|---|
| 30 | #include <rmt.h>
|
|---|
| 31 | #ifndef FNM_PATHNAME
|
|---|
| 32 | # include <fnmatch.h>
|
|---|
| 33 | #endif
|
|---|
| 34 |
|
|---|
| 35 | #ifndef HAVE_LCHOWN
|
|---|
| 36 | # define lchown(f,u,g) 0
|
|---|
| 37 | #endif
|
|---|
| 38 |
|
|---|
| 39 | static void copyin_regular_file(struct cpio_file_stat* file_hdr,
|
|---|
| 40 | int in_file_des);
|
|---|
| 41 |
|
|---|
| 42 | void
|
|---|
| 43 | warn_junk_bytes (long bytes_skipped)
|
|---|
| 44 | {
|
|---|
| 45 | error (0, 0, ngettext ("warning: skipped %ld byte of junk",
|
|---|
| 46 | "warning: skipped %ld bytes of junk", bytes_skipped),
|
|---|
| 47 | bytes_skipped);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | |
|---|
| 51 |
|
|---|
| 52 | static int
|
|---|
| 53 | query_rename(struct cpio_file_stat* file_hdr, FILE *tty_in, FILE *tty_out,
|
|---|
| 54 | FILE *rename_in)
|
|---|
| 55 | {
|
|---|
| 56 | char *str_res; /* Result for string function. */
|
|---|
| 57 | static dynamic_string new_name; /* New file name for rename option. */
|
|---|
| 58 | static int initialized_new_name = false;
|
|---|
| 59 | if (!initialized_new_name)
|
|---|
| 60 | {
|
|---|
| 61 | ds_init (&new_name, 128);
|
|---|
| 62 | initialized_new_name = true;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | if (rename_flag)
|
|---|
| 66 | {
|
|---|
| 67 | fprintf (tty_out, _("rename %s -> "), file_hdr->c_name);
|
|---|
| 68 | fflush (tty_out);
|
|---|
| 69 | str_res = ds_fgets (tty_in, &new_name);
|
|---|
| 70 | }
|
|---|
| 71 | else
|
|---|
| 72 | {
|
|---|
| 73 | str_res = ds_fgetstr (rename_in, &new_name, '\n');
|
|---|
| 74 | }
|
|---|
| 75 | if (str_res == NULL || str_res[0] == 0)
|
|---|
| 76 | {
|
|---|
| 77 | return -1;
|
|---|
| 78 | }
|
|---|
| 79 | else
|
|---|
| 80 | /* Debian hack: file_hrd.c_name is sometimes set to
|
|---|
| 81 | point to static memory by code in tar.c. This
|
|---|
| 82 | causes a segfault. This has been fixed and an
|
|---|
| 83 | additional check to ensure that the file name
|
|---|
| 84 | is not too long has been added. (Reported by
|
|---|
| 85 | Horst Knobloch.) This bug has been reported to
|
|---|
| 86 | "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM */
|
|---|
| 87 | {
|
|---|
| 88 | if (archive_format != arf_tar && archive_format != arf_ustar)
|
|---|
| 89 | {
|
|---|
| 90 | free (file_hdr->c_name);
|
|---|
| 91 | file_hdr->c_name = xstrdup (new_name.ds_string);
|
|---|
| 92 | }
|
|---|
| 93 | else
|
|---|
| 94 | {
|
|---|
| 95 | if (is_tar_filename_too_long (new_name.ds_string))
|
|---|
| 96 | error (0, 0, _("%s: file name too long"),
|
|---|
| 97 | new_name.ds_string);
|
|---|
| 98 | else
|
|---|
| 99 | strcpy (file_hdr->c_name, new_name.ds_string);
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | return 0;
|
|---|
| 103 | }
|
|---|
| 104 | |
|---|
| 105 |
|
|---|
| 106 | /* Skip the padding on IN_FILE_DES after a header or file,
|
|---|
| 107 | up to the next header.
|
|---|
| 108 | The number of bytes skipped is based on OFFSET -- the current offset
|
|---|
| 109 | from the last start of a header (or file) -- and the current
|
|---|
| 110 | header type. */
|
|---|
| 111 |
|
|---|
| 112 | static void
|
|---|
| 113 | tape_skip_padding (int in_file_des, int offset)
|
|---|
| 114 | {
|
|---|
| 115 | int pad;
|
|---|
| 116 |
|
|---|
| 117 | if (archive_format == arf_crcascii || archive_format == arf_newascii)
|
|---|
| 118 | pad = (4 - (offset % 4)) % 4;
|
|---|
| 119 | else if (archive_format == arf_binary || archive_format == arf_hpbinary)
|
|---|
| 120 | pad = (2 - (offset % 2)) % 2;
|
|---|
| 121 | else if (archive_format == arf_tar || archive_format == arf_ustar)
|
|---|
| 122 | pad = (512 - (offset % 512)) % 512;
|
|---|
| 123 | else
|
|---|
| 124 | pad = 0;
|
|---|
| 125 |
|
|---|
| 126 | if (pad != 0)
|
|---|
| 127 | tape_toss_input (in_file_des, pad);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | |
|---|
| 131 |
|
|---|
| 132 | static void
|
|---|
| 133 | list_file(struct cpio_file_stat* file_hdr, int in_file_des)
|
|---|
| 134 | {
|
|---|
| 135 | if (verbose_flag)
|
|---|
| 136 | {
|
|---|
| 137 | #ifdef CP_IFLNK
|
|---|
| 138 | if ((file_hdr->c_mode & CP_IFMT) == CP_IFLNK)
|
|---|
| 139 | {
|
|---|
| 140 | if (archive_format != arf_tar && archive_format != arf_ustar)
|
|---|
| 141 | {
|
|---|
| 142 | char *link_name = NULL; /* Name of hard and symbolic links. */
|
|---|
| 143 |
|
|---|
| 144 | link_name = (char *) xmalloc ((unsigned int) file_hdr->c_filesize + 1);
|
|---|
| 145 | link_name[file_hdr->c_filesize] = '\0';
|
|---|
| 146 | tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
|
|---|
| 147 | long_format (file_hdr, link_name);
|
|---|
| 148 | free (link_name);
|
|---|
| 149 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 150 | return;
|
|---|
| 151 | }
|
|---|
| 152 | else
|
|---|
| 153 | {
|
|---|
| 154 | long_format (file_hdr, file_hdr->c_tar_linkname);
|
|---|
| 155 | return;
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 | else
|
|---|
| 159 | #endif
|
|---|
| 160 | long_format (file_hdr, (char *) 0);
|
|---|
| 161 | }
|
|---|
| 162 | else
|
|---|
| 163 | {
|
|---|
| 164 | /* Debian hack: Modified to print a list of filenames
|
|---|
| 165 | terminiated by a null character when the -t and -0
|
|---|
| 166 | flags are used. This has been submitted as a
|
|---|
| 167 | suggestion to "bug-gnu-utils@prep.ai.mit.edu". -BEM */
|
|---|
| 168 | printf ("%s%c", file_hdr->c_name, name_end);
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | crc = 0;
|
|---|
| 172 | tape_toss_input (in_file_des, file_hdr->c_filesize);
|
|---|
| 173 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 174 | if (only_verify_crc_flag)
|
|---|
| 175 | {
|
|---|
| 176 | #ifdef CP_IFLNK
|
|---|
| 177 | if ((file_hdr->c_mode & CP_IFMT) == CP_IFLNK)
|
|---|
| 178 | {
|
|---|
| 179 | return; /* links don't have a checksum */
|
|---|
| 180 | }
|
|---|
| 181 | #endif
|
|---|
| 182 | if (crc != file_hdr->c_chksum)
|
|---|
| 183 | {
|
|---|
| 184 | error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
|
|---|
| 185 | file_hdr->c_name, crc, file_hdr->c_chksum);
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 | |
|---|
| 190 |
|
|---|
| 191 | static int
|
|---|
| 192 | try_existing_file(struct cpio_file_stat* file_hdr, int in_file_des,
|
|---|
| 193 | int *existing_dir)
|
|---|
| 194 | {
|
|---|
| 195 | struct stat file_stat;
|
|---|
| 196 |
|
|---|
| 197 | *existing_dir = false;
|
|---|
| 198 | if (lstat (file_hdr->c_name, &file_stat) == 0)
|
|---|
| 199 | {
|
|---|
| 200 | if (S_ISDIR (file_stat.st_mode)
|
|---|
| 201 | && ((file_hdr->c_mode & CP_IFMT) == CP_IFDIR))
|
|---|
| 202 | {
|
|---|
| 203 | /* If there is already a directory there that
|
|---|
| 204 | we are trying to create, don't complain about
|
|---|
| 205 | it. */
|
|---|
| 206 | *existing_dir = true;
|
|---|
| 207 | return 0;
|
|---|
| 208 | }
|
|---|
| 209 | else if (!unconditional_flag
|
|---|
| 210 | && file_hdr->c_mtime <= file_stat.st_mtime)
|
|---|
| 211 | {
|
|---|
| 212 | error (0, 0, _("%s not created: newer or same age version exists"),
|
|---|
| 213 | file_hdr->c_name);
|
|---|
| 214 | tape_toss_input (in_file_des, file_hdr->c_filesize);
|
|---|
| 215 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 216 | return -1; /* Go to the next file. */
|
|---|
| 217 | }
|
|---|
| 218 | else if (S_ISDIR (file_stat.st_mode)
|
|---|
| 219 | ? rmdir (file_hdr->c_name)
|
|---|
| 220 | : unlink (file_hdr->c_name))
|
|---|
| 221 | {
|
|---|
| 222 | error (0, errno, _("cannot remove current %s"),
|
|---|
| 223 | file_hdr->c_name);
|
|---|
| 224 | tape_toss_input (in_file_des, file_hdr->c_filesize);
|
|---|
| 225 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 226 | return -1; /* Go to the next file. */
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 | return 0;
|
|---|
| 230 | }
|
|---|
| 231 | |
|---|
| 232 |
|
|---|
| 233 | /* The newc and crc formats store multiply linked copies of the same file
|
|---|
| 234 | in the archive only once. The actual data is attached to the last link
|
|---|
| 235 | in the archive, and the other links all have a filesize of 0. When a
|
|---|
| 236 | file in the archive has multiple links and a filesize of 0, its data is
|
|---|
| 237 | probably "attatched" to another file in the archive, so we can't create
|
|---|
| 238 | it right away. We have to "defer" creating it until we have created
|
|---|
| 239 | the file that has the data "attatched" to it. We keep a list of the
|
|---|
| 240 | "defered" links on deferments. */
|
|---|
| 241 |
|
|---|
| 242 | struct deferment *deferments = NULL;
|
|---|
| 243 |
|
|---|
| 244 | /* Add a file header to the deferments list. For now they all just
|
|---|
| 245 | go on one list, although we could optimize this if necessary. */
|
|---|
| 246 |
|
|---|
| 247 | static void
|
|---|
| 248 | defer_copyin (struct cpio_file_stat *file_hdr)
|
|---|
| 249 | {
|
|---|
| 250 | struct deferment *d;
|
|---|
| 251 | d = create_deferment (file_hdr);
|
|---|
| 252 | d->next = deferments;
|
|---|
| 253 | deferments = d;
|
|---|
| 254 | return;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | /* We just created a file that (probably) has some other links to it
|
|---|
| 258 | which have been defered. Go through all of the links on the deferments
|
|---|
| 259 | list and create any which are links to this file. */
|
|---|
| 260 |
|
|---|
| 261 | static void
|
|---|
| 262 | create_defered_links (struct cpio_file_stat *file_hdr)
|
|---|
| 263 | {
|
|---|
| 264 | struct deferment *d;
|
|---|
| 265 | struct deferment *d_prev;
|
|---|
| 266 | int ino;
|
|---|
| 267 | int maj;
|
|---|
| 268 | int min;
|
|---|
| 269 | int link_res;
|
|---|
| 270 | ino = file_hdr->c_ino;
|
|---|
| 271 | maj = file_hdr->c_dev_maj;
|
|---|
| 272 | min = file_hdr->c_dev_min;
|
|---|
| 273 | d = deferments;
|
|---|
| 274 | d_prev = NULL;
|
|---|
| 275 | while (d != NULL)
|
|---|
| 276 | {
|
|---|
| 277 | if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
|
|---|
| 278 | && (d->header.c_dev_min == min) )
|
|---|
| 279 | {
|
|---|
| 280 | struct deferment *d_free;
|
|---|
| 281 | link_res = link_to_name (d->header.c_name, file_hdr->c_name);
|
|---|
| 282 | if (link_res < 0)
|
|---|
| 283 | {
|
|---|
| 284 | error (0, errno, _("cannot link %s to %s"),
|
|---|
| 285 | d->header.c_name, file_hdr->c_name);
|
|---|
| 286 | }
|
|---|
| 287 | if (d_prev != NULL)
|
|---|
| 288 | d_prev->next = d->next;
|
|---|
| 289 | else
|
|---|
| 290 | deferments = d->next;
|
|---|
| 291 | d_free = d;
|
|---|
| 292 | d = d->next;
|
|---|
| 293 | free_deferment (d_free);
|
|---|
| 294 | }
|
|---|
| 295 | else
|
|---|
| 296 | {
|
|---|
| 297 | d_prev = d;
|
|---|
| 298 | d = d->next;
|
|---|
| 299 | }
|
|---|
| 300 | }
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | /* We are skipping a file but there might be other links to it that we
|
|---|
| 304 | did not skip, so we have to copy its data for the other links. Find
|
|---|
| 305 | the first link that we didn't skip and try to create that. That will
|
|---|
| 306 | then create the other deferred links. */
|
|---|
| 307 |
|
|---|
| 308 | static int
|
|---|
| 309 | create_defered_links_to_skipped (struct cpio_file_stat *file_hdr,
|
|---|
| 310 | int in_file_des)
|
|---|
| 311 | {
|
|---|
| 312 | struct deferment *d;
|
|---|
| 313 | struct deferment *d_prev;
|
|---|
| 314 | int ino;
|
|---|
| 315 | int maj;
|
|---|
| 316 | int min;
|
|---|
| 317 | if (file_hdr->c_filesize == 0)
|
|---|
| 318 | {
|
|---|
| 319 | /* The file doesn't have any data attached to it so we don't have
|
|---|
| 320 | to bother. */
|
|---|
| 321 | return -1;
|
|---|
| 322 | }
|
|---|
| 323 | ino = file_hdr->c_ino;
|
|---|
| 324 | maj = file_hdr->c_dev_maj;
|
|---|
| 325 | min = file_hdr->c_dev_min;
|
|---|
| 326 | d = deferments;
|
|---|
| 327 | d_prev = NULL;
|
|---|
| 328 | while (d != NULL)
|
|---|
| 329 | {
|
|---|
| 330 | if ( (d->header.c_ino == ino) && (d->header.c_dev_maj == maj)
|
|---|
| 331 | && (d->header.c_dev_min == min) )
|
|---|
| 332 | {
|
|---|
| 333 | if (d_prev != NULL)
|
|---|
| 334 | d_prev->next = d->next;
|
|---|
| 335 | else
|
|---|
| 336 | deferments = d->next;
|
|---|
| 337 | free (file_hdr->c_name);
|
|---|
| 338 | file_hdr->c_name = xstrdup(d->header.c_name);
|
|---|
| 339 | free_deferment (d);
|
|---|
| 340 | copyin_regular_file(file_hdr, in_file_des);
|
|---|
| 341 | return 0;
|
|---|
| 342 | }
|
|---|
| 343 | else
|
|---|
| 344 | {
|
|---|
| 345 | d_prev = d;
|
|---|
| 346 | d = d->next;
|
|---|
| 347 | }
|
|---|
| 348 | }
|
|---|
| 349 | return -1;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | /* If we had a multiply linked file that really was empty then we would
|
|---|
| 353 | have defered all of its links, since we never found any with data
|
|---|
| 354 | "attached", and they will still be on the deferment list even when
|
|---|
| 355 | we are done reading the whole archive. Write out all of these
|
|---|
| 356 | empty links that are still on the deferments list. */
|
|---|
| 357 |
|
|---|
| 358 | static void
|
|---|
| 359 | create_final_defers ()
|
|---|
| 360 | {
|
|---|
| 361 | struct deferment *d;
|
|---|
| 362 | int link_res;
|
|---|
| 363 | int out_file_des;
|
|---|
| 364 |
|
|---|
| 365 | for (d = deferments; d != NULL; d = d->next)
|
|---|
| 366 | {
|
|---|
| 367 | /* Debian hack: A line, which could cause an endless loop, was
|
|---|
| 368 | removed (97/1/2). It was reported by Ronald F. Guilmette to
|
|---|
| 369 | the upstream maintainers. -BEM */
|
|---|
| 370 | /* Debian hack: This was reported by Horst Knobloch. This bug has
|
|---|
| 371 | been reported to "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM
|
|---|
| 372 | */
|
|---|
| 373 | link_res = link_to_maj_min_ino (d->header.c_name,
|
|---|
| 374 | d->header.c_dev_maj, d->header.c_dev_min,
|
|---|
| 375 | d->header.c_ino);
|
|---|
| 376 | if (link_res == 0)
|
|---|
| 377 | {
|
|---|
| 378 | continue;
|
|---|
| 379 | }
|
|---|
| 380 | out_file_des = open (d->header.c_name,
|
|---|
| 381 | O_CREAT | O_WRONLY | O_BINARY, 0600);
|
|---|
| 382 | if (out_file_des < 0 && create_dir_flag)
|
|---|
| 383 | {
|
|---|
| 384 | create_all_directories (d->header.c_name);
|
|---|
| 385 | out_file_des = open (d->header.c_name,
|
|---|
| 386 | O_CREAT | O_WRONLY | O_BINARY,
|
|---|
| 387 | 0600);
|
|---|
| 388 | }
|
|---|
| 389 | if (out_file_des < 0)
|
|---|
| 390 | {
|
|---|
| 391 | open_error (d->header.c_name);
|
|---|
| 392 | continue;
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | if (close (out_file_des) < 0)
|
|---|
| 396 | close_error (d->header.c_name);
|
|---|
| 397 |
|
|---|
| 398 | set_perms (&d->header);
|
|---|
| 399 | }
|
|---|
| 400 | }
|
|---|
| 401 | |
|---|
| 402 |
|
|---|
| 403 | static void
|
|---|
| 404 | copyin_regular_file (struct cpio_file_stat* file_hdr, int in_file_des)
|
|---|
| 405 | {
|
|---|
| 406 | int out_file_des; /* Output file descriptor. */
|
|---|
| 407 |
|
|---|
| 408 | if (to_stdout_option)
|
|---|
| 409 | out_file_des = STDOUT_FILENO;
|
|---|
| 410 | else
|
|---|
| 411 | {
|
|---|
| 412 | /* Can the current file be linked to a previously copied file? */
|
|---|
| 413 | if (file_hdr->c_nlink > 1
|
|---|
| 414 | && (archive_format == arf_newascii
|
|---|
| 415 | || archive_format == arf_crcascii) )
|
|---|
| 416 | {
|
|---|
| 417 | int link_res;
|
|---|
| 418 | if (file_hdr->c_filesize == 0)
|
|---|
| 419 | {
|
|---|
| 420 | /* The newc and crc formats store multiply linked copies
|
|---|
| 421 | of the same file in the archive only once. The
|
|---|
| 422 | actual data is attached to the last link in the
|
|---|
| 423 | archive, and the other links all have a filesize
|
|---|
| 424 | of 0. Since this file has multiple links and a
|
|---|
| 425 | filesize of 0, its data is probably attatched to
|
|---|
| 426 | another file in the archive. Save the link, and
|
|---|
| 427 | process it later when we get the actual data. We
|
|---|
| 428 | can't just create it with length 0 and add the
|
|---|
| 429 | data later, in case the file is readonly. We still
|
|---|
| 430 | lose if its parent directory is readonly (and we aren't
|
|---|
| 431 | running as root), but there's nothing we can do about
|
|---|
| 432 | that. */
|
|---|
| 433 | defer_copyin (file_hdr);
|
|---|
| 434 | tape_toss_input (in_file_des, file_hdr->c_filesize);
|
|---|
| 435 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 436 | return;
|
|---|
| 437 | }
|
|---|
| 438 | /* If the file has data (filesize != 0), then presumably
|
|---|
| 439 | any other links have already been defer_copyin'ed(),
|
|---|
| 440 | but GNU cpio version 2.0-2.2 didn't do that, so we
|
|---|
| 441 | still have to check for links here (and also in case
|
|---|
| 442 | the archive was created and later appeneded to). */
|
|---|
| 443 | /* Debian hack: (97/1/2) This was reported by Ronald
|
|---|
| 444 | F. Guilmette to the upstream maintainers. -BEM */
|
|---|
| 445 | link_res = link_to_maj_min_ino (file_hdr->c_name,
|
|---|
| 446 | file_hdr->c_dev_maj, file_hdr->c_dev_min,
|
|---|
| 447 | file_hdr->c_ino);
|
|---|
| 448 | if (link_res == 0)
|
|---|
| 449 | {
|
|---|
| 450 | tape_toss_input (in_file_des, file_hdr->c_filesize);
|
|---|
| 451 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 452 | return;
|
|---|
| 453 | }
|
|---|
| 454 | }
|
|---|
| 455 | else if (file_hdr->c_nlink > 1
|
|---|
| 456 | && archive_format != arf_tar
|
|---|
| 457 | && archive_format != arf_ustar)
|
|---|
| 458 | {
|
|---|
| 459 | int link_res;
|
|---|
| 460 | /* Debian hack: (97/1/2) This was reported by Ronald
|
|---|
| 461 | F. Guilmette to the upstream maintainers. -BEM */
|
|---|
| 462 | link_res = link_to_maj_min_ino (file_hdr->c_name,
|
|---|
| 463 | file_hdr->c_dev_maj,
|
|---|
| 464 | file_hdr->c_dev_min,
|
|---|
| 465 | file_hdr->c_ino);
|
|---|
| 466 | if (link_res == 0)
|
|---|
| 467 | {
|
|---|
| 468 | tape_toss_input (in_file_des, file_hdr->c_filesize);
|
|---|
| 469 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 470 | return;
|
|---|
| 471 | }
|
|---|
| 472 | }
|
|---|
| 473 | else if ((archive_format == arf_tar || archive_format == arf_ustar)
|
|---|
| 474 | && file_hdr->c_tar_linkname
|
|---|
| 475 | && file_hdr->c_tar_linkname[0] != '\0')
|
|---|
| 476 | {
|
|---|
| 477 | int link_res;
|
|---|
| 478 | link_res = link_to_name (file_hdr->c_name, file_hdr->c_tar_linkname);
|
|---|
| 479 | if (link_res < 0)
|
|---|
| 480 | {
|
|---|
| 481 | error (0, errno, _("cannot link %s to %s"),
|
|---|
| 482 | file_hdr->c_tar_linkname, file_hdr->c_name);
|
|---|
| 483 | }
|
|---|
| 484 | return;
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | /* If not linked, copy the contents of the file. */
|
|---|
| 488 | out_file_des = open (file_hdr->c_name,
|
|---|
| 489 | O_CREAT | O_WRONLY | O_BINARY, 0600);
|
|---|
| 490 |
|
|---|
| 491 | if (out_file_des < 0 && create_dir_flag)
|
|---|
| 492 | {
|
|---|
| 493 | create_all_directories (file_hdr->c_name);
|
|---|
| 494 | out_file_des = open (file_hdr->c_name,
|
|---|
| 495 | O_CREAT | O_WRONLY | O_BINARY,
|
|---|
| 496 | 0600);
|
|---|
| 497 | }
|
|---|
| 498 |
|
|---|
| 499 | if (out_file_des < 0)
|
|---|
| 500 | {
|
|---|
| 501 | open_error (file_hdr->c_name);
|
|---|
| 502 | tape_toss_input (in_file_des, file_hdr->c_filesize);
|
|---|
| 503 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 504 | return;
|
|---|
| 505 | }
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | crc = 0;
|
|---|
| 509 | if (swap_halfwords_flag)
|
|---|
| 510 | {
|
|---|
| 511 | if ((file_hdr->c_filesize % 4) == 0)
|
|---|
| 512 | swapping_halfwords = true;
|
|---|
| 513 | else
|
|---|
| 514 | error (0, 0, _("cannot swap halfwords of %s: odd number of halfwords"),
|
|---|
| 515 | file_hdr->c_name);
|
|---|
| 516 | }
|
|---|
| 517 | if (swap_bytes_flag)
|
|---|
| 518 | {
|
|---|
| 519 | if ((file_hdr->c_filesize % 2) == 0)
|
|---|
| 520 | swapping_bytes = true;
|
|---|
| 521 | else
|
|---|
| 522 | error (0, 0, _("cannot swap bytes of %s: odd number of bytes"),
|
|---|
| 523 | file_hdr->c_name);
|
|---|
| 524 | }
|
|---|
| 525 | copy_files_tape_to_disk (in_file_des, out_file_des, file_hdr->c_filesize);
|
|---|
| 526 | disk_empty_output_buffer (out_file_des);
|
|---|
| 527 |
|
|---|
| 528 | if (to_stdout_option)
|
|---|
| 529 | {
|
|---|
| 530 | if (archive_format == arf_crcascii)
|
|---|
| 531 | {
|
|---|
| 532 | if (crc != file_hdr->c_chksum)
|
|---|
| 533 | error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
|
|---|
| 534 | file_hdr->c_name, crc, file_hdr->c_chksum);
|
|---|
| 535 | }
|
|---|
| 536 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 537 | return;
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | /* Debian hack to fix a bug in the --sparse option.
|
|---|
| 541 | This bug has been reported to
|
|---|
| 542 | "bug-gnu-utils@prep.ai.mit.edu". (96/7/10) -BEM */
|
|---|
| 543 | if (delayed_seek_count > 0)
|
|---|
| 544 | {
|
|---|
| 545 | lseek (out_file_des, delayed_seek_count-1, SEEK_CUR);
|
|---|
| 546 | write (out_file_des, "", 1);
|
|---|
| 547 | delayed_seek_count = 0;
|
|---|
| 548 | }
|
|---|
| 549 | if (close (out_file_des) < 0)
|
|---|
| 550 | close_error (file_hdr->c_name);
|
|---|
| 551 |
|
|---|
| 552 | if (archive_format == arf_crcascii)
|
|---|
| 553 | {
|
|---|
| 554 | if (crc != file_hdr->c_chksum)
|
|---|
| 555 | error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
|
|---|
| 556 | file_hdr->c_name, crc, file_hdr->c_chksum);
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | set_perms (file_hdr);
|
|---|
| 560 |
|
|---|
| 561 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 562 | if (file_hdr->c_nlink > 1
|
|---|
| 563 | && (archive_format == arf_newascii || archive_format == arf_crcascii) )
|
|---|
| 564 | {
|
|---|
| 565 | /* (see comment above for how the newc and crc formats
|
|---|
| 566 | store multiple links). Now that we have the data
|
|---|
| 567 | for this file, create any other links to it which
|
|---|
| 568 | we defered. */
|
|---|
| 569 | create_defered_links (file_hdr);
|
|---|
| 570 | }
|
|---|
| 571 | }
|
|---|
| 572 | |
|---|
| 573 |
|
|---|
| 574 | static void
|
|---|
| 575 | copyin_directory(struct cpio_file_stat* file_hdr, int existing_dir)
|
|---|
| 576 | {
|
|---|
| 577 | int res; /* Result of various function calls. */
|
|---|
| 578 | #ifdef HPUX_CDF
|
|---|
| 579 | int cdf_flag; /* True if file is a CDF. */
|
|---|
| 580 | int cdf_char; /* Index of `+' char indicating a CDF. */
|
|---|
| 581 | #endif
|
|---|
| 582 |
|
|---|
| 583 | if (to_stdout_option)
|
|---|
| 584 | return;
|
|---|
| 585 |
|
|---|
| 586 | /* Strip any trailing `/'s off the filename; tar puts
|
|---|
| 587 | them on. We might as well do it here in case anybody
|
|---|
| 588 | else does too, since they cause strange things to happen. */
|
|---|
| 589 | strip_trailing_slashes (file_hdr->c_name);
|
|---|
| 590 |
|
|---|
| 591 | /* Ignore the current directory. It must already exist,
|
|---|
| 592 | and we don't want to change its permission, ownership
|
|---|
| 593 | or time. */
|
|---|
| 594 | if (file_hdr->c_name[0] == '.' && file_hdr->c_name[1] == '\0')
|
|---|
| 595 | {
|
|---|
| 596 | return;
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | #ifdef HPUX_CDF
|
|---|
| 600 | cdf_flag = 0;
|
|---|
| 601 | #endif
|
|---|
| 602 | if (!existing_dir)
|
|---|
| 603 |
|
|---|
| 604 | {
|
|---|
| 605 | #ifdef HPUX_CDF
|
|---|
| 606 | /* If the directory name ends in a + and is SUID,
|
|---|
| 607 | then it is a CDF. Strip the trailing + from
|
|---|
| 608 | the name before creating it. */
|
|---|
| 609 | cdf_char = strlen (file_hdr->c_name) - 1;
|
|---|
| 610 | if ( (cdf_char > 0) &&
|
|---|
| 611 | (file_hdr->c_mode & 04000) &&
|
|---|
| 612 | (file_hdr->c_name [cdf_char] == '+') )
|
|---|
| 613 | {
|
|---|
| 614 | file_hdr->c_name [cdf_char] = '\0';
|
|---|
| 615 | cdf_flag = 1;
|
|---|
| 616 | }
|
|---|
| 617 | #endif
|
|---|
| 618 | res = mkdir (file_hdr->c_name, file_hdr->c_mode);
|
|---|
| 619 | }
|
|---|
| 620 | else
|
|---|
| 621 | res = 0;
|
|---|
| 622 | if (res < 0 && create_dir_flag)
|
|---|
| 623 | {
|
|---|
| 624 | create_all_directories (file_hdr->c_name);
|
|---|
| 625 | res = mkdir (file_hdr->c_name, file_hdr->c_mode);
|
|---|
| 626 | }
|
|---|
| 627 | if (res < 0)
|
|---|
| 628 | {
|
|---|
| 629 | /* In some odd cases where the file_hdr->c_name includes `.',
|
|---|
| 630 | the directory may have actually been created by
|
|---|
| 631 | create_all_directories(), so the mkdir will fail
|
|---|
| 632 | because the directory exists. If that's the case,
|
|---|
| 633 | don't complain about it. */
|
|---|
| 634 | struct stat file_stat;
|
|---|
| 635 | if (errno != EEXIST)
|
|---|
| 636 | {
|
|---|
| 637 | mkdir_error (file_hdr->c_name);
|
|---|
| 638 | return;
|
|---|
| 639 | }
|
|---|
| 640 | if (lstat (file_hdr->c_name, &file_stat))
|
|---|
| 641 | {
|
|---|
| 642 | stat_error (file_hdr->c_name);
|
|---|
| 643 | return;
|
|---|
| 644 | }
|
|---|
| 645 | if (!(S_ISDIR (file_stat.st_mode)))
|
|---|
| 646 | {
|
|---|
| 647 | error (0, 0, _("%s is not a directory"),
|
|---|
| 648 | quotearg_colon (file_hdr->c_name));
|
|---|
| 649 | return;
|
|---|
| 650 | }
|
|---|
| 651 | }
|
|---|
| 652 |
|
|---|
| 653 | set_perms (file_hdr);
|
|---|
| 654 | }
|
|---|
| 655 | |
|---|
| 656 |
|
|---|
| 657 | static void
|
|---|
| 658 | copyin_device(struct cpio_file_stat* file_hdr)
|
|---|
| 659 | {
|
|---|
| 660 | int res; /* Result of various function calls. */
|
|---|
| 661 |
|
|---|
| 662 | if (to_stdout_option)
|
|---|
| 663 | return;
|
|---|
| 664 |
|
|---|
| 665 | if (file_hdr->c_nlink > 1 && archive_format != arf_tar
|
|---|
| 666 | && archive_format != arf_ustar)
|
|---|
| 667 | {
|
|---|
| 668 | int link_res;
|
|---|
| 669 | /* Debian hack: This was reported by Horst
|
|---|
| 670 | Knobloch. This bug has been reported to
|
|---|
| 671 | "bug-gnu-utils@prep.ai.mit.edu". (99/1/6) -BEM */
|
|---|
| 672 | link_res = link_to_maj_min_ino (file_hdr->c_name,
|
|---|
| 673 | file_hdr->c_dev_maj, file_hdr->c_dev_min,
|
|---|
| 674 | file_hdr->c_ino);
|
|---|
| 675 | if (link_res == 0)
|
|---|
| 676 | {
|
|---|
| 677 | return;
|
|---|
| 678 | }
|
|---|
| 679 | }
|
|---|
| 680 | else if (archive_format == arf_ustar &&
|
|---|
| 681 | file_hdr->c_tar_linkname &&
|
|---|
| 682 | file_hdr->c_tar_linkname [0] != '\0')
|
|---|
| 683 | {
|
|---|
| 684 | int link_res;
|
|---|
| 685 | link_res = link_to_name (file_hdr->c_name,
|
|---|
| 686 | file_hdr->c_tar_linkname);
|
|---|
| 687 | if (link_res < 0)
|
|---|
| 688 | {
|
|---|
| 689 | error (0, errno, _("cannot link %s to %s"),
|
|---|
| 690 | file_hdr->c_tar_linkname, file_hdr->c_name);
|
|---|
| 691 | /* Something must be wrong, because we couldn't
|
|---|
| 692 | find the file to link to. But can we assume
|
|---|
| 693 | that the device maj/min numbers are correct
|
|---|
| 694 | and fall through to the mknod? It's probably
|
|---|
| 695 | safer to just return, rather than possibly
|
|---|
| 696 | creating a bogus device file. */
|
|---|
| 697 | }
|
|---|
| 698 | return;
|
|---|
| 699 | }
|
|---|
| 700 |
|
|---|
| 701 | res = mknod (file_hdr->c_name, file_hdr->c_mode,
|
|---|
| 702 | makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min));
|
|---|
| 703 | if (res < 0 && create_dir_flag)
|
|---|
| 704 | {
|
|---|
| 705 | create_all_directories (file_hdr->c_name);
|
|---|
| 706 | res = mknod (file_hdr->c_name, file_hdr->c_mode,
|
|---|
| 707 | makedev (file_hdr->c_rdev_maj, file_hdr->c_rdev_min));
|
|---|
| 708 | }
|
|---|
| 709 | if (res < 0)
|
|---|
| 710 | {
|
|---|
| 711 | mknod_error (file_hdr->c_name);
|
|---|
| 712 | return;
|
|---|
| 713 | }
|
|---|
| 714 | if (!no_chown_flag)
|
|---|
| 715 | {
|
|---|
| 716 | uid_t uid = set_owner_flag ? set_owner : file_hdr->c_uid;
|
|---|
| 717 | gid_t gid = set_group_flag ? set_group : file_hdr->c_gid;
|
|---|
| 718 | if ((chown (file_hdr->c_name, uid, gid) < 0)
|
|---|
| 719 | && errno != EPERM)
|
|---|
| 720 | chown_error_details (file_hdr->c_name, uid, gid);
|
|---|
| 721 | }
|
|---|
| 722 | /* chown may have turned off some permissions we wanted. */
|
|---|
| 723 | if (chmod (file_hdr->c_name, file_hdr->c_mode) < 0)
|
|---|
| 724 | chmod_error_details (file_hdr->c_name, file_hdr->c_mode);
|
|---|
| 725 | if (retain_time_flag)
|
|---|
| 726 | set_file_times (file_hdr->c_name, file_hdr->c_mtime, file_hdr->c_mtime);
|
|---|
| 727 | }
|
|---|
| 728 | |
|---|
| 729 |
|
|---|
| 730 | static void
|
|---|
| 731 | copyin_link(struct cpio_file_stat *file_hdr, int in_file_des)
|
|---|
| 732 | {
|
|---|
| 733 | char *link_name = NULL; /* Name of hard and symbolic links. */
|
|---|
| 734 | int res; /* Result of various function calls. */
|
|---|
| 735 |
|
|---|
| 736 | if (to_stdout_option)
|
|---|
| 737 | return;
|
|---|
| 738 |
|
|---|
| 739 | if (archive_format != arf_tar && archive_format != arf_ustar)
|
|---|
| 740 | {
|
|---|
| 741 | link_name = (char *) xmalloc ((unsigned int) file_hdr->c_filesize + 1);
|
|---|
| 742 | link_name[file_hdr->c_filesize] = '\0';
|
|---|
| 743 | tape_buffered_read (link_name, in_file_des, file_hdr->c_filesize);
|
|---|
| 744 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 745 | }
|
|---|
| 746 | else
|
|---|
| 747 | {
|
|---|
| 748 | link_name = xstrdup (file_hdr->c_tar_linkname);
|
|---|
| 749 | }
|
|---|
| 750 |
|
|---|
| 751 | res = UMASKED_SYMLINK (link_name, file_hdr->c_name,
|
|---|
| 752 | file_hdr->c_mode);
|
|---|
| 753 | if (res < 0 && create_dir_flag)
|
|---|
| 754 | {
|
|---|
| 755 | create_all_directories (file_hdr->c_name);
|
|---|
| 756 | res = UMASKED_SYMLINK (link_name, file_hdr->c_name,
|
|---|
| 757 | file_hdr->c_mode);
|
|---|
| 758 | }
|
|---|
| 759 | if (res < 0)
|
|---|
| 760 | {
|
|---|
| 761 | error (0, errno, _("%s: Cannot symlink to %s"),
|
|---|
| 762 | quotearg_colon (link_name), quote_n (1, file_hdr->c_name));
|
|---|
| 763 | free (link_name);
|
|---|
| 764 | return;
|
|---|
| 765 | }
|
|---|
| 766 | if (!no_chown_flag)
|
|---|
| 767 | {
|
|---|
| 768 | uid_t uid = set_owner_flag ? set_owner : file_hdr->c_uid;
|
|---|
| 769 | gid_t gid = set_group_flag ? set_group : file_hdr->c_gid;
|
|---|
| 770 | if ((lchown (file_hdr->c_name, uid, gid) < 0)
|
|---|
| 771 | && errno != EPERM)
|
|---|
| 772 | chown_error_details (file_hdr->c_name, uid, gid);
|
|---|
| 773 | }
|
|---|
| 774 | free (link_name);
|
|---|
| 775 | }
|
|---|
| 776 | |
|---|
| 777 |
|
|---|
| 778 | static void
|
|---|
| 779 | copyin_file (struct cpio_file_stat* file_hdr, int in_file_des)
|
|---|
| 780 | {
|
|---|
| 781 | int existing_dir;
|
|---|
| 782 |
|
|---|
| 783 | if (!to_stdout_option
|
|---|
| 784 | && try_existing_file (file_hdr, in_file_des, &existing_dir) < 0)
|
|---|
| 785 | return;
|
|---|
| 786 |
|
|---|
| 787 | /* Do the real copy or link. */
|
|---|
| 788 | switch (file_hdr->c_mode & CP_IFMT)
|
|---|
| 789 | {
|
|---|
| 790 | case CP_IFREG:
|
|---|
| 791 | copyin_regular_file(file_hdr, in_file_des);
|
|---|
| 792 | break;
|
|---|
| 793 |
|
|---|
| 794 | case CP_IFDIR:
|
|---|
| 795 | copyin_directory(file_hdr, existing_dir);
|
|---|
| 796 | break;
|
|---|
| 797 |
|
|---|
| 798 | case CP_IFCHR:
|
|---|
| 799 | case CP_IFBLK:
|
|---|
| 800 | #ifdef CP_IFSOCK
|
|---|
| 801 | case CP_IFSOCK:
|
|---|
| 802 | #endif
|
|---|
| 803 | #ifdef CP_IFIFO
|
|---|
| 804 | case CP_IFIFO:
|
|---|
| 805 | #endif
|
|---|
| 806 | copyin_device(file_hdr);
|
|---|
| 807 | break;
|
|---|
| 808 |
|
|---|
| 809 | #ifdef CP_IFLNK
|
|---|
| 810 | case CP_IFLNK:
|
|---|
| 811 | copyin_link(file_hdr, in_file_des);
|
|---|
| 812 | break;
|
|---|
| 813 | #endif
|
|---|
| 814 |
|
|---|
| 815 | default:
|
|---|
| 816 | error (0, 0, _("%s: unknown file type"), file_hdr->c_name);
|
|---|
| 817 | tape_toss_input (in_file_des, file_hdr->c_filesize);
|
|---|
| 818 | tape_skip_padding (in_file_des, file_hdr->c_filesize);
|
|---|
| 819 | }
|
|---|
| 820 | }
|
|---|
| 821 | |
|---|
| 822 |
|
|---|
| 823 |
|
|---|
| 824 | /* Current time for verbose table. */
|
|---|
| 825 | static time_t current_time;
|
|---|
| 826 |
|
|---|
| 827 |
|
|---|
| 828 | /* Print the file described by FILE_HDR in long format.
|
|---|
| 829 | If LINK_NAME is nonzero, it is the name of the file that
|
|---|
| 830 | this file is a symbolic link to. */
|
|---|
| 831 |
|
|---|
| 832 | void
|
|---|
| 833 | long_format (struct cpio_file_stat *file_hdr, char *link_name)
|
|---|
| 834 | {
|
|---|
| 835 | char mbuf[11];
|
|---|
| 836 | char tbuf[40];
|
|---|
| 837 | time_t when;
|
|---|
| 838 |
|
|---|
| 839 | mode_string (file_hdr->c_mode, mbuf);
|
|---|
| 840 | mbuf[10] = '\0';
|
|---|
| 841 |
|
|---|
| 842 | /* Get time values ready to print. */
|
|---|
| 843 | when = file_hdr->c_mtime;
|
|---|
| 844 | strcpy (tbuf, ctime (&when));
|
|---|
| 845 | if (current_time - when > 6L * 30L * 24L * 60L * 60L
|
|---|
| 846 | || current_time - when < 0L)
|
|---|
| 847 | {
|
|---|
| 848 | /* The file is older than 6 months, or in the future.
|
|---|
| 849 | Show the year instead of the time of day. */
|
|---|
| 850 | strcpy (tbuf + 11, tbuf + 19);
|
|---|
| 851 | }
|
|---|
| 852 | tbuf[16] = '\0';
|
|---|
| 853 |
|
|---|
| 854 | printf ("%s %3lu ", mbuf, file_hdr->c_nlink);
|
|---|
| 855 |
|
|---|
| 856 | if (numeric_uid)
|
|---|
| 857 | printf ("%-8u %-8u ", (unsigned int) file_hdr->c_uid,
|
|---|
| 858 | (unsigned int) file_hdr->c_gid);
|
|---|
| 859 | else
|
|---|
| 860 | printf ("%-8.8s %-8.8s ", getuser (file_hdr->c_uid),
|
|---|
| 861 | getgroup (file_hdr->c_gid));
|
|---|
| 862 |
|
|---|
| 863 | if ((file_hdr->c_mode & CP_IFMT) == CP_IFCHR
|
|---|
| 864 | || (file_hdr->c_mode & CP_IFMT) == CP_IFBLK)
|
|---|
| 865 | printf ("%3lu, %3lu ", file_hdr->c_rdev_maj,
|
|---|
| 866 | file_hdr->c_rdev_min);
|
|---|
| 867 | else
|
|---|
| 868 | printf ("%8lu ", file_hdr->c_filesize);
|
|---|
| 869 |
|
|---|
| 870 | printf ("%s ", tbuf + 4);
|
|---|
| 871 |
|
|---|
| 872 | print_name_with_quoting (file_hdr->c_name);
|
|---|
| 873 | if (link_name)
|
|---|
| 874 | {
|
|---|
| 875 | printf (" -> ");
|
|---|
| 876 | print_name_with_quoting (link_name);
|
|---|
| 877 | }
|
|---|
| 878 | putc ('\n', stdout);
|
|---|
| 879 | }
|
|---|
| 880 |
|
|---|
| 881 | void
|
|---|
| 882 | print_name_with_quoting (register char *p)
|
|---|
| 883 | {
|
|---|
| 884 | register unsigned char c;
|
|---|
| 885 |
|
|---|
| 886 | while ( (c = *p++) )
|
|---|
| 887 | {
|
|---|
| 888 | switch (c)
|
|---|
| 889 | {
|
|---|
| 890 | case '\\':
|
|---|
| 891 | printf ("\\\\");
|
|---|
| 892 | break;
|
|---|
| 893 |
|
|---|
| 894 | case '\n':
|
|---|
| 895 | printf ("\\n");
|
|---|
| 896 | break;
|
|---|
| 897 |
|
|---|
| 898 | case '\b':
|
|---|
| 899 | printf ("\\b");
|
|---|
| 900 | break;
|
|---|
| 901 |
|
|---|
| 902 | case '\r':
|
|---|
| 903 | printf ("\\r");
|
|---|
| 904 | break;
|
|---|
| 905 |
|
|---|
| 906 | case '\t':
|
|---|
| 907 | printf ("\\t");
|
|---|
| 908 | break;
|
|---|
| 909 |
|
|---|
| 910 | case '\f':
|
|---|
| 911 | printf ("\\f");
|
|---|
| 912 | break;
|
|---|
| 913 |
|
|---|
| 914 | case ' ':
|
|---|
| 915 | printf ("\\ ");
|
|---|
| 916 | break;
|
|---|
| 917 |
|
|---|
| 918 | case '"':
|
|---|
| 919 | printf ("\\\"");
|
|---|
| 920 | break;
|
|---|
| 921 |
|
|---|
| 922 | default:
|
|---|
| 923 | if (c > 040 && c < 0177)
|
|---|
| 924 | putchar (c);
|
|---|
| 925 | else
|
|---|
| 926 | printf ("\\%03o", (unsigned int) c);
|
|---|
| 927 | }
|
|---|
| 928 | }
|
|---|
| 929 | }
|
|---|
| 930 |
|
|---|
| 931 | /* Read a pattern file (for the -E option). Put a list of
|
|---|
| 932 | `num_patterns' elements in `save_patterns'. Any patterns that were
|
|---|
| 933 | already in `save_patterns' (from the command line) are preserved. */
|
|---|
| 934 |
|
|---|
| 935 | static void
|
|---|
| 936 | read_pattern_file ()
|
|---|
| 937 | {
|
|---|
| 938 | int max_new_patterns;
|
|---|
| 939 | char **new_save_patterns;
|
|---|
| 940 | int new_num_patterns;
|
|---|
| 941 | int i;
|
|---|
| 942 | dynamic_string pattern_name;
|
|---|
| 943 | FILE *pattern_fp;
|
|---|
| 944 |
|
|---|
| 945 | if (num_patterns < 0)
|
|---|
| 946 | num_patterns = 0;
|
|---|
| 947 | max_new_patterns = 1 + num_patterns;
|
|---|
| 948 | new_save_patterns = (char **) xmalloc (max_new_patterns * sizeof (char *));
|
|---|
| 949 | new_num_patterns = num_patterns;
|
|---|
| 950 | ds_init (&pattern_name, 128);
|
|---|
| 951 |
|
|---|
| 952 | pattern_fp = fopen (pattern_file_name, "r");
|
|---|
| 953 | if (pattern_fp == NULL)
|
|---|
| 954 | open_error (pattern_file_name);
|
|---|
| 955 | while (ds_fgetstr (pattern_fp, &pattern_name, '\n') != NULL)
|
|---|
| 956 | {
|
|---|
| 957 | if (new_num_patterns >= max_new_patterns)
|
|---|
| 958 | {
|
|---|
| 959 | max_new_patterns += 1;
|
|---|
| 960 | new_save_patterns = (char **)
|
|---|
| 961 | xrealloc ((char *) new_save_patterns,
|
|---|
| 962 | max_new_patterns * sizeof (char *));
|
|---|
| 963 | }
|
|---|
| 964 | new_save_patterns[new_num_patterns] = xstrdup (pattern_name.ds_string);
|
|---|
| 965 | ++new_num_patterns;
|
|---|
| 966 | }
|
|---|
| 967 | if (ferror (pattern_fp) || fclose (pattern_fp) == EOF)
|
|---|
| 968 | close_error (pattern_file_name);
|
|---|
| 969 |
|
|---|
| 970 | for (i = 0; i < num_patterns; ++i)
|
|---|
| 971 | new_save_patterns[i] = save_patterns[i];
|
|---|
| 972 |
|
|---|
| 973 | save_patterns = new_save_patterns;
|
|---|
| 974 | num_patterns = new_num_patterns;
|
|---|
| 975 | }
|
|---|
| 976 |
|
|---|
| 977 | |
|---|
| 978 |
|
|---|
| 979 | uintmax_t
|
|---|
| 980 | from_ascii (char const *where, size_t digs, unsigned logbase)
|
|---|
| 981 | {
|
|---|
| 982 | uintmax_t value = 0;
|
|---|
| 983 | char const *buf = where;
|
|---|
| 984 | char const *end = buf + digs;
|
|---|
| 985 | int overflow = 0;
|
|---|
| 986 | static char codetab[] = "0123456789ABCDEF";
|
|---|
| 987 |
|
|---|
| 988 | for (; *buf == ' '; buf++)
|
|---|
| 989 | {
|
|---|
| 990 | if (buf == end)
|
|---|
| 991 | return 0;
|
|---|
| 992 | }
|
|---|
| 993 |
|
|---|
| 994 | while (1)
|
|---|
| 995 | {
|
|---|
| 996 | unsigned d;
|
|---|
| 997 |
|
|---|
| 998 | char *p = strchr (codetab, toupper (*buf));
|
|---|
| 999 | if (!p)
|
|---|
| 1000 | {
|
|---|
| 1001 | error (0, 0, _("Malformed number %.*s"), digs, where);
|
|---|
| 1002 | break;
|
|---|
| 1003 | }
|
|---|
| 1004 |
|
|---|
| 1005 | d = p - codetab;
|
|---|
| 1006 | if ((d >> logbase) > 1)
|
|---|
| 1007 | {
|
|---|
| 1008 | error (0, 0, _("Malformed number %.*s"), digs, where);
|
|---|
| 1009 | break;
|
|---|
| 1010 | }
|
|---|
| 1011 | value += d;
|
|---|
| 1012 | if (++buf == end || *buf == 0)
|
|---|
| 1013 | break;
|
|---|
| 1014 | overflow |= value ^ (value << logbase >> logbase);
|
|---|
| 1015 | value <<= logbase;
|
|---|
| 1016 | }
|
|---|
| 1017 | if (overflow)
|
|---|
| 1018 | error (0, 0, _("Archive value %.*s is out of range"),
|
|---|
| 1019 | digs, where);
|
|---|
| 1020 | return value;
|
|---|
| 1021 | }
|
|---|
| 1022 |
|
|---|
| 1023 | |
|---|
| 1024 |
|
|---|
| 1025 |
|
|---|
| 1026 | /* Return 16-bit integer I with the bytes swapped. */
|
|---|
| 1027 | #define swab_short(i) ((((i) << 8) & 0xff00) | (((i) >> 8) & 0x00ff))
|
|---|
| 1028 |
|
|---|
| 1029 | /* Read the header, including the name of the file, from file
|
|---|
| 1030 | descriptor IN_DES into FILE_HDR. */
|
|---|
| 1031 |
|
|---|
| 1032 | void
|
|---|
| 1033 | read_in_header (struct cpio_file_stat *file_hdr, int in_des)
|
|---|
| 1034 | {
|
|---|
| 1035 | union {
|
|---|
| 1036 | char str[6];
|
|---|
| 1037 | unsigned short num;
|
|---|
| 1038 | struct old_cpio_header old_header;
|
|---|
| 1039 | } magic;
|
|---|
| 1040 | long bytes_skipped = 0; /* Bytes of junk found before magic number. */
|
|---|
| 1041 |
|
|---|
| 1042 | /* Search for a valid magic number. */
|
|---|
| 1043 |
|
|---|
| 1044 | if (archive_format == arf_unknown)
|
|---|
| 1045 | {
|
|---|
| 1046 | char tmpbuf[512];
|
|---|
| 1047 | int check_tar;
|
|---|
| 1048 | int peeked_bytes;
|
|---|
| 1049 |
|
|---|
| 1050 | while (archive_format == arf_unknown)
|
|---|
| 1051 | {
|
|---|
| 1052 | peeked_bytes = tape_buffered_peek (tmpbuf, in_des, 512);
|
|---|
| 1053 | if (peeked_bytes < 6)
|
|---|
| 1054 | error (1, 0, _("premature end of archive"));
|
|---|
| 1055 |
|
|---|
| 1056 | if (!strncmp (tmpbuf, "070701", 6))
|
|---|
| 1057 | archive_format = arf_newascii;
|
|---|
| 1058 | else if (!strncmp (tmpbuf, "070707", 6))
|
|---|
| 1059 | archive_format = arf_oldascii;
|
|---|
| 1060 | else if (!strncmp (tmpbuf, "070702", 6))
|
|---|
| 1061 | {
|
|---|
| 1062 | archive_format = arf_crcascii;
|
|---|
| 1063 | crc_i_flag = true;
|
|---|
| 1064 | }
|
|---|
| 1065 | else if ((*((unsigned short *) tmpbuf) == 070707) ||
|
|---|
| 1066 | (*((unsigned short *) tmpbuf) == swab_short ((unsigned short) 070707)))
|
|---|
| 1067 | archive_format = arf_binary;
|
|---|
| 1068 | else if (peeked_bytes >= 512
|
|---|
| 1069 | && (check_tar = is_tar_header (tmpbuf)))
|
|---|
| 1070 | {
|
|---|
| 1071 | if (check_tar == 2)
|
|---|
| 1072 | archive_format = arf_ustar;
|
|---|
| 1073 | else
|
|---|
| 1074 | archive_format = arf_tar;
|
|---|
| 1075 | }
|
|---|
| 1076 | else
|
|---|
| 1077 | {
|
|---|
| 1078 | tape_buffered_read ((char *) tmpbuf, in_des, 1L);
|
|---|
| 1079 | ++bytes_skipped;
|
|---|
| 1080 | }
|
|---|
| 1081 | }
|
|---|
| 1082 | }
|
|---|
| 1083 |
|
|---|
| 1084 | if (archive_format == arf_tar || archive_format == arf_ustar)
|
|---|
| 1085 | {
|
|---|
| 1086 | if (append_flag)
|
|---|
| 1087 | last_header_start = input_bytes - io_block_size +
|
|---|
| 1088 | (in_buff - input_buffer);
|
|---|
| 1089 | if (bytes_skipped > 0)
|
|---|
| 1090 | warn_junk_bytes (bytes_skipped);
|
|---|
| 1091 |
|
|---|
| 1092 | read_in_tar_header (file_hdr, in_des);
|
|---|
| 1093 | return;
|
|---|
| 1094 | }
|
|---|
| 1095 |
|
|---|
| 1096 | file_hdr->c_tar_linkname = NULL;
|
|---|
| 1097 |
|
|---|
| 1098 | tape_buffered_read (magic.str, in_des, 6L);
|
|---|
| 1099 | while (1)
|
|---|
| 1100 | {
|
|---|
| 1101 | if (append_flag)
|
|---|
| 1102 | last_header_start = input_bytes - io_block_size
|
|---|
| 1103 | + (in_buff - input_buffer) - 6;
|
|---|
| 1104 | if (archive_format == arf_newascii
|
|---|
| 1105 | && !strncmp (magic.str, "070701", 6))
|
|---|
| 1106 | {
|
|---|
| 1107 | if (bytes_skipped > 0)
|
|---|
| 1108 | warn_junk_bytes (bytes_skipped);
|
|---|
| 1109 | file_hdr->c_magic = 070701;
|
|---|
| 1110 | read_in_new_ascii (file_hdr, in_des);
|
|---|
| 1111 | break;
|
|---|
| 1112 | }
|
|---|
| 1113 | if (archive_format == arf_crcascii
|
|---|
| 1114 | && !strncmp (magic.str, "070702", 6))
|
|---|
| 1115 | {
|
|---|
| 1116 | if (bytes_skipped > 0)
|
|---|
| 1117 | warn_junk_bytes (bytes_skipped);
|
|---|
| 1118 | file_hdr->c_magic = 070702;
|
|---|
| 1119 | read_in_new_ascii (file_hdr, in_des);
|
|---|
| 1120 | break;
|
|---|
| 1121 | }
|
|---|
| 1122 | if ( (archive_format == arf_oldascii || archive_format == arf_hpoldascii)
|
|---|
| 1123 | && !strncmp (magic.str, "070707", 6))
|
|---|
| 1124 | {
|
|---|
| 1125 | if (bytes_skipped > 0)
|
|---|
| 1126 | warn_junk_bytes (bytes_skipped);
|
|---|
| 1127 | file_hdr->c_magic = 070707;
|
|---|
| 1128 | read_in_old_ascii (file_hdr, in_des);
|
|---|
| 1129 | break;
|
|---|
| 1130 | }
|
|---|
| 1131 | if ( (archive_format == arf_binary || archive_format == arf_hpbinary)
|
|---|
| 1132 | && (magic.num == 070707
|
|---|
| 1133 | || magic.num == swab_short ((unsigned short) 070707)))
|
|---|
| 1134 | {
|
|---|
| 1135 | /* Having to skip 1 byte because of word alignment is normal. */
|
|---|
| 1136 | if (bytes_skipped > 0)
|
|---|
| 1137 | warn_junk_bytes (bytes_skipped);
|
|---|
| 1138 | file_hdr->c_magic = 070707;
|
|---|
| 1139 | read_in_binary (file_hdr, &magic.old_header, in_des);
|
|---|
| 1140 | break;
|
|---|
| 1141 | }
|
|---|
| 1142 | bytes_skipped++;
|
|---|
| 1143 | memmove (magic.str, magic.str + 1, 5);
|
|---|
| 1144 | tape_buffered_read (magic.str, in_des, 1L);
|
|---|
| 1145 | }
|
|---|
| 1146 | }
|
|---|
| 1147 |
|
|---|
| 1148 | /* Fill in FILE_HDR by reading an old-format ASCII format cpio header from
|
|---|
| 1149 | file descriptor IN_DES, except for the magic number, which is
|
|---|
| 1150 | already filled in. */
|
|---|
| 1151 |
|
|---|
| 1152 | void
|
|---|
| 1153 | read_in_old_ascii (struct cpio_file_stat *file_hdr, int in_des)
|
|---|
| 1154 | {
|
|---|
| 1155 | struct old_ascii_header ascii_header;
|
|---|
| 1156 | unsigned long dev;
|
|---|
| 1157 |
|
|---|
| 1158 | tape_buffered_read (ascii_header.c_dev, in_des,
|
|---|
| 1159 | sizeof ascii_header - sizeof ascii_header.c_magic);
|
|---|
| 1160 | dev = FROM_OCTAL (ascii_header.c_dev);
|
|---|
| 1161 | file_hdr->c_dev_maj = major (dev);
|
|---|
| 1162 | file_hdr->c_dev_min = minor (dev);
|
|---|
| 1163 |
|
|---|
| 1164 | file_hdr->c_ino = FROM_OCTAL (ascii_header.c_ino);
|
|---|
| 1165 | file_hdr->c_mode = FROM_OCTAL (ascii_header.c_mode);
|
|---|
| 1166 | file_hdr->c_uid = FROM_OCTAL (ascii_header.c_uid);
|
|---|
| 1167 | file_hdr->c_gid = FROM_OCTAL (ascii_header.c_gid);
|
|---|
| 1168 | file_hdr->c_nlink = FROM_OCTAL (ascii_header.c_nlink);
|
|---|
| 1169 | dev = FROM_OCTAL (ascii_header.c_rdev);
|
|---|
| 1170 | file_hdr->c_rdev_maj = major (dev);
|
|---|
| 1171 | file_hdr->c_rdev_min = minor (dev);
|
|---|
| 1172 |
|
|---|
| 1173 | file_hdr->c_mtime = FROM_OCTAL (ascii_header.c_mtime);
|
|---|
| 1174 | file_hdr->c_namesize = FROM_OCTAL (ascii_header.c_namesize);
|
|---|
| 1175 | file_hdr->c_filesize = FROM_OCTAL (ascii_header.c_filesize);
|
|---|
| 1176 |
|
|---|
| 1177 | /* Read file name from input. */
|
|---|
| 1178 | if (file_hdr->c_name != NULL)
|
|---|
| 1179 | free (file_hdr->c_name);
|
|---|
| 1180 | file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize + 1);
|
|---|
| 1181 | tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
|
|---|
| 1182 |
|
|---|
| 1183 | /* HP/UX cpio creates archives that look just like ordinary archives,
|
|---|
| 1184 | but for devices it sets major = 0, minor = 1, and puts the
|
|---|
| 1185 | actual major/minor number in the filesize field. See if this
|
|---|
| 1186 | is an HP/UX cpio archive, and if so fix it. We have to do this
|
|---|
| 1187 | here because process_copy_in() assumes filesize is always 0
|
|---|
| 1188 | for devices. */
|
|---|
| 1189 | switch (file_hdr->c_mode & CP_IFMT)
|
|---|
| 1190 | {
|
|---|
| 1191 | case CP_IFCHR:
|
|---|
| 1192 | case CP_IFBLK:
|
|---|
| 1193 | #ifdef CP_IFSOCK
|
|---|
| 1194 | case CP_IFSOCK:
|
|---|
| 1195 | #endif
|
|---|
| 1196 | #ifdef CP_IFIFO
|
|---|
| 1197 | case CP_IFIFO:
|
|---|
| 1198 | #endif
|
|---|
| 1199 | if (file_hdr->c_filesize != 0
|
|---|
| 1200 | && file_hdr->c_rdev_maj == 0
|
|---|
| 1201 | && file_hdr->c_rdev_min == 1)
|
|---|
| 1202 | {
|
|---|
| 1203 | file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
|
|---|
| 1204 | file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
|
|---|
| 1205 | file_hdr->c_filesize = 0;
|
|---|
| 1206 | }
|
|---|
| 1207 | break;
|
|---|
| 1208 | default:
|
|---|
| 1209 | break;
|
|---|
| 1210 | }
|
|---|
| 1211 | }
|
|---|
| 1212 |
|
|---|
| 1213 | /* Fill in FILE_HDR by reading a new-format ASCII format cpio header from
|
|---|
| 1214 | file descriptor IN_DES, except for the magic number, which is
|
|---|
| 1215 | already filled in. */
|
|---|
| 1216 |
|
|---|
| 1217 | void
|
|---|
| 1218 | read_in_new_ascii (struct cpio_file_stat *file_hdr, int in_des)
|
|---|
| 1219 | {
|
|---|
| 1220 | struct new_ascii_header ascii_header;
|
|---|
| 1221 |
|
|---|
| 1222 | tape_buffered_read (ascii_header.c_ino, in_des,
|
|---|
| 1223 | sizeof ascii_header - sizeof ascii_header.c_magic);
|
|---|
| 1224 |
|
|---|
| 1225 | file_hdr->c_ino = FROM_HEX (ascii_header.c_ino);
|
|---|
| 1226 | file_hdr->c_mode = FROM_HEX (ascii_header.c_mode);
|
|---|
| 1227 | file_hdr->c_uid = FROM_HEX (ascii_header.c_uid);
|
|---|
| 1228 | file_hdr->c_gid = FROM_HEX (ascii_header.c_gid);
|
|---|
| 1229 | file_hdr->c_nlink = FROM_HEX (ascii_header.c_nlink);
|
|---|
| 1230 | file_hdr->c_mtime = FROM_HEX (ascii_header.c_mtime);
|
|---|
| 1231 | file_hdr->c_filesize = FROM_HEX (ascii_header.c_filesize);
|
|---|
| 1232 | file_hdr->c_dev_maj = FROM_HEX (ascii_header.c_dev_maj);
|
|---|
| 1233 | file_hdr->c_dev_min = FROM_HEX (ascii_header.c_dev_min);
|
|---|
| 1234 | file_hdr->c_rdev_maj = FROM_HEX (ascii_header.c_rdev_maj);
|
|---|
| 1235 | file_hdr->c_rdev_min = FROM_HEX (ascii_header.c_rdev_min);
|
|---|
| 1236 | file_hdr->c_namesize = FROM_HEX (ascii_header.c_namesize);
|
|---|
| 1237 | file_hdr->c_chksum = FROM_HEX (ascii_header.c_chksum);
|
|---|
| 1238 |
|
|---|
| 1239 | /* Read file name from input. */
|
|---|
| 1240 | if (file_hdr->c_name != NULL)
|
|---|
| 1241 | free (file_hdr->c_name);
|
|---|
| 1242 | file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
|
|---|
| 1243 | tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
|
|---|
| 1244 |
|
|---|
| 1245 | /* In SVR4 ASCII format, the amount of space allocated for the header
|
|---|
| 1246 | is rounded up to the next long-word, so we might need to drop
|
|---|
| 1247 | 1-3 bytes. */
|
|---|
| 1248 | tape_skip_padding (in_des, file_hdr->c_namesize + 110);
|
|---|
| 1249 | }
|
|---|
| 1250 |
|
|---|
| 1251 | /* Fill in FILE_HDR by reading a binary format cpio header from
|
|---|
| 1252 | file descriptor IN_DES, except for the first 6 bytes (the magic
|
|---|
| 1253 | number, device, and inode number), which are already filled in. */
|
|---|
| 1254 |
|
|---|
| 1255 | void
|
|---|
| 1256 | read_in_binary (struct cpio_file_stat *file_hdr,
|
|---|
| 1257 | struct old_cpio_header *short_hdr,
|
|---|
| 1258 | int in_des)
|
|---|
| 1259 | {
|
|---|
| 1260 | file_hdr->c_magic = short_hdr->c_magic;
|
|---|
| 1261 |
|
|---|
| 1262 | tape_buffered_read (((char *) short_hdr) + 6, in_des,
|
|---|
| 1263 | sizeof *short_hdr - 6 /* = 20 */);
|
|---|
| 1264 |
|
|---|
| 1265 | /* If the magic number is byte swapped, fix the header. */
|
|---|
| 1266 | if (file_hdr->c_magic == swab_short ((unsigned short) 070707))
|
|---|
| 1267 | {
|
|---|
| 1268 | static int warned = 0;
|
|---|
| 1269 |
|
|---|
| 1270 | /* Alert the user that they might have to do byte swapping on
|
|---|
| 1271 | the file contents. */
|
|---|
| 1272 | if (warned == 0)
|
|---|
| 1273 | {
|
|---|
| 1274 | error (0, 0, _("warning: archive header has reverse byte-order"));
|
|---|
| 1275 | warned = 1;
|
|---|
| 1276 | }
|
|---|
| 1277 | swab_array ((char *) &short_hdr, 13);
|
|---|
| 1278 | }
|
|---|
| 1279 |
|
|---|
| 1280 | file_hdr->c_dev_maj = major (short_hdr->c_dev);
|
|---|
| 1281 | file_hdr->c_dev_min = minor (short_hdr->c_dev);
|
|---|
| 1282 | file_hdr->c_ino = short_hdr->c_ino;
|
|---|
| 1283 | file_hdr->c_mode = short_hdr->c_mode;
|
|---|
| 1284 | file_hdr->c_uid = short_hdr->c_uid;
|
|---|
| 1285 | file_hdr->c_gid = short_hdr->c_gid;
|
|---|
| 1286 | file_hdr->c_nlink = short_hdr->c_nlink;
|
|---|
| 1287 | file_hdr->c_rdev_maj = major (short_hdr->c_rdev);
|
|---|
| 1288 | file_hdr->c_rdev_min = minor (short_hdr->c_rdev);
|
|---|
| 1289 | file_hdr->c_mtime = (unsigned long) short_hdr->c_mtimes[0] << 16
|
|---|
| 1290 | | short_hdr->c_mtimes[1];
|
|---|
| 1291 |
|
|---|
| 1292 | file_hdr->c_namesize = short_hdr->c_namesize;
|
|---|
| 1293 | file_hdr->c_filesize = (unsigned long) short_hdr->c_filesizes[0] << 16
|
|---|
| 1294 | | short_hdr->c_filesizes[1];
|
|---|
| 1295 |
|
|---|
| 1296 | /* Read file name from input. */
|
|---|
| 1297 | if (file_hdr->c_name != NULL)
|
|---|
| 1298 | free (file_hdr->c_name);
|
|---|
| 1299 | file_hdr->c_name = (char *) xmalloc (file_hdr->c_namesize);
|
|---|
| 1300 | tape_buffered_read (file_hdr->c_name, in_des, (long) file_hdr->c_namesize);
|
|---|
| 1301 |
|
|---|
| 1302 | /* In binary mode, the amount of space allocated in the header for
|
|---|
| 1303 | the filename is `c_namesize' rounded up to the next short-word,
|
|---|
| 1304 | so we might need to drop a byte. */
|
|---|
| 1305 | if (file_hdr->c_namesize % 2)
|
|---|
| 1306 | tape_toss_input (in_des, 1L);
|
|---|
| 1307 |
|
|---|
| 1308 | /* HP/UX cpio creates archives that look just like ordinary archives,
|
|---|
| 1309 | but for devices it sets major = 0, minor = 1, and puts the
|
|---|
| 1310 | actual major/minor number in the filesize field. See if this
|
|---|
| 1311 | is an HP/UX cpio archive, and if so fix it. We have to do this
|
|---|
| 1312 | here because process_copy_in() assumes filesize is always 0
|
|---|
| 1313 | for devices. */
|
|---|
| 1314 | switch (file_hdr->c_mode & CP_IFMT)
|
|---|
| 1315 | {
|
|---|
| 1316 | case CP_IFCHR:
|
|---|
| 1317 | case CP_IFBLK:
|
|---|
| 1318 | #ifdef CP_IFSOCK
|
|---|
| 1319 | case CP_IFSOCK:
|
|---|
| 1320 | #endif
|
|---|
| 1321 | #ifdef CP_IFIFO
|
|---|
| 1322 | case CP_IFIFO:
|
|---|
| 1323 | #endif
|
|---|
| 1324 | if (file_hdr->c_filesize != 0
|
|---|
| 1325 | && file_hdr->c_rdev_maj == 0
|
|---|
| 1326 | && file_hdr->c_rdev_min == 1)
|
|---|
| 1327 | {
|
|---|
| 1328 | file_hdr->c_rdev_maj = major (file_hdr->c_filesize);
|
|---|
| 1329 | file_hdr->c_rdev_min = minor (file_hdr->c_filesize);
|
|---|
| 1330 | file_hdr->c_filesize = 0;
|
|---|
| 1331 | }
|
|---|
| 1332 | break;
|
|---|
| 1333 | default:
|
|---|
| 1334 | break;
|
|---|
| 1335 | }
|
|---|
| 1336 | }
|
|---|
| 1337 |
|
|---|
| 1338 | /* Exchange the bytes of each element of the array of COUNT shorts
|
|---|
| 1339 | starting at PTR. */
|
|---|
| 1340 |
|
|---|
| 1341 | void
|
|---|
| 1342 | swab_array (char *ptr, int count)
|
|---|
| 1343 | {
|
|---|
| 1344 | char tmp;
|
|---|
| 1345 |
|
|---|
| 1346 | while (count-- > 0)
|
|---|
| 1347 | {
|
|---|
| 1348 | tmp = *ptr;
|
|---|
| 1349 | *ptr = *(ptr + 1);
|
|---|
| 1350 | ++ptr;
|
|---|
| 1351 | *ptr = tmp;
|
|---|
| 1352 | ++ptr;
|
|---|
| 1353 | }
|
|---|
| 1354 | }
|
|---|
| 1355 |
|
|---|
| 1356 | /* Read the collection from standard input and create files
|
|---|
| 1357 | in the file system. */
|
|---|
| 1358 |
|
|---|
| 1359 | void
|
|---|
| 1360 | process_copy_in ()
|
|---|
| 1361 | {
|
|---|
| 1362 | char done = false; /* True if trailer reached. */
|
|---|
| 1363 | FILE *tty_in = NULL; /* Interactive file for rename option. */
|
|---|
| 1364 | FILE *tty_out = NULL; /* Interactive file for rename option. */
|
|---|
| 1365 | FILE *rename_in = NULL; /* Batch file for rename option. */
|
|---|
| 1366 | struct stat file_stat; /* Output file stat record. */
|
|---|
| 1367 | struct cpio_file_stat file_hdr; /* Output header information. */
|
|---|
| 1368 | int in_file_des; /* Input file descriptor. */
|
|---|
| 1369 | char skip_file; /* Flag for use with patterns. */
|
|---|
| 1370 | int i; /* Loop index variable. */
|
|---|
| 1371 |
|
|---|
| 1372 | umask (0); /* Reset umask to preserve modes of
|
|---|
| 1373 | created files */
|
|---|
| 1374 |
|
|---|
| 1375 | /* Initialize the copy in. */
|
|---|
| 1376 | if (pattern_file_name)
|
|---|
| 1377 | {
|
|---|
| 1378 | read_pattern_file ();
|
|---|
| 1379 | }
|
|---|
| 1380 | file_hdr.c_name = NULL;
|
|---|
| 1381 |
|
|---|
| 1382 | if (rename_batch_file)
|
|---|
| 1383 | {
|
|---|
| 1384 | rename_in = fopen (rename_batch_file, "r");
|
|---|
| 1385 | if (rename_in == NULL)
|
|---|
| 1386 | {
|
|---|
| 1387 | error (2, errno, TTY_NAME);
|
|---|
| 1388 | }
|
|---|
| 1389 | }
|
|---|
| 1390 | else if (rename_flag)
|
|---|
| 1391 | {
|
|---|
| 1392 | /* Open interactive file pair for rename operation. */
|
|---|
| 1393 | tty_in = fopen (TTY_NAME, "r");
|
|---|
| 1394 | if (tty_in == NULL)
|
|---|
| 1395 | {
|
|---|
| 1396 | error (2, errno, TTY_NAME);
|
|---|
| 1397 | }
|
|---|
| 1398 | tty_out = fopen (TTY_NAME, "w");
|
|---|
| 1399 | if (tty_out == NULL)
|
|---|
| 1400 | {
|
|---|
| 1401 | error (2, errno, TTY_NAME);
|
|---|
| 1402 | }
|
|---|
| 1403 | }
|
|---|
| 1404 |
|
|---|
| 1405 | /* Get date and time if needed for processing the table option. */
|
|---|
| 1406 | if (table_flag && verbose_flag)
|
|---|
| 1407 | {
|
|---|
| 1408 | time (¤t_time);
|
|---|
| 1409 | }
|
|---|
| 1410 |
|
|---|
| 1411 | /* Check whether the input file might be a tape. */
|
|---|
| 1412 | in_file_des = archive_des;
|
|---|
| 1413 | if (_isrmt (in_file_des))
|
|---|
| 1414 | {
|
|---|
| 1415 | input_is_special = 1;
|
|---|
| 1416 | input_is_seekable = 0;
|
|---|
| 1417 | }
|
|---|
| 1418 | else
|
|---|
| 1419 | {
|
|---|
| 1420 | if (fstat (in_file_des, &file_stat))
|
|---|
| 1421 | error (1, errno, _("standard input is closed"));
|
|---|
| 1422 | input_is_special =
|
|---|
| 1423 | #ifdef S_ISBLK
|
|---|
| 1424 | S_ISBLK (file_stat.st_mode) ||
|
|---|
| 1425 | #endif
|
|---|
| 1426 | S_ISCHR (file_stat.st_mode);
|
|---|
| 1427 | input_is_seekable = S_ISREG (file_stat.st_mode);
|
|---|
| 1428 | }
|
|---|
| 1429 | output_is_seekable = true;
|
|---|
| 1430 |
|
|---|
| 1431 | /* While there is more input in the collection, process the input. */
|
|---|
| 1432 | while (!done)
|
|---|
| 1433 | {
|
|---|
| 1434 | swapping_halfwords = swapping_bytes = false;
|
|---|
| 1435 |
|
|---|
| 1436 | /* Start processing the next file by reading the header. */
|
|---|
| 1437 | read_in_header (&file_hdr, in_file_des);
|
|---|
| 1438 |
|
|---|
| 1439 | #ifdef DEBUG_CPIO
|
|---|
| 1440 | if (debug_flag)
|
|---|
| 1441 | {
|
|---|
| 1442 | struct cpio_file_stat *h;
|
|---|
| 1443 | h = &file_hdr;
|
|---|
| 1444 | fprintf (stderr,
|
|---|
| 1445 | "magic = 0%o, ino = %d, mode = 0%o, uid = %d, gid = %d\n",
|
|---|
| 1446 | h->c_magic, h->c_ino, h->c_mode, h->c_uid, h->c_gid);
|
|---|
| 1447 | fprintf (stderr,
|
|---|
| 1448 | "nlink = %d, mtime = %d, filesize = %d, dev_maj = 0x%x\n",
|
|---|
| 1449 | h->c_nlink, h->c_mtime, h->c_filesize, h->c_dev_maj);
|
|---|
| 1450 | fprintf (stderr,
|
|---|
| 1451 | "dev_min = 0x%x, rdev_maj = 0x%x, rdev_min = 0x%x, namesize = %d\n",
|
|---|
| 1452 | h->c_dev_min, h->c_rdev_maj, h->c_rdev_min, h->c_namesize);
|
|---|
| 1453 | fprintf (stderr,
|
|---|
| 1454 | "chksum = %d, name = \"%s\", tar_linkname = \"%s\"\n",
|
|---|
| 1455 | h->c_chksum, h->c_name,
|
|---|
| 1456 | h->c_tar_linkname ? h->c_tar_linkname : "(null)" );
|
|---|
| 1457 |
|
|---|
| 1458 | }
|
|---|
| 1459 | #endif
|
|---|
| 1460 | /* Is this the header for the TRAILER file? */
|
|---|
| 1461 | if (strcmp (CPIO_TRAILER_NAME, file_hdr.c_name) == 0)
|
|---|
| 1462 | {
|
|---|
| 1463 | done = true;
|
|---|
| 1464 | break;
|
|---|
| 1465 | }
|
|---|
| 1466 |
|
|---|
| 1467 | cpio_safer_name_suffix (file_hdr.c_name, false, !no_abs_paths_flag,
|
|---|
| 1468 | false);
|
|---|
| 1469 |
|
|---|
| 1470 | /* Does the file name match one of the given patterns? */
|
|---|
| 1471 | if (num_patterns <= 0)
|
|---|
| 1472 | skip_file = false;
|
|---|
| 1473 | else
|
|---|
| 1474 | {
|
|---|
| 1475 | skip_file = copy_matching_files;
|
|---|
| 1476 | for (i = 0; i < num_patterns
|
|---|
| 1477 | && skip_file == copy_matching_files; i++)
|
|---|
| 1478 | {
|
|---|
| 1479 | if (fnmatch (save_patterns[i], file_hdr.c_name, 0) == 0)
|
|---|
| 1480 | skip_file = !copy_matching_files;
|
|---|
| 1481 | }
|
|---|
| 1482 | }
|
|---|
| 1483 |
|
|---|
| 1484 | if (skip_file)
|
|---|
| 1485 | {
|
|---|
| 1486 | /* If we're skipping a file with links, there might be other
|
|---|
| 1487 | links that we didn't skip, and this file might have the
|
|---|
| 1488 | data for the links. If it does, we'll copy in the data
|
|---|
| 1489 | to the links, but not to this file. */
|
|---|
| 1490 | if (file_hdr.c_nlink > 1 && (archive_format == arf_newascii
|
|---|
| 1491 | || archive_format == arf_crcascii) )
|
|---|
| 1492 | {
|
|---|
| 1493 | if (create_defered_links_to_skipped(&file_hdr, in_file_des) < 0)
|
|---|
| 1494 | {
|
|---|
| 1495 | tape_toss_input (in_file_des, file_hdr.c_filesize);
|
|---|
| 1496 | tape_skip_padding (in_file_des, file_hdr.c_filesize);
|
|---|
| 1497 | }
|
|---|
| 1498 | }
|
|---|
| 1499 | else
|
|---|
| 1500 | {
|
|---|
| 1501 | tape_toss_input (in_file_des, file_hdr.c_filesize);
|
|---|
| 1502 | tape_skip_padding (in_file_des, file_hdr.c_filesize);
|
|---|
| 1503 | }
|
|---|
| 1504 | }
|
|---|
| 1505 | else if (table_flag)
|
|---|
| 1506 | {
|
|---|
| 1507 | list_file(&file_hdr, in_file_des);
|
|---|
| 1508 | }
|
|---|
| 1509 | else if (append_flag)
|
|---|
| 1510 | {
|
|---|
| 1511 | tape_toss_input (in_file_des, file_hdr.c_filesize);
|
|---|
| 1512 | tape_skip_padding (in_file_des, file_hdr.c_filesize);
|
|---|
| 1513 | }
|
|---|
| 1514 | else if (only_verify_crc_flag)
|
|---|
| 1515 | {
|
|---|
| 1516 | #ifdef CP_IFLNK
|
|---|
| 1517 | if ((file_hdr.c_mode & CP_IFMT) == CP_IFLNK)
|
|---|
| 1518 | {
|
|---|
| 1519 | if (archive_format != arf_tar && archive_format != arf_ustar)
|
|---|
| 1520 | {
|
|---|
| 1521 | tape_toss_input (in_file_des, file_hdr.c_filesize);
|
|---|
| 1522 | tape_skip_padding (in_file_des, file_hdr.c_filesize);
|
|---|
| 1523 | continue;
|
|---|
| 1524 | }
|
|---|
| 1525 | }
|
|---|
| 1526 | #endif
|
|---|
| 1527 | crc = 0;
|
|---|
| 1528 | tape_toss_input (in_file_des, file_hdr.c_filesize);
|
|---|
| 1529 | tape_skip_padding (in_file_des, file_hdr.c_filesize);
|
|---|
| 1530 | if (crc != file_hdr.c_chksum)
|
|---|
| 1531 | {
|
|---|
| 1532 | error (0, 0, _("%s: checksum error (0x%lx, should be 0x%lx)"),
|
|---|
| 1533 | file_hdr.c_name, crc, file_hdr.c_chksum);
|
|---|
| 1534 | }
|
|---|
| 1535 | /* Debian hack: -v and -V now work with --only-verify-crc.
|
|---|
| 1536 | (99/11/10) -BEM */
|
|---|
| 1537 | if (verbose_flag)
|
|---|
| 1538 | {
|
|---|
| 1539 | fprintf (stderr, "%s\n", file_hdr.c_name);
|
|---|
| 1540 | }
|
|---|
| 1541 | if (dot_flag)
|
|---|
| 1542 | {
|
|---|
| 1543 | fputc ('.', stderr);
|
|---|
| 1544 | }
|
|---|
| 1545 | }
|
|---|
| 1546 | else
|
|---|
| 1547 | {
|
|---|
| 1548 | /* Copy the input file into the directory structure. */
|
|---|
| 1549 |
|
|---|
| 1550 | /* Do we need to rename the file? */
|
|---|
| 1551 | if (rename_flag || rename_batch_file)
|
|---|
| 1552 | {
|
|---|
| 1553 | if (query_rename(&file_hdr, tty_in, tty_out, rename_in) < 0)
|
|---|
| 1554 | {
|
|---|
| 1555 | tape_toss_input (in_file_des, file_hdr.c_filesize);
|
|---|
| 1556 | tape_skip_padding (in_file_des, file_hdr.c_filesize);
|
|---|
| 1557 | continue;
|
|---|
| 1558 | }
|
|---|
| 1559 | }
|
|---|
| 1560 |
|
|---|
| 1561 | copyin_file(&file_hdr, in_file_des);
|
|---|
| 1562 |
|
|---|
| 1563 | if (verbose_flag)
|
|---|
| 1564 | fprintf (stderr, "%s\n", file_hdr.c_name);
|
|---|
| 1565 | if (dot_flag)
|
|---|
| 1566 | fputc ('.', stderr);
|
|---|
| 1567 | }
|
|---|
| 1568 | }
|
|---|
| 1569 |
|
|---|
| 1570 | if (dot_flag)
|
|---|
| 1571 | fputc ('\n', stderr);
|
|---|
| 1572 |
|
|---|
| 1573 | if (append_flag)
|
|---|
| 1574 | return;
|
|---|
| 1575 |
|
|---|
| 1576 | if (archive_format == arf_newascii || archive_format == arf_crcascii)
|
|---|
| 1577 | {
|
|---|
| 1578 | create_final_defers ();
|
|---|
| 1579 | }
|
|---|
| 1580 | if (!quiet_flag)
|
|---|
| 1581 | {
|
|---|
| 1582 | int blocks;
|
|---|
| 1583 | blocks = (input_bytes + io_block_size - 1) / io_block_size;
|
|---|
| 1584 | fprintf (stderr, ngettext ("%d block\n", "%d blocks\n", blocks), blocks);
|
|---|
| 1585 | }
|
|---|
| 1586 | }
|
|---|
| 1587 |
|
|---|