archive_write_disk.c revision 228759
1/*- 2 * Copyright (c) 2003-2007 Tim Kientzle 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "archive_platform.h" 28__FBSDID("$FreeBSD: head/lib/libarchive/archive_write_disk.c 201159 2009-12-29 05:35:40Z kientzle $"); 29 30#ifdef HAVE_SYS_TYPES_H 31#include <sys/types.h> 32#endif 33#ifdef HAVE_SYS_ACL_H 34#include <sys/acl.h> 35#endif 36#ifdef HAVE_SYS_EXTATTR_H 37#include <sys/extattr.h> 38#endif 39#ifdef HAVE_SYS_XATTR_H 40#include <sys/xattr.h> 41#endif 42#ifdef HAVE_ATTR_XATTR_H 43#include <attr/xattr.h> 44#endif 45#ifdef HAVE_SYS_IOCTL_H 46#include <sys/ioctl.h> 47#endif 48#ifdef HAVE_SYS_STAT_H 49#include <sys/stat.h> 50#endif 51#ifdef HAVE_SYS_TIME_H 52#include <sys/time.h> 53#endif 54#ifdef HAVE_SYS_UTIME_H 55#include <sys/utime.h> 56#endif 57#ifdef HAVE_ERRNO_H 58#include <errno.h> 59#endif 60#ifdef HAVE_FCNTL_H 61#include <fcntl.h> 62#endif 63#ifdef HAVE_GRP_H 64#include <grp.h> 65#endif 66#ifdef HAVE_LINUX_FS_H 67#include <linux/fs.h> /* for Linux file flags */ 68#endif 69/* 70 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h. 71 * As the include guards don't agree, the order of include is important. 72 */ 73#ifdef HAVE_LINUX_EXT2_FS_H 74#include <linux/ext2_fs.h> /* for Linux file flags */ 75#endif 76#if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__) 77#include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */ 78#endif 79#ifdef HAVE_LIMITS_H 80#include <limits.h> 81#endif 82#ifdef HAVE_PWD_H 83#include <pwd.h> 84#endif 85#include <stdio.h> 86#ifdef HAVE_STDLIB_H 87#include <stdlib.h> 88#endif 89#ifdef HAVE_STRING_H 90#include <string.h> 91#endif 92#ifdef HAVE_UNISTD_H 93#include <unistd.h> 94#endif 95#ifdef HAVE_UTIME_H 96#include <utime.h> 97#endif 98 99#include "archive.h" 100#include "archive_string.h" 101#include "archive_entry.h" 102#include "archive_private.h" 103 104#ifndef O_BINARY 105#define O_BINARY 0 106#endif 107 108struct fixup_entry { 109 struct fixup_entry *next; 110 mode_t mode; 111 int64_t atime; 112 int64_t birthtime; 113 int64_t mtime; 114 unsigned long atime_nanos; 115 unsigned long birthtime_nanos; 116 unsigned long mtime_nanos; 117 unsigned long fflags_set; 118 int fixup; /* bitmask of what needs fixing */ 119 char *name; 120}; 121 122/* 123 * We use a bitmask to track which operations remain to be done for 124 * this file. In particular, this helps us avoid unnecessary 125 * operations when it's possible to take care of one step as a 126 * side-effect of another. For example, mkdir() can specify the mode 127 * for the newly-created object but symlink() cannot. This means we 128 * can skip chmod() if mkdir() succeeded, but we must explicitly 129 * chmod() if we're trying to create a directory that already exists 130 * (mkdir() failed) or if we're restoring a symlink. Similarly, we 131 * need to verify UID/GID before trying to restore SUID/SGID bits; 132 * that verification can occur explicitly through a stat() call or 133 * implicitly because of a successful chown() call. 134 */ 135#define TODO_MODE_FORCE 0x40000000 136#define TODO_MODE_BASE 0x20000000 137#define TODO_SUID 0x10000000 138#define TODO_SUID_CHECK 0x08000000 139#define TODO_SGID 0x04000000 140#define TODO_SGID_CHECK 0x02000000 141#define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID) 142#define TODO_TIMES ARCHIVE_EXTRACT_TIME 143#define TODO_OWNER ARCHIVE_EXTRACT_OWNER 144#define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS 145#define TODO_ACLS ARCHIVE_EXTRACT_ACL 146#define TODO_XATTR ARCHIVE_EXTRACT_XATTR 147 148struct archive_write_disk { 149 struct archive archive; 150 151 mode_t user_umask; 152 struct fixup_entry *fixup_list; 153 struct fixup_entry *current_fixup; 154 uid_t user_uid; 155 dev_t skip_file_dev; 156 ino_t skip_file_ino; 157 time_t start_time; 158 159 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid); 160 void (*cleanup_gid)(void *private); 161 void *lookup_gid_data; 162 uid_t (*lookup_uid)(void *private, const char *gname, gid_t gid); 163 void (*cleanup_uid)(void *private); 164 void *lookup_uid_data; 165 166 /* 167 * Full path of last file to satisfy symlink checks. 168 */ 169 struct archive_string path_safe; 170 171 /* 172 * Cached stat data from disk for the current entry. 173 * If this is valid, pst points to st. Otherwise, 174 * pst is null. 175 */ 176 struct stat st; 177 struct stat *pst; 178 179 /* Information about the object being restored right now. */ 180 struct archive_entry *entry; /* Entry being extracted. */ 181 char *name; /* Name of entry, possibly edited. */ 182 struct archive_string _name_data; /* backing store for 'name' */ 183 /* Tasks remaining for this object. */ 184 int todo; 185 /* Tasks deferred until end-of-archive. */ 186 int deferred; 187 /* Options requested by the client. */ 188 int flags; 189 /* Handle for the file we're restoring. */ 190 int fd; 191 /* Current offset for writing data to the file. */ 192 off_t offset; 193 /* Last offset actually written to disk. */ 194 off_t fd_offset; 195 /* Maximum size of file, -1 if unknown. */ 196 off_t filesize; 197 /* Dir we were in before this restore; only for deep paths. */ 198 int restore_pwd; 199 /* Mode we should use for this entry; affected by _PERM and umask. */ 200 mode_t mode; 201 /* UID/GID to use in restoring this entry. */ 202 uid_t uid; 203 gid_t gid; 204}; 205 206/* 207 * Default mode for dirs created automatically (will be modified by umask). 208 * Note that POSIX specifies 0777 for implicity-created dirs, "modified 209 * by the process' file creation mask." 210 */ 211#define DEFAULT_DIR_MODE 0777 212/* 213 * Dir modes are restored in two steps: During the extraction, the permissions 214 * in the archive are modified to match the following limits. During 215 * the post-extract fixup pass, the permissions from the archive are 216 * applied. 217 */ 218#define MINIMUM_DIR_MODE 0700 219#define MAXIMUM_DIR_MODE 0775 220 221static int check_symlinks(struct archive_write_disk *); 222static int create_filesystem_object(struct archive_write_disk *); 223static struct fixup_entry *current_fixup(struct archive_write_disk *, const char *pathname); 224#ifdef HAVE_FCHDIR 225static void edit_deep_directories(struct archive_write_disk *ad); 226#endif 227static int cleanup_pathname(struct archive_write_disk *); 228static int create_dir(struct archive_write_disk *, char *); 229static int create_parent_dir(struct archive_write_disk *, char *); 230static int older(struct stat *, struct archive_entry *); 231static int restore_entry(struct archive_write_disk *); 232#ifdef HAVE_POSIX_ACL 233static int set_acl(struct archive_write_disk *, int fd, struct archive_entry *, 234 acl_type_t, int archive_entry_acl_type, const char *tn); 235#endif 236static int set_acls(struct archive_write_disk *); 237static int set_xattrs(struct archive_write_disk *); 238static int set_fflags(struct archive_write_disk *); 239static int set_fflags_platform(struct archive_write_disk *, int fd, 240 const char *name, mode_t mode, 241 unsigned long fflags_set, unsigned long fflags_clear); 242static int set_ownership(struct archive_write_disk *); 243static int set_mode(struct archive_write_disk *, int mode); 244static int set_time(int, int, const char *, time_t, long, time_t, long); 245static int set_times(struct archive_write_disk *); 246static struct fixup_entry *sort_dir_list(struct fixup_entry *p); 247static gid_t trivial_lookup_gid(void *, const char *, gid_t); 248static uid_t trivial_lookup_uid(void *, const char *, uid_t); 249static ssize_t write_data_block(struct archive_write_disk *, 250 const char *, size_t); 251 252static struct archive_vtable *archive_write_disk_vtable(void); 253 254static int _archive_write_close(struct archive *); 255static int _archive_write_finish(struct archive *); 256static int _archive_write_header(struct archive *, struct archive_entry *); 257static int _archive_write_finish_entry(struct archive *); 258static ssize_t _archive_write_data(struct archive *, const void *, size_t); 259static ssize_t _archive_write_data_block(struct archive *, const void *, size_t, off_t); 260 261static int 262_archive_write_disk_lazy_stat(struct archive_write_disk *a) 263{ 264 if (a->pst != NULL) { 265 /* Already have stat() data available. */ 266 return (ARCHIVE_OK); 267 } 268#ifdef HAVE_FSTAT 269 if (a->fd >= 0 && fstat(a->fd, &a->st) == 0) { 270 a->pst = &a->st; 271 return (ARCHIVE_OK); 272 } 273#endif 274 /* 275 * XXX At this point, symlinks should not be hit, otherwise 276 * XXX a race occured. Do we want to check explicitly for that? 277 */ 278 if (lstat(a->name, &a->st) == 0) { 279 a->pst = &a->st; 280 return (ARCHIVE_OK); 281 } 282 archive_set_error(&a->archive, errno, "Couldn't stat file"); 283 return (ARCHIVE_WARN); 284} 285 286static struct archive_vtable * 287archive_write_disk_vtable(void) 288{ 289 static struct archive_vtable av; 290 static int inited = 0; 291 292 if (!inited) { 293 av.archive_close = _archive_write_close; 294 av.archive_finish = _archive_write_finish; 295 av.archive_write_header = _archive_write_header; 296 av.archive_write_finish_entry = _archive_write_finish_entry; 297 av.archive_write_data = _archive_write_data; 298 av.archive_write_data_block = _archive_write_data_block; 299 } 300 return (&av); 301} 302 303 304int 305archive_write_disk_set_options(struct archive *_a, int flags) 306{ 307 struct archive_write_disk *a = (struct archive_write_disk *)_a; 308 309 a->flags = flags; 310 return (ARCHIVE_OK); 311} 312 313 314/* 315 * Extract this entry to disk. 316 * 317 * TODO: Validate hardlinks. According to the standards, we're 318 * supposed to check each extracted hardlink and squawk if it refers 319 * to a file that we didn't restore. I'm not entirely convinced this 320 * is a good idea, but more importantly: Is there any way to validate 321 * hardlinks without keeping a complete list of filenames from the 322 * entire archive?? Ugh. 323 * 324 */ 325static int 326_archive_write_header(struct archive *_a, struct archive_entry *entry) 327{ 328 struct archive_write_disk *a = (struct archive_write_disk *)_a; 329 struct fixup_entry *fe; 330 int ret, r; 331 332 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 333 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 334 "archive_write_disk_header"); 335 archive_clear_error(&a->archive); 336 if (a->archive.state & ARCHIVE_STATE_DATA) { 337 r = _archive_write_finish_entry(&a->archive); 338 if (r == ARCHIVE_FATAL) 339 return (r); 340 } 341 342 /* Set up for this particular entry. */ 343 a->pst = NULL; 344 a->current_fixup = NULL; 345 a->deferred = 0; 346 if (a->entry) { 347 archive_entry_free(a->entry); 348 a->entry = NULL; 349 } 350 a->entry = archive_entry_clone(entry); 351 a->fd = -1; 352 a->fd_offset = 0; 353 a->offset = 0; 354 a->uid = a->user_uid; 355 a->mode = archive_entry_mode(a->entry); 356 if (archive_entry_size_is_set(a->entry)) 357 a->filesize = archive_entry_size(a->entry); 358 else 359 a->filesize = -1; 360 archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry)); 361 a->name = a->_name_data.s; 362 archive_clear_error(&a->archive); 363 364 /* 365 * Clean up the requested path. This is necessary for correct 366 * dir restores; the dir restore logic otherwise gets messed 367 * up by nonsense like "dir/.". 368 */ 369 ret = cleanup_pathname(a); 370 if (ret != ARCHIVE_OK) 371 return (ret); 372 373 /* 374 * Set the umask to zero so we get predictable mode settings. 375 * This gets done on every call to _write_header in case the 376 * user edits their umask during the extraction for some 377 * reason. This will be reset before we return. Note that we 378 * don't need to do this in _finish_entry, as the chmod(), etc, 379 * system calls don't obey umask. 380 */ 381 a->user_umask = umask(0); 382 /* From here on, early exit requires "goto done" to clean up. */ 383 384 /* Figure out what we need to do for this entry. */ 385 a->todo = TODO_MODE_BASE; 386 if (a->flags & ARCHIVE_EXTRACT_PERM) { 387 a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */ 388 /* 389 * SGID requires an extra "check" step because we 390 * cannot easily predict the GID that the system will 391 * assign. (Different systems assign GIDs to files 392 * based on a variety of criteria, including process 393 * credentials and the gid of the enclosing 394 * directory.) We can only restore the SGID bit if 395 * the file has the right GID, and we only know the 396 * GID if we either set it (see set_ownership) or if 397 * we've actually called stat() on the file after it 398 * was restored. Since there are several places at 399 * which we might verify the GID, we need a TODO bit 400 * to keep track. 401 */ 402 if (a->mode & S_ISGID) 403 a->todo |= TODO_SGID | TODO_SGID_CHECK; 404 /* 405 * Verifying the SUID is simpler, but can still be 406 * done in multiple ways, hence the separate "check" bit. 407 */ 408 if (a->mode & S_ISUID) 409 a->todo |= TODO_SUID | TODO_SUID_CHECK; 410 } else { 411 /* 412 * User didn't request full permissions, so don't 413 * restore SUID, SGID bits and obey umask. 414 */ 415 a->mode &= ~S_ISUID; 416 a->mode &= ~S_ISGID; 417 a->mode &= ~S_ISVTX; 418 a->mode &= ~a->user_umask; 419 } 420#if !defined(_WIN32) || defined(__CYGWIN__) 421 if (a->flags & ARCHIVE_EXTRACT_OWNER) 422 a->todo |= TODO_OWNER; 423#endif 424 if (a->flags & ARCHIVE_EXTRACT_TIME) 425 a->todo |= TODO_TIMES; 426 if (a->flags & ARCHIVE_EXTRACT_ACL) 427 a->todo |= TODO_ACLS; 428 if (a->flags & ARCHIVE_EXTRACT_XATTR) 429 a->todo |= TODO_XATTR; 430 if (a->flags & ARCHIVE_EXTRACT_FFLAGS) 431 a->todo |= TODO_FFLAGS; 432 if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) { 433 ret = check_symlinks(a); 434 if (ret != ARCHIVE_OK) 435 goto done; 436 } 437#ifdef HAVE_FCHDIR 438 /* If path exceeds PATH_MAX, shorten the path. */ 439 edit_deep_directories(a); 440#endif 441 442 ret = restore_entry(a); 443 444 /* 445 * TODO: There are rumours that some extended attributes must 446 * be restored before file data is written. If this is true, 447 * then we either need to write all extended attributes both 448 * before and after restoring the data, or find some rule for 449 * determining which must go first and which last. Due to the 450 * many ways people are using xattrs, this may prove to be an 451 * intractable problem. 452 */ 453 454#ifdef HAVE_FCHDIR 455 /* If we changed directory above, restore it here. */ 456 if (a->restore_pwd >= 0) { 457 r = fchdir(a->restore_pwd); 458 if (r != 0) { 459 archive_set_error(&a->archive, errno, "chdir() failure"); 460 ret = ARCHIVE_FATAL; 461 } 462 close(a->restore_pwd); 463 a->restore_pwd = -1; 464 } 465#endif 466 467 /* 468 * Fixup uses the unedited pathname from archive_entry_pathname(), 469 * because it is relative to the base dir and the edited path 470 * might be relative to some intermediate dir as a result of the 471 * deep restore logic. 472 */ 473 if (a->deferred & TODO_MODE) { 474 fe = current_fixup(a, archive_entry_pathname(entry)); 475 fe->fixup |= TODO_MODE_BASE; 476 fe->mode = a->mode; 477 } 478 479 if ((a->deferred & TODO_TIMES) 480 && (archive_entry_mtime_is_set(entry) 481 || archive_entry_atime_is_set(entry))) { 482 fe = current_fixup(a, archive_entry_pathname(entry)); 483 fe->fixup |= TODO_TIMES; 484 if (archive_entry_atime_is_set(entry)) { 485 fe->atime = archive_entry_atime(entry); 486 fe->atime_nanos = archive_entry_atime_nsec(entry); 487 } else { 488 /* If atime is unset, use start time. */ 489 fe->atime = a->start_time; 490 fe->atime_nanos = 0; 491 } 492 if (archive_entry_mtime_is_set(entry)) { 493 fe->mtime = archive_entry_mtime(entry); 494 fe->mtime_nanos = archive_entry_mtime_nsec(entry); 495 } else { 496 /* If mtime is unset, use start time. */ 497 fe->mtime = a->start_time; 498 fe->mtime_nanos = 0; 499 } 500 if (archive_entry_birthtime_is_set(entry)) { 501 fe->birthtime = archive_entry_birthtime(entry); 502 fe->birthtime_nanos = archive_entry_birthtime_nsec(entry); 503 } else { 504 /* If birthtime is unset, use mtime. */ 505 fe->birthtime = fe->mtime; 506 fe->birthtime_nanos = fe->mtime_nanos; 507 } 508 } 509 510 if (a->deferred & TODO_FFLAGS) { 511 fe = current_fixup(a, archive_entry_pathname(entry)); 512 fe->fixup |= TODO_FFLAGS; 513 /* TODO: Complete this.. defer fflags from below. */ 514 } 515 516 /* We've created the object and are ready to pour data into it. */ 517 if (ret >= ARCHIVE_WARN) 518 a->archive.state = ARCHIVE_STATE_DATA; 519 /* 520 * If it's not open, tell our client not to try writing. 521 * In particular, dirs, links, etc, don't get written to. 522 */ 523 if (a->fd < 0) { 524 archive_entry_set_size(entry, 0); 525 a->filesize = 0; 526 } 527done: 528 /* Restore the user's umask before returning. */ 529 umask(a->user_umask); 530 531 return (ret); 532} 533 534int 535archive_write_disk_set_skip_file(struct archive *_a, dev_t d, ino_t i) 536{ 537 struct archive_write_disk *a = (struct archive_write_disk *)_a; 538 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 539 ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file"); 540 a->skip_file_dev = d; 541 a->skip_file_ino = i; 542 return (ARCHIVE_OK); 543} 544 545static ssize_t 546write_data_block(struct archive_write_disk *a, const char *buff, size_t size) 547{ 548 uint64_t start_size = size; 549 ssize_t bytes_written = 0; 550 ssize_t block_size = 0, bytes_to_write; 551 552 if (size == 0) 553 return (ARCHIVE_OK); 554 555 if (a->filesize == 0 || a->fd < 0) { 556 archive_set_error(&a->archive, 0, 557 "Attempt to write to an empty file"); 558 return (ARCHIVE_WARN); 559 } 560 561 if (a->flags & ARCHIVE_EXTRACT_SPARSE) { 562#if HAVE_STRUCT_STAT_ST_BLKSIZE 563 int r; 564 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK) 565 return (r); 566 block_size = a->pst->st_blksize; 567#else 568 /* XXX TODO XXX Is there a more appropriate choice here ? */ 569 /* This needn't match the filesystem allocation size. */ 570 block_size = 16*1024; 571#endif 572 } 573 574 /* If this write would run beyond the file size, truncate it. */ 575 if (a->filesize >= 0 && (off_t)(a->offset + size) > a->filesize) 576 start_size = size = (size_t)(a->filesize - a->offset); 577 578 /* Write the data. */ 579 while (size > 0) { 580 if (block_size == 0) { 581 bytes_to_write = size; 582 } else { 583 /* We're sparsifying the file. */ 584 const char *p, *end; 585 off_t block_end; 586 587 /* Skip leading zero bytes. */ 588 for (p = buff, end = buff + size; p < end; ++p) { 589 if (*p != '\0') 590 break; 591 } 592 a->offset += p - buff; 593 size -= p - buff; 594 buff = p; 595 if (size == 0) 596 break; 597 598 /* Calculate next block boundary after offset. */ 599 block_end 600 = (a->offset / block_size + 1) * block_size; 601 602 /* If the adjusted write would cross block boundary, 603 * truncate it to the block boundary. */ 604 bytes_to_write = size; 605 if (a->offset + bytes_to_write > block_end) 606 bytes_to_write = block_end - a->offset; 607 } 608 /* Seek if necessary to the specified offset. */ 609 if (a->offset != a->fd_offset) { 610 if (lseek(a->fd, a->offset, SEEK_SET) < 0) { 611 archive_set_error(&a->archive, errno, 612 "Seek failed"); 613 return (ARCHIVE_FATAL); 614 } 615 a->fd_offset = a->offset; 616 a->archive.file_position = a->offset; 617 a->archive.raw_position = a->offset; 618 } 619 bytes_written = write(a->fd, buff, bytes_to_write); 620 if (bytes_written < 0) { 621 archive_set_error(&a->archive, errno, "Write failed"); 622 return (ARCHIVE_WARN); 623 } 624 buff += bytes_written; 625 size -= bytes_written; 626 a->offset += bytes_written; 627 a->archive.file_position += bytes_written; 628 a->archive.raw_position += bytes_written; 629 a->fd_offset = a->offset; 630 } 631 return (start_size - size); 632} 633 634static ssize_t 635_archive_write_data_block(struct archive *_a, 636 const void *buff, size_t size, off_t offset) 637{ 638 struct archive_write_disk *a = (struct archive_write_disk *)_a; 639 ssize_t r; 640 641 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 642 ARCHIVE_STATE_DATA, "archive_write_disk_block"); 643 644 a->offset = offset; 645 r = write_data_block(a, buff, size); 646 if (r < ARCHIVE_OK) 647 return (r); 648 if ((size_t)r < size) { 649 archive_set_error(&a->archive, 0, 650 "Write request too large"); 651 return (ARCHIVE_WARN); 652 } 653 return (ARCHIVE_OK); 654} 655 656static ssize_t 657_archive_write_data(struct archive *_a, const void *buff, size_t size) 658{ 659 struct archive_write_disk *a = (struct archive_write_disk *)_a; 660 661 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 662 ARCHIVE_STATE_DATA, "archive_write_data"); 663 664 return (write_data_block(a, buff, size)); 665} 666 667static int 668_archive_write_finish_entry(struct archive *_a) 669{ 670 struct archive_write_disk *a = (struct archive_write_disk *)_a; 671 int ret = ARCHIVE_OK; 672 673 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 674 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 675 "archive_write_finish_entry"); 676 if (a->archive.state & ARCHIVE_STATE_HEADER) 677 return (ARCHIVE_OK); 678 archive_clear_error(&a->archive); 679 680 /* Pad or truncate file to the right size. */ 681 if (a->fd < 0) { 682 /* There's no file. */ 683 } else if (a->filesize < 0) { 684 /* File size is unknown, so we can't set the size. */ 685 } else if (a->fd_offset == a->filesize) { 686 /* Last write ended at exactly the filesize; we're done. */ 687 /* Hopefully, this is the common case. */ 688 } else { 689#if HAVE_FTRUNCATE 690 if (ftruncate(a->fd, a->filesize) == -1 && 691 a->filesize == 0) { 692 archive_set_error(&a->archive, errno, 693 "File size could not be restored"); 694 return (ARCHIVE_FAILED); 695 } 696#endif 697 /* 698 * Not all platforms implement the XSI option to 699 * extend files via ftruncate. Stat() the file again 700 * to see what happened. 701 */ 702 a->pst = NULL; 703 if ((ret = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK) 704 return (ret); 705 /* We can use lseek()/write() to extend the file if 706 * ftruncate didn't work or isn't available. */ 707 if (a->st.st_size < a->filesize) { 708 const char nul = '\0'; 709 if (lseek(a->fd, a->filesize - 1, SEEK_SET) < 0) { 710 archive_set_error(&a->archive, errno, 711 "Seek failed"); 712 return (ARCHIVE_FATAL); 713 } 714 if (write(a->fd, &nul, 1) < 0) { 715 archive_set_error(&a->archive, errno, 716 "Write to restore size failed"); 717 return (ARCHIVE_FATAL); 718 } 719 a->pst = NULL; 720 } 721 } 722 723 /* Restore metadata. */ 724 725 /* 726 * Look up the "real" UID only if we're going to need it. 727 * TODO: the TODO_SGID condition can be dropped here, can't it? 728 */ 729 if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) { 730 a->uid = a->lookup_uid(a->lookup_uid_data, 731 archive_entry_uname(a->entry), 732 archive_entry_uid(a->entry)); 733 } 734 /* Look up the "real" GID only if we're going to need it. */ 735 /* TODO: the TODO_SUID condition can be dropped here, can't it? */ 736 if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) { 737 a->gid = a->lookup_gid(a->lookup_gid_data, 738 archive_entry_gname(a->entry), 739 archive_entry_gid(a->entry)); 740 } 741 /* 742 * If restoring ownership, do it before trying to restore suid/sgid 743 * bits. If we set the owner, we know what it is and can skip 744 * a stat() call to examine the ownership of the file on disk. 745 */ 746 if (a->todo & TODO_OWNER) 747 ret = set_ownership(a); 748 if (a->todo & TODO_MODE) { 749 int r2 = set_mode(a, a->mode); 750 if (r2 < ret) ret = r2; 751 } 752 if (a->todo & TODO_ACLS) { 753 int r2 = set_acls(a); 754 if (r2 < ret) ret = r2; 755 } 756 757 /* 758 * Security-related extended attributes (such as 759 * security.capability on Linux) have to be restored last, 760 * since they're implicitly removed by other file changes. 761 */ 762 if (a->todo & TODO_XATTR) { 763 int r2 = set_xattrs(a); 764 if (r2 < ret) ret = r2; 765 } 766 767 /* 768 * Some flags prevent file modification; they must be restored after 769 * file contents are written. 770 */ 771 if (a->todo & TODO_FFLAGS) { 772 int r2 = set_fflags(a); 773 if (r2 < ret) ret = r2; 774 } 775 /* 776 * Time has to be restored after all other metadata; 777 * otherwise atime will get changed. 778 */ 779 if (a->todo & TODO_TIMES) { 780 int r2 = set_times(a); 781 if (r2 < ret) ret = r2; 782 } 783 784 /* If there's an fd, we can close it now. */ 785 if (a->fd >= 0) { 786 close(a->fd); 787 a->fd = -1; 788 } 789 /* If there's an entry, we can release it now. */ 790 if (a->entry) { 791 archive_entry_free(a->entry); 792 a->entry = NULL; 793 } 794 a->archive.state = ARCHIVE_STATE_HEADER; 795 return (ret); 796} 797 798int 799archive_write_disk_set_group_lookup(struct archive *_a, 800 void *private_data, 801 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid), 802 void (*cleanup_gid)(void *private)) 803{ 804 struct archive_write_disk *a = (struct archive_write_disk *)_a; 805 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 806 ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup"); 807 808 a->lookup_gid = lookup_gid; 809 a->cleanup_gid = cleanup_gid; 810 a->lookup_gid_data = private_data; 811 return (ARCHIVE_OK); 812} 813 814int 815archive_write_disk_set_user_lookup(struct archive *_a, 816 void *private_data, 817 uid_t (*lookup_uid)(void *private, const char *uname, uid_t uid), 818 void (*cleanup_uid)(void *private)) 819{ 820 struct archive_write_disk *a = (struct archive_write_disk *)_a; 821 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 822 ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup"); 823 824 a->lookup_uid = lookup_uid; 825 a->cleanup_uid = cleanup_uid; 826 a->lookup_uid_data = private_data; 827 return (ARCHIVE_OK); 828} 829 830 831/* 832 * Create a new archive_write_disk object and initialize it with global state. 833 */ 834struct archive * 835archive_write_disk_new(void) 836{ 837 struct archive_write_disk *a; 838 839 a = (struct archive_write_disk *)malloc(sizeof(*a)); 840 if (a == NULL) 841 return (NULL); 842 memset(a, 0, sizeof(*a)); 843 a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC; 844 /* We're ready to write a header immediately. */ 845 a->archive.state = ARCHIVE_STATE_HEADER; 846 a->archive.vtable = archive_write_disk_vtable(); 847 a->lookup_uid = trivial_lookup_uid; 848 a->lookup_gid = trivial_lookup_gid; 849 a->start_time = time(NULL); 850#ifdef HAVE_GETEUID 851 a->user_uid = geteuid(); 852#endif /* HAVE_GETEUID */ 853 if (archive_string_ensure(&a->path_safe, 512) == NULL) { 854 free(a); 855 return (NULL); 856 } 857 return (&a->archive); 858} 859 860 861/* 862 * If pathname is longer than PATH_MAX, chdir to a suitable 863 * intermediate dir and edit the path down to a shorter suffix. Note 864 * that this routine never returns an error; if the chdir() attempt 865 * fails for any reason, we just go ahead with the long pathname. The 866 * object creation is likely to fail, but any error will get handled 867 * at that time. 868 */ 869#ifdef HAVE_FCHDIR 870static void 871edit_deep_directories(struct archive_write_disk *a) 872{ 873 int ret; 874 char *tail = a->name; 875 876 a->restore_pwd = -1; 877 878 /* If path is short, avoid the open() below. */ 879 if (strlen(tail) <= PATH_MAX) 880 return; 881 882 /* Try to record our starting dir. */ 883 a->restore_pwd = open(".", O_RDONLY | O_BINARY); 884 if (a->restore_pwd < 0) 885 return; 886 887 /* As long as the path is too long... */ 888 while (strlen(tail) > PATH_MAX) { 889 /* Locate a dir prefix shorter than PATH_MAX. */ 890 tail += PATH_MAX - 8; 891 while (tail > a->name && *tail != '/') 892 tail--; 893 /* Exit if we find a too-long path component. */ 894 if (tail <= a->name) 895 return; 896 /* Create the intermediate dir and chdir to it. */ 897 *tail = '\0'; /* Terminate dir portion */ 898 ret = create_dir(a, a->name); 899 if (ret == ARCHIVE_OK && chdir(a->name) != 0) 900 ret = ARCHIVE_FAILED; 901 *tail = '/'; /* Restore the / we removed. */ 902 if (ret != ARCHIVE_OK) 903 return; 904 tail++; 905 /* The chdir() succeeded; we've now shortened the path. */ 906 a->name = tail; 907 } 908 return; 909} 910#endif 911 912/* 913 * The main restore function. 914 */ 915static int 916restore_entry(struct archive_write_disk *a) 917{ 918 int ret = ARCHIVE_OK, en; 919 920 if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) { 921 /* 922 * TODO: Fix this. Apparently, there are platforms 923 * that still allow root to hose the entire filesystem 924 * by unlinking a dir. The S_ISDIR() test above 925 * prevents us from using unlink() here if the new 926 * object is a dir, but that doesn't mean the old 927 * object isn't a dir. 928 */ 929 if (unlink(a->name) == 0) { 930 /* We removed it, reset cached stat. */ 931 a->pst = NULL; 932 } else if (errno == ENOENT) { 933 /* File didn't exist, that's just as good. */ 934 } else if (rmdir(a->name) == 0) { 935 /* It was a dir, but now it's gone. */ 936 a->pst = NULL; 937 } else { 938 /* We tried, but couldn't get rid of it. */ 939 archive_set_error(&a->archive, errno, 940 "Could not unlink"); 941 return(ARCHIVE_FAILED); 942 } 943 } 944 945 /* Try creating it first; if this fails, we'll try to recover. */ 946 en = create_filesystem_object(a); 947 948 if ((en == ENOTDIR || en == ENOENT) 949 && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) { 950 /* If the parent dir doesn't exist, try creating it. */ 951 create_parent_dir(a, a->name); 952 /* Now try to create the object again. */ 953 en = create_filesystem_object(a); 954 } 955 956 if ((en == EISDIR || en == EEXIST) 957 && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) { 958 /* If we're not overwriting, we're done. */ 959 archive_set_error(&a->archive, en, "Already exists"); 960 return (ARCHIVE_FAILED); 961 } 962 963 /* 964 * Some platforms return EISDIR if you call 965 * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some 966 * return EEXIST. POSIX is ambiguous, requiring EISDIR 967 * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT) 968 * on an existing item. 969 */ 970 if (en == EISDIR) { 971 /* A dir is in the way of a non-dir, rmdir it. */ 972 if (rmdir(a->name) != 0) { 973 archive_set_error(&a->archive, errno, 974 "Can't remove already-existing dir"); 975 return (ARCHIVE_FAILED); 976 } 977 a->pst = NULL; 978 /* Try again. */ 979 en = create_filesystem_object(a); 980 } else if (en == EEXIST) { 981 /* 982 * We know something is in the way, but we don't know what; 983 * we need to find out before we go any further. 984 */ 985 int r = 0; 986 /* 987 * The SECURE_SYMLINK logic has already removed a 988 * symlink to a dir if the client wants that. So 989 * follow the symlink if we're creating a dir. 990 */ 991 if (S_ISDIR(a->mode)) 992 r = stat(a->name, &a->st); 993 /* 994 * If it's not a dir (or it's a broken symlink), 995 * then don't follow it. 996 */ 997 if (r != 0 || !S_ISDIR(a->mode)) 998 r = lstat(a->name, &a->st); 999 if (r != 0) { 1000 archive_set_error(&a->archive, errno, 1001 "Can't stat existing object"); 1002 return (ARCHIVE_FAILED); 1003 } 1004 1005 /* 1006 * NO_OVERWRITE_NEWER doesn't apply to directories. 1007 */ 1008 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER) 1009 && !S_ISDIR(a->st.st_mode)) { 1010 if (!older(&(a->st), a->entry)) { 1011 archive_set_error(&a->archive, 0, 1012 "File on disk is not older; skipping."); 1013 return (ARCHIVE_FAILED); 1014 } 1015 } 1016 1017 /* If it's our archive, we're done. */ 1018 if (a->skip_file_dev > 0 && 1019 a->skip_file_ino > 0 && 1020 a->st.st_dev == a->skip_file_dev && 1021 a->st.st_ino == a->skip_file_ino) { 1022 archive_set_error(&a->archive, 0, "Refusing to overwrite archive"); 1023 return (ARCHIVE_FAILED); 1024 } 1025 1026 if (!S_ISDIR(a->st.st_mode)) { 1027 /* A non-dir is in the way, unlink it. */ 1028 if (unlink(a->name) != 0) { 1029 archive_set_error(&a->archive, errno, 1030 "Can't unlink already-existing object"); 1031 return (ARCHIVE_FAILED); 1032 } 1033 a->pst = NULL; 1034 /* Try again. */ 1035 en = create_filesystem_object(a); 1036 } else if (!S_ISDIR(a->mode)) { 1037 /* A dir is in the way of a non-dir, rmdir it. */ 1038 if (rmdir(a->name) != 0) { 1039 archive_set_error(&a->archive, errno, 1040 "Can't remove already-existing dir"); 1041 return (ARCHIVE_FAILED); 1042 } 1043 /* Try again. */ 1044 en = create_filesystem_object(a); 1045 } else { 1046 /* 1047 * There's a dir in the way of a dir. Don't 1048 * waste time with rmdir()/mkdir(), just fix 1049 * up the permissions on the existing dir. 1050 * Note that we don't change perms on existing 1051 * dirs unless _EXTRACT_PERM is specified. 1052 */ 1053 if ((a->mode != a->st.st_mode) 1054 && (a->todo & TODO_MODE_FORCE)) 1055 a->deferred |= (a->todo & TODO_MODE); 1056 /* Ownership doesn't need deferred fixup. */ 1057 en = 0; /* Forget the EEXIST. */ 1058 } 1059 } 1060 1061 if (en) { 1062 /* Everything failed; give up here. */ 1063 archive_set_error(&a->archive, en, "Can't create '%s'", 1064 a->name); 1065 return (ARCHIVE_FAILED); 1066 } 1067 1068 a->pst = NULL; /* Cached stat data no longer valid. */ 1069 return (ret); 1070} 1071 1072/* 1073 * Returns 0 if creation succeeds, or else returns errno value from 1074 * the failed system call. Note: This function should only ever perform 1075 * a single system call. 1076 */ 1077static int 1078create_filesystem_object(struct archive_write_disk *a) 1079{ 1080 /* Create the entry. */ 1081 const char *linkname; 1082 mode_t final_mode, mode; 1083 int r; 1084 1085 /* We identify hard/symlinks according to the link names. */ 1086 /* Since link(2) and symlink(2) don't handle modes, we're done here. */ 1087 linkname = archive_entry_hardlink(a->entry); 1088 if (linkname != NULL) { 1089#if !HAVE_LINK 1090 return (EPERM); 1091#else 1092 r = link(linkname, a->name) ? errno : 0; 1093 /* 1094 * New cpio and pax formats allow hardlink entries 1095 * to carry data, so we may have to open the file 1096 * for hardlink entries. 1097 * 1098 * If the hardlink was successfully created and 1099 * the archive doesn't have carry data for it, 1100 * consider it to be non-authoritive for meta data. 1101 * This is consistent with GNU tar and BSD pax. 1102 * If the hardlink does carry data, let the last 1103 * archive entry decide ownership. 1104 */ 1105 if (r == 0 && a->filesize <= 0) { 1106 a->todo = 0; 1107 a->deferred = 0; 1108 } if (r == 0 && a->filesize > 0) { 1109 a->fd = open(a->name, O_WRONLY | O_TRUNC | O_BINARY); 1110 if (a->fd < 0) 1111 r = errno; 1112 } 1113 return (r); 1114#endif 1115 } 1116 linkname = archive_entry_symlink(a->entry); 1117 if (linkname != NULL) { 1118#if HAVE_SYMLINK 1119 return symlink(linkname, a->name) ? errno : 0; 1120#else 1121 return (EPERM); 1122#endif 1123 } 1124 1125 /* 1126 * The remaining system calls all set permissions, so let's 1127 * try to take advantage of that to avoid an extra chmod() 1128 * call. (Recall that umask is set to zero right now!) 1129 */ 1130 1131 /* Mode we want for the final restored object (w/o file type bits). */ 1132 final_mode = a->mode & 07777; 1133 /* 1134 * The mode that will actually be restored in this step. Note 1135 * that SUID, SGID, etc, require additional work to ensure 1136 * security, so we never restore them at this point. 1137 */ 1138 mode = final_mode & 0777; 1139 1140 switch (a->mode & AE_IFMT) { 1141 default: 1142 /* POSIX requires that we fall through here. */ 1143 /* FALLTHROUGH */ 1144 case AE_IFREG: 1145 a->fd = open(a->name, 1146 O_WRONLY | O_CREAT | O_EXCL | O_BINARY, mode); 1147 r = (a->fd < 0); 1148 break; 1149 case AE_IFCHR: 1150#ifdef HAVE_MKNOD 1151 /* Note: we use AE_IFCHR for the case label, and 1152 * S_IFCHR for the mknod() call. This is correct. */ 1153 r = mknod(a->name, mode | S_IFCHR, 1154 archive_entry_rdev(a->entry)); 1155 break; 1156#else 1157 /* TODO: Find a better way to warn about our inability 1158 * to restore a char device node. */ 1159 return (EINVAL); 1160#endif /* HAVE_MKNOD */ 1161 case AE_IFBLK: 1162#ifdef HAVE_MKNOD 1163 r = mknod(a->name, mode | S_IFBLK, 1164 archive_entry_rdev(a->entry)); 1165 break; 1166#else 1167 /* TODO: Find a better way to warn about our inability 1168 * to restore a block device node. */ 1169 return (EINVAL); 1170#endif /* HAVE_MKNOD */ 1171 case AE_IFDIR: 1172 mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE; 1173 r = mkdir(a->name, mode); 1174 if (r == 0) { 1175 /* Defer setting dir times. */ 1176 a->deferred |= (a->todo & TODO_TIMES); 1177 a->todo &= ~TODO_TIMES; 1178 /* Never use an immediate chmod(). */ 1179 /* We can't avoid the chmod() entirely if EXTRACT_PERM 1180 * because of SysV SGID inheritance. */ 1181 if ((mode != final_mode) 1182 || (a->flags & ARCHIVE_EXTRACT_PERM)) 1183 a->deferred |= (a->todo & TODO_MODE); 1184 a->todo &= ~TODO_MODE; 1185 } 1186 break; 1187 case AE_IFIFO: 1188#ifdef HAVE_MKFIFO 1189 r = mkfifo(a->name, mode); 1190 break; 1191#else 1192 /* TODO: Find a better way to warn about our inability 1193 * to restore a fifo. */ 1194 return (EINVAL); 1195#endif /* HAVE_MKFIFO */ 1196 } 1197 1198 /* All the system calls above set errno on failure. */ 1199 if (r) 1200 return (errno); 1201 1202 /* If we managed to set the final mode, we've avoided a chmod(). */ 1203 if (mode == final_mode) 1204 a->todo &= ~TODO_MODE; 1205 return (0); 1206} 1207 1208/* 1209 * Cleanup function for archive_extract. Mostly, this involves processing 1210 * the fixup list, which is used to address a number of problems: 1211 * * Dir permissions might prevent us from restoring a file in that 1212 * dir, so we restore the dir with minimum 0700 permissions first, 1213 * then correct the mode at the end. 1214 * * Similarly, the act of restoring a file touches the directory 1215 * and changes the timestamp on the dir, so we have to touch-up dir 1216 * timestamps at the end as well. 1217 * * Some file flags can interfere with the restore by, for example, 1218 * preventing the creation of hardlinks to those files. 1219 * 1220 * Note that tar/cpio do not require that archives be in a particular 1221 * order; there is no way to know when the last file has been restored 1222 * within a directory, so there's no way to optimize the memory usage 1223 * here by fixing up the directory any earlier than the 1224 * end-of-archive. 1225 * 1226 * XXX TODO: Directory ACLs should be restored here, for the same 1227 * reason we set directory perms here. XXX 1228 */ 1229static int 1230_archive_write_close(struct archive *_a) 1231{ 1232 struct archive_write_disk *a = (struct archive_write_disk *)_a; 1233 struct fixup_entry *next, *p; 1234 int ret; 1235 1236 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC, 1237 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, 1238 "archive_write_disk_close"); 1239 ret = _archive_write_finish_entry(&a->archive); 1240 1241 /* Sort dir list so directories are fixed up in depth-first order. */ 1242 p = sort_dir_list(a->fixup_list); 1243 1244 while (p != NULL) { 1245 a->pst = NULL; /* Mark stat cache as out-of-date. */ 1246 if (p->fixup & TODO_TIMES) { 1247#ifdef HAVE_UTIMES 1248 /* {f,l,}utimes() are preferred, when available. */ 1249#if defined(_WIN32) && !defined(__CYGWIN__) 1250 struct __timeval times[2]; 1251#else 1252 struct timeval times[2]; 1253#endif 1254 times[0].tv_sec = p->atime; 1255 times[0].tv_usec = p->atime_nanos / 1000; 1256#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME 1257 /* if it's valid and not mtime, push the birthtime first */ 1258 if (((times[1].tv_sec = p->birthtime) < p->mtime) && 1259 (p->birthtime > 0)) 1260 { 1261 times[1].tv_usec = p->birthtime_nanos / 1000; 1262 utimes(p->name, times); 1263 } 1264#endif 1265 times[1].tv_sec = p->mtime; 1266 times[1].tv_usec = p->mtime_nanos / 1000; 1267#ifdef HAVE_LUTIMES 1268 lutimes(p->name, times); 1269#else 1270 utimes(p->name, times); 1271#endif 1272#else 1273 /* utime() is more portable, but less precise. */ 1274 struct utimbuf times; 1275 times.modtime = p->mtime; 1276 times.actime = p->atime; 1277 1278 utime(p->name, ×); 1279#endif 1280 } 1281 if (p->fixup & TODO_MODE_BASE) 1282 chmod(p->name, p->mode); 1283 1284 if (p->fixup & TODO_FFLAGS) 1285 set_fflags_platform(a, -1, p->name, 1286 p->mode, p->fflags_set, 0); 1287 1288 next = p->next; 1289 free(p->name); 1290 free(p); 1291 p = next; 1292 } 1293 a->fixup_list = NULL; 1294 return (ret); 1295} 1296 1297static int 1298_archive_write_finish(struct archive *_a) 1299{ 1300 struct archive_write_disk *a = (struct archive_write_disk *)_a; 1301 int ret; 1302 ret = _archive_write_close(&a->archive); 1303 if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL) 1304 (a->cleanup_gid)(a->lookup_gid_data); 1305 if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL) 1306 (a->cleanup_uid)(a->lookup_uid_data); 1307 if (a->entry) 1308 archive_entry_free(a->entry); 1309 archive_string_free(&a->_name_data); 1310 archive_string_free(&a->archive.error_string); 1311 archive_string_free(&a->path_safe); 1312 free(a); 1313 return (ret); 1314} 1315 1316/* 1317 * Simple O(n log n) merge sort to order the fixup list. In 1318 * particular, we want to restore dir timestamps depth-first. 1319 */ 1320static struct fixup_entry * 1321sort_dir_list(struct fixup_entry *p) 1322{ 1323 struct fixup_entry *a, *b, *t; 1324 1325 if (p == NULL) 1326 return (NULL); 1327 /* A one-item list is already sorted. */ 1328 if (p->next == NULL) 1329 return (p); 1330 1331 /* Step 1: split the list. */ 1332 t = p; 1333 a = p->next->next; 1334 while (a != NULL) { 1335 /* Step a twice, t once. */ 1336 a = a->next; 1337 if (a != NULL) 1338 a = a->next; 1339 t = t->next; 1340 } 1341 /* Now, t is at the mid-point, so break the list here. */ 1342 b = t->next; 1343 t->next = NULL; 1344 a = p; 1345 1346 /* Step 2: Recursively sort the two sub-lists. */ 1347 a = sort_dir_list(a); 1348 b = sort_dir_list(b); 1349 1350 /* Step 3: Merge the returned lists. */ 1351 /* Pick the first element for the merged list. */ 1352 if (strcmp(a->name, b->name) > 0) { 1353 t = p = a; 1354 a = a->next; 1355 } else { 1356 t = p = b; 1357 b = b->next; 1358 } 1359 1360 /* Always put the later element on the list first. */ 1361 while (a != NULL && b != NULL) { 1362 if (strcmp(a->name, b->name) > 0) { 1363 t->next = a; 1364 a = a->next; 1365 } else { 1366 t->next = b; 1367 b = b->next; 1368 } 1369 t = t->next; 1370 } 1371 1372 /* Only one list is non-empty, so just splice it on. */ 1373 if (a != NULL) 1374 t->next = a; 1375 if (b != NULL) 1376 t->next = b; 1377 1378 return (p); 1379} 1380 1381/* 1382 * Returns a new, initialized fixup entry. 1383 * 1384 * TODO: Reduce the memory requirements for this list by using a tree 1385 * structure rather than a simple list of names. 1386 */ 1387static struct fixup_entry * 1388new_fixup(struct archive_write_disk *a, const char *pathname) 1389{ 1390 struct fixup_entry *fe; 1391 1392 fe = (struct fixup_entry *)malloc(sizeof(struct fixup_entry)); 1393 if (fe == NULL) 1394 return (NULL); 1395 fe->next = a->fixup_list; 1396 a->fixup_list = fe; 1397 fe->fixup = 0; 1398 fe->name = strdup(pathname); 1399 return (fe); 1400} 1401 1402/* 1403 * Returns a fixup structure for the current entry. 1404 */ 1405static struct fixup_entry * 1406current_fixup(struct archive_write_disk *a, const char *pathname) 1407{ 1408 if (a->current_fixup == NULL) 1409 a->current_fixup = new_fixup(a, pathname); 1410 return (a->current_fixup); 1411} 1412 1413/* TODO: Make this work. */ 1414/* 1415 * TODO: The deep-directory support bypasses this; disable deep directory 1416 * support if we're doing symlink checks. 1417 */ 1418/* 1419 * TODO: Someday, integrate this with the deep dir support; they both 1420 * scan the path and both can be optimized by comparing against other 1421 * recent paths. 1422 */ 1423/* TODO: Extend this to support symlinks on Windows Vista and later. */ 1424static int 1425check_symlinks(struct archive_write_disk *a) 1426{ 1427#if !defined(HAVE_LSTAT) 1428 /* Platform doesn't have lstat, so we can't look for symlinks. */ 1429 (void)a; /* UNUSED */ 1430 return (ARCHIVE_OK); 1431#else 1432 char *pn, *p; 1433 char c; 1434 int r; 1435 struct stat st; 1436 1437 /* 1438 * Guard against symlink tricks. Reject any archive entry whose 1439 * destination would be altered by a symlink. 1440 */ 1441 /* Whatever we checked last time doesn't need to be re-checked. */ 1442 pn = a->name; 1443 p = a->path_safe.s; 1444 while ((*pn != '\0') && (*p == *pn)) 1445 ++p, ++pn; 1446 c = pn[0]; 1447 /* Keep going until we've checked the entire name. */ 1448 while (pn[0] != '\0' && (pn[0] != '/' || pn[1] != '\0')) { 1449 /* Skip the next path element. */ 1450 while (*pn != '\0' && *pn != '/') 1451 ++pn; 1452 c = pn[0]; 1453 pn[0] = '\0'; 1454 /* Check that we haven't hit a symlink. */ 1455 r = lstat(a->name, &st); 1456 if (r != 0) { 1457 /* We've hit a dir that doesn't exist; stop now. */ 1458 if (errno == ENOENT) 1459 break; 1460 } else if (S_ISLNK(st.st_mode)) { 1461 if (c == '\0') { 1462 /* 1463 * Last element is symlink; remove it 1464 * so we can overwrite it with the 1465 * item being extracted. 1466 */ 1467 if (unlink(a->name)) { 1468 archive_set_error(&a->archive, errno, 1469 "Could not remove symlink %s", 1470 a->name); 1471 pn[0] = c; 1472 return (ARCHIVE_FAILED); 1473 } 1474 a->pst = NULL; 1475 /* 1476 * Even if we did remove it, a warning 1477 * is in order. The warning is silly, 1478 * though, if we're just replacing one 1479 * symlink with another symlink. 1480 */ 1481 if (!S_ISLNK(a->mode)) { 1482 archive_set_error(&a->archive, 0, 1483 "Removing symlink %s", 1484 a->name); 1485 } 1486 /* Symlink gone. No more problem! */ 1487 pn[0] = c; 1488 return (0); 1489 } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) { 1490 /* User asked us to remove problems. */ 1491 if (unlink(a->name) != 0) { 1492 archive_set_error(&a->archive, 0, 1493 "Cannot remove intervening symlink %s", 1494 a->name); 1495 pn[0] = c; 1496 return (ARCHIVE_FAILED); 1497 } 1498 a->pst = NULL; 1499 } else { 1500 archive_set_error(&a->archive, 0, 1501 "Cannot extract through symlink %s", 1502 a->name); 1503 pn[0] = c; 1504 return (ARCHIVE_FAILED); 1505 } 1506 } 1507 } 1508 pn[0] = c; 1509 /* We've checked and/or cleaned the whole path, so remember it. */ 1510 archive_strcpy(&a->path_safe, a->name); 1511 return (ARCHIVE_OK); 1512#endif 1513} 1514 1515#if defined(_WIN32) || defined(__CYGWIN__) 1516static int 1517guidword(const char *p, int n) 1518{ 1519 int i; 1520 1521 for (i = 0; i < n; i++) { 1522 if ((*p >= '0' && *p <= '9') || 1523 (*p >= 'a' && *p <= 'f') || 1524 (*p >= 'A' && *p <= 'F')) 1525 p++; 1526 else 1527 return (-1); 1528 } 1529 return (0); 1530} 1531 1532/* 1533 * 1. Convert a path separator from '\' to '/' . 1534 * We shouldn't check multi-byte character directly because some 1535 * character-set have been using the '\' character for a part of 1536 * its multibyte character code. 1537 * 2. Replace unusable characters in Windows with underscore('_'). 1538 * See also : http://msdn.microsoft.com/en-us/library/aa365247.aspx 1539 */ 1540static int 1541cleanup_pathname_win(struct archive_write_disk *a) 1542{ 1543 wchar_t wc; 1544 char *p; 1545 size_t alen, l; 1546 1547 p = a->name; 1548 /* Skip leading "\\.\" or "\\?\" or "\\?\UNC\" or 1549 * "\\?\Volume{GUID}\" 1550 * (absolute path prefixes used by Windows API) */ 1551 if ((p[0] == '\\' || p[0] == '/') && (p[1] == '\\' || p[1] == '/' ) && 1552 (p[2] == '.' || p[2] == '?') && (p[3] == '\\' || p[3] == '/')) 1553 { 1554 /* A path begin with "\\?\UNC\" */ 1555 if (p[2] == '?' && 1556 (p[4] == 'U' || p[4] == 'u') && 1557 (p[5] == 'N' || p[5] == 'n') && 1558 (p[6] == 'C' || p[6] == 'c') && 1559 (p[7] == '\\' || p[7] == '/')) 1560 p += 8; 1561 /* A path begin with "\\?\Volume{GUID}\" */ 1562 else if (p[2] == '?' && 1563 (p[4] == 'V' || p[4] == 'v') && 1564 (p[5] == 'O' || p[5] == 'o') && 1565 (p[6] == 'L' || p[6] == 'l') && 1566 (p[7] == 'U' || p[7] == 'u') && 1567 (p[8] == 'M' || p[8] == 'm') && 1568 (p[9] == 'E' || p[9] == 'e') && 1569 p[10] == '{') { 1570 if (guidword(p+11, 8) == 0 && p[19] == '-' && 1571 guidword(p+20, 4) == 0 && p[24] == '-' && 1572 guidword(p+25, 4) == 0 && p[29] == '-' && 1573 guidword(p+30, 4) == 0 && p[34] == '-' && 1574 guidword(p+35, 12) == 0 && p[47] == '}' && 1575 (p[48] == '\\' || p[48] == '/')) 1576 p += 49; 1577 else 1578 p += 4; 1579 /* A path begin with "\\.\PhysicalDriveX" */ 1580 } else if (p[2] == '.' && 1581 (p[4] == 'P' || p[4] == 'p') && 1582 (p[5] == 'H' || p[5] == 'h') && 1583 (p[6] == 'Y' || p[6] == 'y') && 1584 (p[7] == 'S' || p[7] == 's') && 1585 (p[8] == 'I' || p[8] == 'i') && 1586 (p[9] == 'C' || p[9] == 'c') && 1587 (p[9] == 'A' || p[9] == 'a') && 1588 (p[9] == 'L' || p[9] == 'l') && 1589 (p[9] == 'D' || p[9] == 'd') && 1590 (p[9] == 'R' || p[9] == 'r') && 1591 (p[9] == 'I' || p[9] == 'i') && 1592 (p[9] == 'V' || p[9] == 'v') && 1593 (p[9] == 'E' || p[9] == 'e') && 1594 (p[10] >= '0' && p[10] <= '9') && 1595 p[11] == '\0') { 1596 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1597 "Path is a physical drive name"); 1598 return (ARCHIVE_FAILED); 1599 } else 1600 p += 4; 1601 } 1602 1603 /* Skip leading drive letter from archives created 1604 * on Windows. */ 1605 if (((p[0] >= 'a' && p[0] <= 'z') || 1606 (p[0] >= 'A' && p[0] <= 'Z')) && 1607 p[1] == ':') { 1608 if (p[2] == '\0') { 1609 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1610 "Path is a drive name"); 1611 return (ARCHIVE_FAILED); 1612 } 1613 if (p[2] == '\\' || p[2] == '/') 1614 p += 3; 1615 } 1616 1617 for (; *p != '\0'; p++) { 1618 /* Rewrite the path name if its character is a unusable. */ 1619 if (*p == ':' || *p == '*' || *p == '?' || *p == '"' || 1620 *p == '<' || *p == '>' || *p == '|') 1621 *p = '_'; 1622 } 1623 alen = p - a->name; 1624 if (alen == 0 || strchr(a->name, '\\') == NULL) 1625 return (ARCHIVE_OK); 1626 /* 1627 * Convert path separator. 1628 */ 1629 p = a->name; 1630 while (*p != '\0' && alen) { 1631 l = mbtowc(&wc, p, alen); 1632 if (l == -1) { 1633 while (*p != '\0') { 1634 if (*p == '\\') 1635 *p = '/'; 1636 ++p; 1637 } 1638 break; 1639 } 1640 if (l == 1 && wc == L'\\') 1641 *p = '/'; 1642 p += l; 1643 alen -= l; 1644 } 1645 return (ARCHIVE_OK); 1646} 1647#endif 1648 1649/* 1650 * Canonicalize the pathname. In particular, this strips duplicate 1651 * '/' characters, '.' elements, and trailing '/'. It also raises an 1652 * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is 1653 * set) any '..' in the path. 1654 */ 1655static int 1656cleanup_pathname(struct archive_write_disk *a) 1657{ 1658 char *dest, *src; 1659 char separator = '\0'; 1660 1661 dest = src = a->name; 1662 if (*src == '\0') { 1663 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1664 "Invalid empty pathname"); 1665 return (ARCHIVE_FAILED); 1666 } 1667 1668#if defined(_WIN32) || defined(__CYGWIN__) 1669 if (cleanup_pathname_win(a) != ARCHIVE_OK) 1670 return (ARCHIVE_FAILED); 1671#endif 1672 /* Skip leading '/'. */ 1673 if (*src == '/') 1674 separator = *src++; 1675 1676 /* Scan the pathname one element at a time. */ 1677 for (;;) { 1678 /* src points to first char after '/' */ 1679 if (src[0] == '\0') { 1680 break; 1681 } else if (src[0] == '/') { 1682 /* Found '//', ignore second one. */ 1683 src++; 1684 continue; 1685 } else if (src[0] == '.') { 1686 if (src[1] == '\0') { 1687 /* Ignore trailing '.' */ 1688 break; 1689 } else if (src[1] == '/') { 1690 /* Skip './'. */ 1691 src += 2; 1692 continue; 1693 } else if (src[1] == '.') { 1694 if (src[2] == '/' || src[2] == '\0') { 1695 /* Conditionally warn about '..' */ 1696 if (a->flags & ARCHIVE_EXTRACT_SECURE_NODOTDOT) { 1697 archive_set_error(&a->archive, 1698 ARCHIVE_ERRNO_MISC, 1699 "Path contains '..'"); 1700 return (ARCHIVE_FAILED); 1701 } 1702 } 1703 /* 1704 * Note: Under no circumstances do we 1705 * remove '..' elements. In 1706 * particular, restoring 1707 * '/foo/../bar/' should create the 1708 * 'foo' dir as a side-effect. 1709 */ 1710 } 1711 } 1712 1713 /* Copy current element, including leading '/'. */ 1714 if (separator) 1715 *dest++ = '/'; 1716 while (*src != '\0' && *src != '/') { 1717 *dest++ = *src++; 1718 } 1719 1720 if (*src == '\0') 1721 break; 1722 1723 /* Skip '/' separator. */ 1724 separator = *src++; 1725 } 1726 /* 1727 * We've just copied zero or more path elements, not including the 1728 * final '/'. 1729 */ 1730 if (dest == a->name) { 1731 /* 1732 * Nothing got copied. The path must have been something 1733 * like '.' or '/' or './' or '/././././/./'. 1734 */ 1735 if (separator) 1736 *dest++ = '/'; 1737 else 1738 *dest++ = '.'; 1739 } 1740 /* Terminate the result. */ 1741 *dest = '\0'; 1742 return (ARCHIVE_OK); 1743} 1744 1745/* 1746 * Create the parent directory of the specified path, assuming path 1747 * is already in mutable storage. 1748 */ 1749static int 1750create_parent_dir(struct archive_write_disk *a, char *path) 1751{ 1752 char *slash; 1753 int r; 1754 1755 /* Remove tail element to obtain parent name. */ 1756 slash = strrchr(path, '/'); 1757 if (slash == NULL) 1758 return (ARCHIVE_OK); 1759 *slash = '\0'; 1760 r = create_dir(a, path); 1761 *slash = '/'; 1762 return (r); 1763} 1764 1765/* 1766 * Create the specified dir, recursing to create parents as necessary. 1767 * 1768 * Returns ARCHIVE_OK if the path exists when we're done here. 1769 * Otherwise, returns ARCHIVE_FAILED. 1770 * Assumes path is in mutable storage; path is unchanged on exit. 1771 */ 1772static int 1773create_dir(struct archive_write_disk *a, char *path) 1774{ 1775 struct stat st; 1776 struct fixup_entry *le; 1777 char *slash, *base; 1778 mode_t mode_final, mode; 1779 int r; 1780 1781 /* Check for special names and just skip them. */ 1782 slash = strrchr(path, '/'); 1783 if (slash == NULL) 1784 base = path; 1785 else 1786 base = slash + 1; 1787 1788 if (base[0] == '\0' || 1789 (base[0] == '.' && base[1] == '\0') || 1790 (base[0] == '.' && base[1] == '.' && base[2] == '\0')) { 1791 /* Don't bother trying to create null path, '.', or '..'. */ 1792 if (slash != NULL) { 1793 *slash = '\0'; 1794 r = create_dir(a, path); 1795 *slash = '/'; 1796 return (r); 1797 } 1798 return (ARCHIVE_OK); 1799 } 1800 1801 /* 1802 * Yes, this should be stat() and not lstat(). Using lstat() 1803 * here loses the ability to extract through symlinks. Also note 1804 * that this should not use the a->st cache. 1805 */ 1806 if (stat(path, &st) == 0) { 1807 if (S_ISDIR(st.st_mode)) 1808 return (ARCHIVE_OK); 1809 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) { 1810 archive_set_error(&a->archive, EEXIST, 1811 "Can't create directory '%s'", path); 1812 return (ARCHIVE_FAILED); 1813 } 1814 if (unlink(path) != 0) { 1815 archive_set_error(&a->archive, errno, 1816 "Can't create directory '%s': " 1817 "Conflicting file cannot be removed", path); 1818 return (ARCHIVE_FAILED); 1819 } 1820 } else if (errno != ENOENT && errno != ENOTDIR) { 1821 /* Stat failed? */ 1822 archive_set_error(&a->archive, errno, "Can't test directory '%s'", path); 1823 return (ARCHIVE_FAILED); 1824 } else if (slash != NULL) { 1825 *slash = '\0'; 1826 r = create_dir(a, path); 1827 *slash = '/'; 1828 if (r != ARCHIVE_OK) 1829 return (r); 1830 } 1831 1832 /* 1833 * Mode we want for the final restored directory. Per POSIX, 1834 * implicitly-created dirs must be created obeying the umask. 1835 * There's no mention whether this is different for privileged 1836 * restores (which the rest of this code handles by pretending 1837 * umask=0). I've chosen here to always obey the user's umask for 1838 * implicit dirs, even if _EXTRACT_PERM was specified. 1839 */ 1840 mode_final = DEFAULT_DIR_MODE & ~a->user_umask; 1841 /* Mode we want on disk during the restore process. */ 1842 mode = mode_final; 1843 mode |= MINIMUM_DIR_MODE; 1844 mode &= MAXIMUM_DIR_MODE; 1845 if (mkdir(path, mode) == 0) { 1846 if (mode != mode_final) { 1847 le = new_fixup(a, path); 1848 le->fixup |=TODO_MODE_BASE; 1849 le->mode = mode_final; 1850 } 1851 return (ARCHIVE_OK); 1852 } 1853 1854 /* 1855 * Without the following check, a/b/../b/c/d fails at the 1856 * second visit to 'b', so 'd' can't be created. Note that we 1857 * don't add it to the fixup list here, as it's already been 1858 * added. 1859 */ 1860 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) 1861 return (ARCHIVE_OK); 1862 1863 archive_set_error(&a->archive, errno, "Failed to create dir '%s'", 1864 path); 1865 return (ARCHIVE_FAILED); 1866} 1867 1868/* 1869 * Note: Although we can skip setting the user id if the desired user 1870 * id matches the current user, we cannot skip setting the group, as 1871 * many systems set the gid based on the containing directory. So 1872 * we have to perform a chown syscall if we want to set the SGID 1873 * bit. (The alternative is to stat() and then possibly chown(); it's 1874 * more efficient to skip the stat() and just always chown().) Note 1875 * that a successful chown() here clears the TODO_SGID_CHECK bit, which 1876 * allows set_mode to skip the stat() check for the GID. 1877 */ 1878static int 1879set_ownership(struct archive_write_disk *a) 1880{ 1881#ifndef __CYGWIN__ 1882/* unfortunately, on win32 there is no 'root' user with uid 0, 1883 so we just have to try the chown and see if it works */ 1884 1885 /* If we know we can't change it, don't bother trying. */ 1886 if (a->user_uid != 0 && a->user_uid != a->uid) { 1887 archive_set_error(&a->archive, errno, 1888 "Can't set UID=%d", a->uid); 1889 return (ARCHIVE_WARN); 1890 } 1891#endif 1892 1893#ifdef HAVE_FCHOWN 1894 /* If we have an fd, we can avoid a race. */ 1895 if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0) { 1896 /* We've set owner and know uid/gid are correct. */ 1897 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK); 1898 return (ARCHIVE_OK); 1899 } 1900#endif 1901 1902 /* We prefer lchown() but will use chown() if that's all we have. */ 1903 /* Of course, if we have neither, this will always fail. */ 1904#ifdef HAVE_LCHOWN 1905 if (lchown(a->name, a->uid, a->gid) == 0) { 1906 /* We've set owner and know uid/gid are correct. */ 1907 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK); 1908 return (ARCHIVE_OK); 1909 } 1910#elif HAVE_CHOWN 1911 if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0) { 1912 /* We've set owner and know uid/gid are correct. */ 1913 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK); 1914 return (ARCHIVE_OK); 1915 } 1916#endif 1917 1918 archive_set_error(&a->archive, errno, 1919 "Can't set user=%d/group=%d for %s", a->uid, a->gid, 1920 a->name); 1921 return (ARCHIVE_WARN); 1922} 1923 1924 1925#if defined(HAVE_UTIMENSAT) && defined(HAVE_FUTIMENS) 1926/* 1927 * utimensat() and futimens() are defined in POSIX.1-2008. They provide ns 1928 * resolution and setting times on fd and on symlinks, too. 1929 */ 1930static int 1931set_time(int fd, int mode, const char *name, 1932 time_t atime, long atime_nsec, 1933 time_t mtime, long mtime_nsec) 1934{ 1935 struct timespec ts[2]; 1936 ts[0].tv_sec = atime; 1937 ts[0].tv_nsec = atime_nsec; 1938 ts[1].tv_sec = mtime; 1939 ts[1].tv_nsec = mtime_nsec; 1940 if (fd >= 0) 1941 return futimens(fd, ts); 1942 return utimensat(AT_FDCWD, name, ts, AT_SYMLINK_NOFOLLOW); 1943} 1944#elif HAVE_UTIMES 1945/* 1946 * The utimes()-family functions provide ��s-resolution and 1947 * a way to set time on an fd or a symlink. We prefer them 1948 * when they're available and utimensat/futimens aren't there. 1949 */ 1950static int 1951set_time(int fd, int mode, const char *name, 1952 time_t atime, long atime_nsec, 1953 time_t mtime, long mtime_nsec) 1954{ 1955#if defined(_WIN32) && !defined(__CYGWIN__) 1956 struct __timeval times[2]; 1957#else 1958 struct timeval times[2]; 1959#endif 1960 1961 times[0].tv_sec = atime; 1962 times[0].tv_usec = atime_nsec / 1000; 1963 times[1].tv_sec = mtime; 1964 times[1].tv_usec = mtime_nsec / 1000; 1965 1966#ifdef HAVE_FUTIMES 1967 if (fd >= 0) 1968 return (futimes(fd, times)); 1969#else 1970 (void)fd; /* UNUSED */ 1971#endif 1972#ifdef HAVE_LUTIMES 1973 (void)mode; /* UNUSED */ 1974 return (lutimes(name, times)); 1975#else 1976 if (S_ISLNK(mode)) 1977 return (0); 1978 return (utimes(name, times)); 1979#endif 1980} 1981#elif defined(HAVE_UTIME) 1982/* 1983 * utime() is an older, more standard interface that we'll use 1984 * if utimes() isn't available. 1985 */ 1986static int 1987set_time(int fd, int mode, const char *name, 1988 time_t atime, long atime_nsec, 1989 time_t mtime, long mtime_nsec) 1990{ 1991 struct utimbuf times; 1992 (void)fd; /* UNUSED */ 1993 (void)name; /* UNUSED */ 1994 (void)atime_nsec; /* UNUSED */ 1995 (void)mtime_nsec; /* UNUSED */ 1996 times.actime = atime; 1997 times.modtime = mtime; 1998 if (S_ISLNK(mode)) 1999 return (ARCHIVE_OK); 2000 return (utime(name, ×)); 2001} 2002#else 2003static int 2004set_time(int fd, int mode, const char *name, 2005 time_t atime, long atime_nsec, 2006 time_t mtime, long mtime_nsec) 2007{ 2008 return (ARCHIVE_WARN); 2009} 2010#endif 2011 2012static int 2013set_times(struct archive_write_disk *a) 2014{ 2015 time_t atime = a->start_time, mtime = a->start_time; 2016 long atime_nsec = 0, mtime_nsec = 0; 2017 2018 /* If no time was provided, we're done. */ 2019 if (!archive_entry_atime_is_set(a->entry) 2020#if HAVE_STRUCT_STAT_ST_BIRTHTIME 2021 && !archive_entry_birthtime_is_set(a->entry) 2022#endif 2023 && !archive_entry_mtime_is_set(a->entry)) 2024 return (ARCHIVE_OK); 2025 2026 /* If no atime was specified, use start time instead. */ 2027 /* In theory, it would be marginally more correct to use 2028 * time(NULL) here, but that would cost us an extra syscall 2029 * for little gain. */ 2030 if (archive_entry_atime_is_set(a->entry)) { 2031 atime = archive_entry_atime(a->entry); 2032 atime_nsec = archive_entry_atime_nsec(a->entry); 2033 } 2034 2035 /* 2036 * If you have struct stat.st_birthtime, we assume BSD birthtime 2037 * semantics, in which {f,l,}utimes() updates birthtime to earliest 2038 * mtime. So we set the time twice, first using the birthtime, 2039 * then using the mtime. 2040 */ 2041#if HAVE_STRUCT_STAT_ST_BIRTHTIME 2042 /* If birthtime is set, flush that through to disk first. */ 2043 if (archive_entry_birthtime_is_set(a->entry)) 2044 if (set_time(a->fd, a->mode, a->name, atime, atime_nsec, 2045 archive_entry_birthtime(a->entry), 2046 archive_entry_birthtime_nsec(a->entry))) { 2047 archive_set_error(&a->archive, errno, 2048 "Can't update time for %s", 2049 a->name); 2050 return (ARCHIVE_WARN); 2051 } 2052#endif 2053 2054 if (archive_entry_mtime_is_set(a->entry)) { 2055 mtime = archive_entry_mtime(a->entry); 2056 mtime_nsec = archive_entry_mtime_nsec(a->entry); 2057 } 2058 if (set_time(a->fd, a->mode, a->name, 2059 atime, atime_nsec, mtime, mtime_nsec)) { 2060 archive_set_error(&a->archive, errno, 2061 "Can't update time for %s", 2062 a->name); 2063 return (ARCHIVE_WARN); 2064 } 2065 2066 /* 2067 * Note: POSIX does not provide a portable way to restore ctime. 2068 * (Apart from resetting the system clock, which is distasteful.) 2069 * So, any restoration of ctime will necessarily be OS-specific. 2070 */ 2071 2072 return (ARCHIVE_OK); 2073} 2074 2075static int 2076set_mode(struct archive_write_disk *a, int mode) 2077{ 2078 int r = ARCHIVE_OK; 2079 mode &= 07777; /* Strip off file type bits. */ 2080 2081 if (a->todo & TODO_SGID_CHECK) { 2082 /* 2083 * If we don't know the GID is right, we must stat() 2084 * to verify it. We can't just check the GID of this 2085 * process, since systems sometimes set GID from 2086 * the enclosing dir or based on ACLs. 2087 */ 2088 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK) 2089 return (r); 2090 if (a->pst->st_gid != a->gid) { 2091 mode &= ~ S_ISGID; 2092#if !defined(_WIN32) || defined(__CYGWIN__) 2093 if (a->flags & ARCHIVE_EXTRACT_OWNER) { 2094 /* 2095 * This is only an error if you 2096 * requested owner restore. If you 2097 * didn't, we'll try to restore 2098 * sgid/suid, but won't consider it a 2099 * problem if we can't. 2100 */ 2101 archive_set_error(&a->archive, -1, 2102 "Can't restore SGID bit"); 2103 r = ARCHIVE_WARN; 2104 } 2105#endif 2106 } 2107 /* While we're here, double-check the UID. */ 2108 if (a->pst->st_uid != a->uid 2109 && (a->todo & TODO_SUID)) { 2110 mode &= ~ S_ISUID; 2111#if !defined(_WIN32) || defined(__CYGWIN__) 2112 if (a->flags & ARCHIVE_EXTRACT_OWNER) { 2113 archive_set_error(&a->archive, -1, 2114 "Can't restore SUID bit"); 2115 r = ARCHIVE_WARN; 2116 } 2117#endif 2118 } 2119 a->todo &= ~TODO_SGID_CHECK; 2120 a->todo &= ~TODO_SUID_CHECK; 2121 } else if (a->todo & TODO_SUID_CHECK) { 2122 /* 2123 * If we don't know the UID is right, we can just check 2124 * the user, since all systems set the file UID from 2125 * the process UID. 2126 */ 2127 if (a->user_uid != a->uid) { 2128 mode &= ~ S_ISUID; 2129#if !defined(_WIN32) || defined(__CYGWIN__) 2130 if (a->flags & ARCHIVE_EXTRACT_OWNER) { 2131 archive_set_error(&a->archive, -1, 2132 "Can't make file SUID"); 2133 r = ARCHIVE_WARN; 2134 } 2135#endif 2136 } 2137 a->todo &= ~TODO_SUID_CHECK; 2138 } 2139 2140 if (S_ISLNK(a->mode)) { 2141#ifdef HAVE_LCHMOD 2142 /* 2143 * If this is a symlink, use lchmod(). If the 2144 * platform doesn't support lchmod(), just skip it. A 2145 * platform that doesn't provide a way to set 2146 * permissions on symlinks probably ignores 2147 * permissions on symlinks, so a failure here has no 2148 * impact. 2149 */ 2150 if (lchmod(a->name, mode) != 0) { 2151 archive_set_error(&a->archive, errno, 2152 "Can't set permissions to 0%o", (int)mode); 2153 r = ARCHIVE_WARN; 2154 } 2155#endif 2156 } else if (!S_ISDIR(a->mode)) { 2157 /* 2158 * If it's not a symlink and not a dir, then use 2159 * fchmod() or chmod(), depending on whether we have 2160 * an fd. Dirs get their perms set during the 2161 * post-extract fixup, which is handled elsewhere. 2162 */ 2163#ifdef HAVE_FCHMOD 2164 if (a->fd >= 0) { 2165 if (fchmod(a->fd, mode) != 0) { 2166 archive_set_error(&a->archive, errno, 2167 "Can't set permissions to 0%o", (int)mode); 2168 r = ARCHIVE_WARN; 2169 } 2170 } else 2171#endif 2172 /* If this platform lacks fchmod(), then 2173 * we'll just use chmod(). */ 2174 if (chmod(a->name, mode) != 0) { 2175 archive_set_error(&a->archive, errno, 2176 "Can't set permissions to 0%o", (int)mode); 2177 r = ARCHIVE_WARN; 2178 } 2179 } 2180 return (r); 2181} 2182 2183static int 2184set_fflags(struct archive_write_disk *a) 2185{ 2186 struct fixup_entry *le; 2187 unsigned long set, clear; 2188 int r; 2189 int critical_flags; 2190 mode_t mode = archive_entry_mode(a->entry); 2191 2192 /* 2193 * Make 'critical_flags' hold all file flags that can't be 2194 * immediately restored. For example, on BSD systems, 2195 * SF_IMMUTABLE prevents hardlinks from being created, so 2196 * should not be set until after any hardlinks are created. To 2197 * preserve some semblance of portability, this uses #ifdef 2198 * extensively. Ugly, but it works. 2199 * 2200 * Yes, Virginia, this does create a security race. It's mitigated 2201 * somewhat by the practice of creating dirs 0700 until the extract 2202 * is done, but it would be nice if we could do more than that. 2203 * People restoring critical file systems should be wary of 2204 * other programs that might try to muck with files as they're 2205 * being restored. 2206 */ 2207 /* Hopefully, the compiler will optimize this mess into a constant. */ 2208 critical_flags = 0; 2209#ifdef SF_IMMUTABLE 2210 critical_flags |= SF_IMMUTABLE; 2211#endif 2212#ifdef UF_IMMUTABLE 2213 critical_flags |= UF_IMMUTABLE; 2214#endif 2215#ifdef SF_APPEND 2216 critical_flags |= SF_APPEND; 2217#endif 2218#ifdef UF_APPEND 2219 critical_flags |= UF_APPEND; 2220#endif 2221#ifdef EXT2_APPEND_FL 2222 critical_flags |= EXT2_APPEND_FL; 2223#endif 2224#ifdef EXT2_IMMUTABLE_FL 2225 critical_flags |= EXT2_IMMUTABLE_FL; 2226#endif 2227 2228 if (a->todo & TODO_FFLAGS) { 2229 archive_entry_fflags(a->entry, &set, &clear); 2230 2231 /* 2232 * The first test encourages the compiler to eliminate 2233 * all of this if it's not necessary. 2234 */ 2235 if ((critical_flags != 0) && (set & critical_flags)) { 2236 le = current_fixup(a, a->name); 2237 le->fixup |= TODO_FFLAGS; 2238 le->fflags_set = set; 2239 /* Store the mode if it's not already there. */ 2240 if ((le->fixup & TODO_MODE) == 0) 2241 le->mode = mode; 2242 } else { 2243 r = set_fflags_platform(a, a->fd, 2244 a->name, mode, set, clear); 2245 if (r != ARCHIVE_OK) 2246 return (r); 2247 } 2248 } 2249 return (ARCHIVE_OK); 2250} 2251 2252 2253#if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && defined(HAVE_STRUCT_STAT_ST_FLAGS) 2254/* 2255 * BSD reads flags using stat() and sets them with one of {f,l,}chflags() 2256 */ 2257static int 2258set_fflags_platform(struct archive_write_disk *a, int fd, const char *name, 2259 mode_t mode, unsigned long set, unsigned long clear) 2260{ 2261 int r; 2262 2263 (void)mode; /* UNUSED */ 2264 if (set == 0 && clear == 0) 2265 return (ARCHIVE_OK); 2266 2267 /* 2268 * XXX Is the stat here really necessary? Or can I just use 2269 * the 'set' flags directly? In particular, I'm not sure 2270 * about the correct approach if we're overwriting an existing 2271 * file that already has flags on it. XXX 2272 */ 2273 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK) 2274 return (r); 2275 2276 a->st.st_flags &= ~clear; 2277 a->st.st_flags |= set; 2278#ifdef HAVE_FCHFLAGS 2279 /* If platform has fchflags() and we were given an fd, use it. */ 2280 if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0) 2281 return (ARCHIVE_OK); 2282#endif 2283 /* 2284 * If we can't use the fd to set the flags, we'll use the 2285 * pathname to set flags. We prefer lchflags() but will use 2286 * chflags() if we must. 2287 */ 2288#ifdef HAVE_LCHFLAGS 2289 if (lchflags(name, a->st.st_flags) == 0) 2290 return (ARCHIVE_OK); 2291#elif defined(HAVE_CHFLAGS) 2292 if (S_ISLNK(a->st.st_mode)) { 2293 archive_set_error(&a->archive, errno, 2294 "Can't set file flags on symlink."); 2295 return (ARCHIVE_WARN); 2296 } 2297 if (chflags(name, a->st.st_flags) == 0) 2298 return (ARCHIVE_OK); 2299#endif 2300 archive_set_error(&a->archive, errno, 2301 "Failed to set file flags"); 2302 return (ARCHIVE_WARN); 2303} 2304 2305#elif defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS) 2306/* 2307 * Linux uses ioctl() to read and write file flags. 2308 */ 2309static int 2310set_fflags_platform(struct archive_write_disk *a, int fd, const char *name, 2311 mode_t mode, unsigned long set, unsigned long clear) 2312{ 2313 int ret; 2314 int myfd = fd; 2315 unsigned long newflags, oldflags; 2316 unsigned long sf_mask = 0; 2317 2318 if (set == 0 && clear == 0) 2319 return (ARCHIVE_OK); 2320 /* Only regular files and dirs can have flags. */ 2321 if (!S_ISREG(mode) && !S_ISDIR(mode)) 2322 return (ARCHIVE_OK); 2323 2324 /* If we weren't given an fd, open it ourselves. */ 2325 if (myfd < 0) 2326 myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY); 2327 if (myfd < 0) 2328 return (ARCHIVE_OK); 2329 2330 /* 2331 * Linux has no define for the flags that are only settable by 2332 * the root user. This code may seem a little complex, but 2333 * there seem to be some Linux systems that lack these 2334 * defines. (?) The code below degrades reasonably gracefully 2335 * if sf_mask is incomplete. 2336 */ 2337#ifdef EXT2_IMMUTABLE_FL 2338 sf_mask |= EXT2_IMMUTABLE_FL; 2339#endif 2340#ifdef EXT2_APPEND_FL 2341 sf_mask |= EXT2_APPEND_FL; 2342#endif 2343 /* 2344 * XXX As above, this would be way simpler if we didn't have 2345 * to read the current flags from disk. XXX 2346 */ 2347 ret = ARCHIVE_OK; 2348 /* Try setting the flags as given. */ 2349 if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) { 2350 newflags = (oldflags & ~clear) | set; 2351 if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0) 2352 goto cleanup; 2353 if (errno != EPERM) 2354 goto fail; 2355 } 2356 /* If we couldn't set all the flags, try again with a subset. */ 2357 if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) { 2358 newflags &= ~sf_mask; 2359 oldflags &= sf_mask; 2360 newflags |= oldflags; 2361 if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0) 2362 goto cleanup; 2363 } 2364 /* We couldn't set the flags, so report the failure. */ 2365fail: 2366 archive_set_error(&a->archive, errno, 2367 "Failed to set file flags"); 2368 ret = ARCHIVE_WARN; 2369cleanup: 2370 if (fd < 0) 2371 close(myfd); 2372 return (ret); 2373} 2374 2375#else 2376 2377/* 2378 * Of course, some systems have neither BSD chflags() nor Linux' flags 2379 * support through ioctl(). 2380 */ 2381static int 2382set_fflags_platform(struct archive_write_disk *a, int fd, const char *name, 2383 mode_t mode, unsigned long set, unsigned long clear) 2384{ 2385 (void)a; /* UNUSED */ 2386 (void)fd; /* UNUSED */ 2387 (void)name; /* UNUSED */ 2388 (void)mode; /* UNUSED */ 2389 (void)set; /* UNUSED */ 2390 (void)clear; /* UNUSED */ 2391 return (ARCHIVE_OK); 2392} 2393 2394#endif /* __linux */ 2395 2396#ifndef HAVE_POSIX_ACL 2397/* Default empty function body to satisfy mainline code. */ 2398static int 2399set_acls(struct archive_write_disk *a) 2400{ 2401 (void)a; /* UNUSED */ 2402 return (ARCHIVE_OK); 2403} 2404 2405#else 2406 2407/* 2408 * XXX TODO: What about ACL types other than ACCESS and DEFAULT? 2409 */ 2410static int 2411set_acls(struct archive_write_disk *a) 2412{ 2413 int ret; 2414 2415 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_ACCESS, 2416 ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access"); 2417 if (ret != ARCHIVE_OK) 2418 return (ret); 2419 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_DEFAULT, 2420 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default"); 2421 return (ret); 2422} 2423 2424 2425static int 2426set_acl(struct archive_write_disk *a, int fd, struct archive_entry *entry, 2427 acl_type_t acl_type, int ae_requested_type, const char *tname) 2428{ 2429 acl_t acl; 2430 acl_entry_t acl_entry; 2431 acl_permset_t acl_permset; 2432 int ret; 2433 int ae_type, ae_permset, ae_tag, ae_id; 2434 uid_t ae_uid; 2435 gid_t ae_gid; 2436 const char *ae_name; 2437 int entries; 2438 const char *name; 2439 2440 ret = ARCHIVE_OK; 2441 entries = archive_entry_acl_reset(entry, ae_requested_type); 2442 if (entries == 0) 2443 return (ARCHIVE_OK); 2444 acl = acl_init(entries); 2445 while (archive_entry_acl_next(entry, ae_requested_type, &ae_type, 2446 &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) { 2447 acl_create_entry(&acl, &acl_entry); 2448 2449 switch (ae_tag) { 2450 case ARCHIVE_ENTRY_ACL_USER: 2451 acl_set_tag_type(acl_entry, ACL_USER); 2452 ae_uid = a->lookup_uid(a->lookup_uid_data, 2453 ae_name, ae_id); 2454 acl_set_qualifier(acl_entry, &ae_uid); 2455 break; 2456 case ARCHIVE_ENTRY_ACL_GROUP: 2457 acl_set_tag_type(acl_entry, ACL_GROUP); 2458 ae_gid = a->lookup_gid(a->lookup_gid_data, 2459 ae_name, ae_id); 2460 acl_set_qualifier(acl_entry, &ae_gid); 2461 break; 2462 case ARCHIVE_ENTRY_ACL_USER_OBJ: 2463 acl_set_tag_type(acl_entry, ACL_USER_OBJ); 2464 break; 2465 case ARCHIVE_ENTRY_ACL_GROUP_OBJ: 2466 acl_set_tag_type(acl_entry, ACL_GROUP_OBJ); 2467 break; 2468 case ARCHIVE_ENTRY_ACL_MASK: 2469 acl_set_tag_type(acl_entry, ACL_MASK); 2470 break; 2471 case ARCHIVE_ENTRY_ACL_OTHER: 2472 acl_set_tag_type(acl_entry, ACL_OTHER); 2473 break; 2474 default: 2475 /* XXX */ 2476 break; 2477 } 2478 2479 acl_get_permset(acl_entry, &acl_permset); 2480 acl_clear_perms(acl_permset); 2481 if (ae_permset & ARCHIVE_ENTRY_ACL_EXECUTE) 2482 acl_add_perm(acl_permset, ACL_EXECUTE); 2483 if (ae_permset & ARCHIVE_ENTRY_ACL_WRITE) 2484 acl_add_perm(acl_permset, ACL_WRITE); 2485 if (ae_permset & ARCHIVE_ENTRY_ACL_READ) 2486 acl_add_perm(acl_permset, ACL_READ); 2487 } 2488 2489 name = archive_entry_pathname(entry); 2490 2491 /* Try restoring the ACL through 'fd' if we can. */ 2492#if HAVE_ACL_SET_FD 2493 if (fd >= 0 && acl_type == ACL_TYPE_ACCESS && acl_set_fd(fd, acl) == 0) 2494 ret = ARCHIVE_OK; 2495 else 2496#else 2497#if HAVE_ACL_SET_FD_NP 2498 if (fd >= 0 && acl_set_fd_np(fd, acl, acl_type) == 0) 2499 ret = ARCHIVE_OK; 2500 else 2501#endif 2502#endif 2503 if (acl_set_file(name, acl_type, acl) != 0) { 2504 archive_set_error(&a->archive, errno, "Failed to set %s acl", tname); 2505 ret = ARCHIVE_WARN; 2506 } 2507 acl_free(acl); 2508 return (ret); 2509} 2510#endif 2511 2512#if HAVE_LSETXATTR 2513/* 2514 * Restore extended attributes - Linux implementation 2515 */ 2516static int 2517set_xattrs(struct archive_write_disk *a) 2518{ 2519 struct archive_entry *entry = a->entry; 2520 static int warning_done = 0; 2521 int ret = ARCHIVE_OK; 2522 int i = archive_entry_xattr_reset(entry); 2523 2524 while (i--) { 2525 const char *name; 2526 const void *value; 2527 size_t size; 2528 archive_entry_xattr_next(entry, &name, &value, &size); 2529 if (name != NULL && 2530 strncmp(name, "xfsroot.", 8) != 0 && 2531 strncmp(name, "system.", 7) != 0) { 2532 int e; 2533#if HAVE_FSETXATTR 2534 if (a->fd >= 0) 2535 e = fsetxattr(a->fd, name, value, size, 0); 2536 else 2537#endif 2538 { 2539 e = lsetxattr(archive_entry_pathname(entry), 2540 name, value, size, 0); 2541 } 2542 if (e == -1) { 2543 if (errno == ENOTSUP) { 2544 if (!warning_done) { 2545 warning_done = 1; 2546 archive_set_error(&a->archive, errno, 2547 "Cannot restore extended " 2548 "attributes on this file " 2549 "system"); 2550 } 2551 } else 2552 archive_set_error(&a->archive, errno, 2553 "Failed to set extended attribute"); 2554 ret = ARCHIVE_WARN; 2555 } 2556 } else { 2557 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2558 "Invalid extended attribute encountered"); 2559 ret = ARCHIVE_WARN; 2560 } 2561 } 2562 return (ret); 2563} 2564#elif HAVE_EXTATTR_SET_FILE && HAVE_DECL_EXTATTR_NAMESPACE_USER 2565/* 2566 * Restore extended attributes - FreeBSD implementation 2567 */ 2568static int 2569set_xattrs(struct archive_write_disk *a) 2570{ 2571 struct archive_entry *entry = a->entry; 2572 static int warning_done = 0; 2573 int ret = ARCHIVE_OK; 2574 int i = archive_entry_xattr_reset(entry); 2575 2576 while (i--) { 2577 const char *name; 2578 const void *value; 2579 size_t size; 2580 archive_entry_xattr_next(entry, &name, &value, &size); 2581 if (name != NULL) { 2582 int e; 2583 int namespace; 2584 2585 if (strncmp(name, "user.", 5) == 0) { 2586 /* "user." attributes go to user namespace */ 2587 name += 5; 2588 namespace = EXTATTR_NAMESPACE_USER; 2589 } else { 2590 /* Warn about other extended attributes. */ 2591 archive_set_error(&a->archive, 2592 ARCHIVE_ERRNO_FILE_FORMAT, 2593 "Can't restore extended attribute ``%s''", 2594 name); 2595 ret = ARCHIVE_WARN; 2596 continue; 2597 } 2598 errno = 0; 2599#if HAVE_EXTATTR_SET_FD 2600 if (a->fd >= 0) 2601 e = extattr_set_fd(a->fd, namespace, name, value, size); 2602 else 2603#endif 2604 /* TODO: should we use extattr_set_link() instead? */ 2605 { 2606 e = extattr_set_file(archive_entry_pathname(entry), 2607 namespace, name, value, size); 2608 } 2609 if (e != (int)size) { 2610 if (errno == ENOTSUP) { 2611 if (!warning_done) { 2612 warning_done = 1; 2613 archive_set_error(&a->archive, errno, 2614 "Cannot restore extended " 2615 "attributes on this file " 2616 "system"); 2617 } 2618 } else { 2619 archive_set_error(&a->archive, errno, 2620 "Failed to set extended attribute"); 2621 } 2622 2623 ret = ARCHIVE_WARN; 2624 } 2625 } 2626 } 2627 return (ret); 2628} 2629#else 2630/* 2631 * Restore extended attributes - stub implementation for unsupported systems 2632 */ 2633static int 2634set_xattrs(struct archive_write_disk *a) 2635{ 2636 static int warning_done = 0; 2637 2638 /* If there aren't any extended attributes, then it's okay not 2639 * to extract them, otherwise, issue a single warning. */ 2640 if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) { 2641 warning_done = 1; 2642 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2643 "Cannot restore extended attributes on this system"); 2644 return (ARCHIVE_WARN); 2645 } 2646 /* Warning was already emitted; suppress further warnings. */ 2647 return (ARCHIVE_OK); 2648} 2649#endif 2650 2651 2652/* 2653 * Trivial implementations of gid/uid lookup functions. 2654 * These are normally overridden by the client, but these stub 2655 * versions ensure that we always have something that works. 2656 */ 2657static gid_t 2658trivial_lookup_gid(void *private_data, const char *gname, gid_t gid) 2659{ 2660 (void)private_data; /* UNUSED */ 2661 (void)gname; /* UNUSED */ 2662 return (gid); 2663} 2664 2665static uid_t 2666trivial_lookup_uid(void *private_data, const char *uname, uid_t uid) 2667{ 2668 (void)private_data; /* UNUSED */ 2669 (void)uname; /* UNUSED */ 2670 return (uid); 2671} 2672 2673/* 2674 * Test if file on disk is older than entry. 2675 */ 2676static int 2677older(struct stat *st, struct archive_entry *entry) 2678{ 2679 /* First, test the seconds and return if we have a definite answer. */ 2680 /* Definitely older. */ 2681 if (st->st_mtime < archive_entry_mtime(entry)) 2682 return (1); 2683 /* Definitely younger. */ 2684 if (st->st_mtime > archive_entry_mtime(entry)) 2685 return (0); 2686 /* If this platform supports fractional seconds, try those. */ 2687#if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC 2688 /* Definitely older. */ 2689 if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry)) 2690 return (1); 2691#elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC 2692 /* Definitely older. */ 2693 if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry)) 2694 return (1); 2695#elif HAVE_STRUCT_STAT_ST_MTIME_N 2696 /* older. */ 2697 if (st->st_mtime_n < archive_entry_mtime_nsec(entry)) 2698 return (1); 2699#elif HAVE_STRUCT_STAT_ST_UMTIME 2700 /* older. */ 2701 if (st->st_umtime * 1000 < archive_entry_mtime_nsec(entry)) 2702 return (1); 2703#elif HAVE_STRUCT_STAT_ST_MTIME_USEC 2704 /* older. */ 2705 if (st->st_mtime_usec * 1000 < archive_entry_mtime_nsec(entry)) 2706 return (1); 2707#else 2708 /* This system doesn't have high-res timestamps. */ 2709#endif 2710 /* Same age or newer, so not older. */ 2711 return (0); 2712} 2713