1 /*- 2 * Copyright (c) 2003-2010 Tim Kientzle 3 * Copyright (c) 2012 Michihiro NAKAJIMA 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "archive_platform.h" 29 __FBSDID("$FreeBSD$"); 30 31 #if !defined(_WIN32) || defined(__CYGWIN__) 32 33 #ifdef HAVE_SYS_TYPES_H 34 #include <sys/types.h> 35 #endif 36 #ifdef HAVE_SYS_ACL_H 37 #include <sys/acl.h> 38 #endif 39 #ifdef HAVE_SYS_EXTATTR_H 40 #include <sys/extattr.h> 41 #endif 42 #if HAVE_SYS_XATTR_H 43 #include <sys/xattr.h> 44 #elif HAVE_ATTR_XATTR_H 45 #include <attr/xattr.h> 46 #endif 47 #ifdef HAVE_SYS_EA_H 48 #include <sys/ea.h> 49 #endif 50 #ifdef HAVE_SYS_IOCTL_H 51 #include <sys/ioctl.h> 52 #endif 53 #ifdef HAVE_SYS_STAT_H 54 #include <sys/stat.h> 55 #endif 56 #ifdef HAVE_SYS_TIME_H 57 #include <sys/time.h> 58 #endif 59 #ifdef HAVE_SYS_UTIME_H 60 #include <sys/utime.h> 61 #endif 62 #ifdef HAVE_COPYFILE_H 63 #include <copyfile.h> 64 #endif 65 #ifdef HAVE_ERRNO_H 66 #include <errno.h> 67 #endif 68 #ifdef HAVE_FCNTL_H 69 #include <fcntl.h> 70 #endif 71 #ifdef HAVE_GRP_H 72 #include <grp.h> 73 #endif 74 #ifdef HAVE_LANGINFO_H 75 #include <langinfo.h> 76 #endif 77 #ifdef HAVE_LINUX_FS_H 78 #include <linux/fs.h> /* for Linux file flags */ 79 #endif 80 /* 81 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h. 82 * As the include guards don't agree, the order of include is important. 83 */ 84 #ifdef HAVE_LINUX_EXT2_FS_H 85 #include <linux/ext2_fs.h> /* for Linux file flags */ 86 #endif 87 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__) 88 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */ 89 #endif 90 #ifdef HAVE_LIMITS_H 91 #include <limits.h> 92 #endif 93 #ifdef HAVE_PWD_H 94 #include <pwd.h> 95 #endif 96 #include <stdio.h> 97 #ifdef HAVE_STDLIB_H 98 #include <stdlib.h> 99 #endif 100 #ifdef HAVE_STRING_H 101 #include <string.h> 102 #endif 103 #ifdef HAVE_UNISTD_H 104 #include <unistd.h> 105 #endif 106 #ifdef HAVE_UTIME_H 107 #include <utime.h> 108 #endif 109 #ifdef F_GETTIMES /* Tru64 specific */ 110 #include <sys/fcntl1.h> 111 #endif 112 113 /* 114 * Macro to cast st_mtime and time_t to an int64 so that 2 numbers can reliably be compared. 115 * 116 * It assumes that the input is an integer type of no more than 64 bits. 117 * If the number is less than zero, t must be a signed type, so it fits in 118 * int64_t. Otherwise, it's a nonnegative value so we can cast it to uint64_t 119 * without loss. But it could be a large unsigned value, so we have to clip it 120 * to INT64_MAX.* 121 */ 122 #define to_int64_time(t) \ 123 ((t) < 0 ? (int64_t)(t) : (uint64_t)(t) > (uint64_t)INT64_MAX ? INT64_MAX : (int64_t)(t)) 124 125 #if __APPLE__ 126 #include <TargetConditionals.h> 127 #if TARGET_OS_MAC && !TARGET_OS_EMBEDDED && HAVE_QUARANTINE_H 128 #include <quarantine.h> 129 #define HAVE_QUARANTINE 1 130 #endif 131 #endif 132 133 #ifdef HAVE_ZLIB_H 134 #include <zlib.h> 135 #endif 136 137 /* TODO: Support Mac OS 'quarantine' feature. This is really just a 138 * standard tag to mark files that have been downloaded as "tainted". 139 * On Mac OS, we should mark the extracted files as tainted if the 140 * archive being read was tainted. Windows has a similar feature; we 141 * should investigate ways to support this generically. */ 142 143 #include "archive.h" 144 #include "archive_acl_private.h" 145 #include "archive_string.h" 146 #include "archive_endian.h" 147 #include "archive_entry.h" 148 #include "archive_private.h" 149 #include "archive_write_disk_private.h" 150 151 #ifndef O_BINARY 152 #define O_BINARY 0 153 #endif 154 #ifndef O_CLOEXEC 155 #define O_CLOEXEC 0 156 #endif 157 158 /* Ignore non-int O_NOFOLLOW constant. */ 159 /* gnulib's fcntl.h does this on AIX, but it seems practical everywhere */ 160 #if defined O_NOFOLLOW && !(INT_MIN <= O_NOFOLLOW && O_NOFOLLOW <= INT_MAX) 161 #undef O_NOFOLLOW 162 #endif 163 164 #ifndef O_NOFOLLOW 165 #define O_NOFOLLOW 0 166 #endif 167 168 struct fixup_entry { 169 struct fixup_entry *next; 170 struct archive_acl acl; 171 mode_t mode; 172 int64_t atime; 173 int64_t birthtime; 174 int64_t mtime; 175 int64_t ctime; 176 unsigned long atime_nanos; 177 unsigned long birthtime_nanos; 178 unsigned long mtime_nanos; 179 unsigned long ctime_nanos; 180 unsigned long fflags_set; 181 size_t mac_metadata_size; 182 void *mac_metadata; 183 int fixup; /* bitmask of what needs fixing */ 184 char *name; 185 }; 186 187 /* 188 * We use a bitmask to track which operations remain to be done for 189 * this file. In particular, this helps us avoid unnecessary 190 * operations when it's possible to take care of one step as a 191 * side-effect of another. For example, mkdir() can specify the mode 192 * for the newly-created object but symlink() cannot. This means we 193 * can skip chmod() if mkdir() succeeded, but we must explicitly 194 * chmod() if we're trying to create a directory that already exists 195 * (mkdir() failed) or if we're restoring a symlink. Similarly, we 196 * need to verify UID/GID before trying to restore SUID/SGID bits; 197 * that verification can occur explicitly through a stat() call or 198 * implicitly because of a successful chown() call. 199 */ 200 #define TODO_MODE_FORCE 0x40000000 201 #define TODO_MODE_BASE 0x20000000 202 #define TODO_SUID 0x10000000 203 #define TODO_SUID_CHECK 0x08000000 204 #define TODO_SGID 0x04000000 205 #define TODO_SGID_CHECK 0x02000000 206 #define TODO_APPLEDOUBLE 0x01000000 207 #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID) 208 #define TODO_TIMES ARCHIVE_EXTRACT_TIME 209 #define TODO_OWNER ARCHIVE_EXTRACT_OWNER 210 #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS 211 #define TODO_ACLS ARCHIVE_EXTRACT_ACL 212 #define TODO_XATTR ARCHIVE_EXTRACT_XATTR 213 #define TODO_MAC_METADATA ARCHIVE_EXTRACT_MAC_METADATA 214 #define TODO_HFS_COMPRESSION ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED 215 216 struct archive_write_disk { 217 struct archive archive; 218 219 mode_t user_umask; 220 struct fixup_entry *fixup_list; 221 struct fixup_entry *current_fixup; 222 int64_t user_uid; 223 int skip_file_set; 224 int64_t skip_file_dev; 225 int64_t skip_file_ino; 226 time_t start_time; 227 228 int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid); 229 void (*cleanup_gid)(void *private); 230 void *lookup_gid_data; 231 int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid); 232 void (*cleanup_uid)(void *private); 233 void *lookup_uid_data; 234 235 /* 236 * Full path of last file to satisfy symlink checks. 237 */ 238 struct archive_string path_safe; 239 240 /* 241 * Cached stat data from disk for the current entry. 242 * If this is valid, pst points to st. Otherwise, 243 * pst is null. 244 */ 245 struct stat st; 246 struct stat *pst; 247 248 /* Information about the object being restored right now. */ 249 struct archive_entry *entry; /* Entry being extracted. */ 250 char *name; /* Name of entry, possibly edited. */ 251 struct archive_string _name_data; /* backing store for 'name' */ 252 /* Tasks remaining for this object. */ 253 int todo; 254 /* Tasks deferred until end-of-archive. */ 255 int deferred; 256 /* Options requested by the client. */ 257 int flags; 258 /* Handle for the file we're restoring. */ 259 int fd; 260 /* Current offset for writing data to the file. */ 261 int64_t offset; 262 /* Last offset actually written to disk. */ 263 int64_t fd_offset; 264 /* Total bytes actually written to files. */ 265 int64_t total_bytes_written; 266 /* Maximum size of file, -1 if unknown. */ 267 int64_t filesize; 268 /* Dir we were in before this restore; only for deep paths. */ 269 int restore_pwd; 270 /* Mode we should use for this entry; affected by _PERM and umask. */ 271 mode_t mode; 272 /* UID/GID to use in restoring this entry. */ 273 int64_t uid; 274 int64_t gid; 275 /* 276 * HFS+ Compression. 277 */ 278 /* Xattr "com.apple.decmpfs". */ 279 uint32_t decmpfs_attr_size; 280 unsigned char *decmpfs_header_p; 281 /* ResourceFork set options used for fsetxattr. */ 282 int rsrc_xattr_options; 283 /* Xattr "com.apple.ResourceFork". */ 284 unsigned char *resource_fork; 285 size_t resource_fork_allocated_size; 286 unsigned int decmpfs_block_count; 287 uint32_t *decmpfs_block_info; 288 /* Buffer for compressed data. */ 289 unsigned char *compressed_buffer; 290 size_t compressed_buffer_size; 291 size_t compressed_buffer_remaining; 292 /* The offset of the ResourceFork where compressed data will 293 * be placed. */ 294 uint32_t compressed_rsrc_position; 295 uint32_t compressed_rsrc_position_v; 296 /* Buffer for uncompressed data. */ 297 char *uncompressed_buffer; 298 size_t block_remaining_bytes; 299 size_t file_remaining_bytes; 300 #ifdef HAVE_ZLIB_H 301 z_stream stream; 302 int stream_valid; 303 int decmpfs_compression_level; 304 #endif 305 }; 306 307 /* 308 * Default mode for dirs created automatically (will be modified by umask). 309 * Note that POSIX specifies 0777 for implicitly-created dirs, "modified 310 * by the process' file creation mask." 311 */ 312 #define DEFAULT_DIR_MODE 0777 313 /* 314 * Dir modes are restored in two steps: During the extraction, the permissions 315 * in the archive are modified to match the following limits. During 316 * the post-extract fixup pass, the permissions from the archive are 317 * applied. 318 */ 319 #define MINIMUM_DIR_MODE 0700 320 #define MAXIMUM_DIR_MODE 0775 321 322 /* 323 * Maximum uncompressed size of a decmpfs block. 324 */ 325 #define MAX_DECMPFS_BLOCK_SIZE (64 * 1024) 326 /* 327 * HFS+ compression type. 328 */ 329 #define CMP_XATTR 3/* Compressed data in xattr. */ 330 #define CMP_RESOURCE_FORK 4/* Compressed data in resource fork. */ 331 /* 332 * HFS+ compression resource fork. 333 */ 334 #define RSRC_H_SIZE 260 /* Base size of Resource fork header. */ 335 #define RSRC_F_SIZE 50 /* Size of Resource fork footer. */ 336 /* Size to write compressed data to resource fork. */ 337 #define COMPRESSED_W_SIZE (64 * 1024) 338 /* decmpfs definitions. */ 339 #define MAX_DECMPFS_XATTR_SIZE 3802 340 #ifndef DECMPFS_XATTR_NAME 341 #define DECMPFS_XATTR_NAME "com.apple.decmpfs" 342 #endif 343 #define DECMPFS_MAGIC 0x636d7066 344 #define DECMPFS_COMPRESSION_MAGIC 0 345 #define DECMPFS_COMPRESSION_TYPE 4 346 #define DECMPFS_UNCOMPRESSED_SIZE 8 347 #define DECMPFS_HEADER_SIZE 16 348 349 #define HFS_BLOCKS(s) ((s) >> 12) 350 351 static void fsobj_error(int *, struct archive_string *, int, const char *, 352 const char *); 353 static int check_symlinks_fsobj(char *, int *, struct archive_string *, 354 int); 355 static int check_symlinks(struct archive_write_disk *); 356 static int create_filesystem_object(struct archive_write_disk *); 357 static struct fixup_entry *current_fixup(struct archive_write_disk *, 358 const char *pathname); 359 #if defined(HAVE_FCHDIR) && defined(PATH_MAX) 360 static void edit_deep_directories(struct archive_write_disk *ad); 361 #endif 362 static int cleanup_pathname_fsobj(char *, int *, struct archive_string *, 363 int); 364 static int cleanup_pathname(struct archive_write_disk *); 365 static int create_dir(struct archive_write_disk *, char *); 366 static int create_parent_dir(struct archive_write_disk *, char *); 367 static ssize_t hfs_write_data_block(struct archive_write_disk *, 368 const char *, size_t); 369 static int fixup_appledouble(struct archive_write_disk *, const char *); 370 static int older(struct stat *, struct archive_entry *); 371 static int restore_entry(struct archive_write_disk *); 372 static int set_mac_metadata(struct archive_write_disk *, const char *, 373 const void *, size_t); 374 static int set_xattrs(struct archive_write_disk *); 375 static int clear_nochange_fflags(struct archive_write_disk *); 376 static int set_fflags(struct archive_write_disk *); 377 static int set_fflags_platform(struct archive_write_disk *, int fd, 378 const char *name, mode_t mode, 379 unsigned long fflags_set, unsigned long fflags_clear); 380 static int set_ownership(struct archive_write_disk *); 381 static int set_mode(struct archive_write_disk *, int mode); 382 static int set_time(int, int, const char *, time_t, long, time_t, long); 383 static int set_times(struct archive_write_disk *, int, int, const char *, 384 time_t, long, time_t, long, time_t, long, time_t, long); 385 static int set_times_from_entry(struct archive_write_disk *); 386 static struct fixup_entry *sort_dir_list(struct fixup_entry *p); 387 static ssize_t write_data_block(struct archive_write_disk *, 388 const char *, size_t); 389 390 static struct archive_vtable *archive_write_disk_vtable(void); 391 392 static int _archive_write_disk_close(struct archive *); 393 static int _archive_write_disk_free(struct archive *); 394 static int _archive_write_disk_header(struct archive *, 395 struct archive_entry *); 396 static int64_t _archive_write_disk_filter_bytes(struct archive *, int); 397 static int _archive_write_disk_finish_entry(struct archive *); 398 static ssize_t _archive_write_disk_data(struct archive *, const void *, 399 size_t); 400 static ssize_t _archive_write_disk_data_block(struct archive *, const void *, 401 size_t, int64_t); 402 403 static int 404 lazy_stat(struct archive_write_disk *a) 405 { 406 if (a->pst != NULL) { 407 /* Already have stat() data available. */ 408 return (ARCHIVE_OK); 409 } 410 #ifdef HAVE_FSTAT 411 if (a->fd >= 0 && fstat(a->fd, &a->st) == 0) { 412 a->pst = &a->st; 413 return (ARCHIVE_OK); 414 } 415 #endif 416 /* 417 * XXX At this point, symlinks should not be hit, otherwise 418 * XXX a race occurred. Do we want to check explicitly for that? 419 */ 420 if (lstat(a->name, &a->st) == 0) { 421 a->pst = &a->st; 422 return (ARCHIVE_OK); 423 } 424 archive_set_error(&a->archive, errno, "Couldn't stat file"); 425 return (ARCHIVE_WARN); 426 } 427 428 static struct archive_vtable * 429 archive_write_disk_vtable(void) 430 { 431 static struct archive_vtable av; 432 static int inited = 0; 433 434 if (!inited) { 435 av.archive_close = _archive_write_disk_close; 436 av.archive_filter_bytes = _archive_write_disk_filter_bytes; 437 av.archive_free = _archive_write_disk_free; 438 av.archive_write_header = _archive_write_disk_header; 439 av.archive_write_finish_entry 440 = _archive_write_disk_finish_entry; 441 av.archive_write_data = _archive_write_disk_data; 442 av.archive_write_data_block = _archive_write_disk_data_block; 443 inited = 1; 444 } 445 return (&av); 446 } 447 448 static int64_t 449 _archive_write_disk_filter_bytes(struct archive *_a, int n) 450 { 451 struct archive_write_disk *a = (struct archive_write_disk *)_a; 452 (void)n; /* UNUSED */ 453 if (n == -1 || n == 0) 454 return (a->total_bytes_written); 455 return (-1); 456 } 457 458 459 int 460 archive_write_disk_set_options(struct archive *_a, int flags) 461 { 462 struct archive_write_disk *a = (struct archive_write_disk *)_a; 463 464 a->flags = flags; 465 return (ARCHIVE_OK); 466 } 467 468 469 /* 470 * Extract this entry to disk. 471 * 472 * TODO: Validate hardlinks. According to the standards, we're 473 * supposed to check each extracted hardlink and squawk if it refers 474 * to a file that we didn't restore. I'm not entirely convinced this 475 * is a good idea, but more importantly: Is there any way to validate 476 * hardlinks without keeping a complete list of filenames from the 477 * entire archive?? Ugh. 478 * 479 */ 480 static int 481 _archive_write_disk_header(struct archive *_a, struct archive_entry *entry) 482 { 483 struct archive_write_disk *a = (struct archive_write_disk *)_a; 484 struct fixup_entry *fe; 485 int ret, r; 486 487 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 488 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 489 "archive_write_disk_header"); 490 archive_clear_error(&a->archive); 491 if (a->archive.state & ARCHIVE_STATE_DATA) { 492 r = _archive_write_disk_finish_entry(&a->archive); 493 if (r == ARCHIVE_FATAL) 494 return (r); 495 } 496 497 /* Set up for this particular entry. */ 498 a->pst = NULL; 499 a->current_fixup = NULL; 500 a->deferred = 0; 501 if (a->entry) { 502 archive_entry_free(a->entry); 503 a->entry = NULL; 504 } 505 a->entry = archive_entry_clone(entry); 506 a->fd = -1; 507 a->fd_offset = 0; 508 a->offset = 0; 509 a->restore_pwd = -1; 510 a->uid = a->user_uid; 511 a->mode = archive_entry_mode(a->entry); 512 if (archive_entry_size_is_set(a->entry)) 513 a->filesize = archive_entry_size(a->entry); 514 else 515 a->filesize = -1; 516 archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry)); 517 a->name = a->_name_data.s; 518 archive_clear_error(&a->archive); 519 520 /* 521 * Clean up the requested path. This is necessary for correct 522 * dir restores; the dir restore logic otherwise gets messed 523 * up by nonsense like "dir/.". 524 */ 525 ret = cleanup_pathname(a); 526 if (ret != ARCHIVE_OK) 527 return (ret); 528 529 /* 530 * Query the umask so we get predictable mode settings. 531 * This gets done on every call to _write_header in case the 532 * user edits their umask during the extraction for some 533 * reason. 534 */ 535 umask(a->user_umask = umask(0)); 536 537 /* Figure out what we need to do for this entry. */ 538 a->todo = TODO_MODE_BASE; 539 if (a->flags & ARCHIVE_EXTRACT_PERM) { 540 a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */ 541 /* 542 * SGID requires an extra "check" step because we 543 * cannot easily predict the GID that the system will 544 * assign. (Different systems assign GIDs to files 545 * based on a variety of criteria, including process 546 * credentials and the gid of the enclosing 547 * directory.) We can only restore the SGID bit if 548 * the file has the right GID, and we only know the 549 * GID if we either set it (see set_ownership) or if 550 * we've actually called stat() on the file after it 551 * was restored. Since there are several places at 552 * which we might verify the GID, we need a TODO bit 553 * to keep track. 554 */ 555 if (a->mode & S_ISGID) 556 a->todo |= TODO_SGID | TODO_SGID_CHECK; 557 /* 558 * Verifying the SUID is simpler, but can still be 559 * done in multiple ways, hence the separate "check" bit. 560 */ 561 if (a->mode & S_ISUID) 562 a->todo |= TODO_SUID | TODO_SUID_CHECK; 563 } else { 564 /* 565 * User didn't request full permissions, so don't 566 * restore SUID, SGID bits and obey umask. 567 */ 568 a->mode &= ~S_ISUID; 569 a->mode &= ~S_ISGID; 570 a->mode &= ~S_ISVTX; 571 a->mode &= ~a->user_umask; 572 } 573 if (a->flags & ARCHIVE_EXTRACT_OWNER) 574 a->todo |= TODO_OWNER; 575 if (a->flags & ARCHIVE_EXTRACT_TIME) 576 a->todo |= TODO_TIMES; 577 if (a->flags & ARCHIVE_EXTRACT_ACL) { 578 #if ARCHIVE_ACL_DARWIN 579 /* 580 * On MacOS, platform ACLs get stored in mac_metadata, too. 581 * If we intend to extract mac_metadata and it is present 582 * we skip extracting libarchive NFSv4 ACLs. 583 */ 584 size_t metadata_size; 585 586 if ((a->flags & ARCHIVE_EXTRACT_MAC_METADATA) == 0 || 587 archive_entry_mac_metadata(a->entry, 588 &metadata_size) == NULL || metadata_size == 0) 589 #endif 590 #if ARCHIVE_ACL_LIBRICHACL 591 /* 592 * RichACLs are stored in an extended attribute. 593 * If we intend to extract extended attributes and have this 594 * attribute we skip extracting libarchive NFSv4 ACLs. 595 */ 596 short extract_acls = 1; 597 if (a->flags & ARCHIVE_EXTRACT_XATTR && ( 598 archive_entry_acl_types(a->entry) & 599 ARCHIVE_ENTRY_ACL_TYPE_NFS4)) { 600 const char *attr_name; 601 const void *attr_value; 602 size_t attr_size; 603 int i = archive_entry_xattr_reset(a->entry); 604 while (i--) { 605 archive_entry_xattr_next(a->entry, &attr_name, 606 &attr_value, &attr_size); 607 if (attr_name != NULL && attr_value != NULL && 608 attr_size > 0 && strcmp(attr_name, 609 "trusted.richacl") == 0) { 610 extract_acls = 0; 611 break; 612 } 613 } 614 } 615 if (extract_acls) 616 #endif 617 #if ARCHIVE_ACL_DARWIN || ARCHIVE_ACL_LIBRICHACL 618 { 619 #endif 620 if (archive_entry_filetype(a->entry) == AE_IFDIR) 621 a->deferred |= TODO_ACLS; 622 else 623 a->todo |= TODO_ACLS; 624 #if ARCHIVE_ACL_DARWIN || ARCHIVE_ACL_LIBRICHACL 625 } 626 #endif 627 } 628 if (a->flags & ARCHIVE_EXTRACT_MAC_METADATA) { 629 if (archive_entry_filetype(a->entry) == AE_IFDIR) 630 a->deferred |= TODO_MAC_METADATA; 631 else 632 a->todo |= TODO_MAC_METADATA; 633 } 634 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H) 635 if ((a->flags & ARCHIVE_EXTRACT_NO_HFS_COMPRESSION) == 0) { 636 unsigned long set, clear; 637 archive_entry_fflags(a->entry, &set, &clear); 638 if ((set & ~clear) & UF_COMPRESSED) { 639 a->todo |= TODO_HFS_COMPRESSION; 640 a->decmpfs_block_count = (unsigned)-1; 641 } 642 } 643 if ((a->flags & ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED) != 0 && 644 (a->mode & AE_IFMT) == AE_IFREG && a->filesize > 0) { 645 a->todo |= TODO_HFS_COMPRESSION; 646 a->decmpfs_block_count = (unsigned)-1; 647 } 648 { 649 const char *p; 650 651 /* Check if the current file name is a type of the 652 * resource fork file. */ 653 p = strrchr(a->name, '/'); 654 if (p == NULL) 655 p = a->name; 656 else 657 p++; 658 if (p[0] == '.' && p[1] == '_') { 659 /* Do not compress "._XXX" files. */ 660 a->todo &= ~TODO_HFS_COMPRESSION; 661 if (a->filesize > 0) 662 a->todo |= TODO_APPLEDOUBLE; 663 } 664 } 665 #endif 666 667 if (a->flags & ARCHIVE_EXTRACT_XATTR) { 668 #if ARCHIVE_XATTR_DARWIN 669 /* 670 * On MacOS, extended attributes get stored in mac_metadata, 671 * too. If we intend to extract mac_metadata and it is present 672 * we skip extracting extended attributes. 673 */ 674 size_t metadata_size; 675 676 if ((a->flags & ARCHIVE_EXTRACT_MAC_METADATA) == 0 || 677 archive_entry_mac_metadata(a->entry, 678 &metadata_size) == NULL || metadata_size == 0) 679 #endif 680 a->todo |= TODO_XATTR; 681 } 682 if (a->flags & ARCHIVE_EXTRACT_FFLAGS) 683 a->todo |= TODO_FFLAGS; 684 if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) { 685 ret = check_symlinks(a); 686 if (ret != ARCHIVE_OK) 687 return (ret); 688 } 689 #if defined(HAVE_FCHDIR) && defined(PATH_MAX) 690 /* If path exceeds PATH_MAX, shorten the path. */ 691 edit_deep_directories(a); 692 #endif 693 694 ret = restore_entry(a); 695 696 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H) 697 /* 698 * Check if the filesystem the file is restoring on supports 699 * HFS+ Compression. If not, cancel HFS+ Compression. 700 */ 701 if (a->todo | TODO_HFS_COMPRESSION) { 702 /* 703 * NOTE: UF_COMPRESSED is ignored even if the filesystem 704 * supports HFS+ Compression because the file should 705 * have at least an extended attribute "com.apple.decmpfs" 706 * before the flag is set to indicate that the file have 707 * been compressed. If the filesystem does not support 708 * HFS+ Compression the system call will fail. 709 */ 710 if (a->fd < 0 || fchflags(a->fd, UF_COMPRESSED) != 0) 711 a->todo &= ~TODO_HFS_COMPRESSION; 712 } 713 #endif 714 715 /* 716 * TODO: There are rumours that some extended attributes must 717 * be restored before file data is written. If this is true, 718 * then we either need to write all extended attributes both 719 * before and after restoring the data, or find some rule for 720 * determining which must go first and which last. Due to the 721 * many ways people are using xattrs, this may prove to be an 722 * intractable problem. 723 */ 724 725 #ifdef HAVE_FCHDIR 726 /* If we changed directory above, restore it here. */ 727 if (a->restore_pwd >= 0) { 728 r = fchdir(a->restore_pwd); 729 if (r != 0) { 730 archive_set_error(&a->archive, errno, 731 "chdir() failure"); 732 ret = ARCHIVE_FATAL; 733 } 734 close(a->restore_pwd); 735 a->restore_pwd = -1; 736 } 737 #endif 738 739 /* 740 * Fixup uses the unedited pathname from archive_entry_pathname(), 741 * because it is relative to the base dir and the edited path 742 * might be relative to some intermediate dir as a result of the 743 * deep restore logic. 744 */ 745 if (a->deferred & TODO_MODE) { 746 fe = current_fixup(a, archive_entry_pathname(entry)); 747 if (fe == NULL) 748 return (ARCHIVE_FATAL); 749 fe->fixup |= TODO_MODE_BASE; 750 fe->mode = a->mode; 751 } 752 753 if ((a->deferred & TODO_TIMES) 754 && (archive_entry_mtime_is_set(entry) 755 || archive_entry_atime_is_set(entry))) { 756 fe = current_fixup(a, archive_entry_pathname(entry)); 757 if (fe == NULL) 758 return (ARCHIVE_FATAL); 759 fe->mode = a->mode; 760 fe->fixup |= TODO_TIMES; 761 if (archive_entry_atime_is_set(entry)) { 762 fe->atime = archive_entry_atime(entry); 763 fe->atime_nanos = archive_entry_atime_nsec(entry); 764 } else { 765 /* If atime is unset, use start time. */ 766 fe->atime = a->start_time; 767 fe->atime_nanos = 0; 768 } 769 if (archive_entry_mtime_is_set(entry)) { 770 fe->mtime = archive_entry_mtime(entry); 771 fe->mtime_nanos = archive_entry_mtime_nsec(entry); 772 } else { 773 /* If mtime is unset, use start time. */ 774 fe->mtime = a->start_time; 775 fe->mtime_nanos = 0; 776 } 777 if (archive_entry_birthtime_is_set(entry)) { 778 fe->birthtime = archive_entry_birthtime(entry); 779 fe->birthtime_nanos = archive_entry_birthtime_nsec( 780 entry); 781 } else { 782 /* If birthtime is unset, use mtime. */ 783 fe->birthtime = fe->mtime; 784 fe->birthtime_nanos = fe->mtime_nanos; 785 } 786 } 787 788 if (a->deferred & TODO_ACLS) { 789 fe = current_fixup(a, archive_entry_pathname(entry)); 790 if (fe == NULL) 791 return (ARCHIVE_FATAL); 792 fe->fixup |= TODO_ACLS; 793 archive_acl_copy(&fe->acl, archive_entry_acl(entry)); 794 } 795 796 if (a->deferred & TODO_MAC_METADATA) { 797 const void *metadata; 798 size_t metadata_size; 799 metadata = archive_entry_mac_metadata(a->entry, &metadata_size); 800 if (metadata != NULL && metadata_size > 0) { 801 fe = current_fixup(a, archive_entry_pathname(entry)); 802 if (fe == NULL) 803 return (ARCHIVE_FATAL); 804 fe->mac_metadata = malloc(metadata_size); 805 if (fe->mac_metadata != NULL) { 806 memcpy(fe->mac_metadata, metadata, 807 metadata_size); 808 fe->mac_metadata_size = metadata_size; 809 fe->fixup |= TODO_MAC_METADATA; 810 } 811 } 812 } 813 814 if (a->deferred & TODO_FFLAGS) { 815 fe = current_fixup(a, archive_entry_pathname(entry)); 816 if (fe == NULL) 817 return (ARCHIVE_FATAL); 818 fe->fixup |= TODO_FFLAGS; 819 /* TODO: Complete this.. defer fflags from below. */ 820 } 821 822 /* We've created the object and are ready to pour data into it. */ 823 if (ret >= ARCHIVE_WARN) 824 a->archive.state = ARCHIVE_STATE_DATA; 825 /* 826 * If it's not open, tell our client not to try writing. 827 * In particular, dirs, links, etc, don't get written to. 828 */ 829 if (a->fd < 0) { 830 archive_entry_set_size(entry, 0); 831 a->filesize = 0; 832 } 833 834 return (ret); 835 } 836 837 int 838 archive_write_disk_set_skip_file(struct archive *_a, la_int64_t d, la_int64_t i) 839 { 840 struct archive_write_disk *a = (struct archive_write_disk *)_a; 841 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 842 ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file"); 843 a->skip_file_set = 1; 844 a->skip_file_dev = d; 845 a->skip_file_ino = i; 846 return (ARCHIVE_OK); 847 } 848 849 static ssize_t 850 write_data_block(struct archive_write_disk *a, const char *buff, size_t size) 851 { 852 uint64_t start_size = size; 853 ssize_t bytes_written = 0; 854 ssize_t block_size = 0, bytes_to_write; 855 856 if (size == 0) 857 return (ARCHIVE_OK); 858 859 if (a->filesize == 0 || a->fd < 0) { 860 archive_set_error(&a->archive, 0, 861 "Attempt to write to an empty file"); 862 return (ARCHIVE_WARN); 863 } 864 865 if (a->flags & ARCHIVE_EXTRACT_SPARSE) { 866 #if HAVE_STRUCT_STAT_ST_BLKSIZE 867 int r; 868 if ((r = lazy_stat(a)) != ARCHIVE_OK) 869 return (r); 870 block_size = a->pst->st_blksize; 871 #else 872 /* XXX TODO XXX Is there a more appropriate choice here ? */ 873 /* This needn't match the filesystem allocation size. */ 874 block_size = 16*1024; 875 #endif 876 } 877 878 /* If this write would run beyond the file size, truncate it. */ 879 if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize) 880 start_size = size = (size_t)(a->filesize - a->offset); 881 882 /* Write the data. */ 883 while (size > 0) { 884 if (block_size == 0) { 885 bytes_to_write = size; 886 } else { 887 /* We're sparsifying the file. */ 888 const char *p, *end; 889 int64_t block_end; 890 891 /* Skip leading zero bytes. */ 892 for (p = buff, end = buff + size; p < end; ++p) { 893 if (*p != '\0') 894 break; 895 } 896 a->offset += p - buff; 897 size -= p - buff; 898 buff = p; 899 if (size == 0) 900 break; 901 902 /* Calculate next block boundary after offset. */ 903 block_end 904 = (a->offset / block_size + 1) * block_size; 905 906 /* If the adjusted write would cross block boundary, 907 * truncate it to the block boundary. */ 908 bytes_to_write = size; 909 if (a->offset + bytes_to_write > block_end) 910 bytes_to_write = block_end - a->offset; 911 } 912 /* Seek if necessary to the specified offset. */ 913 if (a->offset != a->fd_offset) { 914 if (lseek(a->fd, a->offset, SEEK_SET) < 0) { 915 archive_set_error(&a->archive, errno, 916 "Seek failed"); 917 return (ARCHIVE_FATAL); 918 } 919 a->fd_offset = a->offset; 920 } 921 bytes_written = write(a->fd, buff, bytes_to_write); 922 if (bytes_written < 0) { 923 archive_set_error(&a->archive, errno, "Write failed"); 924 return (ARCHIVE_WARN); 925 } 926 buff += bytes_written; 927 size -= bytes_written; 928 a->total_bytes_written += bytes_written; 929 a->offset += bytes_written; 930 a->fd_offset = a->offset; 931 } 932 return (start_size - size); 933 } 934 935 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\ 936 && defined(HAVE_ZLIB_H) 937 938 /* 939 * Set UF_COMPRESSED file flag. 940 * This have to be called after hfs_write_decmpfs() because if the 941 * file does not have "com.apple.decmpfs" xattr the flag is ignored. 942 */ 943 static int 944 hfs_set_compressed_fflag(struct archive_write_disk *a) 945 { 946 int r; 947 948 if ((r = lazy_stat(a)) != ARCHIVE_OK) 949 return (r); 950 951 a->st.st_flags |= UF_COMPRESSED; 952 if (fchflags(a->fd, a->st.st_flags) != 0) { 953 archive_set_error(&a->archive, errno, 954 "Failed to set UF_COMPRESSED file flag"); 955 return (ARCHIVE_WARN); 956 } 957 return (ARCHIVE_OK); 958 } 959 960 /* 961 * HFS+ Compression decmpfs 962 * 963 * +------------------------------+ +0 964 * | Magic(LE 4 bytes) | 965 * +------------------------------+ 966 * | Type(LE 4 bytes) | 967 * +------------------------------+ 968 * | Uncompressed size(LE 8 bytes)| 969 * +------------------------------+ +16 970 * | | 971 * | Compressed data | 972 * | (Placed only if Type == 3) | 973 * | | 974 * +------------------------------+ +3802 = MAX_DECMPFS_XATTR_SIZE 975 * 976 * Type is 3: decmpfs has compressed data. 977 * Type is 4: Resource Fork has compressed data. 978 */ 979 /* 980 * Write "com.apple.decmpfs" 981 */ 982 static int 983 hfs_write_decmpfs(struct archive_write_disk *a) 984 { 985 int r; 986 uint32_t compression_type; 987 988 r = fsetxattr(a->fd, DECMPFS_XATTR_NAME, a->decmpfs_header_p, 989 a->decmpfs_attr_size, 0, 0); 990 if (r < 0) { 991 archive_set_error(&a->archive, errno, 992 "Cannot restore xattr:%s", DECMPFS_XATTR_NAME); 993 compression_type = archive_le32dec( 994 &a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE]); 995 if (compression_type == CMP_RESOURCE_FORK) 996 fremovexattr(a->fd, XATTR_RESOURCEFORK_NAME, 997 XATTR_SHOWCOMPRESSION); 998 return (ARCHIVE_WARN); 999 } 1000 return (ARCHIVE_OK); 1001 } 1002 1003 /* 1004 * HFS+ Compression Resource Fork 1005 * 1006 * +-----------------------------+ 1007 * | Header(260 bytes) | 1008 * +-----------------------------+ 1009 * | Block count(LE 4 bytes) | 1010 * +-----------------------------+ --+ 1011 * +-- | Offset (LE 4 bytes) | | 1012 * | | [distance from Block count] | | Block 0 1013 * | +-----------------------------+ | 1014 * | | Compressed size(LE 4 bytes) | | 1015 * | +-----------------------------+ --+ 1016 * | | | 1017 * | | .................. | 1018 * | | | 1019 * | +-----------------------------+ --+ 1020 * | | Offset (LE 4 bytes) | | 1021 * | +-----------------------------+ | Block (Block count -1) 1022 * | | Compressed size(LE 4 bytes) | | 1023 * +-> +-----------------------------+ --+ 1024 * | Compressed data(n bytes) | Block 0 1025 * +-----------------------------+ 1026 * | | 1027 * | .................. | 1028 * | | 1029 * +-----------------------------+ 1030 * | Compressed data(n bytes) | Block (Block count -1) 1031 * +-----------------------------+ 1032 * | Footer(50 bytes) | 1033 * +-----------------------------+ 1034 * 1035 */ 1036 /* 1037 * Write the header of "com.apple.ResourceFork" 1038 */ 1039 static int 1040 hfs_write_resource_fork(struct archive_write_disk *a, unsigned char *buff, 1041 size_t bytes, uint32_t position) 1042 { 1043 int ret; 1044 1045 ret = fsetxattr(a->fd, XATTR_RESOURCEFORK_NAME, buff, bytes, 1046 position, a->rsrc_xattr_options); 1047 if (ret < 0) { 1048 archive_set_error(&a->archive, errno, 1049 "Cannot restore xattr: %s at %u pos %u bytes", 1050 XATTR_RESOURCEFORK_NAME, 1051 (unsigned)position, 1052 (unsigned)bytes); 1053 return (ARCHIVE_WARN); 1054 } 1055 a->rsrc_xattr_options &= ~XATTR_CREATE; 1056 return (ARCHIVE_OK); 1057 } 1058 1059 static int 1060 hfs_write_compressed_data(struct archive_write_disk *a, size_t bytes_compressed) 1061 { 1062 int ret; 1063 1064 ret = hfs_write_resource_fork(a, a->compressed_buffer, 1065 bytes_compressed, a->compressed_rsrc_position); 1066 if (ret == ARCHIVE_OK) 1067 a->compressed_rsrc_position += bytes_compressed; 1068 return (ret); 1069 } 1070 1071 static int 1072 hfs_write_resource_fork_header(struct archive_write_disk *a) 1073 { 1074 unsigned char *buff; 1075 uint32_t rsrc_bytes; 1076 uint32_t rsrc_header_bytes; 1077 1078 /* 1079 * Write resource fork header + block info. 1080 */ 1081 buff = a->resource_fork; 1082 rsrc_bytes = a->compressed_rsrc_position - RSRC_F_SIZE; 1083 rsrc_header_bytes = 1084 RSRC_H_SIZE + /* Header base size. */ 1085 4 + /* Block count. */ 1086 (a->decmpfs_block_count * 8);/* Block info */ 1087 archive_be32enc(buff, 0x100); 1088 archive_be32enc(buff + 4, rsrc_bytes); 1089 archive_be32enc(buff + 8, rsrc_bytes - 256); 1090 archive_be32enc(buff + 12, 0x32); 1091 memset(buff + 16, 0, 240); 1092 archive_be32enc(buff + 256, rsrc_bytes - 260); 1093 return hfs_write_resource_fork(a, buff, rsrc_header_bytes, 0); 1094 } 1095 1096 static size_t 1097 hfs_set_resource_fork_footer(unsigned char *buff, size_t buff_size) 1098 { 1099 static const char rsrc_footer[RSRC_F_SIZE] = { 1100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1103 0x00, 0x1c, 0x00, 0x32, 0x00, 0x00, 'c', 'm', 1104 'p', 'f', 0x00, 0x00, 0x00, 0x0a, 0x00, 0x01, 1105 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1106 0x00, 0x00 1107 }; 1108 if (buff_size < sizeof(rsrc_footer)) 1109 return (0); 1110 memcpy(buff, rsrc_footer, sizeof(rsrc_footer)); 1111 return (sizeof(rsrc_footer)); 1112 } 1113 1114 static int 1115 hfs_reset_compressor(struct archive_write_disk *a) 1116 { 1117 int ret; 1118 1119 if (a->stream_valid) 1120 ret = deflateReset(&a->stream); 1121 else 1122 ret = deflateInit(&a->stream, a->decmpfs_compression_level); 1123 1124 if (ret != Z_OK) { 1125 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1126 "Failed to initialize compressor"); 1127 return (ARCHIVE_FATAL); 1128 } else 1129 a->stream_valid = 1; 1130 1131 return (ARCHIVE_OK); 1132 } 1133 1134 static int 1135 hfs_decompress(struct archive_write_disk *a) 1136 { 1137 uint32_t *block_info; 1138 unsigned int block_count; 1139 uint32_t data_pos, data_size; 1140 ssize_t r; 1141 ssize_t bytes_written, bytes_to_write; 1142 unsigned char *b; 1143 1144 block_info = (uint32_t *)(a->resource_fork + RSRC_H_SIZE); 1145 block_count = archive_le32dec(block_info++); 1146 while (block_count--) { 1147 data_pos = RSRC_H_SIZE + archive_le32dec(block_info++); 1148 data_size = archive_le32dec(block_info++); 1149 r = fgetxattr(a->fd, XATTR_RESOURCEFORK_NAME, 1150 a->compressed_buffer, data_size, data_pos, 0); 1151 if (r != data_size) { 1152 archive_set_error(&a->archive, 1153 (r < 0)?errno:ARCHIVE_ERRNO_MISC, 1154 "Failed to read resource fork"); 1155 return (ARCHIVE_WARN); 1156 } 1157 if (a->compressed_buffer[0] == 0xff) { 1158 bytes_to_write = data_size -1; 1159 b = a->compressed_buffer + 1; 1160 } else { 1161 uLong dest_len = MAX_DECMPFS_BLOCK_SIZE; 1162 int zr; 1163 1164 zr = uncompress((Bytef *)a->uncompressed_buffer, 1165 &dest_len, a->compressed_buffer, data_size); 1166 if (zr != Z_OK) { 1167 archive_set_error(&a->archive, 1168 ARCHIVE_ERRNO_MISC, 1169 "Failed to decompress resource fork"); 1170 return (ARCHIVE_WARN); 1171 } 1172 bytes_to_write = dest_len; 1173 b = (unsigned char *)a->uncompressed_buffer; 1174 } 1175 do { 1176 bytes_written = write(a->fd, b, bytes_to_write); 1177 if (bytes_written < 0) { 1178 archive_set_error(&a->archive, errno, 1179 "Write failed"); 1180 return (ARCHIVE_WARN); 1181 } 1182 bytes_to_write -= bytes_written; 1183 b += bytes_written; 1184 } while (bytes_to_write > 0); 1185 } 1186 r = fremovexattr(a->fd, XATTR_RESOURCEFORK_NAME, 0); 1187 if (r == -1) { 1188 archive_set_error(&a->archive, errno, 1189 "Failed to remove resource fork"); 1190 return (ARCHIVE_WARN); 1191 } 1192 return (ARCHIVE_OK); 1193 } 1194 1195 static int 1196 hfs_drive_compressor(struct archive_write_disk *a, const char *buff, 1197 size_t size) 1198 { 1199 unsigned char *buffer_compressed; 1200 size_t bytes_compressed; 1201 size_t bytes_used; 1202 int ret; 1203 1204 ret = hfs_reset_compressor(a); 1205 if (ret != ARCHIVE_OK) 1206 return (ret); 1207 1208 if (a->compressed_buffer == NULL) { 1209 size_t block_size; 1210 1211 block_size = COMPRESSED_W_SIZE + RSRC_F_SIZE + 1212 + compressBound(MAX_DECMPFS_BLOCK_SIZE); 1213 a->compressed_buffer = malloc(block_size); 1214 if (a->compressed_buffer == NULL) { 1215 archive_set_error(&a->archive, ENOMEM, 1216 "Can't allocate memory for Resource Fork"); 1217 return (ARCHIVE_FATAL); 1218 } 1219 a->compressed_buffer_size = block_size; 1220 a->compressed_buffer_remaining = block_size; 1221 } 1222 1223 buffer_compressed = a->compressed_buffer + 1224 a->compressed_buffer_size - a->compressed_buffer_remaining; 1225 a->stream.next_in = (Bytef *)(uintptr_t)(const void *)buff; 1226 a->stream.avail_in = size; 1227 a->stream.next_out = buffer_compressed; 1228 a->stream.avail_out = a->compressed_buffer_remaining; 1229 do { 1230 ret = deflate(&a->stream, Z_FINISH); 1231 switch (ret) { 1232 case Z_OK: 1233 case Z_STREAM_END: 1234 break; 1235 default: 1236 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1237 "Failed to compress data"); 1238 return (ARCHIVE_FAILED); 1239 } 1240 } while (ret == Z_OK); 1241 bytes_compressed = a->compressed_buffer_remaining - a->stream.avail_out; 1242 1243 /* 1244 * If the compressed size is larger than the original size, 1245 * throw away compressed data, use uncompressed data instead. 1246 */ 1247 if (bytes_compressed > size) { 1248 buffer_compressed[0] = 0xFF;/* uncompressed marker. */ 1249 memcpy(buffer_compressed + 1, buff, size); 1250 bytes_compressed = size + 1; 1251 } 1252 a->compressed_buffer_remaining -= bytes_compressed; 1253 1254 /* 1255 * If the compressed size is smaller than MAX_DECMPFS_XATTR_SIZE 1256 * and the block count in the file is only one, store compressed 1257 * data to decmpfs xattr instead of the resource fork. 1258 */ 1259 if (a->decmpfs_block_count == 1 && 1260 (a->decmpfs_attr_size + bytes_compressed) 1261 <= MAX_DECMPFS_XATTR_SIZE) { 1262 archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE], 1263 CMP_XATTR); 1264 memcpy(a->decmpfs_header_p + DECMPFS_HEADER_SIZE, 1265 buffer_compressed, bytes_compressed); 1266 a->decmpfs_attr_size += bytes_compressed; 1267 a->compressed_buffer_remaining = a->compressed_buffer_size; 1268 /* 1269 * Finish HFS+ Compression. 1270 * - Write the decmpfs xattr. 1271 * - Set the UF_COMPRESSED file flag. 1272 */ 1273 ret = hfs_write_decmpfs(a); 1274 if (ret == ARCHIVE_OK) 1275 ret = hfs_set_compressed_fflag(a); 1276 return (ret); 1277 } 1278 1279 /* Update block info. */ 1280 archive_le32enc(a->decmpfs_block_info++, 1281 a->compressed_rsrc_position_v - RSRC_H_SIZE); 1282 archive_le32enc(a->decmpfs_block_info++, bytes_compressed); 1283 a->compressed_rsrc_position_v += bytes_compressed; 1284 1285 /* 1286 * Write the compressed data to the resource fork. 1287 */ 1288 bytes_used = a->compressed_buffer_size - a->compressed_buffer_remaining; 1289 while (bytes_used >= COMPRESSED_W_SIZE) { 1290 ret = hfs_write_compressed_data(a, COMPRESSED_W_SIZE); 1291 if (ret != ARCHIVE_OK) 1292 return (ret); 1293 bytes_used -= COMPRESSED_W_SIZE; 1294 if (bytes_used > COMPRESSED_W_SIZE) 1295 memmove(a->compressed_buffer, 1296 a->compressed_buffer + COMPRESSED_W_SIZE, 1297 bytes_used); 1298 else 1299 memcpy(a->compressed_buffer, 1300 a->compressed_buffer + COMPRESSED_W_SIZE, 1301 bytes_used); 1302 } 1303 a->compressed_buffer_remaining = a->compressed_buffer_size - bytes_used; 1304 1305 /* 1306 * If the current block is the last block, write the remaining 1307 * compressed data and the resource fork footer. 1308 */ 1309 if (a->file_remaining_bytes == 0) { 1310 size_t rsrc_size; 1311 int64_t bk; 1312 1313 /* Append the resource footer. */ 1314 rsrc_size = hfs_set_resource_fork_footer( 1315 a->compressed_buffer + bytes_used, 1316 a->compressed_buffer_remaining); 1317 ret = hfs_write_compressed_data(a, bytes_used + rsrc_size); 1318 a->compressed_buffer_remaining = a->compressed_buffer_size; 1319 1320 /* If the compressed size is not enough smaller than 1321 * the uncompressed size. cancel HFS+ compression. 1322 * TODO: study a behavior of ditto utility and improve 1323 * the condition to fall back into no HFS+ compression. */ 1324 bk = HFS_BLOCKS(a->compressed_rsrc_position); 1325 bk += bk >> 7; 1326 if (bk > HFS_BLOCKS(a->filesize)) 1327 return hfs_decompress(a); 1328 /* 1329 * Write the resourcefork header. 1330 */ 1331 if (ret == ARCHIVE_OK) 1332 ret = hfs_write_resource_fork_header(a); 1333 /* 1334 * Finish HFS+ Compression. 1335 * - Write the decmpfs xattr. 1336 * - Set the UF_COMPRESSED file flag. 1337 */ 1338 if (ret == ARCHIVE_OK) 1339 ret = hfs_write_decmpfs(a); 1340 if (ret == ARCHIVE_OK) 1341 ret = hfs_set_compressed_fflag(a); 1342 } 1343 return (ret); 1344 } 1345 1346 static ssize_t 1347 hfs_write_decmpfs_block(struct archive_write_disk *a, const char *buff, 1348 size_t size) 1349 { 1350 const char *buffer_to_write; 1351 size_t bytes_to_write; 1352 int ret; 1353 1354 if (a->decmpfs_block_count == (unsigned)-1) { 1355 void *new_block; 1356 size_t new_size; 1357 unsigned int block_count; 1358 1359 if (a->decmpfs_header_p == NULL) { 1360 new_block = malloc(MAX_DECMPFS_XATTR_SIZE 1361 + sizeof(uint32_t)); 1362 if (new_block == NULL) { 1363 archive_set_error(&a->archive, ENOMEM, 1364 "Can't allocate memory for decmpfs"); 1365 return (ARCHIVE_FATAL); 1366 } 1367 a->decmpfs_header_p = new_block; 1368 } 1369 a->decmpfs_attr_size = DECMPFS_HEADER_SIZE; 1370 archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_MAGIC], 1371 DECMPFS_MAGIC); 1372 archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE], 1373 CMP_RESOURCE_FORK); 1374 archive_le64enc(&a->decmpfs_header_p[DECMPFS_UNCOMPRESSED_SIZE], 1375 a->filesize); 1376 1377 /* Calculate a block count of the file. */ 1378 block_count = 1379 (a->filesize + MAX_DECMPFS_BLOCK_SIZE -1) / 1380 MAX_DECMPFS_BLOCK_SIZE; 1381 /* 1382 * Allocate buffer for resource fork. 1383 * Set up related pointers; 1384 */ 1385 new_size = 1386 RSRC_H_SIZE + /* header */ 1387 4 + /* Block count */ 1388 (block_count * sizeof(uint32_t) * 2) + 1389 RSRC_F_SIZE; /* footer */ 1390 if (new_size > a->resource_fork_allocated_size) { 1391 new_block = realloc(a->resource_fork, new_size); 1392 if (new_block == NULL) { 1393 archive_set_error(&a->archive, ENOMEM, 1394 "Can't allocate memory for ResourceFork"); 1395 return (ARCHIVE_FATAL); 1396 } 1397 a->resource_fork_allocated_size = new_size; 1398 a->resource_fork = new_block; 1399 } 1400 1401 /* Allocate uncompressed buffer */ 1402 if (a->uncompressed_buffer == NULL) { 1403 new_block = malloc(MAX_DECMPFS_BLOCK_SIZE); 1404 if (new_block == NULL) { 1405 archive_set_error(&a->archive, ENOMEM, 1406 "Can't allocate memory for decmpfs"); 1407 return (ARCHIVE_FATAL); 1408 } 1409 a->uncompressed_buffer = new_block; 1410 } 1411 a->block_remaining_bytes = MAX_DECMPFS_BLOCK_SIZE; 1412 a->file_remaining_bytes = a->filesize; 1413 a->compressed_buffer_remaining = a->compressed_buffer_size; 1414 1415 /* 1416 * Set up a resource fork. 1417 */ 1418 a->rsrc_xattr_options = XATTR_CREATE; 1419 /* Get the position where we are going to set a bunch 1420 * of block info. */ 1421 a->decmpfs_block_info = 1422 (uint32_t *)(a->resource_fork + RSRC_H_SIZE); 1423 /* Set the block count to the resource fork. */ 1424 archive_le32enc(a->decmpfs_block_info++, block_count); 1425 /* Get the position where we are going to set compressed 1426 * data. */ 1427 a->compressed_rsrc_position = 1428 RSRC_H_SIZE + 4 + (block_count * 8); 1429 a->compressed_rsrc_position_v = a->compressed_rsrc_position; 1430 a->decmpfs_block_count = block_count; 1431 } 1432 1433 /* Ignore redundant bytes. */ 1434 if (a->file_remaining_bytes == 0) 1435 return ((ssize_t)size); 1436 1437 /* Do not overrun a block size. */ 1438 if (size > a->block_remaining_bytes) 1439 bytes_to_write = a->block_remaining_bytes; 1440 else 1441 bytes_to_write = size; 1442 /* Do not overrun the file size. */ 1443 if (bytes_to_write > a->file_remaining_bytes) 1444 bytes_to_write = a->file_remaining_bytes; 1445 1446 /* For efficiency, if a copy length is full of the uncompressed 1447 * buffer size, do not copy writing data to it. */ 1448 if (bytes_to_write == MAX_DECMPFS_BLOCK_SIZE) 1449 buffer_to_write = buff; 1450 else { 1451 memcpy(a->uncompressed_buffer + 1452 MAX_DECMPFS_BLOCK_SIZE - a->block_remaining_bytes, 1453 buff, bytes_to_write); 1454 buffer_to_write = a->uncompressed_buffer; 1455 } 1456 a->block_remaining_bytes -= bytes_to_write; 1457 a->file_remaining_bytes -= bytes_to_write; 1458 1459 if (a->block_remaining_bytes == 0 || a->file_remaining_bytes == 0) { 1460 ret = hfs_drive_compressor(a, buffer_to_write, 1461 MAX_DECMPFS_BLOCK_SIZE - a->block_remaining_bytes); 1462 if (ret < 0) 1463 return (ret); 1464 a->block_remaining_bytes = MAX_DECMPFS_BLOCK_SIZE; 1465 } 1466 /* Ignore redundant bytes. */ 1467 if (a->file_remaining_bytes == 0) 1468 return ((ssize_t)size); 1469 return (bytes_to_write); 1470 } 1471 1472 static ssize_t 1473 hfs_write_data_block(struct archive_write_disk *a, const char *buff, 1474 size_t size) 1475 { 1476 uint64_t start_size = size; 1477 ssize_t bytes_written = 0; 1478 ssize_t bytes_to_write; 1479 1480 if (size == 0) 1481 return (ARCHIVE_OK); 1482 1483 if (a->filesize == 0 || a->fd < 0) { 1484 archive_set_error(&a->archive, 0, 1485 "Attempt to write to an empty file"); 1486 return (ARCHIVE_WARN); 1487 } 1488 1489 /* If this write would run beyond the file size, truncate it. */ 1490 if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize) 1491 start_size = size = (size_t)(a->filesize - a->offset); 1492 1493 /* Write the data. */ 1494 while (size > 0) { 1495 bytes_to_write = size; 1496 /* Seek if necessary to the specified offset. */ 1497 if (a->offset < a->fd_offset) { 1498 /* Can't support backward move. */ 1499 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1500 "Seek failed"); 1501 return (ARCHIVE_FATAL); 1502 } else if (a->offset > a->fd_offset) { 1503 int64_t skip = a->offset - a->fd_offset; 1504 char nullblock[1024]; 1505 1506 memset(nullblock, 0, sizeof(nullblock)); 1507 while (skip > 0) { 1508 if (skip > (int64_t)sizeof(nullblock)) 1509 bytes_written = hfs_write_decmpfs_block( 1510 a, nullblock, sizeof(nullblock)); 1511 else 1512 bytes_written = hfs_write_decmpfs_block( 1513 a, nullblock, skip); 1514 if (bytes_written < 0) { 1515 archive_set_error(&a->archive, errno, 1516 "Write failed"); 1517 return (ARCHIVE_WARN); 1518 } 1519 skip -= bytes_written; 1520 } 1521 1522 a->fd_offset = a->offset; 1523 } 1524 bytes_written = 1525 hfs_write_decmpfs_block(a, buff, bytes_to_write); 1526 if (bytes_written < 0) 1527 return (bytes_written); 1528 buff += bytes_written; 1529 size -= bytes_written; 1530 a->total_bytes_written += bytes_written; 1531 a->offset += bytes_written; 1532 a->fd_offset = a->offset; 1533 } 1534 return (start_size - size); 1535 } 1536 #else 1537 static ssize_t 1538 hfs_write_data_block(struct archive_write_disk *a, const char *buff, 1539 size_t size) 1540 { 1541 return (write_data_block(a, buff, size)); 1542 } 1543 #endif 1544 1545 static ssize_t 1546 _archive_write_disk_data_block(struct archive *_a, 1547 const void *buff, size_t size, int64_t offset) 1548 { 1549 struct archive_write_disk *a = (struct archive_write_disk *)_a; 1550 ssize_t r; 1551 1552 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 1553 ARCHIVE_STATE_DATA, "archive_write_data_block"); 1554 1555 a->offset = offset; 1556 if (a->todo & TODO_HFS_COMPRESSION) 1557 r = hfs_write_data_block(a, buff, size); 1558 else 1559 r = write_data_block(a, buff, size); 1560 if (r < ARCHIVE_OK) 1561 return (r); 1562 if ((size_t)r < size) { 1563 archive_set_error(&a->archive, 0, 1564 "Too much data: Truncating file at %ju bytes", 1565 (uintmax_t)a->filesize); 1566 return (ARCHIVE_WARN); 1567 } 1568 #if ARCHIVE_VERSION_NUMBER < 3999000 1569 return (ARCHIVE_OK); 1570 #else 1571 return (size); 1572 #endif 1573 } 1574 1575 static ssize_t 1576 _archive_write_disk_data(struct archive *_a, const void *buff, size_t size) 1577 { 1578 struct archive_write_disk *a = (struct archive_write_disk *)_a; 1579 1580 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 1581 ARCHIVE_STATE_DATA, "archive_write_data"); 1582 1583 if (a->todo & TODO_HFS_COMPRESSION) 1584 return (hfs_write_data_block(a, buff, size)); 1585 return (write_data_block(a, buff, size)); 1586 } 1587 1588 static int 1589 _archive_write_disk_finish_entry(struct archive *_a) 1590 { 1591 struct archive_write_disk *a = (struct archive_write_disk *)_a; 1592 int ret = ARCHIVE_OK; 1593 1594 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 1595 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 1596 "archive_write_finish_entry"); 1597 if (a->archive.state & ARCHIVE_STATE_HEADER) 1598 return (ARCHIVE_OK); 1599 archive_clear_error(&a->archive); 1600 1601 /* Pad or truncate file to the right size. */ 1602 if (a->fd < 0) { 1603 /* There's no file. */ 1604 } else if (a->filesize < 0) { 1605 /* File size is unknown, so we can't set the size. */ 1606 } else if (a->fd_offset == a->filesize) { 1607 /* Last write ended at exactly the filesize; we're done. */ 1608 /* Hopefully, this is the common case. */ 1609 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H) 1610 } else if (a->todo & TODO_HFS_COMPRESSION) { 1611 char null_d[1024]; 1612 ssize_t r; 1613 1614 if (a->file_remaining_bytes) 1615 memset(null_d, 0, sizeof(null_d)); 1616 while (a->file_remaining_bytes) { 1617 if (a->file_remaining_bytes > sizeof(null_d)) 1618 r = hfs_write_data_block( 1619 a, null_d, sizeof(null_d)); 1620 else 1621 r = hfs_write_data_block( 1622 a, null_d, a->file_remaining_bytes); 1623 if (r < 0) 1624 return ((int)r); 1625 } 1626 #endif 1627 } else { 1628 #if HAVE_FTRUNCATE 1629 if (ftruncate(a->fd, a->filesize) == -1 && 1630 a->filesize == 0) { 1631 archive_set_error(&a->archive, errno, 1632 "File size could not be restored"); 1633 return (ARCHIVE_FAILED); 1634 } 1635 #endif 1636 /* 1637 * Not all platforms implement the XSI option to 1638 * extend files via ftruncate. Stat() the file again 1639 * to see what happened. 1640 */ 1641 a->pst = NULL; 1642 if ((ret = lazy_stat(a)) != ARCHIVE_OK) 1643 return (ret); 1644 /* We can use lseek()/write() to extend the file if 1645 * ftruncate didn't work or isn't available. */ 1646 if (a->st.st_size < a->filesize) { 1647 const char nul = '\0'; 1648 if (lseek(a->fd, a->filesize - 1, SEEK_SET) < 0) { 1649 archive_set_error(&a->archive, errno, 1650 "Seek failed"); 1651 return (ARCHIVE_FATAL); 1652 } 1653 if (write(a->fd, &nul, 1) < 0) { 1654 archive_set_error(&a->archive, errno, 1655 "Write to restore size failed"); 1656 return (ARCHIVE_FATAL); 1657 } 1658 a->pst = NULL; 1659 } 1660 } 1661 1662 /* Restore metadata. */ 1663 1664 /* 1665 * This is specific to Mac OS X. 1666 * If the current file is an AppleDouble file, it should be 1667 * linked with the data fork file and remove it. 1668 */ 1669 if (a->todo & TODO_APPLEDOUBLE) { 1670 int r2 = fixup_appledouble(a, a->name); 1671 if (r2 == ARCHIVE_EOF) { 1672 /* The current file has been successfully linked 1673 * with the data fork file and removed. So there 1674 * is nothing to do on the current file. */ 1675 goto finish_metadata; 1676 } 1677 if (r2 < ret) ret = r2; 1678 } 1679 1680 /* 1681 * Look up the "real" UID only if we're going to need it. 1682 * TODO: the TODO_SGID condition can be dropped here, can't it? 1683 */ 1684 if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) { 1685 a->uid = archive_write_disk_uid(&a->archive, 1686 archive_entry_uname(a->entry), 1687 archive_entry_uid(a->entry)); 1688 } 1689 /* Look up the "real" GID only if we're going to need it. */ 1690 /* TODO: the TODO_SUID condition can be dropped here, can't it? */ 1691 if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) { 1692 a->gid = archive_write_disk_gid(&a->archive, 1693 archive_entry_gname(a->entry), 1694 archive_entry_gid(a->entry)); 1695 } 1696 1697 /* 1698 * Restore ownership before set_mode tries to restore suid/sgid 1699 * bits. If we set the owner, we know what it is and can skip 1700 * a stat() call to examine the ownership of the file on disk. 1701 */ 1702 if (a->todo & TODO_OWNER) { 1703 int r2 = set_ownership(a); 1704 if (r2 < ret) ret = r2; 1705 } 1706 1707 /* 1708 * HYPOTHESIS: 1709 * If we're not root, we won't be setting any security 1710 * attributes that may be wiped by the set_mode() routine 1711 * below. We also can't set xattr on non-owner-writable files, 1712 * which may be the state after set_mode(). Perform 1713 * set_xattrs() first based on these constraints. 1714 */ 1715 if (a->user_uid != 0 && 1716 (a->todo & TODO_XATTR)) { 1717 int r2 = set_xattrs(a); 1718 if (r2 < ret) ret = r2; 1719 } 1720 1721 /* 1722 * set_mode must precede ACLs on systems such as Solaris and 1723 * FreeBSD where setting the mode implicitly clears extended ACLs 1724 */ 1725 if (a->todo & TODO_MODE) { 1726 int r2 = set_mode(a, a->mode); 1727 if (r2 < ret) ret = r2; 1728 } 1729 1730 /* 1731 * Security-related extended attributes (such as 1732 * security.capability on Linux) have to be restored last, 1733 * since they're implicitly removed by other file changes. 1734 * We do this last only when root. 1735 */ 1736 if (a->user_uid == 0 && 1737 (a->todo & TODO_XATTR)) { 1738 int r2 = set_xattrs(a); 1739 if (r2 < ret) ret = r2; 1740 } 1741 1742 /* 1743 * Some flags prevent file modification; they must be restored after 1744 * file contents are written. 1745 */ 1746 if (a->todo & TODO_FFLAGS) { 1747 int r2 = set_fflags(a); 1748 if (r2 < ret) ret = r2; 1749 } 1750 1751 /* 1752 * Time must follow most other metadata; 1753 * otherwise atime will get changed. 1754 */ 1755 if (a->todo & TODO_TIMES) { 1756 int r2 = set_times_from_entry(a); 1757 if (r2 < ret) ret = r2; 1758 } 1759 1760 /* 1761 * Mac extended metadata includes ACLs. 1762 */ 1763 if (a->todo & TODO_MAC_METADATA) { 1764 const void *metadata; 1765 size_t metadata_size; 1766 metadata = archive_entry_mac_metadata(a->entry, &metadata_size); 1767 if (metadata != NULL && metadata_size > 0) { 1768 int r2 = set_mac_metadata(a, archive_entry_pathname( 1769 a->entry), metadata, metadata_size); 1770 if (r2 < ret) ret = r2; 1771 } 1772 } 1773 1774 /* 1775 * ACLs must be restored after timestamps because there are 1776 * ACLs that prevent attribute changes (including time). 1777 */ 1778 if (a->todo & TODO_ACLS) { 1779 int r2; 1780 r2 = archive_write_disk_set_acls(&a->archive, a->fd, 1781 archive_entry_pathname(a->entry), 1782 archive_entry_acl(a->entry), 1783 archive_entry_mode(a->entry)); 1784 if (r2 < ret) ret = r2; 1785 } 1786 1787 finish_metadata: 1788 /* If there's an fd, we can close it now. */ 1789 if (a->fd >= 0) { 1790 close(a->fd); 1791 a->fd = -1; 1792 } 1793 /* If there's an entry, we can release it now. */ 1794 archive_entry_free(a->entry); 1795 a->entry = NULL; 1796 a->archive.state = ARCHIVE_STATE_HEADER; 1797 return (ret); 1798 } 1799 1800 int 1801 archive_write_disk_set_group_lookup(struct archive *_a, 1802 void *private_data, 1803 la_int64_t (*lookup_gid)(void *private, const char *gname, la_int64_t gid), 1804 void (*cleanup_gid)(void *private)) 1805 { 1806 struct archive_write_disk *a = (struct archive_write_disk *)_a; 1807 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 1808 ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup"); 1809 1810 if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL) 1811 (a->cleanup_gid)(a->lookup_gid_data); 1812 1813 a->lookup_gid = lookup_gid; 1814 a->cleanup_gid = cleanup_gid; 1815 a->lookup_gid_data = private_data; 1816 return (ARCHIVE_OK); 1817 } 1818 1819 int 1820 archive_write_disk_set_user_lookup(struct archive *_a, 1821 void *private_data, 1822 int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid), 1823 void (*cleanup_uid)(void *private)) 1824 { 1825 struct archive_write_disk *a = (struct archive_write_disk *)_a; 1826 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 1827 ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup"); 1828 1829 if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL) 1830 (a->cleanup_uid)(a->lookup_uid_data); 1831 1832 a->lookup_uid = lookup_uid; 1833 a->cleanup_uid = cleanup_uid; 1834 a->lookup_uid_data = private_data; 1835 return (ARCHIVE_OK); 1836 } 1837 1838 int64_t 1839 archive_write_disk_gid(struct archive *_a, const char *name, la_int64_t id) 1840 { 1841 struct archive_write_disk *a = (struct archive_write_disk *)_a; 1842 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 1843 ARCHIVE_STATE_ANY, "archive_write_disk_gid"); 1844 if (a->lookup_gid) 1845 return (a->lookup_gid)(a->lookup_gid_data, name, id); 1846 return (id); 1847 } 1848 1849 int64_t 1850 archive_write_disk_uid(struct archive *_a, const char *name, la_int64_t id) 1851 { 1852 struct archive_write_disk *a = (struct archive_write_disk *)_a; 1853 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 1854 ARCHIVE_STATE_ANY, "archive_write_disk_uid"); 1855 if (a->lookup_uid) 1856 return (a->lookup_uid)(a->lookup_uid_data, name, id); 1857 return (id); 1858 } 1859 1860 /* 1861 * Create a new archive_write_disk object and initialize it with global state. 1862 */ 1863 struct archive * 1864 archive_write_disk_new(void) 1865 { 1866 struct archive_write_disk *a; 1867 1868 a = (struct archive_write_disk *)calloc(1, sizeof(*a)); 1869 if (a == NULL) 1870 return (NULL); 1871 a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC; 1872 /* We're ready to write a header immediately. */ 1873 a->archive.state = ARCHIVE_STATE_HEADER; 1874 a->archive.vtable = archive_write_disk_vtable(); 1875 a->start_time = time(NULL); 1876 /* Query and restore the umask. */ 1877 umask(a->user_umask = umask(0)); 1878 #ifdef HAVE_GETEUID 1879 a->user_uid = geteuid(); 1880 #endif /* HAVE_GETEUID */ 1881 if (archive_string_ensure(&a->path_safe, 512) == NULL) { 1882 free(a); 1883 return (NULL); 1884 } 1885 #ifdef HAVE_ZLIB_H 1886 a->decmpfs_compression_level = 5; 1887 #endif 1888 return (&a->archive); 1889 } 1890 1891 1892 /* 1893 * If pathname is longer than PATH_MAX, chdir to a suitable 1894 * intermediate dir and edit the path down to a shorter suffix. Note 1895 * that this routine never returns an error; if the chdir() attempt 1896 * fails for any reason, we just go ahead with the long pathname. The 1897 * object creation is likely to fail, but any error will get handled 1898 * at that time. 1899 */ 1900 #if defined(HAVE_FCHDIR) && defined(PATH_MAX) 1901 static void 1902 edit_deep_directories(struct archive_write_disk *a) 1903 { 1904 int ret; 1905 char *tail = a->name; 1906 1907 /* If path is short, avoid the open() below. */ 1908 if (strlen(tail) < PATH_MAX) 1909 return; 1910 1911 /* Try to record our starting dir. */ 1912 a->restore_pwd = open(".", O_RDONLY | O_BINARY | O_CLOEXEC); 1913 __archive_ensure_cloexec_flag(a->restore_pwd); 1914 if (a->restore_pwd < 0) 1915 return; 1916 1917 /* As long as the path is too long... */ 1918 while (strlen(tail) >= PATH_MAX) { 1919 /* Locate a dir prefix shorter than PATH_MAX. */ 1920 tail += PATH_MAX - 8; 1921 while (tail > a->name && *tail != '/') 1922 tail--; 1923 /* Exit if we find a too-long path component. */ 1924 if (tail <= a->name) 1925 return; 1926 /* Create the intermediate dir and chdir to it. */ 1927 *tail = '\0'; /* Terminate dir portion */ 1928 ret = create_dir(a, a->name); 1929 if (ret == ARCHIVE_OK && chdir(a->name) != 0) 1930 ret = ARCHIVE_FAILED; 1931 *tail = '/'; /* Restore the / we removed. */ 1932 if (ret != ARCHIVE_OK) 1933 return; 1934 tail++; 1935 /* The chdir() succeeded; we've now shortened the path. */ 1936 a->name = tail; 1937 } 1938 return; 1939 } 1940 #endif 1941 1942 /* 1943 * The main restore function. 1944 */ 1945 static int 1946 restore_entry(struct archive_write_disk *a) 1947 { 1948 int ret = ARCHIVE_OK, en; 1949 1950 if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) { 1951 /* 1952 * TODO: Fix this. Apparently, there are platforms 1953 * that still allow root to hose the entire filesystem 1954 * by unlinking a dir. The S_ISDIR() test above 1955 * prevents us from using unlink() here if the new 1956 * object is a dir, but that doesn't mean the old 1957 * object isn't a dir. 1958 */ 1959 if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS) 1960 (void)clear_nochange_fflags(a); 1961 if (unlink(a->name) == 0) { 1962 /* We removed it, reset cached stat. */ 1963 a->pst = NULL; 1964 } else if (errno == ENOENT) { 1965 /* File didn't exist, that's just as good. */ 1966 } else if (rmdir(a->name) == 0) { 1967 /* It was a dir, but now it's gone. */ 1968 a->pst = NULL; 1969 } else { 1970 /* We tried, but couldn't get rid of it. */ 1971 archive_set_error(&a->archive, errno, 1972 "Could not unlink"); 1973 return(ARCHIVE_FAILED); 1974 } 1975 } 1976 1977 /* Try creating it first; if this fails, we'll try to recover. */ 1978 en = create_filesystem_object(a); 1979 1980 if ((en == ENOTDIR || en == ENOENT) 1981 && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) { 1982 /* If the parent dir doesn't exist, try creating it. */ 1983 create_parent_dir(a, a->name); 1984 /* Now try to create the object again. */ 1985 en = create_filesystem_object(a); 1986 } 1987 1988 if ((en == ENOENT) && (archive_entry_hardlink(a->entry) != NULL)) { 1989 archive_set_error(&a->archive, en, 1990 "Hard-link target '%s' does not exist.", 1991 archive_entry_hardlink(a->entry)); 1992 return (ARCHIVE_FAILED); 1993 } 1994 1995 if ((en == EISDIR || en == EEXIST) 1996 && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) { 1997 /* If we're not overwriting, we're done. */ 1998 if (S_ISDIR(a->mode)) { 1999 /* Don't overwrite any settings on existing directories. */ 2000 a->todo = 0; 2001 } 2002 archive_entry_unset_size(a->entry); 2003 return (ARCHIVE_OK); 2004 } 2005 2006 /* 2007 * Some platforms return EISDIR if you call 2008 * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some 2009 * return EEXIST. POSIX is ambiguous, requiring EISDIR 2010 * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT) 2011 * on an existing item. 2012 */ 2013 if (en == EISDIR) { 2014 /* A dir is in the way of a non-dir, rmdir it. */ 2015 if (rmdir(a->name) != 0) { 2016 archive_set_error(&a->archive, errno, 2017 "Can't remove already-existing dir"); 2018 return (ARCHIVE_FAILED); 2019 } 2020 a->pst = NULL; 2021 /* Try again. */ 2022 en = create_filesystem_object(a); 2023 } else if (en == EEXIST) { 2024 /* 2025 * We know something is in the way, but we don't know what; 2026 * we need to find out before we go any further. 2027 */ 2028 int r = 0; 2029 /* 2030 * The SECURE_SYMLINKS logic has already removed a 2031 * symlink to a dir if the client wants that. So 2032 * follow the symlink if we're creating a dir. 2033 */ 2034 if (S_ISDIR(a->mode)) 2035 r = stat(a->name, &a->st); 2036 /* 2037 * If it's not a dir (or it's a broken symlink), 2038 * then don't follow it. 2039 */ 2040 if (r != 0 || !S_ISDIR(a->mode)) 2041 r = lstat(a->name, &a->st); 2042 if (r != 0) { 2043 archive_set_error(&a->archive, errno, 2044 "Can't stat existing object"); 2045 return (ARCHIVE_FAILED); 2046 } 2047 2048 /* 2049 * NO_OVERWRITE_NEWER doesn't apply to directories. 2050 */ 2051 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER) 2052 && !S_ISDIR(a->st.st_mode)) { 2053 if (!older(&(a->st), a->entry)) { 2054 archive_entry_unset_size(a->entry); 2055 return (ARCHIVE_OK); 2056 } 2057 } 2058 2059 /* If it's our archive, we're done. */ 2060 if (a->skip_file_set && 2061 a->st.st_dev == (dev_t)a->skip_file_dev && 2062 a->st.st_ino == (ino_t)a->skip_file_ino) { 2063 archive_set_error(&a->archive, 0, 2064 "Refusing to overwrite archive"); 2065 return (ARCHIVE_FAILED); 2066 } 2067 2068 if (!S_ISDIR(a->st.st_mode)) { 2069 /* A non-dir is in the way, unlink it. */ 2070 if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS) 2071 (void)clear_nochange_fflags(a); 2072 if (unlink(a->name) != 0) { 2073 archive_set_error(&a->archive, errno, 2074 "Can't unlink already-existing object"); 2075 return (ARCHIVE_FAILED); 2076 } 2077 a->pst = NULL; 2078 /* Try again. */ 2079 en = create_filesystem_object(a); 2080 } else if (!S_ISDIR(a->mode)) { 2081 /* A dir is in the way of a non-dir, rmdir it. */ 2082 if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS) 2083 (void)clear_nochange_fflags(a); 2084 if (rmdir(a->name) != 0) { 2085 archive_set_error(&a->archive, errno, 2086 "Can't replace existing directory with non-directory"); 2087 return (ARCHIVE_FAILED); 2088 } 2089 /* Try again. */ 2090 en = create_filesystem_object(a); 2091 } else { 2092 /* 2093 * There's a dir in the way of a dir. Don't 2094 * waste time with rmdir()/mkdir(), just fix 2095 * up the permissions on the existing dir. 2096 * Note that we don't change perms on existing 2097 * dirs unless _EXTRACT_PERM is specified. 2098 */ 2099 if ((a->mode != a->st.st_mode) 2100 && (a->todo & TODO_MODE_FORCE)) 2101 a->deferred |= (a->todo & TODO_MODE); 2102 /* Ownership doesn't need deferred fixup. */ 2103 en = 0; /* Forget the EEXIST. */ 2104 } 2105 } 2106 2107 if (en) { 2108 /* Everything failed; give up here. */ 2109 if ((&a->archive)->error == NULL) 2110 archive_set_error(&a->archive, en, "Can't create '%s'", 2111 a->name); 2112 return (ARCHIVE_FAILED); 2113 } 2114 2115 a->pst = NULL; /* Cached stat data no longer valid. */ 2116 return (ret); 2117 } 2118 2119 /* 2120 * Returns 0 if creation succeeds, or else returns errno value from 2121 * the failed system call. Note: This function should only ever perform 2122 * a single system call. 2123 */ 2124 static int 2125 create_filesystem_object(struct archive_write_disk *a) 2126 { 2127 /* Create the entry. */ 2128 const char *linkname; 2129 mode_t final_mode, mode; 2130 int r; 2131 /* these for check_symlinks_fsobj */ 2132 char *linkname_copy; /* non-const copy of linkname */ 2133 struct stat st; 2134 struct archive_string error_string; 2135 int error_number; 2136 2137 /* We identify hard/symlinks according to the link names. */ 2138 /* Since link(2) and symlink(2) don't handle modes, we're done here. */ 2139 linkname = archive_entry_hardlink(a->entry); 2140 if (linkname != NULL) { 2141 #if !HAVE_LINK 2142 return (EPERM); 2143 #else 2144 archive_string_init(&error_string); 2145 linkname_copy = strdup(linkname); 2146 if (linkname_copy == NULL) { 2147 return (EPERM); 2148 } 2149 /* 2150 * TODO: consider using the cleaned-up path as the link 2151 * target? 2152 */ 2153 r = cleanup_pathname_fsobj(linkname_copy, &error_number, 2154 &error_string, a->flags); 2155 if (r != ARCHIVE_OK) { 2156 archive_set_error(&a->archive, error_number, "%s", 2157 error_string.s); 2158 free(linkname_copy); 2159 archive_string_free(&error_string); 2160 /* 2161 * EPERM is more appropriate than error_number for our 2162 * callers 2163 */ 2164 return (EPERM); 2165 } 2166 r = check_symlinks_fsobj(linkname_copy, &error_number, 2167 &error_string, a->flags); 2168 if (r != ARCHIVE_OK) { 2169 archive_set_error(&a->archive, error_number, "%s", 2170 error_string.s); 2171 free(linkname_copy); 2172 archive_string_free(&error_string); 2173 /* 2174 * EPERM is more appropriate than error_number for our 2175 * callers 2176 */ 2177 return (EPERM); 2178 } 2179 free(linkname_copy); 2180 archive_string_free(&error_string); 2181 r = link(linkname, a->name) ? errno : 0; 2182 /* 2183 * New cpio and pax formats allow hardlink entries 2184 * to carry data, so we may have to open the file 2185 * for hardlink entries. 2186 * 2187 * If the hardlink was successfully created and 2188 * the archive doesn't have carry data for it, 2189 * consider it to be non-authoritative for meta data. 2190 * This is consistent with GNU tar and BSD pax. 2191 * If the hardlink does carry data, let the last 2192 * archive entry decide ownership. 2193 */ 2194 if (r == 0 && a->filesize <= 0) { 2195 a->todo = 0; 2196 a->deferred = 0; 2197 } else if (r == 0 && a->filesize > 0) { 2198 #ifdef HAVE_LSTAT 2199 r = lstat(a->name, &st); 2200 #else 2201 r = stat(a->name, &st); 2202 #endif 2203 if (r != 0) 2204 r = errno; 2205 else if ((st.st_mode & AE_IFMT) == AE_IFREG) { 2206 a->fd = open(a->name, O_WRONLY | O_TRUNC | 2207 O_BINARY | O_CLOEXEC | O_NOFOLLOW); 2208 __archive_ensure_cloexec_flag(a->fd); 2209 if (a->fd < 0) 2210 r = errno; 2211 } 2212 } 2213 return (r); 2214 #endif 2215 } 2216 linkname = archive_entry_symlink(a->entry); 2217 if (linkname != NULL) { 2218 #if HAVE_SYMLINK 2219 return symlink(linkname, a->name) ? errno : 0; 2220 #else 2221 return (EPERM); 2222 #endif 2223 } 2224 2225 /* 2226 * The remaining system calls all set permissions, so let's 2227 * try to take advantage of that to avoid an extra chmod() 2228 * call. (Recall that umask is set to zero right now!) 2229 */ 2230 2231 /* Mode we want for the final restored object (w/o file type bits). */ 2232 final_mode = a->mode & 07777; 2233 /* 2234 * The mode that will actually be restored in this step. Note 2235 * that SUID, SGID, etc, require additional work to ensure 2236 * security, so we never restore them at this point. 2237 */ 2238 mode = final_mode & 0777 & ~a->user_umask; 2239 2240 /* 2241 * Always create writable such that [f]setxattr() works if we're not 2242 * root. 2243 */ 2244 if (a->user_uid != 0 && 2245 a->todo & (TODO_HFS_COMPRESSION | TODO_XATTR)) { 2246 mode |= 0200; 2247 } 2248 2249 switch (a->mode & AE_IFMT) { 2250 default: 2251 /* POSIX requires that we fall through here. */ 2252 /* FALLTHROUGH */ 2253 case AE_IFREG: 2254 a->fd = open(a->name, 2255 O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, mode); 2256 __archive_ensure_cloexec_flag(a->fd); 2257 r = (a->fd < 0); 2258 break; 2259 case AE_IFCHR: 2260 #ifdef HAVE_MKNOD 2261 /* Note: we use AE_IFCHR for the case label, and 2262 * S_IFCHR for the mknod() call. This is correct. */ 2263 r = mknod(a->name, mode | S_IFCHR, 2264 archive_entry_rdev(a->entry)); 2265 break; 2266 #else 2267 /* TODO: Find a better way to warn about our inability 2268 * to restore a char device node. */ 2269 return (EINVAL); 2270 #endif /* HAVE_MKNOD */ 2271 case AE_IFBLK: 2272 #ifdef HAVE_MKNOD 2273 r = mknod(a->name, mode | S_IFBLK, 2274 archive_entry_rdev(a->entry)); 2275 break; 2276 #else 2277 /* TODO: Find a better way to warn about our inability 2278 * to restore a block device node. */ 2279 return (EINVAL); 2280 #endif /* HAVE_MKNOD */ 2281 case AE_IFDIR: 2282 mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE; 2283 r = mkdir(a->name, mode); 2284 if (r == 0) { 2285 /* Defer setting dir times. */ 2286 a->deferred |= (a->todo & TODO_TIMES); 2287 a->todo &= ~TODO_TIMES; 2288 /* Never use an immediate chmod(). */ 2289 /* We can't avoid the chmod() entirely if EXTRACT_PERM 2290 * because of SysV SGID inheritance. */ 2291 if ((mode != final_mode) 2292 || (a->flags & ARCHIVE_EXTRACT_PERM)) 2293 a->deferred |= (a->todo & TODO_MODE); 2294 a->todo &= ~TODO_MODE; 2295 } 2296 break; 2297 case AE_IFIFO: 2298 #ifdef HAVE_MKFIFO 2299 r = mkfifo(a->name, mode); 2300 break; 2301 #else 2302 /* TODO: Find a better way to warn about our inability 2303 * to restore a fifo. */ 2304 return (EINVAL); 2305 #endif /* HAVE_MKFIFO */ 2306 } 2307 2308 /* All the system calls above set errno on failure. */ 2309 if (r) 2310 return (errno); 2311 2312 /* If we managed to set the final mode, we've avoided a chmod(). */ 2313 if (mode == final_mode) 2314 a->todo &= ~TODO_MODE; 2315 return (0); 2316 } 2317 2318 /* 2319 * Cleanup function for archive_extract. Mostly, this involves processing 2320 * the fixup list, which is used to address a number of problems: 2321 * * Dir permissions might prevent us from restoring a file in that 2322 * dir, so we restore the dir with minimum 0700 permissions first, 2323 * then correct the mode at the end. 2324 * * Similarly, the act of restoring a file touches the directory 2325 * and changes the timestamp on the dir, so we have to touch-up dir 2326 * timestamps at the end as well. 2327 * * Some file flags can interfere with the restore by, for example, 2328 * preventing the creation of hardlinks to those files. 2329 * * Mac OS extended metadata includes ACLs, so must be deferred on dirs. 2330 * 2331 * Note that tar/cpio do not require that archives be in a particular 2332 * order; there is no way to know when the last file has been restored 2333 * within a directory, so there's no way to optimize the memory usage 2334 * here by fixing up the directory any earlier than the 2335 * end-of-archive. 2336 * 2337 * XXX TODO: Directory ACLs should be restored here, for the same 2338 * reason we set directory perms here. XXX 2339 */ 2340 static int 2341 _archive_write_disk_close(struct archive *_a) 2342 { 2343 struct archive_write_disk *a = (struct archive_write_disk *)_a; 2344 struct fixup_entry *next, *p; 2345 int ret; 2346 2347 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 2348 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 2349 "archive_write_disk_close"); 2350 ret = _archive_write_disk_finish_entry(&a->archive); 2351 2352 /* Sort dir list so directories are fixed up in depth-first order. */ 2353 p = sort_dir_list(a->fixup_list); 2354 2355 while (p != NULL) { 2356 a->pst = NULL; /* Mark stat cache as out-of-date. */ 2357 if (p->fixup & TODO_TIMES) { 2358 set_times(a, -1, p->mode, p->name, 2359 p->atime, p->atime_nanos, 2360 p->birthtime, p->birthtime_nanos, 2361 p->mtime, p->mtime_nanos, 2362 p->ctime, p->ctime_nanos); 2363 } 2364 if (p->fixup & TODO_MODE_BASE) 2365 chmod(p->name, p->mode); 2366 if (p->fixup & TODO_ACLS) 2367 archive_write_disk_set_acls(&a->archive, -1, p->name, 2368 &p->acl, p->mode); 2369 if (p->fixup & TODO_FFLAGS) 2370 set_fflags_platform(a, -1, p->name, 2371 p->mode, p->fflags_set, 0); 2372 if (p->fixup & TODO_MAC_METADATA) 2373 set_mac_metadata(a, p->name, p->mac_metadata, 2374 p->mac_metadata_size); 2375 next = p->next; 2376 archive_acl_clear(&p->acl); 2377 free(p->mac_metadata); 2378 free(p->name); 2379 free(p); 2380 p = next; 2381 } 2382 a->fixup_list = NULL; 2383 return (ret); 2384 } 2385 2386 static int 2387 _archive_write_disk_free(struct archive *_a) 2388 { 2389 struct archive_write_disk *a; 2390 int ret; 2391 if (_a == NULL) 2392 return (ARCHIVE_OK); 2393 archive_check_magic(_a, ARCHIVE_WRITE_DISK_MAGIC, 2394 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_disk_free"); 2395 a = (struct archive_write_disk *)_a; 2396 ret = _archive_write_disk_close(&a->archive); 2397 archive_write_disk_set_group_lookup(&a->archive, NULL, NULL, NULL); 2398 archive_write_disk_set_user_lookup(&a->archive, NULL, NULL, NULL); 2399 archive_entry_free(a->entry); 2400 archive_string_free(&a->_name_data); 2401 archive_string_free(&a->archive.error_string); 2402 archive_string_free(&a->path_safe); 2403 a->archive.magic = 0; 2404 __archive_clean(&a->archive); 2405 free(a->decmpfs_header_p); 2406 free(a->resource_fork); 2407 free(a->compressed_buffer); 2408 free(a->uncompressed_buffer); 2409 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\ 2410 && defined(HAVE_ZLIB_H) 2411 if (a->stream_valid) { 2412 switch (deflateEnd(&a->stream)) { 2413 case Z_OK: 2414 break; 2415 default: 2416 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 2417 "Failed to clean up compressor"); 2418 ret = ARCHIVE_FATAL; 2419 break; 2420 } 2421 } 2422 #endif 2423 free(a); 2424 return (ret); 2425 } 2426 2427 /* 2428 * Simple O(n log n) merge sort to order the fixup list. In 2429 * particular, we want to restore dir timestamps depth-first. 2430 */ 2431 static struct fixup_entry * 2432 sort_dir_list(struct fixup_entry *p) 2433 { 2434 struct fixup_entry *a, *b, *t; 2435 2436 if (p == NULL) 2437 return (NULL); 2438 /* A one-item list is already sorted. */ 2439 if (p->next == NULL) 2440 return (p); 2441 2442 /* Step 1: split the list. */ 2443 t = p; 2444 a = p->next->next; 2445 while (a != NULL) { 2446 /* Step a twice, t once. */ 2447 a = a->next; 2448 if (a != NULL) 2449 a = a->next; 2450 t = t->next; 2451 } 2452 /* Now, t is at the mid-point, so break the list here. */ 2453 b = t->next; 2454 t->next = NULL; 2455 a = p; 2456 2457 /* Step 2: Recursively sort the two sub-lists. */ 2458 a = sort_dir_list(a); 2459 b = sort_dir_list(b); 2460 2461 /* Step 3: Merge the returned lists. */ 2462 /* Pick the first element for the merged list. */ 2463 if (strcmp(a->name, b->name) > 0) { 2464 t = p = a; 2465 a = a->next; 2466 } else { 2467 t = p = b; 2468 b = b->next; 2469 } 2470 2471 /* Always put the later element on the list first. */ 2472 while (a != NULL && b != NULL) { 2473 if (strcmp(a->name, b->name) > 0) { 2474 t->next = a; 2475 a = a->next; 2476 } else { 2477 t->next = b; 2478 b = b->next; 2479 } 2480 t = t->next; 2481 } 2482 2483 /* Only one list is non-empty, so just splice it on. */ 2484 if (a != NULL) 2485 t->next = a; 2486 if (b != NULL) 2487 t->next = b; 2488 2489 return (p); 2490 } 2491 2492 /* 2493 * Returns a new, initialized fixup entry. 2494 * 2495 * TODO: Reduce the memory requirements for this list by using a tree 2496 * structure rather than a simple list of names. 2497 */ 2498 static struct fixup_entry * 2499 new_fixup(struct archive_write_disk *a, const char *pathname) 2500 { 2501 struct fixup_entry *fe; 2502 2503 fe = (struct fixup_entry *)calloc(1, sizeof(struct fixup_entry)); 2504 if (fe == NULL) { 2505 archive_set_error(&a->archive, ENOMEM, 2506 "Can't allocate memory for a fixup"); 2507 return (NULL); 2508 } 2509 fe->next = a->fixup_list; 2510 a->fixup_list = fe; 2511 fe->fixup = 0; 2512 fe->name = strdup(pathname); 2513 return (fe); 2514 } 2515 2516 /* 2517 * Returns a fixup structure for the current entry. 2518 */ 2519 static struct fixup_entry * 2520 current_fixup(struct archive_write_disk *a, const char *pathname) 2521 { 2522 if (a->current_fixup == NULL) 2523 a->current_fixup = new_fixup(a, pathname); 2524 return (a->current_fixup); 2525 } 2526 2527 /* Error helper for new *_fsobj functions */ 2528 static void 2529 fsobj_error(int *a_eno, struct archive_string *a_estr, 2530 int err, const char *errstr, const char *path) 2531 { 2532 if (a_eno) 2533 *a_eno = err; 2534 if (a_estr) 2535 archive_string_sprintf(a_estr, "%s%s", errstr, path); 2536 } 2537 2538 /* 2539 * TODO: Someday, integrate this with the deep dir support; they both 2540 * scan the path and both can be optimized by comparing against other 2541 * recent paths. 2542 */ 2543 /* TODO: Extend this to support symlinks on Windows Vista and later. */ 2544 2545 /* 2546 * Checks the given path to see if any elements along it are symlinks. Returns 2547 * ARCHIVE_OK if there are none, otherwise puts an error in errmsg. 2548 */ 2549 static int 2550 check_symlinks_fsobj(char *path, int *a_eno, struct archive_string *a_estr, 2551 int flags) 2552 { 2553 #if !defined(HAVE_LSTAT) 2554 /* Platform doesn't have lstat, so we can't look for symlinks. */ 2555 (void)path; /* UNUSED */ 2556 (void)error_number; /* UNUSED */ 2557 (void)error_string; /* UNUSED */ 2558 (void)flags; /* UNUSED */ 2559 return (ARCHIVE_OK); 2560 #else 2561 int res = ARCHIVE_OK; 2562 char *tail; 2563 char *head; 2564 int last; 2565 char c; 2566 int r; 2567 struct stat st; 2568 int restore_pwd; 2569 2570 /* Nothing to do here if name is empty */ 2571 if(path[0] == '\0') 2572 return (ARCHIVE_OK); 2573 2574 /* 2575 * Guard against symlink tricks. Reject any archive entry whose 2576 * destination would be altered by a symlink. 2577 * 2578 * Walk the filename in chunks separated by '/'. For each segment: 2579 * - if it doesn't exist, continue 2580 * - if it's symlink, abort or remove it 2581 * - if it's a directory and it's not the last chunk, cd into it 2582 * As we go: 2583 * head points to the current (relative) path 2584 * tail points to the temporary \0 terminating the segment we're 2585 * currently examining 2586 * c holds what used to be in *tail 2587 * last is 1 if this is the last tail 2588 */ 2589 restore_pwd = open(".", O_RDONLY | O_BINARY | O_CLOEXEC); 2590 __archive_ensure_cloexec_flag(restore_pwd); 2591 if (restore_pwd < 0) 2592 return (ARCHIVE_FATAL); 2593 head = path; 2594 tail = path; 2595 last = 0; 2596 /* TODO: reintroduce a safe cache here? */ 2597 /* Skip the root directory if the path is absolute. */ 2598 if(tail == path && tail[0] == '/') 2599 ++tail; 2600 /* Keep going until we've checked the entire name. 2601 * head, tail, path all alias the same string, which is 2602 * temporarily zeroed at tail, so be careful restoring the 2603 * stashed (c=tail[0]) for error messages. 2604 * Exiting the loop with break is okay; continue is not. 2605 */ 2606 while (!last) { 2607 /* 2608 * Skip the separator we just consumed, plus any adjacent ones 2609 */ 2610 while (*tail == '/') 2611 ++tail; 2612 /* Skip the next path element. */ 2613 while (*tail != '\0' && *tail != '/') 2614 ++tail; 2615 /* is this the last path component? */ 2616 last = (tail[0] == '\0') || (tail[0] == '/' && tail[1] == '\0'); 2617 /* temporarily truncate the string here */ 2618 c = tail[0]; 2619 tail[0] = '\0'; 2620 /* Check that we haven't hit a symlink. */ 2621 r = lstat(head, &st); 2622 if (r != 0) { 2623 tail[0] = c; 2624 /* We've hit a dir that doesn't exist; stop now. */ 2625 if (errno == ENOENT) { 2626 break; 2627 } else { 2628 /* 2629 * Treat any other error as fatal - best to be 2630 * paranoid here. 2631 * Note: This effectively disables deep 2632 * directory support when security checks are 2633 * enabled. Otherwise, very long pathnames that 2634 * trigger an error here could evade the 2635 * sandbox. 2636 * TODO: We could do better, but it would 2637 * probably require merging the symlink checks 2638 * with the deep-directory editing. 2639 */ 2640 fsobj_error(a_eno, a_estr, errno, 2641 "Could not stat ", path); 2642 res = ARCHIVE_FAILED; 2643 break; 2644 } 2645 } else if (S_ISDIR(st.st_mode)) { 2646 if (!last) { 2647 if (chdir(head) != 0) { 2648 tail[0] = c; 2649 fsobj_error(a_eno, a_estr, errno, 2650 "Could not chdir ", path); 2651 res = (ARCHIVE_FATAL); 2652 break; 2653 } 2654 /* Our view is now from inside this dir: */ 2655 head = tail + 1; 2656 } 2657 } else if (S_ISLNK(st.st_mode)) { 2658 if (last) { 2659 /* 2660 * Last element is symlink; remove it 2661 * so we can overwrite it with the 2662 * item being extracted. 2663 */ 2664 if (unlink(head)) { 2665 tail[0] = c; 2666 fsobj_error(a_eno, a_estr, errno, 2667 "Could not remove symlink ", 2668 path); 2669 res = ARCHIVE_FAILED; 2670 break; 2671 } 2672 /* 2673 * Even if we did remove it, a warning 2674 * is in order. The warning is silly, 2675 * though, if we're just replacing one 2676 * symlink with another symlink. 2677 */ 2678 tail[0] = c; 2679 /* 2680 * FIXME: not sure how important this is to 2681 * restore 2682 */ 2683 /* 2684 if (!S_ISLNK(path)) { 2685 fsobj_error(a_eno, a_estr, 0, 2686 "Removing symlink ", path); 2687 } 2688 */ 2689 /* Symlink gone. No more problem! */ 2690 res = ARCHIVE_OK; 2691 break; 2692 } else if (flags & ARCHIVE_EXTRACT_UNLINK) { 2693 /* User asked us to remove problems. */ 2694 if (unlink(head) != 0) { 2695 tail[0] = c; 2696 fsobj_error(a_eno, a_estr, 0, 2697 "Cannot remove intervening " 2698 "symlink ", path); 2699 res = ARCHIVE_FAILED; 2700 break; 2701 } 2702 tail[0] = c; 2703 } else if ((flags & 2704 ARCHIVE_EXTRACT_SECURE_SYMLINKS) == 0) { 2705 /* 2706 * We are not the last element and we want to 2707 * follow symlinks if they are a directory. 2708 * 2709 * This is needed to extract hardlinks over 2710 * symlinks. 2711 */ 2712 r = stat(head, &st); 2713 if (r != 0) { 2714 tail[0] = c; 2715 if (errno == ENOENT) { 2716 break; 2717 } else { 2718 fsobj_error(a_eno, a_estr, 2719 errno, 2720 "Could not stat ", path); 2721 res = (ARCHIVE_FAILED); 2722 break; 2723 } 2724 } else if (S_ISDIR(st.st_mode)) { 2725 if (chdir(head) != 0) { 2726 tail[0] = c; 2727 fsobj_error(a_eno, a_estr, 2728 errno, 2729 "Could not chdir ", path); 2730 res = (ARCHIVE_FATAL); 2731 break; 2732 } 2733 /* 2734 * Our view is now from inside 2735 * this dir: 2736 */ 2737 head = tail + 1; 2738 } else { 2739 tail[0] = c; 2740 fsobj_error(a_eno, a_estr, 0, 2741 "Cannot extract through " 2742 "symlink ", path); 2743 res = ARCHIVE_FAILED; 2744 break; 2745 } 2746 } else { 2747 tail[0] = c; 2748 fsobj_error(a_eno, a_estr, 0, 2749 "Cannot extract through symlink ", path); 2750 res = ARCHIVE_FAILED; 2751 break; 2752 } 2753 } 2754 /* be sure to always maintain this */ 2755 tail[0] = c; 2756 if (tail[0] != '\0') 2757 tail++; /* Advance to the next segment. */ 2758 } 2759 /* Catches loop exits via break */ 2760 tail[0] = c; 2761 #ifdef HAVE_FCHDIR 2762 /* If we changed directory above, restore it here. */ 2763 if (restore_pwd >= 0) { 2764 r = fchdir(restore_pwd); 2765 if (r != 0) { 2766 fsobj_error(a_eno, a_estr, errno, 2767 "chdir() failure", ""); 2768 } 2769 close(restore_pwd); 2770 restore_pwd = -1; 2771 if (r != 0) { 2772 res = (ARCHIVE_FATAL); 2773 } 2774 } 2775 #endif 2776 /* TODO: reintroduce a safe cache here? */ 2777 return res; 2778 #endif 2779 } 2780 2781 /* 2782 * Check a->name for symlinks, returning ARCHIVE_OK if its clean, otherwise 2783 * calls archive_set_error and returns ARCHIVE_{FATAL,FAILED} 2784 */ 2785 static int 2786 check_symlinks(struct archive_write_disk *a) 2787 { 2788 struct archive_string error_string; 2789 int error_number; 2790 int rc; 2791 archive_string_init(&error_string); 2792 rc = check_symlinks_fsobj(a->name, &error_number, &error_string, 2793 a->flags); 2794 if (rc != ARCHIVE_OK) { 2795 archive_set_error(&a->archive, error_number, "%s", 2796 error_string.s); 2797 } 2798 archive_string_free(&error_string); 2799 a->pst = NULL; /* to be safe */ 2800 return rc; 2801 } 2802 2803 2804 #if defined(__CYGWIN__) 2805 /* 2806 * 1. Convert a path separator from '\' to '/' . 2807 * We shouldn't check multibyte character directly because some 2808 * character-set have been using the '\' character for a part of 2809 * its multibyte character code. 2810 * 2. Replace unusable characters in Windows with underscore('_'). 2811 * See also : http://msdn.microsoft.com/en-us/library/aa365247.aspx 2812 */ 2813 static void 2814 cleanup_pathname_win(char *path) 2815 { 2816 wchar_t wc; 2817 char *p; 2818 size_t alen, l; 2819 int mb, complete, utf8; 2820 2821 alen = 0; 2822 mb = 0; 2823 complete = 1; 2824 utf8 = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)? 1: 0; 2825 for (p = path; *p != '\0'; p++) { 2826 ++alen; 2827 if (*p == '\\') { 2828 /* If previous byte is smaller than 128, 2829 * this is not second byte of multibyte characters, 2830 * so we can replace '\' with '/'. */ 2831 if (utf8 || !mb) 2832 *p = '/'; 2833 else 2834 complete = 0;/* uncompleted. */ 2835 } else if (*(unsigned char *)p > 127) 2836 mb = 1; 2837 else 2838 mb = 0; 2839 /* Rewrite the path name if its next character is unusable. */ 2840 if (*p == ':' || *p == '*' || *p == '?' || *p == '"' || 2841 *p == '<' || *p == '>' || *p == '|') 2842 *p = '_'; 2843 } 2844 if (complete) 2845 return; 2846 2847 /* 2848 * Convert path separator in wide-character. 2849 */ 2850 p = path; 2851 while (*p != '\0' && alen) { 2852 l = mbtowc(&wc, p, alen); 2853 if (l == (size_t)-1) { 2854 while (*p != '\0') { 2855 if (*p == '\\') 2856 *p = '/'; 2857 ++p; 2858 } 2859 break; 2860 } 2861 if (l == 1 && wc == L'\\') 2862 *p = '/'; 2863 p += l; 2864 alen -= l; 2865 } 2866 } 2867 #endif 2868 2869 /* 2870 * Canonicalize the pathname. In particular, this strips duplicate 2871 * '/' characters, '.' elements, and trailing '/'. It also raises an 2872 * error for an empty path, a trailing '..', (if _SECURE_NODOTDOT is 2873 * set) any '..' in the path or (if ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS 2874 * is set) if the path is absolute. 2875 */ 2876 static int 2877 cleanup_pathname_fsobj(char *path, int *a_eno, struct archive_string *a_estr, 2878 int flags) 2879 { 2880 char *dest, *src; 2881 char separator = '\0'; 2882 2883 dest = src = path; 2884 if (*src == '\0') { 2885 fsobj_error(a_eno, a_estr, ARCHIVE_ERRNO_MISC, 2886 "Invalid empty ", "pathname"); 2887 return (ARCHIVE_FAILED); 2888 } 2889 2890 #if defined(__CYGWIN__) 2891 cleanup_pathname_win(path); 2892 #endif 2893 /* Skip leading '/'. */ 2894 if (*src == '/') { 2895 if (flags & ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS) { 2896 fsobj_error(a_eno, a_estr, ARCHIVE_ERRNO_MISC, 2897 "Path is ", "absolute"); 2898 return (ARCHIVE_FAILED); 2899 } 2900 2901 separator = *src++; 2902 } 2903 2904 /* Scan the pathname one element at a time. */ 2905 for (;;) { 2906 /* src points to first char after '/' */ 2907 if (src[0] == '\0') { 2908 break; 2909 } else if (src[0] == '/') { 2910 /* Found '//', ignore second one. */ 2911 src++; 2912 continue; 2913 } else if (src[0] == '.') { 2914 if (src[1] == '\0') { 2915 /* Ignore trailing '.' */ 2916 break; 2917 } else if (src[1] == '/') { 2918 /* Skip './'. */ 2919 src += 2; 2920 continue; 2921 } else if (src[1] == '.') { 2922 if (src[2] == '/' || src[2] == '\0') { 2923 /* Conditionally warn about '..' */ 2924 if (flags 2925 & ARCHIVE_EXTRACT_SECURE_NODOTDOT) { 2926 fsobj_error(a_eno, a_estr, 2927 ARCHIVE_ERRNO_MISC, 2928 "Path contains ", "'..'"); 2929 return (ARCHIVE_FAILED); 2930 } 2931 } 2932 /* 2933 * Note: Under no circumstances do we 2934 * remove '..' elements. In 2935 * particular, restoring 2936 * '/foo/../bar/' should create the 2937 * 'foo' dir as a side-effect. 2938 */ 2939 } 2940 } 2941 2942 /* Copy current element, including leading '/'. */ 2943 if (separator) 2944 *dest++ = '/'; 2945 while (*src != '\0' && *src != '/') { 2946 *dest++ = *src++; 2947 } 2948 2949 if (*src == '\0') 2950 break; 2951 2952 /* Skip '/' separator. */ 2953 separator = *src++; 2954 } 2955 /* 2956 * We've just copied zero or more path elements, not including the 2957 * final '/'. 2958 */ 2959 if (dest == path) { 2960 /* 2961 * Nothing got copied. The path must have been something 2962 * like '.' or '/' or './' or '/././././/./'. 2963 */ 2964 if (separator) 2965 *dest++ = '/'; 2966 else 2967 *dest++ = '.'; 2968 } 2969 /* Terminate the result. */ 2970 *dest = '\0'; 2971 return (ARCHIVE_OK); 2972 } 2973 2974 static int 2975 cleanup_pathname(struct archive_write_disk *a) 2976 { 2977 struct archive_string error_string; 2978 int error_number; 2979 int rc; 2980 archive_string_init(&error_string); 2981 rc = cleanup_pathname_fsobj(a->name, &error_number, &error_string, 2982 a->flags); 2983 if (rc != ARCHIVE_OK) { 2984 archive_set_error(&a->archive, error_number, "%s", 2985 error_string.s); 2986 } 2987 archive_string_free(&error_string); 2988 return rc; 2989 } 2990 2991 /* 2992 * Create the parent directory of the specified path, assuming path 2993 * is already in mutable storage. 2994 */ 2995 static int 2996 create_parent_dir(struct archive_write_disk *a, char *path) 2997 { 2998 char *slash; 2999 int r; 3000 3001 /* Remove tail element to obtain parent name. */ 3002 slash = strrchr(path, '/'); 3003 if (slash == NULL) 3004 return (ARCHIVE_OK); 3005 *slash = '\0'; 3006 r = create_dir(a, path); 3007 *slash = '/'; 3008 return (r); 3009 } 3010 3011 /* 3012 * Create the specified dir, recursing to create parents as necessary. 3013 * 3014 * Returns ARCHIVE_OK if the path exists when we're done here. 3015 * Otherwise, returns ARCHIVE_FAILED. 3016 * Assumes path is in mutable storage; path is unchanged on exit. 3017 */ 3018 static int 3019 create_dir(struct archive_write_disk *a, char *path) 3020 { 3021 struct stat st; 3022 struct fixup_entry *le; 3023 char *slash, *base; 3024 mode_t mode_final, mode; 3025 int r; 3026 3027 /* Check for special names and just skip them. */ 3028 slash = strrchr(path, '/'); 3029 if (slash == NULL) 3030 base = path; 3031 else 3032 base = slash + 1; 3033 3034 if (base[0] == '\0' || 3035 (base[0] == '.' && base[1] == '\0') || 3036 (base[0] == '.' && base[1] == '.' && base[2] == '\0')) { 3037 /* Don't bother trying to create null path, '.', or '..'. */ 3038 if (slash != NULL) { 3039 *slash = '\0'; 3040 r = create_dir(a, path); 3041 *slash = '/'; 3042 return (r); 3043 } 3044 return (ARCHIVE_OK); 3045 } 3046 3047 /* 3048 * Yes, this should be stat() and not lstat(). Using lstat() 3049 * here loses the ability to extract through symlinks. Also note 3050 * that this should not use the a->st cache. 3051 */ 3052 if (stat(path, &st) == 0) { 3053 if (S_ISDIR(st.st_mode)) 3054 return (ARCHIVE_OK); 3055 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) { 3056 archive_set_error(&a->archive, EEXIST, 3057 "Can't create directory '%s'", path); 3058 return (ARCHIVE_FAILED); 3059 } 3060 if (unlink(path) != 0) { 3061 archive_set_error(&a->archive, errno, 3062 "Can't create directory '%s': " 3063 "Conflicting file cannot be removed", 3064 path); 3065 return (ARCHIVE_FAILED); 3066 } 3067 } else if (errno != ENOENT && errno != ENOTDIR) { 3068 /* Stat failed? */ 3069 archive_set_error(&a->archive, errno, 3070 "Can't test directory '%s'", path); 3071 return (ARCHIVE_FAILED); 3072 } else if (slash != NULL) { 3073 *slash = '\0'; 3074 r = create_dir(a, path); 3075 *slash = '/'; 3076 if (r != ARCHIVE_OK) 3077 return (r); 3078 } 3079 3080 /* 3081 * Mode we want for the final restored directory. Per POSIX, 3082 * implicitly-created dirs must be created obeying the umask. 3083 * There's no mention whether this is different for privileged 3084 * restores (which the rest of this code handles by pretending 3085 * umask=0). I've chosen here to always obey the user's umask for 3086 * implicit dirs, even if _EXTRACT_PERM was specified. 3087 */ 3088 mode_final = DEFAULT_DIR_MODE & ~a->user_umask; 3089 /* Mode we want on disk during the restore process. */ 3090 mode = mode_final; 3091 mode |= MINIMUM_DIR_MODE; 3092 mode &= MAXIMUM_DIR_MODE; 3093 if (mkdir(path, mode) == 0) { 3094 if (mode != mode_final) { 3095 le = new_fixup(a, path); 3096 if (le == NULL) 3097 return (ARCHIVE_FATAL); 3098 le->fixup |=TODO_MODE_BASE; 3099 le->mode = mode_final; 3100 } 3101 return (ARCHIVE_OK); 3102 } 3103 3104 /* 3105 * Without the following check, a/b/../b/c/d fails at the 3106 * second visit to 'b', so 'd' can't be created. Note that we 3107 * don't add it to the fixup list here, as it's already been 3108 * added. 3109 */ 3110 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) 3111 return (ARCHIVE_OK); 3112 3113 archive_set_error(&a->archive, errno, "Failed to create dir '%s'", 3114 path); 3115 return (ARCHIVE_FAILED); 3116 } 3117 3118 /* 3119 * Note: Although we can skip setting the user id if the desired user 3120 * id matches the current user, we cannot skip setting the group, as 3121 * many systems set the gid based on the containing directory. So 3122 * we have to perform a chown syscall if we want to set the SGID 3123 * bit. (The alternative is to stat() and then possibly chown(); it's 3124 * more efficient to skip the stat() and just always chown().) Note 3125 * that a successful chown() here clears the TODO_SGID_CHECK bit, which 3126 * allows set_mode to skip the stat() check for the GID. 3127 */ 3128 static int 3129 set_ownership(struct archive_write_disk *a) 3130 { 3131 #ifndef __CYGWIN__ 3132 /* unfortunately, on win32 there is no 'root' user with uid 0, 3133 so we just have to try the chown and see if it works */ 3134 3135 /* If we know we can't change it, don't bother trying. */ 3136 if (a->user_uid != 0 && a->user_uid != a->uid) { 3137 archive_set_error(&a->archive, errno, 3138 "Can't set UID=%jd", (intmax_t)a->uid); 3139 return (ARCHIVE_WARN); 3140 } 3141 #endif 3142 3143 #ifdef HAVE_FCHOWN 3144 /* If we have an fd, we can avoid a race. */ 3145 if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0) { 3146 /* We've set owner and know uid/gid are correct. */ 3147 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK); 3148 return (ARCHIVE_OK); 3149 } 3150 #endif 3151 3152 /* We prefer lchown() but will use chown() if that's all we have. */ 3153 /* Of course, if we have neither, this will always fail. */ 3154 #ifdef HAVE_LCHOWN 3155 if (lchown(a->name, a->uid, a->gid) == 0) { 3156 /* We've set owner and know uid/gid are correct. */ 3157 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK); 3158 return (ARCHIVE_OK); 3159 } 3160 #elif HAVE_CHOWN 3161 if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0) { 3162 /* We've set owner and know uid/gid are correct. */ 3163 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK); 3164 return (ARCHIVE_OK); 3165 } 3166 #endif 3167 3168 archive_set_error(&a->archive, errno, 3169 "Can't set user=%jd/group=%jd for %s", 3170 (intmax_t)a->uid, (intmax_t)a->gid, a->name); 3171 return (ARCHIVE_WARN); 3172 } 3173 3174 /* 3175 * Note: Returns 0 on success, non-zero on failure. 3176 */ 3177 static int 3178 set_time(int fd, int mode, const char *name, 3179 time_t atime, long atime_nsec, 3180 time_t mtime, long mtime_nsec) 3181 { 3182 /* Select the best implementation for this platform. */ 3183 #if defined(HAVE_UTIMENSAT) && defined(HAVE_FUTIMENS) 3184 /* 3185 * utimensat() and futimens() are defined in 3186 * POSIX.1-2008. They support ns resolution and setting times 3187 * on fds and symlinks. 3188 */ 3189 struct timespec ts[2]; 3190 (void)mode; /* UNUSED */ 3191 ts[0].tv_sec = atime; 3192 ts[0].tv_nsec = atime_nsec; 3193 ts[1].tv_sec = mtime; 3194 ts[1].tv_nsec = mtime_nsec; 3195 if (fd >= 0) 3196 return futimens(fd, ts); 3197 return utimensat(AT_FDCWD, name, ts, AT_SYMLINK_NOFOLLOW); 3198 3199 #elif HAVE_UTIMES 3200 /* 3201 * The utimes()-family functions support µs-resolution and 3202 * setting times fds and symlinks. utimes() is documented as 3203 * LEGACY by POSIX, futimes() and lutimes() are not described 3204 * in POSIX. 3205 */ 3206 struct timeval times[2]; 3207 3208 times[0].tv_sec = atime; 3209 times[0].tv_usec = atime_nsec / 1000; 3210 times[1].tv_sec = mtime; 3211 times[1].tv_usec = mtime_nsec / 1000; 3212 3213 #ifdef HAVE_FUTIMES 3214 if (fd >= 0) 3215 return (futimes(fd, times)); 3216 #else 3217 (void)fd; /* UNUSED */ 3218 #endif 3219 #ifdef HAVE_LUTIMES 3220 (void)mode; /* UNUSED */ 3221 return (lutimes(name, times)); 3222 #else 3223 if (S_ISLNK(mode)) 3224 return (0); 3225 return (utimes(name, times)); 3226 #endif 3227 3228 #elif defined(HAVE_UTIME) 3229 /* 3230 * utime() is POSIX-standard but only supports 1s resolution and 3231 * does not support fds or symlinks. 3232 */ 3233 struct utimbuf times; 3234 (void)fd; /* UNUSED */ 3235 (void)name; /* UNUSED */ 3236 (void)atime_nsec; /* UNUSED */ 3237 (void)mtime_nsec; /* UNUSED */ 3238 times.actime = atime; 3239 times.modtime = mtime; 3240 if (S_ISLNK(mode)) 3241 return (ARCHIVE_OK); 3242 return (utime(name, ×)); 3243 3244 #else 3245 /* 3246 * We don't know how to set the time on this platform. 3247 */ 3248 (void)fd; /* UNUSED */ 3249 (void)mode; /* UNUSED */ 3250 (void)name; /* UNUSED */ 3251 (void)atime_nsec; /* UNUSED */ 3252 (void)mtime_nsec; /* UNUSED */ 3253 return (ARCHIVE_WARN); 3254 #endif 3255 } 3256 3257 #ifdef F_SETTIMES 3258 static int 3259 set_time_tru64(int fd, int mode, const char *name, 3260 time_t atime, long atime_nsec, 3261 time_t mtime, long mtime_nsec, 3262 time_t ctime, long ctime_nsec) 3263 { 3264 struct attr_timbuf tstamp; 3265 tstamp.atime.tv_sec = atime; 3266 tstamp.mtime.tv_sec = mtime; 3267 tstamp.ctime.tv_sec = ctime; 3268 #if defined (__hpux) && defined (__ia64) 3269 tstamp.atime.tv_nsec = atime_nsec; 3270 tstamp.mtime.tv_nsec = mtime_nsec; 3271 tstamp.ctime.tv_nsec = ctime_nsec; 3272 #else 3273 tstamp.atime.tv_usec = atime_nsec / 1000; 3274 tstamp.mtime.tv_usec = mtime_nsec / 1000; 3275 tstamp.ctime.tv_usec = ctime_nsec / 1000; 3276 #endif 3277 return (fcntl(fd,F_SETTIMES,&tstamp)); 3278 } 3279 #endif /* F_SETTIMES */ 3280 3281 static int 3282 set_times(struct archive_write_disk *a, 3283 int fd, int mode, const char *name, 3284 time_t atime, long atime_nanos, 3285 time_t birthtime, long birthtime_nanos, 3286 time_t mtime, long mtime_nanos, 3287 time_t cctime, long ctime_nanos) 3288 { 3289 /* Note: set_time doesn't use libarchive return conventions! 3290 * It uses syscall conventions. So 0 here instead of ARCHIVE_OK. */ 3291 int r1 = 0, r2 = 0; 3292 3293 #ifdef F_SETTIMES 3294 /* 3295 * on Tru64 try own fcntl first which can restore even the 3296 * ctime, fall back to default code path below if it fails 3297 * or if we are not running as root 3298 */ 3299 if (a->user_uid == 0 && 3300 set_time_tru64(fd, mode, name, 3301 atime, atime_nanos, mtime, 3302 mtime_nanos, cctime, ctime_nanos) == 0) { 3303 return (ARCHIVE_OK); 3304 } 3305 #else /* Tru64 */ 3306 (void)cctime; /* UNUSED */ 3307 (void)ctime_nanos; /* UNUSED */ 3308 #endif /* Tru64 */ 3309 3310 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME 3311 /* 3312 * If you have struct stat.st_birthtime, we assume BSD 3313 * birthtime semantics, in which {f,l,}utimes() updates 3314 * birthtime to earliest mtime. So we set the time twice, 3315 * first using the birthtime, then using the mtime. If 3316 * birthtime == mtime, this isn't necessary, so we skip it. 3317 * If birthtime > mtime, then this won't work, so we skip it. 3318 */ 3319 if (birthtime < mtime 3320 || (birthtime == mtime && birthtime_nanos < mtime_nanos)) 3321 r1 = set_time(fd, mode, name, 3322 atime, atime_nanos, 3323 birthtime, birthtime_nanos); 3324 #else 3325 (void)birthtime; /* UNUSED */ 3326 (void)birthtime_nanos; /* UNUSED */ 3327 #endif 3328 r2 = set_time(fd, mode, name, 3329 atime, atime_nanos, 3330 mtime, mtime_nanos); 3331 if (r1 != 0 || r2 != 0) { 3332 archive_set_error(&a->archive, errno, 3333 "Can't restore time"); 3334 return (ARCHIVE_WARN); 3335 } 3336 return (ARCHIVE_OK); 3337 } 3338 3339 static int 3340 set_times_from_entry(struct archive_write_disk *a) 3341 { 3342 time_t atime, birthtime, mtime, cctime; 3343 long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec; 3344 3345 /* Suitable defaults. */ 3346 atime = birthtime = mtime = cctime = a->start_time; 3347 atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0; 3348 3349 /* If no time was provided, we're done. */ 3350 if (!archive_entry_atime_is_set(a->entry) 3351 #if HAVE_STRUCT_STAT_ST_BIRTHTIME 3352 && !archive_entry_birthtime_is_set(a->entry) 3353 #endif 3354 && !archive_entry_mtime_is_set(a->entry)) 3355 return (ARCHIVE_OK); 3356 3357 if (archive_entry_atime_is_set(a->entry)) { 3358 atime = archive_entry_atime(a->entry); 3359 atime_nsec = archive_entry_atime_nsec(a->entry); 3360 } 3361 if (archive_entry_birthtime_is_set(a->entry)) { 3362 birthtime = archive_entry_birthtime(a->entry); 3363 birthtime_nsec = archive_entry_birthtime_nsec(a->entry); 3364 } 3365 if (archive_entry_mtime_is_set(a->entry)) { 3366 mtime = archive_entry_mtime(a->entry); 3367 mtime_nsec = archive_entry_mtime_nsec(a->entry); 3368 } 3369 if (archive_entry_ctime_is_set(a->entry)) { 3370 cctime = archive_entry_ctime(a->entry); 3371 ctime_nsec = archive_entry_ctime_nsec(a->entry); 3372 } 3373 3374 return set_times(a, a->fd, a->mode, a->name, 3375 atime, atime_nsec, 3376 birthtime, birthtime_nsec, 3377 mtime, mtime_nsec, 3378 cctime, ctime_nsec); 3379 } 3380 3381 static int 3382 set_mode(struct archive_write_disk *a, int mode) 3383 { 3384 int r = ARCHIVE_OK; 3385 mode &= 07777; /* Strip off file type bits. */ 3386 3387 if (a->todo & TODO_SGID_CHECK) { 3388 /* 3389 * If we don't know the GID is right, we must stat() 3390 * to verify it. We can't just check the GID of this 3391 * process, since systems sometimes set GID from 3392 * the enclosing dir or based on ACLs. 3393 */ 3394 if ((r = lazy_stat(a)) != ARCHIVE_OK) 3395 return (r); 3396 if (a->pst->st_gid != a->gid) { 3397 mode &= ~ S_ISGID; 3398 if (a->flags & ARCHIVE_EXTRACT_OWNER) { 3399 /* 3400 * This is only an error if you 3401 * requested owner restore. If you 3402 * didn't, we'll try to restore 3403 * sgid/suid, but won't consider it a 3404 * problem if we can't. 3405 */ 3406 archive_set_error(&a->archive, -1, 3407 "Can't restore SGID bit"); 3408 r = ARCHIVE_WARN; 3409 } 3410 } 3411 /* While we're here, double-check the UID. */ 3412 if (a->pst->st_uid != a->uid 3413 && (a->todo & TODO_SUID)) { 3414 mode &= ~ S_ISUID; 3415 if (a->flags & ARCHIVE_EXTRACT_OWNER) { 3416 archive_set_error(&a->archive, -1, 3417 "Can't restore SUID bit"); 3418 r = ARCHIVE_WARN; 3419 } 3420 } 3421 a->todo &= ~TODO_SGID_CHECK; 3422 a->todo &= ~TODO_SUID_CHECK; 3423 } else if (a->todo & TODO_SUID_CHECK) { 3424 /* 3425 * If we don't know the UID is right, we can just check 3426 * the user, since all systems set the file UID from 3427 * the process UID. 3428 */ 3429 if (a->user_uid != a->uid) { 3430 mode &= ~ S_ISUID; 3431 if (a->flags & ARCHIVE_EXTRACT_OWNER) { 3432 archive_set_error(&a->archive, -1, 3433 "Can't make file SUID"); 3434 r = ARCHIVE_WARN; 3435 } 3436 } 3437 a->todo &= ~TODO_SUID_CHECK; 3438 } 3439 3440 if (S_ISLNK(a->mode)) { 3441 #ifdef HAVE_LCHMOD 3442 /* 3443 * If this is a symlink, use lchmod(). If the 3444 * platform doesn't support lchmod(), just skip it. A 3445 * platform that doesn't provide a way to set 3446 * permissions on symlinks probably ignores 3447 * permissions on symlinks, so a failure here has no 3448 * impact. 3449 */ 3450 if (lchmod(a->name, mode) != 0) { 3451 switch (errno) { 3452 case ENOTSUP: 3453 case ENOSYS: 3454 #if ENOTSUP != EOPNOTSUPP 3455 case EOPNOTSUPP: 3456 #endif 3457 /* 3458 * if lchmod is defined but the platform 3459 * doesn't support it, silently ignore 3460 * error 3461 */ 3462 break; 3463 default: 3464 archive_set_error(&a->archive, errno, 3465 "Can't set permissions to 0%o", (int)mode); 3466 r = ARCHIVE_WARN; 3467 } 3468 } 3469 #endif 3470 } else if (!S_ISDIR(a->mode)) { 3471 /* 3472 * If it's not a symlink and not a dir, then use 3473 * fchmod() or chmod(), depending on whether we have 3474 * an fd. Dirs get their perms set during the 3475 * post-extract fixup, which is handled elsewhere. 3476 */ 3477 #ifdef HAVE_FCHMOD 3478 if (a->fd >= 0) { 3479 if (fchmod(a->fd, mode) != 0) { 3480 archive_set_error(&a->archive, errno, 3481 "Can't set permissions to 0%o", (int)mode); 3482 r = ARCHIVE_WARN; 3483 } 3484 } else 3485 #endif 3486 /* If this platform lacks fchmod(), then 3487 * we'll just use chmod(). */ 3488 if (chmod(a->name, mode) != 0) { 3489 archive_set_error(&a->archive, errno, 3490 "Can't set permissions to 0%o", (int)mode); 3491 r = ARCHIVE_WARN; 3492 } 3493 } 3494 return (r); 3495 } 3496 3497 static int 3498 set_fflags(struct archive_write_disk *a) 3499 { 3500 struct fixup_entry *le; 3501 unsigned long set, clear; 3502 int r; 3503 int critical_flags; 3504 mode_t mode = archive_entry_mode(a->entry); 3505 3506 /* 3507 * Make 'critical_flags' hold all file flags that can't be 3508 * immediately restored. For example, on BSD systems, 3509 * SF_IMMUTABLE prevents hardlinks from being created, so 3510 * should not be set until after any hardlinks are created. To 3511 * preserve some semblance of portability, this uses #ifdef 3512 * extensively. Ugly, but it works. 3513 * 3514 * Yes, Virginia, this does create a security race. It's mitigated 3515 * somewhat by the practice of creating dirs 0700 until the extract 3516 * is done, but it would be nice if we could do more than that. 3517 * People restoring critical file systems should be wary of 3518 * other programs that might try to muck with files as they're 3519 * being restored. 3520 */ 3521 /* Hopefully, the compiler will optimize this mess into a constant. */ 3522 critical_flags = 0; 3523 #ifdef SF_IMMUTABLE 3524 critical_flags |= SF_IMMUTABLE; 3525 #endif 3526 #ifdef UF_IMMUTABLE 3527 critical_flags |= UF_IMMUTABLE; 3528 #endif 3529 #ifdef SF_APPEND 3530 critical_flags |= SF_APPEND; 3531 #endif 3532 #ifdef UF_APPEND 3533 critical_flags |= UF_APPEND; 3534 #endif 3535 #if defined(FS_APPEND_FL) 3536 critical_flags |= FS_APPEND_FL; 3537 #elif defined(EXT2_APPEND_FL) 3538 critical_flags |= EXT2_APPEND_FL; 3539 #endif 3540 #if defined(FS_IMMUTABLE_FL) 3541 critical_flags |= FS_IMMUTABLE_FL; 3542 #elif defined(EXT2_IMMUTABLE_FL) 3543 critical_flags |= EXT2_IMMUTABLE_FL; 3544 #endif 3545 #ifdef FS_JOURNAL_DATA_FL 3546 critical_flags |= FS_JOURNAL_DATA_FL; 3547 #endif 3548 3549 if (a->todo & TODO_FFLAGS) { 3550 archive_entry_fflags(a->entry, &set, &clear); 3551 3552 /* 3553 * The first test encourages the compiler to eliminate 3554 * all of this if it's not necessary. 3555 */ 3556 if ((critical_flags != 0) && (set & critical_flags)) { 3557 le = current_fixup(a, a->name); 3558 if (le == NULL) 3559 return (ARCHIVE_FATAL); 3560 le->fixup |= TODO_FFLAGS; 3561 le->fflags_set = set; 3562 /* Store the mode if it's not already there. */ 3563 if ((le->fixup & TODO_MODE) == 0) 3564 le->mode = mode; 3565 } else { 3566 r = set_fflags_platform(a, a->fd, 3567 a->name, mode, set, clear); 3568 if (r != ARCHIVE_OK) 3569 return (r); 3570 } 3571 } 3572 return (ARCHIVE_OK); 3573 } 3574 3575 static int 3576 clear_nochange_fflags(struct archive_write_disk *a) 3577 { 3578 int nochange_flags; 3579 mode_t mode = archive_entry_mode(a->entry); 3580 3581 /* Hopefully, the compiler will optimize this mess into a constant. */ 3582 nochange_flags = 0; 3583 #ifdef SF_IMMUTABLE 3584 nochange_flags |= SF_IMMUTABLE; 3585 #endif 3586 #ifdef UF_IMMUTABLE 3587 nochange_flags |= UF_IMMUTABLE; 3588 #endif 3589 #ifdef SF_APPEND 3590 nochange_flags |= SF_APPEND; 3591 #endif 3592 #ifdef UF_APPEND 3593 nochange_flags |= UF_APPEND; 3594 #endif 3595 #ifdef EXT2_APPEND_FL 3596 nochange_flags |= EXT2_APPEND_FL; 3597 #endif 3598 #ifdef EXT2_IMMUTABLE_FL 3599 nochange_flags |= EXT2_IMMUTABLE_FL; 3600 #endif 3601 3602 return (set_fflags_platform(a, a->fd, a->name, mode, 0, 3603 nochange_flags)); 3604 } 3605 3606 3607 #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && defined(HAVE_STRUCT_STAT_ST_FLAGS) 3608 /* 3609 * BSD reads flags using stat() and sets them with one of {f,l,}chflags() 3610 */ 3611 static int 3612 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name, 3613 mode_t mode, unsigned long set, unsigned long clear) 3614 { 3615 int r; 3616 3617 (void)mode; /* UNUSED */ 3618 if (set == 0 && clear == 0) 3619 return (ARCHIVE_OK); 3620 3621 /* 3622 * XXX Is the stat here really necessary? Or can I just use 3623 * the 'set' flags directly? In particular, I'm not sure 3624 * about the correct approach if we're overwriting an existing 3625 * file that already has flags on it. XXX 3626 */ 3627 if ((r = lazy_stat(a)) != ARCHIVE_OK) 3628 return (r); 3629 3630 a->st.st_flags &= ~clear; 3631 a->st.st_flags |= set; 3632 #ifdef HAVE_FCHFLAGS 3633 /* If platform has fchflags() and we were given an fd, use it. */ 3634 if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0) 3635 return (ARCHIVE_OK); 3636 #endif 3637 /* 3638 * If we can't use the fd to set the flags, we'll use the 3639 * pathname to set flags. We prefer lchflags() but will use 3640 * chflags() if we must. 3641 */ 3642 #ifdef HAVE_LCHFLAGS 3643 if (lchflags(name, a->st.st_flags) == 0) 3644 return (ARCHIVE_OK); 3645 #elif defined(HAVE_CHFLAGS) 3646 if (S_ISLNK(a->st.st_mode)) { 3647 archive_set_error(&a->archive, errno, 3648 "Can't set file flags on symlink."); 3649 return (ARCHIVE_WARN); 3650 } 3651 if (chflags(name, a->st.st_flags) == 0) 3652 return (ARCHIVE_OK); 3653 #endif 3654 archive_set_error(&a->archive, errno, 3655 "Failed to set file flags"); 3656 return (ARCHIVE_WARN); 3657 } 3658 3659 #elif (defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS) && \ 3660 defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \ 3661 (defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS) && \ 3662 defined(HAVE_WORKING_EXT2_IOC_GETFLAGS)) 3663 /* 3664 * Linux uses ioctl() to read and write file flags. 3665 */ 3666 static int 3667 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name, 3668 mode_t mode, unsigned long set, unsigned long clear) 3669 { 3670 int ret; 3671 int myfd = fd; 3672 int newflags, oldflags; 3673 int sf_mask = 0; 3674 3675 if (set == 0 && clear == 0) 3676 return (ARCHIVE_OK); 3677 /* Only regular files and dirs can have flags. */ 3678 if (!S_ISREG(mode) && !S_ISDIR(mode)) 3679 return (ARCHIVE_OK); 3680 3681 /* If we weren't given an fd, open it ourselves. */ 3682 if (myfd < 0) { 3683 myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY | O_CLOEXEC); 3684 __archive_ensure_cloexec_flag(myfd); 3685 } 3686 if (myfd < 0) 3687 return (ARCHIVE_OK); 3688 3689 /* 3690 * Linux has no define for the flags that are only settable by 3691 * the root user. This code may seem a little complex, but 3692 * there seem to be some Linux systems that lack these 3693 * defines. (?) The code below degrades reasonably gracefully 3694 * if sf_mask is incomplete. 3695 */ 3696 #if defined(FS_IMMUTABLE_FL) 3697 sf_mask |= FS_IMMUTABLE_FL; 3698 #elif defined(EXT2_IMMUTABLE_FL) 3699 sf_mask |= EXT2_IMMUTABLE_FL; 3700 #endif 3701 #if defined(FS_APPEND_FL) 3702 sf_mask |= FS_APPEND_FL; 3703 #elif defined(EXT2_APPEND_FL) 3704 sf_mask |= EXT2_APPEND_FL; 3705 #endif 3706 #if defined(FS_JOURNAL_DATA_FL) 3707 sf_mask |= FS_JOURNAL_DATA_FL; 3708 #endif 3709 /* 3710 * XXX As above, this would be way simpler if we didn't have 3711 * to read the current flags from disk. XXX 3712 */ 3713 ret = ARCHIVE_OK; 3714 3715 /* Read the current file flags. */ 3716 if (ioctl(myfd, 3717 #ifdef FS_IOC_GETFLAGS 3718 FS_IOC_GETFLAGS, 3719 #else 3720 EXT2_IOC_GETFLAGS, 3721 #endif 3722 &oldflags) < 0) 3723 goto fail; 3724 3725 /* Try setting the flags as given. */ 3726 newflags = (oldflags & ~clear) | set; 3727 if (ioctl(myfd, 3728 #ifdef FS_IOC_SETFLAGS 3729 FS_IOC_SETFLAGS, 3730 #else 3731 EXT2_IOC_SETFLAGS, 3732 #endif 3733 &newflags) >= 0) 3734 goto cleanup; 3735 if (errno != EPERM) 3736 goto fail; 3737 3738 /* If we couldn't set all the flags, try again with a subset. */ 3739 newflags &= ~sf_mask; 3740 oldflags &= sf_mask; 3741 newflags |= oldflags; 3742 if (ioctl(myfd, 3743 #ifdef FS_IOC_SETFLAGS 3744 FS_IOC_SETFLAGS, 3745 #else 3746 EXT2_IOC_SETFLAGS, 3747 #endif 3748 &newflags) >= 0) 3749 goto cleanup; 3750 3751 /* We couldn't set the flags, so report the failure. */ 3752 fail: 3753 archive_set_error(&a->archive, errno, 3754 "Failed to set file flags"); 3755 ret = ARCHIVE_WARN; 3756 cleanup: 3757 if (fd < 0) 3758 close(myfd); 3759 return (ret); 3760 } 3761 3762 #else 3763 3764 /* 3765 * Of course, some systems have neither BSD chflags() nor Linux' flags 3766 * support through ioctl(). 3767 */ 3768 static int 3769 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name, 3770 mode_t mode, unsigned long set, unsigned long clear) 3771 { 3772 (void)a; /* UNUSED */ 3773 (void)fd; /* UNUSED */ 3774 (void)name; /* UNUSED */ 3775 (void)mode; /* UNUSED */ 3776 (void)set; /* UNUSED */ 3777 (void)clear; /* UNUSED */ 3778 return (ARCHIVE_OK); 3779 } 3780 3781 #endif /* __linux */ 3782 3783 #ifndef HAVE_COPYFILE_H 3784 /* Default is to simply drop Mac extended metadata. */ 3785 static int 3786 set_mac_metadata(struct archive_write_disk *a, const char *pathname, 3787 const void *metadata, size_t metadata_size) 3788 { 3789 (void)a; /* UNUSED */ 3790 (void)pathname; /* UNUSED */ 3791 (void)metadata; /* UNUSED */ 3792 (void)metadata_size; /* UNUSED */ 3793 return (ARCHIVE_OK); 3794 } 3795 3796 static int 3797 fixup_appledouble(struct archive_write_disk *a, const char *pathname) 3798 { 3799 (void)a; /* UNUSED */ 3800 (void)pathname; /* UNUSED */ 3801 return (ARCHIVE_OK); 3802 } 3803 #else 3804 3805 /* 3806 * On Mac OS, we use copyfile() to unpack the metadata and 3807 * apply it to the target file. 3808 */ 3809 3810 #if defined(HAVE_SYS_XATTR_H) 3811 static int 3812 copy_xattrs(struct archive_write_disk *a, int tmpfd, int dffd) 3813 { 3814 ssize_t xattr_size; 3815 char *xattr_names = NULL, *xattr_val = NULL; 3816 int ret = ARCHIVE_OK, xattr_i; 3817 3818 xattr_size = flistxattr(tmpfd, NULL, 0, 0); 3819 if (xattr_size == -1) { 3820 archive_set_error(&a->archive, errno, 3821 "Failed to read metadata(xattr)"); 3822 ret = ARCHIVE_WARN; 3823 goto exit_xattr; 3824 } 3825 xattr_names = malloc(xattr_size); 3826 if (xattr_names == NULL) { 3827 archive_set_error(&a->archive, ENOMEM, 3828 "Can't allocate memory for metadata(xattr)"); 3829 ret = ARCHIVE_FATAL; 3830 goto exit_xattr; 3831 } 3832 xattr_size = flistxattr(tmpfd, xattr_names, xattr_size, 0); 3833 if (xattr_size == -1) { 3834 archive_set_error(&a->archive, errno, 3835 "Failed to read metadata(xattr)"); 3836 ret = ARCHIVE_WARN; 3837 goto exit_xattr; 3838 } 3839 for (xattr_i = 0; xattr_i < xattr_size; 3840 xattr_i += strlen(xattr_names + xattr_i) + 1) { 3841 char *xattr_val_saved; 3842 ssize_t s; 3843 int f; 3844 3845 s = fgetxattr(tmpfd, xattr_names + xattr_i, NULL, 0, 0, 0); 3846 if (s == -1) { 3847 archive_set_error(&a->archive, errno, 3848 "Failed to get metadata(xattr)"); 3849 ret = ARCHIVE_WARN; 3850 goto exit_xattr; 3851 } 3852 xattr_val_saved = xattr_val; 3853 xattr_val = realloc(xattr_val, s); 3854 if (xattr_val == NULL) { 3855 archive_set_error(&a->archive, ENOMEM, 3856 "Failed to get metadata(xattr)"); 3857 ret = ARCHIVE_WARN; 3858 free(xattr_val_saved); 3859 goto exit_xattr; 3860 } 3861 s = fgetxattr(tmpfd, xattr_names + xattr_i, xattr_val, s, 0, 0); 3862 if (s == -1) { 3863 archive_set_error(&a->archive, errno, 3864 "Failed to get metadata(xattr)"); 3865 ret = ARCHIVE_WARN; 3866 goto exit_xattr; 3867 } 3868 f = fsetxattr(dffd, xattr_names + xattr_i, xattr_val, s, 0, 0); 3869 if (f == -1) { 3870 archive_set_error(&a->archive, errno, 3871 "Failed to get metadata(xattr)"); 3872 ret = ARCHIVE_WARN; 3873 goto exit_xattr; 3874 } 3875 } 3876 exit_xattr: 3877 free(xattr_names); 3878 free(xattr_val); 3879 return (ret); 3880 } 3881 #endif 3882 3883 static int 3884 copy_acls(struct archive_write_disk *a, int tmpfd, int dffd) 3885 { 3886 #ifndef HAVE_SYS_ACL_H 3887 return 0; 3888 #else 3889 acl_t acl, dfacl = NULL; 3890 int acl_r, ret = ARCHIVE_OK; 3891 3892 acl = acl_get_fd(tmpfd); 3893 if (acl == NULL) { 3894 if (errno == ENOENT) 3895 /* There are not any ACLs. */ 3896 return (ret); 3897 archive_set_error(&a->archive, errno, 3898 "Failed to get metadata(acl)"); 3899 ret = ARCHIVE_WARN; 3900 goto exit_acl; 3901 } 3902 dfacl = acl_dup(acl); 3903 acl_r = acl_set_fd(dffd, dfacl); 3904 if (acl_r == -1) { 3905 archive_set_error(&a->archive, errno, 3906 "Failed to get metadata(acl)"); 3907 ret = ARCHIVE_WARN; 3908 goto exit_acl; 3909 } 3910 exit_acl: 3911 if (acl) 3912 acl_free(acl); 3913 if (dfacl) 3914 acl_free(dfacl); 3915 return (ret); 3916 #endif 3917 } 3918 3919 static int 3920 create_tempdatafork(struct archive_write_disk *a, const char *pathname) 3921 { 3922 struct archive_string tmpdatafork; 3923 int tmpfd; 3924 3925 archive_string_init(&tmpdatafork); 3926 archive_strcpy(&tmpdatafork, "tar.md.XXXXXX"); 3927 tmpfd = mkstemp(tmpdatafork.s); 3928 if (tmpfd < 0) { 3929 archive_set_error(&a->archive, errno, 3930 "Failed to mkstemp"); 3931 archive_string_free(&tmpdatafork); 3932 return (-1); 3933 } 3934 if (copyfile(pathname, tmpdatafork.s, 0, 3935 COPYFILE_UNPACK | COPYFILE_NOFOLLOW 3936 | COPYFILE_ACL | COPYFILE_XATTR) < 0) { 3937 archive_set_error(&a->archive, errno, 3938 "Failed to restore metadata"); 3939 close(tmpfd); 3940 tmpfd = -1; 3941 } 3942 unlink(tmpdatafork.s); 3943 archive_string_free(&tmpdatafork); 3944 return (tmpfd); 3945 } 3946 3947 static int 3948 copy_metadata(struct archive_write_disk *a, const char *metadata, 3949 const char *datafork, int datafork_compressed) 3950 { 3951 int ret = ARCHIVE_OK; 3952 3953 if (datafork_compressed) { 3954 int dffd, tmpfd; 3955 3956 tmpfd = create_tempdatafork(a, metadata); 3957 if (tmpfd == -1) 3958 return (ARCHIVE_WARN); 3959 3960 /* 3961 * Do not open the data fork compressed by HFS+ compression 3962 * with at least a writing mode(O_RDWR or O_WRONLY). it 3963 * makes the data fork uncompressed. 3964 */ 3965 dffd = open(datafork, 0); 3966 if (dffd == -1) { 3967 archive_set_error(&a->archive, errno, 3968 "Failed to open the data fork for metadata"); 3969 close(tmpfd); 3970 return (ARCHIVE_WARN); 3971 } 3972 3973 #if defined(HAVE_SYS_XATTR_H) 3974 ret = copy_xattrs(a, tmpfd, dffd); 3975 if (ret == ARCHIVE_OK) 3976 #endif 3977 ret = copy_acls(a, tmpfd, dffd); 3978 close(tmpfd); 3979 close(dffd); 3980 } else { 3981 if (copyfile(metadata, datafork, 0, 3982 COPYFILE_UNPACK | COPYFILE_NOFOLLOW 3983 | COPYFILE_ACL | COPYFILE_XATTR) < 0) { 3984 archive_set_error(&a->archive, errno, 3985 "Failed to restore metadata"); 3986 ret = ARCHIVE_WARN; 3987 } 3988 } 3989 return (ret); 3990 } 3991 3992 static int 3993 set_mac_metadata(struct archive_write_disk *a, const char *pathname, 3994 const void *metadata, size_t metadata_size) 3995 { 3996 struct archive_string tmp; 3997 ssize_t written; 3998 int fd; 3999 int ret = ARCHIVE_OK; 4000 4001 /* This would be simpler if copyfile() could just accept the 4002 * metadata as a block of memory; then we could sidestep this 4003 * silly dance of writing the data to disk just so that 4004 * copyfile() can read it back in again. */ 4005 archive_string_init(&tmp); 4006 archive_strcpy(&tmp, pathname); 4007 archive_strcat(&tmp, ".XXXXXX"); 4008 fd = mkstemp(tmp.s); 4009 4010 if (fd < 0) { 4011 archive_set_error(&a->archive, errno, 4012 "Failed to restore metadata"); 4013 archive_string_free(&tmp); 4014 return (ARCHIVE_WARN); 4015 } 4016 written = write(fd, metadata, metadata_size); 4017 close(fd); 4018 if ((size_t)written != metadata_size) { 4019 archive_set_error(&a->archive, errno, 4020 "Failed to restore metadata"); 4021 ret = ARCHIVE_WARN; 4022 } else { 4023 int compressed; 4024 4025 #if defined(UF_COMPRESSED) 4026 if ((a->todo & TODO_HFS_COMPRESSION) != 0 && 4027 (ret = lazy_stat(a)) == ARCHIVE_OK) 4028 compressed = a->st.st_flags & UF_COMPRESSED; 4029 else 4030 #endif 4031 compressed = 0; 4032 ret = copy_metadata(a, tmp.s, pathname, compressed); 4033 } 4034 unlink(tmp.s); 4035 archive_string_free(&tmp); 4036 return (ret); 4037 } 4038 4039 static int 4040 fixup_appledouble(struct archive_write_disk *a, const char *pathname) 4041 { 4042 char buff[8]; 4043 struct stat st; 4044 const char *p; 4045 struct archive_string datafork; 4046 int fd = -1, ret = ARCHIVE_OK; 4047 4048 archive_string_init(&datafork); 4049 /* Check if the current file name is a type of the resource 4050 * fork file. */ 4051 p = strrchr(pathname, '/'); 4052 if (p == NULL) 4053 p = pathname; 4054 else 4055 p++; 4056 if (p[0] != '.' || p[1] != '_') 4057 goto skip_appledouble; 4058 4059 /* 4060 * Check if the data fork file exists. 4061 * 4062 * TODO: Check if this write disk object has handled it. 4063 */ 4064 archive_strncpy(&datafork, pathname, p - pathname); 4065 archive_strcat(&datafork, p + 2); 4066 if (lstat(datafork.s, &st) == -1 || 4067 (st.st_mode & AE_IFMT) != AE_IFREG) 4068 goto skip_appledouble; 4069 4070 /* 4071 * Check if the file is in the AppleDouble form. 4072 */ 4073 fd = open(pathname, O_RDONLY | O_BINARY | O_CLOEXEC); 4074 __archive_ensure_cloexec_flag(fd); 4075 if (fd == -1) { 4076 archive_set_error(&a->archive, errno, 4077 "Failed to open a restoring file"); 4078 ret = ARCHIVE_WARN; 4079 goto skip_appledouble; 4080 } 4081 if (read(fd, buff, 8) == -1) { 4082 archive_set_error(&a->archive, errno, 4083 "Failed to read a restoring file"); 4084 close(fd); 4085 ret = ARCHIVE_WARN; 4086 goto skip_appledouble; 4087 } 4088 close(fd); 4089 /* Check AppleDouble Magic Code. */ 4090 if (archive_be32dec(buff) != 0x00051607) 4091 goto skip_appledouble; 4092 /* Check AppleDouble Version. */ 4093 if (archive_be32dec(buff+4) != 0x00020000) 4094 goto skip_appledouble; 4095 4096 ret = copy_metadata(a, pathname, datafork.s, 4097 #if defined(UF_COMPRESSED) 4098 st.st_flags & UF_COMPRESSED); 4099 #else 4100 0); 4101 #endif 4102 if (ret == ARCHIVE_OK) { 4103 unlink(pathname); 4104 ret = ARCHIVE_EOF; 4105 } 4106 skip_appledouble: 4107 archive_string_free(&datafork); 4108 return (ret); 4109 } 4110 #endif 4111 4112 #if ARCHIVE_XATTR_LINUX || ARCHIVE_XATTR_DARWIN || ARCHIVE_XATTR_AIX 4113 /* 4114 * Restore extended attributes - Linux, Darwin and AIX implementations: 4115 * AIX' ea interface is syntaxwise identical to the Linux xattr interface. 4116 */ 4117 static int 4118 set_xattrs(struct archive_write_disk *a) 4119 { 4120 struct archive_entry *entry = a->entry; 4121 struct archive_string errlist; 4122 int ret = ARCHIVE_OK; 4123 int i = archive_entry_xattr_reset(entry); 4124 short fail = 0; 4125 4126 archive_string_init(&errlist); 4127 4128 while (i--) { 4129 const char *name; 4130 const void *value; 4131 size_t size; 4132 int e; 4133 4134 archive_entry_xattr_next(entry, &name, &value, &size); 4135 4136 if (name == NULL) 4137 continue; 4138 #if ARCHIVE_XATTR_LINUX 4139 /* Linux: quietly skip POSIX.1e ACL extended attributes */ 4140 if (strncmp(name, "system.", 7) == 0 && 4141 (strcmp(name + 7, "posix_acl_access") == 0 || 4142 strcmp(name + 7, "posix_acl_default") == 0)) 4143 continue; 4144 if (strncmp(name, "trusted.SGI_", 12) == 0 && 4145 (strcmp(name + 12, "ACL_DEFAULT") == 0 || 4146 strcmp(name + 12, "ACL_FILE") == 0)) 4147 continue; 4148 4149 /* Linux: xfsroot namespace is obsolete and unsupported */ 4150 if (strncmp(name, "xfsroot.", 8) == 0) { 4151 fail = 1; 4152 archive_strcat(&errlist, name); 4153 archive_strappend_char(&errlist, ' '); 4154 continue; 4155 } 4156 #endif 4157 4158 if (a->fd >= 0) { 4159 #if ARCHIVE_XATTR_LINUX 4160 e = fsetxattr(a->fd, name, value, size, 0); 4161 #elif ARCHIVE_XATTR_DARWIN 4162 e = fsetxattr(a->fd, name, value, size, 0, 0); 4163 #elif ARCHIVE_XATTR_AIX 4164 e = fsetea(a->fd, name, value, size, 0); 4165 #endif 4166 } else { 4167 #if ARCHIVE_XATTR_LINUX 4168 e = lsetxattr(archive_entry_pathname(entry), 4169 name, value, size, 0); 4170 #elif ARCHIVE_XATTR_DARWIN 4171 e = setxattr(archive_entry_pathname(entry), 4172 name, value, size, 0, XATTR_NOFOLLOW); 4173 #elif ARCHIVE_XATTR_AIX 4174 e = lsetea(archive_entry_pathname(entry), 4175 name, value, size, 0); 4176 #endif 4177 } 4178 if (e == -1) { 4179 ret = ARCHIVE_WARN; 4180 archive_strcat(&errlist, name); 4181 archive_strappend_char(&errlist, ' '); 4182 if (errno != ENOTSUP && errno != ENOSYS) 4183 fail = 1; 4184 } 4185 } 4186 4187 if (ret == ARCHIVE_WARN) { 4188 if (fail && errlist.length > 0) { 4189 errlist.length--; 4190 errlist.s[errlist.length] = '\0'; 4191 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 4192 "Cannot restore extended attributes: %s", 4193 errlist.s); 4194 } else 4195 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 4196 "Cannot restore extended " 4197 "attributes on this file system."); 4198 } 4199 4200 archive_string_free(&errlist); 4201 return (ret); 4202 } 4203 #elif ARCHIVE_XATTR_FREEBSD 4204 /* 4205 * Restore extended attributes - FreeBSD implementation 4206 */ 4207 static int 4208 set_xattrs(struct archive_write_disk *a) 4209 { 4210 struct archive_entry *entry = a->entry; 4211 struct archive_string errlist; 4212 int ret = ARCHIVE_OK; 4213 int i = archive_entry_xattr_reset(entry); 4214 short fail = 0; 4215 4216 archive_string_init(&errlist); 4217 4218 while (i--) { 4219 const char *name; 4220 const void *value; 4221 size_t size; 4222 archive_entry_xattr_next(entry, &name, &value, &size); 4223 if (name != NULL) { 4224 ssize_t e; 4225 int namespace; 4226 4227 if (strncmp(name, "user.", 5) == 0) { 4228 /* "user." attributes go to user namespace */ 4229 name += 5; 4230 namespace = EXTATTR_NAMESPACE_USER; 4231 } else { 4232 /* Other namespaces are unsupported */ 4233 archive_strcat(&errlist, name); 4234 archive_strappend_char(&errlist, ' '); 4235 fail = 1; 4236 ret = ARCHIVE_WARN; 4237 continue; 4238 } 4239 4240 if (a->fd >= 0) { 4241 e = extattr_set_fd(a->fd, namespace, name, 4242 value, size); 4243 } else { 4244 e = extattr_set_link( 4245 archive_entry_pathname(entry), namespace, 4246 name, value, size); 4247 } 4248 if (e != (ssize_t)size) { 4249 archive_strcat(&errlist, name); 4250 archive_strappend_char(&errlist, ' '); 4251 ret = ARCHIVE_WARN; 4252 if (errno != ENOTSUP && errno != ENOSYS) 4253 fail = 1; 4254 } 4255 } 4256 } 4257 4258 if (ret == ARCHIVE_WARN) { 4259 if (fail && errlist.length > 0) { 4260 errlist.length--; 4261 errlist.s[errlist.length] = '\0'; 4262 4263 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 4264 "Cannot restore extended attributes: %s", 4265 errlist.s); 4266 } else 4267 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 4268 "Cannot restore extended " 4269 "attributes on this file system."); 4270 } 4271 4272 archive_string_free(&errlist); 4273 return (ret); 4274 } 4275 #else 4276 /* 4277 * Restore extended attributes - stub implementation for unsupported systems 4278 */ 4279 static int 4280 set_xattrs(struct archive_write_disk *a) 4281 { 4282 static int warning_done = 0; 4283 4284 /* If there aren't any extended attributes, then it's okay not 4285 * to extract them, otherwise, issue a single warning. */ 4286 if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) { 4287 warning_done = 1; 4288 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 4289 "Cannot restore extended attributes on this system"); 4290 return (ARCHIVE_WARN); 4291 } 4292 /* Warning was already emitted; suppress further warnings. */ 4293 return (ARCHIVE_OK); 4294 } 4295 #endif 4296 4297 /* 4298 * Test if file on disk is older than entry. 4299 */ 4300 static int 4301 older(struct stat *st, struct archive_entry *entry) 4302 { 4303 /* First, test the seconds and return if we have a definite answer. */ 4304 /* Definitely older. */ 4305 if (to_int64_time(st->st_mtime) < to_int64_time(archive_entry_mtime(entry))) 4306 return (1); 4307 /* Definitely younger. */ 4308 if (to_int64_time(st->st_mtime) > to_int64_time(archive_entry_mtime(entry))) 4309 return (0); 4310 /* If this platform supports fractional seconds, try those. */ 4311 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC 4312 /* Definitely older. */ 4313 if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry)) 4314 return (1); 4315 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC 4316 /* Definitely older. */ 4317 if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry)) 4318 return (1); 4319 #elif HAVE_STRUCT_STAT_ST_MTIME_N 4320 /* older. */ 4321 if (st->st_mtime_n < archive_entry_mtime_nsec(entry)) 4322 return (1); 4323 #elif HAVE_STRUCT_STAT_ST_UMTIME 4324 /* older. */ 4325 if (st->st_umtime * 1000 < archive_entry_mtime_nsec(entry)) 4326 return (1); 4327 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC 4328 /* older. */ 4329 if (st->st_mtime_usec * 1000 < archive_entry_mtime_nsec(entry)) 4330 return (1); 4331 #else 4332 /* This system doesn't have high-res timestamps. */ 4333 #endif 4334 /* Same age or newer, so not older. */ 4335 return (0); 4336 } 4337 4338 #ifndef ARCHIVE_ACL_SUPPORT 4339 int 4340 archive_write_disk_set_acls(struct archive *a, int fd, const char *name, 4341 struct archive_acl *abstract_acl, __LA_MODE_T mode) 4342 { 4343 (void)a; /* UNUSED */ 4344 (void)fd; /* UNUSED */ 4345 (void)name; /* UNUSED */ 4346 (void)abstract_acl; /* UNUSED */ 4347 (void)mode; /* UNUSED */ 4348 return (ARCHIVE_OK); 4349 } 4350 #endif 4351 4352 #endif /* !_WIN32 || __CYGWIN__ */ 4353 4354