2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
36 #ifdef HAVE_SYS_EXTATTR_H
37 #include <sys/extattr.h>
39 #ifdef HAVE_SYS_XATTR_H
40 #include <sys/xattr.h>
42 #ifdef HAVE_ATTR_XATTR_H
43 #include <attr/xattr.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
48 #ifdef HAVE_SYS_STAT_H
51 #ifdef HAVE_SYS_TIME_H
54 #ifdef HAVE_SYS_UTIME_H
55 #include <sys/utime.h>
66 #ifdef HAVE_LINUX_FS_H
67 #include <linux/fs.h> /* for Linux file flags */
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.
73 #ifdef HAVE_LINUX_EXT2_FS_H
74 #include <linux/ext2_fs.h> /* for Linux file flags */
76 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
77 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
100 #include "archive_string.h"
101 #include "archive_entry.h"
102 #include "archive_private.h"
109 struct fixup_entry *next;
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 */
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.
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
148 struct archive_write_disk {
149 struct archive archive;
152 struct fixup_entry *fixup_list;
153 struct fixup_entry *current_fixup;
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;
167 * Full path of last file to satisfy symlink checks.
169 struct archive_string path_safe;
172 * Cached stat data from disk for the current entry.
173 * If this is valid, pst points to st. Otherwise,
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. */
185 /* Tasks deferred until end-of-archive. */
187 /* Options requested by the client. */
189 /* Handle for the file we're restoring. */
191 /* Current offset for writing data to the file. */
193 /* Last offset actually written to disk. */
195 /* Maximum size of file, -1 if unknown. */
197 /* Dir we were in before this restore; only for deep paths. */
199 /* Mode we should use for this entry; affected by _PERM and umask. */
201 /* UID/GID to use in restoring this entry. */
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."
211 #define DEFAULT_DIR_MODE 0777
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
218 #define MINIMUM_DIR_MODE 0700
219 #define MAXIMUM_DIR_MODE 0775
221 static int check_symlinks(struct archive_write_disk *);
222 static int create_filesystem_object(struct archive_write_disk *);
223 static struct fixup_entry *current_fixup(struct archive_write_disk *, const char *pathname);
225 static void edit_deep_directories(struct archive_write_disk *ad);
227 static int cleanup_pathname(struct archive_write_disk *);
228 static int create_dir(struct archive_write_disk *, char *);
229 static int create_parent_dir(struct archive_write_disk *, char *);
230 static int older(struct stat *, struct archive_entry *);
231 static int restore_entry(struct archive_write_disk *);
232 #ifdef HAVE_POSIX_ACL
233 static int set_acl(struct archive_write_disk *, int fd, struct archive_entry *,
234 acl_type_t, int archive_entry_acl_type, const char *tn);
236 static int set_acls(struct archive_write_disk *);
237 static int set_xattrs(struct archive_write_disk *);
238 static int set_fflags(struct archive_write_disk *);
239 static 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);
242 static int set_ownership(struct archive_write_disk *);
243 static int set_mode(struct archive_write_disk *, int mode);
244 static int set_time(int, int, const char *, time_t, long, time_t, long);
245 static int set_times(struct archive_write_disk *);
246 static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
247 static gid_t trivial_lookup_gid(void *, const char *, gid_t);
248 static uid_t trivial_lookup_uid(void *, const char *, uid_t);
249 static ssize_t write_data_block(struct archive_write_disk *,
250 const char *, size_t);
252 static struct archive_vtable *archive_write_disk_vtable(void);
254 static int _archive_write_close(struct archive *);
255 static int _archive_write_free(struct archive *);
256 static int _archive_write_header(struct archive *, struct archive_entry *);
257 static int _archive_write_finish_entry(struct archive *);
258 static ssize_t _archive_write_data(struct archive *, const void *, size_t);
259 static ssize_t _archive_write_data_block(struct archive *, const void *, size_t, off_t);
262 _archive_write_disk_lazy_stat(struct archive_write_disk *a)
264 if (a->pst != NULL) {
265 /* Already have stat() data available. */
269 if (a->fd >= 0 && fstat(a->fd, &a->st) == 0) {
275 * XXX At this point, symlinks should not be hit, otherwise
276 * XXX a race occured. Do we want to check explicitly for that?
278 if (lstat(a->name, &a->st) == 0) {
282 archive_set_error(&a->archive, errno, "Couldn't stat file");
283 return (ARCHIVE_WARN);
286 static struct archive_vtable *
287 archive_write_disk_vtable(void)
289 static struct archive_vtable av;
290 static int inited = 0;
293 av.archive_close = _archive_write_close;
294 av.archive_free = _archive_write_free;
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;
305 archive_write_disk_set_options(struct archive *_a, int flags)
307 struct archive_write_disk *a = (struct archive_write_disk *)_a;
315 * Extract this entry to disk.
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.
326 _archive_write_header(struct archive *_a, struct archive_entry *entry)
328 struct archive_write_disk *a = (struct archive_write_disk *)_a;
329 struct fixup_entry *fe;
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)
342 /* Set up for this particular entry. */
344 a->current_fixup = NULL;
347 archive_entry_free(a->entry);
350 a->entry = archive_entry_clone(entry);
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);
360 archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry));
361 a->name = a->_name_data.s;
362 archive_clear_error(&a->archive);
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/.".
369 ret = cleanup_pathname(a);
370 if (ret != ARCHIVE_OK)
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.
381 a->user_umask = umask(0);
382 /* From here on, early exit requires "goto done" to clean up. */
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. */
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
402 if (a->mode & S_ISGID)
403 a->todo |= TODO_SGID | TODO_SGID_CHECK;
405 * Verifying the SUID is simpler, but can still be
406 * done in multiple ways, hence the separate "check" bit.
408 if (a->mode & S_ISUID)
409 a->todo |= TODO_SUID | TODO_SUID_CHECK;
412 * User didn't request full permissions, so don't
413 * restore SUID, SGID bits and obey umask.
418 a->mode &= ~a->user_umask;
420 #if !defined(_WIN32) || defined(__CYGWIN__)
421 if (a->flags & ARCHIVE_EXTRACT_OWNER)
422 a->todo |= TODO_OWNER;
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)
438 /* If path exceeds PATH_MAX, shorten the path. */
439 edit_deep_directories(a);
442 ret = restore_entry(a);
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.
455 /* If we changed directory above, restore it here. */
456 if (a->restore_pwd >= 0) {
457 r = fchdir(a->restore_pwd);
459 archive_set_error(&a->archive, errno, "chdir() failure");
462 close(a->restore_pwd);
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.
473 if (a->deferred & TODO_MODE) {
474 fe = current_fixup(a, archive_entry_pathname(entry));
475 fe->fixup |= TODO_MODE_BASE;
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);
488 /* If atime is unset, use start time. */
489 fe->atime = a->start_time;
492 if (archive_entry_mtime_is_set(entry)) {
493 fe->mtime = archive_entry_mtime(entry);
494 fe->mtime_nanos = archive_entry_mtime_nsec(entry);
496 /* If mtime is unset, use start time. */
497 fe->mtime = a->start_time;
500 if (archive_entry_birthtime_is_set(entry)) {
501 fe->birthtime = archive_entry_birthtime(entry);
502 fe->birthtime_nanos = archive_entry_birthtime_nsec(entry);
504 /* If birthtime is unset, use mtime. */
505 fe->birthtime = fe->mtime;
506 fe->birthtime_nanos = fe->mtime_nanos;
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. */
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;
520 * If it's not open, tell our client not to try writing.
521 * In particular, dirs, links, etc, don't get written to.
524 archive_entry_set_size(entry, 0);
528 /* Restore the user's umask before returning. */
529 umask(a->user_umask);
535 archive_write_disk_set_skip_file(struct archive *_a, dev_t d, ino_t i)
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;
546 write_data_block(struct archive_write_disk *a, const char *buff, size_t size)
548 uint64_t start_size = size;
549 ssize_t bytes_written = 0;
550 ssize_t block_size = 0, bytes_to_write;
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);
561 if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
562 #if HAVE_STRUCT_STAT_ST_BLKSIZE
564 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
566 block_size = a->pst->st_blksize;
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;
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);
578 /* Write the data. */
580 if (block_size == 0) {
581 bytes_to_write = size;
583 /* We're sparsifying the file. */
587 /* Skip leading zero bytes. */
588 for (p = buff, end = buff + size; p < end; ++p) {
592 a->offset += p - buff;
598 /* Calculate next block boundary after offset. */
600 = (a->offset / block_size + 1) * block_size;
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;
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,
613 return (ARCHIVE_FATAL);
615 a->fd_offset = a->offset;
616 a->archive.file_position = a->offset;
617 a->archive.raw_position = a->offset;
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);
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;
631 return (start_size - size);
635 _archive_write_data_block(struct archive *_a,
636 const void *buff, size_t size, off_t offset)
638 struct archive_write_disk *a = (struct archive_write_disk *)_a;
641 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
642 ARCHIVE_STATE_DATA, "archive_write_disk_block");
645 r = write_data_block(a, buff, size);
648 if ((size_t)r < size) {
649 archive_set_error(&a->archive, 0,
650 "Write request too large");
651 return (ARCHIVE_WARN);
657 _archive_write_data(struct archive *_a, const void *buff, size_t size)
659 struct archive_write_disk *a = (struct archive_write_disk *)_a;
661 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
662 ARCHIVE_STATE_DATA, "archive_write_data");
664 return (write_data_block(a, buff, size));
668 _archive_write_finish_entry(struct archive *_a)
670 struct archive_write_disk *a = (struct archive_write_disk *)_a;
671 int ret = ARCHIVE_OK;
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)
678 archive_clear_error(&a->archive);
680 /* Pad or truncate file to the right size. */
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. */
690 if (ftruncate(a->fd, a->filesize) == -1 &&
692 archive_set_error(&a->archive, errno,
693 "File size could not be restored");
694 return (ARCHIVE_FAILED);
698 * Not all platforms implement the XSI option to
699 * extend files via ftruncate. Stat() the file again
700 * to see what happened.
703 if ((ret = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
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,
712 return (ARCHIVE_FATAL);
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);
723 /* Restore metadata. */
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?
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));
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));
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.
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;
752 if (a->todo & TODO_ACLS) {
753 int r2 = set_acls(a);
754 if (r2 < ret) ret = r2;
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.
762 if (a->todo & TODO_XATTR) {
763 int r2 = set_xattrs(a);
764 if (r2 < ret) ret = r2;
768 * Some flags prevent file modification; they must be restored after
769 * file contents are written.
771 if (a->todo & TODO_FFLAGS) {
772 int r2 = set_fflags(a);
773 if (r2 < ret) ret = r2;
776 * Time has to be restored after all other metadata;
777 * otherwise atime will get changed.
779 if (a->todo & TODO_TIMES) {
780 int r2 = set_times(a);
781 if (r2 < ret) ret = r2;
784 /* If there's an fd, we can close it now. */
789 /* If there's an entry, we can release it now. */
791 archive_entry_free(a->entry);
794 a->archive.state = ARCHIVE_STATE_HEADER;
799 archive_write_disk_set_group_lookup(struct archive *_a,
801 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid),
802 void (*cleanup_gid)(void *private))
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");
808 a->lookup_gid = lookup_gid;
809 a->cleanup_gid = cleanup_gid;
810 a->lookup_gid_data = private_data;
815 archive_write_disk_set_user_lookup(struct archive *_a,
817 uid_t (*lookup_uid)(void *private, const char *uname, uid_t uid),
818 void (*cleanup_uid)(void *private))
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");
824 a->lookup_uid = lookup_uid;
825 a->cleanup_uid = cleanup_uid;
826 a->lookup_uid_data = private_data;
832 * Create a new archive_write_disk object and initialize it with global state.
835 archive_write_disk_new(void)
837 struct archive_write_disk *a;
839 a = (struct archive_write_disk *)malloc(sizeof(*a));
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);
851 a->user_uid = geteuid();
852 #endif /* HAVE_GETEUID */
853 if (archive_string_ensure(&a->path_safe, 512) == NULL) {
857 return (&a->archive);
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
871 edit_deep_directories(struct archive_write_disk *a)
874 char *tail = a->name;
878 /* If path is short, avoid the open() below. */
879 if (strlen(tail) <= PATH_MAX)
882 /* Try to record our starting dir. */
883 a->restore_pwd = open(".", O_RDONLY | O_BINARY);
884 if (a->restore_pwd < 0)
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 != '/')
893 /* Exit if we find a too-long path component. */
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)
905 /* The chdir() succeeded; we've now shortened the path. */
913 * The main restore function.
916 restore_entry(struct archive_write_disk *a)
918 int ret = ARCHIVE_OK, en;
920 if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
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.
929 if (unlink(a->name) == 0) {
930 /* We removed it, reset cached stat. */
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. */
938 /* We tried, but couldn't get rid of it. */
939 archive_set_error(&a->archive, errno,
941 return(ARCHIVE_FAILED);
945 /* Try creating it first; if this fails, we'll try to recover. */
946 en = create_filesystem_object(a);
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);
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);
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.
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);
979 en = create_filesystem_object(a);
980 } else if (en == EEXIST) {
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.
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.
991 if (S_ISDIR(a->mode))
992 r = stat(a->name, &a->st);
994 * If it's not a dir (or it's a broken symlink),
995 * then don't follow it.
997 if (r != 0 || !S_ISDIR(a->mode))
998 r = lstat(a->name, &a->st);
1000 archive_set_error(&a->archive, errno,
1001 "Can't stat existing object");
1002 return (ARCHIVE_FAILED);
1006 * NO_OVERWRITE_NEWER doesn't apply to directories.
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);
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);
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);
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);
1044 en = create_filesystem_object(a);
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.
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. */
1062 /* Everything failed; give up here. */
1063 archive_set_error(&a->archive, en, "Can't create '%s'",
1065 return (ARCHIVE_FAILED);
1068 a->pst = NULL; /* Cached stat data no longer valid. */
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.
1078 create_filesystem_object(struct archive_write_disk *a)
1080 /* Create the entry. */
1081 const char *linkname;
1082 mode_t final_mode, mode;
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) {
1092 r = link(linkname, a->name) ? errno : 0;
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.
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.
1105 if (r == 0 && a->filesize <= 0) {
1108 } if (r == 0 && a->filesize > 0) {
1109 a->fd = open(a->name, O_WRONLY | O_TRUNC | O_BINARY);
1116 linkname = archive_entry_symlink(a->entry);
1117 if (linkname != NULL) {
1119 return symlink(linkname, a->name) ? errno : 0;
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!)
1131 /* Mode we want for the final restored object (w/o file type bits). */
1132 final_mode = a->mode & 07777;
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.
1138 mode = final_mode & 0777;
1140 switch (a->mode & AE_IFMT) {
1142 /* POSIX requires that we fall through here. */
1145 a->fd = open(a->name,
1146 O_WRONLY | O_CREAT | O_EXCL | O_BINARY, mode);
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));
1157 /* TODO: Find a better way to warn about our inability
1158 * to restore a char device node. */
1160 #endif /* HAVE_MKNOD */
1163 r = mknod(a->name, mode | S_IFBLK,
1164 archive_entry_rdev(a->entry));
1167 /* TODO: Find a better way to warn about our inability
1168 * to restore a block device node. */
1170 #endif /* HAVE_MKNOD */
1172 mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
1173 r = mkdir(a->name, mode);
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;
1189 r = mkfifo(a->name, mode);
1192 /* TODO: Find a better way to warn about our inability
1193 * to restore a fifo. */
1195 #endif /* HAVE_MKFIFO */
1198 /* All the system calls above set errno on failure. */
1202 /* If we managed to set the final mode, we've avoided a chmod(). */
1203 if (mode == final_mode)
1204 a->todo &= ~TODO_MODE;
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.
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
1226 * XXX TODO: Directory ACLs should be restored here, for the same
1227 * reason we set directory perms here. XXX
1230 _archive_write_close(struct archive *_a)
1232 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1233 struct fixup_entry *next, *p;
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);
1241 /* Sort dir list so directories are fixed up in depth-first order. */
1242 p = sort_dir_list(a->fixup_list);
1245 a->pst = NULL; /* Mark stat cache as out-of-date. */
1246 if (p->fixup & TODO_TIMES) {
1248 /* {f,l,}utimes() are preferred, when available. */
1249 #if defined(_WIN32) && !defined(__CYGWIN__)
1250 struct __timeval times[2];
1252 struct timeval times[2];
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) &&
1261 times[1].tv_usec = p->birthtime_nanos / 1000;
1262 utimes(p->name, times);
1265 times[1].tv_sec = p->mtime;
1266 times[1].tv_usec = p->mtime_nanos / 1000;
1268 lutimes(p->name, times);
1270 utimes(p->name, times);
1273 /* utime() is more portable, but less precise. */
1274 struct utimbuf times;
1275 times.modtime = p->mtime;
1276 times.actime = p->atime;
1278 utime(p->name, ×);
1281 if (p->fixup & TODO_MODE_BASE)
1282 chmod(p->name, p->mode);
1284 if (p->fixup & TODO_FFLAGS)
1285 set_fflags_platform(a, -1, p->name,
1286 p->mode, p->fflags_set, 0);
1293 a->fixup_list = NULL;
1298 _archive_write_free(struct archive *_a)
1300 struct archive_write_disk *a = (struct archive_write_disk *)_a;
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);
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);
1317 * Simple O(n log n) merge sort to order the fixup list. In
1318 * particular, we want to restore dir timestamps depth-first.
1320 static struct fixup_entry *
1321 sort_dir_list(struct fixup_entry *p)
1323 struct fixup_entry *a, *b, *t;
1327 /* A one-item list is already sorted. */
1328 if (p->next == NULL)
1331 /* Step 1: split the list. */
1335 /* Step a twice, t once. */
1341 /* Now, t is at the mid-point, so break the list here. */
1346 /* Step 2: Recursively sort the two sub-lists. */
1347 a = sort_dir_list(a);
1348 b = sort_dir_list(b);
1350 /* Step 3: Merge the returned lists. */
1351 /* Pick the first element for the merged list. */
1352 if (strcmp(a->name, b->name) > 0) {
1360 /* Always put the later element on the list first. */
1361 while (a != NULL && b != NULL) {
1362 if (strcmp(a->name, b->name) > 0) {
1372 /* Only one list is non-empty, so just splice it on. */
1382 * Returns a new, initialized fixup entry.
1384 * TODO: Reduce the memory requirements for this list by using a tree
1385 * structure rather than a simple list of names.
1387 static struct fixup_entry *
1388 new_fixup(struct archive_write_disk *a, const char *pathname)
1390 struct fixup_entry *fe;
1392 fe = (struct fixup_entry *)malloc(sizeof(struct fixup_entry));
1395 fe->next = a->fixup_list;
1398 fe->name = strdup(pathname);
1403 * Returns a fixup structure for the current entry.
1405 static struct fixup_entry *
1406 current_fixup(struct archive_write_disk *a, const char *pathname)
1408 if (a->current_fixup == NULL)
1409 a->current_fixup = new_fixup(a, pathname);
1410 return (a->current_fixup);
1413 /* TODO: Make this work. */
1415 * TODO: The deep-directory support bypasses this; disable deep directory
1416 * support if we're doing symlink checks.
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
1423 /* TODO: Extend this to support symlinks on Windows Vista and later. */
1425 check_symlinks(struct archive_write_disk *a)
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);
1438 * Guard against symlink tricks. Reject any archive entry whose
1439 * destination would be altered by a symlink.
1441 /* Whatever we checked last time doesn't need to be re-checked. */
1444 while ((*pn != '\0') && (*p == *pn))
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 != '/')
1454 /* Check that we haven't hit a symlink. */
1455 r = lstat(a->name, &st);
1457 /* We've hit a dir that doesn't exist; stop now. */
1458 if (errno == ENOENT)
1460 } else if (S_ISLNK(st.st_mode)) {
1463 * Last element is symlink; remove it
1464 * so we can overwrite it with the
1465 * item being extracted.
1467 if (unlink(a->name)) {
1468 archive_set_error(&a->archive, errno,
1469 "Could not remove symlink %s",
1472 return (ARCHIVE_FAILED);
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.
1481 if (!S_ISLNK(a->mode)) {
1482 archive_set_error(&a->archive, 0,
1483 "Removing symlink %s",
1486 /* Symlink gone. No more problem! */
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",
1496 return (ARCHIVE_FAILED);
1500 archive_set_error(&a->archive, 0,
1501 "Cannot extract through symlink %s",
1504 return (ARCHIVE_FAILED);
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);
1515 #if defined(_WIN32) || defined(__CYGWIN__)
1517 guidword(const char *p, int n)
1521 for (i = 0; i < n; i++) {
1522 if ((*p >= '0' && *p <= '9') ||
1523 (*p >= 'a' && *p <= 'f') ||
1524 (*p >= 'A' && *p <= 'F'))
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
1541 cleanup_pathname_win(struct archive_write_disk *a)
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] == '/'))
1554 /* A path begin with "\\?\UNC\" */
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] == '/'))
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') &&
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] == '/'))
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') &&
1596 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1597 "Path is a physical drive name");
1598 return (ARCHIVE_FAILED);
1603 /* Skip leading drive letter from archives created
1605 if (((p[0] >= 'a' && p[0] <= 'z') ||
1606 (p[0] >= 'A' && p[0] <= 'Z')) &&
1609 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1610 "Path is a drive name");
1611 return (ARCHIVE_FAILED);
1613 if (p[2] == '\\' || p[2] == '/')
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 == '|')
1624 if (alen == 0 || strchr(a->name, '\\') == NULL)
1625 return (ARCHIVE_OK);
1627 * Convert path separator.
1630 while (*p != '\0' && alen) {
1631 l = mbtowc(&wc, p, alen);
1633 while (*p != '\0') {
1640 if (l == 1 && wc == L'\\')
1645 return (ARCHIVE_OK);
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.
1656 cleanup_pathname(struct archive_write_disk *a)
1659 char separator = '\0';
1661 dest = src = a->name;
1663 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1664 "Invalid empty pathname");
1665 return (ARCHIVE_FAILED);
1668 #if defined(_WIN32) || defined(__CYGWIN__)
1669 if (cleanup_pathname_win(a) != ARCHIVE_OK)
1670 return (ARCHIVE_FAILED);
1672 /* Skip leading '/'. */
1676 /* Scan the pathname one element at a time. */
1678 /* src points to first char after '/' */
1679 if (src[0] == '\0') {
1681 } else if (src[0] == '/') {
1682 /* Found '//', ignore second one. */
1685 } else if (src[0] == '.') {
1686 if (src[1] == '\0') {
1687 /* Ignore trailing '.' */
1689 } else if (src[1] == '/') {
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,
1699 "Path contains '..'");
1700 return (ARCHIVE_FAILED);
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.
1713 /* Copy current element, including leading '/'. */
1716 while (*src != '\0' && *src != '/') {
1723 /* Skip '/' separator. */
1727 * We've just copied zero or more path elements, not including the
1730 if (dest == a->name) {
1732 * Nothing got copied. The path must have been something
1733 * like '.' or '/' or './' or '/././././/./'.
1740 /* Terminate the result. */
1742 return (ARCHIVE_OK);
1746 * Create the parent directory of the specified path, assuming path
1747 * is already in mutable storage.
1750 create_parent_dir(struct archive_write_disk *a, char *path)
1755 /* Remove tail element to obtain parent name. */
1756 slash = strrchr(path, '/');
1758 return (ARCHIVE_OK);
1760 r = create_dir(a, path);
1766 * Create the specified dir, recursing to create parents as necessary.
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.
1773 create_dir(struct archive_write_disk *a, char *path)
1776 struct fixup_entry *le;
1778 mode_t mode_final, mode;
1781 /* Check for special names and just skip them. */
1782 slash = strrchr(path, '/');
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) {
1794 r = create_dir(a, path);
1798 return (ARCHIVE_OK);
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.
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);
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);
1820 } else if (errno != ENOENT && errno != ENOTDIR) {
1822 archive_set_error(&a->archive, errno, "Can't test directory '%s'", path);
1823 return (ARCHIVE_FAILED);
1824 } else if (slash != NULL) {
1826 r = create_dir(a, path);
1828 if (r != ARCHIVE_OK)
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.
1840 mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
1841 /* Mode we want on disk during the restore process. */
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;
1851 return (ARCHIVE_OK);
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
1860 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1861 return (ARCHIVE_OK);
1863 archive_set_error(&a->archive, errno, "Failed to create dir '%s'",
1865 return (ARCHIVE_FAILED);
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.
1879 set_ownership(struct archive_write_disk *a)
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 */
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);
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);
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. */
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);
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);
1918 archive_set_error(&a->archive, errno,
1919 "Can't set user=%d/group=%d for %s", a->uid, a->gid,
1921 return (ARCHIVE_WARN);
1925 #if defined(HAVE_UTIMENSAT) && defined(HAVE_FUTIMENS)
1927 * utimensat() and futimens() are defined in POSIX.1-2008. They provide ns
1928 * resolution and setting times on fd and on symlinks, too.
1931 set_time(int fd, int mode, const char *name,
1932 time_t atime, long atime_nsec,
1933 time_t mtime, long mtime_nsec)
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;
1941 return futimens(fd, ts);
1942 return utimensat(AT_FDCWD, name, ts, AT_SYMLINK_NOFOLLOW);
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.
1951 set_time(int fd, int mode, const char *name,
1952 time_t atime, long atime_nsec,
1953 time_t mtime, long mtime_nsec)
1955 #if defined(_WIN32) && !defined(__CYGWIN__)
1956 struct __timeval times[2];
1958 struct timeval times[2];
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;
1968 return (futimes(fd, times));
1970 (void)fd; /* UNUSED */
1973 (void)mode; /* UNUSED */
1974 return (lutimes(name, times));
1978 return (utimes(name, times));
1981 #elif defined(HAVE_UTIME)
1983 * utime() is an older, more standard interface that we'll use
1984 * if utimes() isn't available.
1987 set_time(int fd, int mode, const char *name,
1988 time_t atime, long atime_nsec,
1989 time_t mtime, long mtime_nsec)
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;
1999 return (ARCHIVE_OK);
2000 return (utime(name, ×));
2004 set_time(int fd, int mode, const char *name,
2005 time_t atime, long atime_nsec,
2006 time_t mtime, long mtime_nsec)
2008 return (ARCHIVE_WARN);
2013 set_times(struct archive_write_disk *a)
2015 time_t atime = a->start_time, mtime = a->start_time;
2016 long atime_nsec = 0, mtime_nsec = 0;
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)
2023 && !archive_entry_mtime_is_set(a->entry))
2024 return (ARCHIVE_OK);
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);
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.
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",
2050 return (ARCHIVE_WARN);
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);
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",
2063 return (ARCHIVE_WARN);
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.
2072 return (ARCHIVE_OK);
2076 set_mode(struct archive_write_disk *a, int mode)
2079 mode &= 07777; /* Strip off file type bits. */
2081 if (a->todo & TODO_SGID_CHECK) {
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.
2088 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
2090 if (a->pst->st_gid != a->gid) {
2092 #if !defined(_WIN32) || defined(__CYGWIN__)
2093 if (a->flags & ARCHIVE_EXTRACT_OWNER) {
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.
2101 archive_set_error(&a->archive, -1,
2102 "Can't restore SGID bit");
2107 /* While we're here, double-check the UID. */
2108 if (a->pst->st_uid != a->uid
2109 && (a->todo & TODO_SUID)) {
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");
2119 a->todo &= ~TODO_SGID_CHECK;
2120 a->todo &= ~TODO_SUID_CHECK;
2121 } else if (a->todo & TODO_SUID_CHECK) {
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
2127 if (a->user_uid != a->uid) {
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");
2137 a->todo &= ~TODO_SUID_CHECK;
2140 if (S_ISLNK(a->mode)) {
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
2150 if (lchmod(a->name, mode) != 0) {
2151 archive_set_error(&a->archive, errno,
2152 "Can't set permissions to 0%o", (int)mode);
2156 } else if (!S_ISDIR(a->mode)) {
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.
2165 if (fchmod(a->fd, mode) != 0) {
2166 archive_set_error(&a->archive, errno,
2167 "Can't set permissions to 0%o", (int)mode);
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);
2184 set_fflags(struct archive_write_disk *a)
2186 struct fixup_entry *le;
2187 unsigned long set, clear;
2190 mode_t mode = archive_entry_mode(a->entry);
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.
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
2207 /* Hopefully, the compiler will optimize this mess into a constant. */
2210 critical_flags |= SF_IMMUTABLE;
2213 critical_flags |= UF_IMMUTABLE;
2216 critical_flags |= SF_APPEND;
2219 critical_flags |= UF_APPEND;
2221 #ifdef EXT2_APPEND_FL
2222 critical_flags |= EXT2_APPEND_FL;
2224 #ifdef EXT2_IMMUTABLE_FL
2225 critical_flags |= EXT2_IMMUTABLE_FL;
2228 if (a->todo & TODO_FFLAGS) {
2229 archive_entry_fflags(a->entry, &set, &clear);
2232 * The first test encourages the compiler to eliminate
2233 * all of this if it's not necessary.
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)
2243 r = set_fflags_platform(a, a->fd,
2244 a->name, mode, set, clear);
2245 if (r != ARCHIVE_OK)
2249 return (ARCHIVE_OK);
2253 #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && defined(HAVE_STRUCT_STAT_ST_FLAGS)
2255 * BSD reads flags using stat() and sets them with one of {f,l,}chflags()
2258 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
2259 mode_t mode, unsigned long set, unsigned long clear)
2263 (void)mode; /* UNUSED */
2264 if (set == 0 && clear == 0)
2265 return (ARCHIVE_OK);
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
2273 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
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);
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.
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);
2297 if (chflags(name, a->st.st_flags) == 0)
2298 return (ARCHIVE_OK);
2300 archive_set_error(&a->archive, errno,
2301 "Failed to set file flags");
2302 return (ARCHIVE_WARN);
2305 #elif defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS)
2307 * Linux uses ioctl() to read and write file flags.
2310 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
2311 mode_t mode, unsigned long set, unsigned long clear)
2315 unsigned long newflags, oldflags;
2316 unsigned long sf_mask = 0;
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);
2324 /* If we weren't given an fd, open it ourselves. */
2326 myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY);
2328 return (ARCHIVE_OK);
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.
2337 #ifdef EXT2_IMMUTABLE_FL
2338 sf_mask |= EXT2_IMMUTABLE_FL;
2340 #ifdef EXT2_APPEND_FL
2341 sf_mask |= EXT2_APPEND_FL;
2344 * XXX As above, this would be way simpler if we didn't have
2345 * to read the current flags from disk. XXX
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)
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)
2364 /* We couldn't set the flags, so report the failure. */
2366 archive_set_error(&a->archive, errno,
2367 "Failed to set file flags");
2378 * Of course, some systems have neither BSD chflags() nor Linux' flags
2379 * support through ioctl().
2382 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
2383 mode_t mode, unsigned long set, unsigned long clear)
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);
2394 #endif /* __linux */
2396 #ifndef HAVE_POSIX_ACL
2397 /* Default empty function body to satisfy mainline code. */
2399 set_acls(struct archive_write_disk *a)
2401 (void)a; /* UNUSED */
2402 return (ARCHIVE_OK);
2408 * XXX TODO: What about ACL types other than ACCESS and DEFAULT?
2411 set_acls(struct archive_write_disk *a)
2415 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_ACCESS,
2416 ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
2417 if (ret != ARCHIVE_OK)
2419 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_DEFAULT,
2420 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
2426 set_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)
2430 acl_entry_t acl_entry;
2431 acl_permset_t acl_permset;
2433 int ae_type, ae_permset, ae_tag, ae_id;
2436 const char *ae_name;
2441 entries = archive_entry_acl_reset(entry, ae_requested_type);
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);
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,
2454 acl_set_qualifier(acl_entry, &ae_uid);
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,
2460 acl_set_qualifier(acl_entry, &ae_gid);
2462 case ARCHIVE_ENTRY_ACL_USER_OBJ:
2463 acl_set_tag_type(acl_entry, ACL_USER_OBJ);
2465 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
2466 acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
2468 case ARCHIVE_ENTRY_ACL_MASK:
2469 acl_set_tag_type(acl_entry, ACL_MASK);
2471 case ARCHIVE_ENTRY_ACL_OTHER:
2472 acl_set_tag_type(acl_entry, ACL_OTHER);
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);
2489 name = archive_entry_pathname(entry);
2491 /* Try restoring the ACL through 'fd' if we can. */
2493 if (fd >= 0 && acl_type == ACL_TYPE_ACCESS && acl_set_fd(fd, acl) == 0)
2497 #if HAVE_ACL_SET_FD_NP
2498 if (fd >= 0 && acl_set_fd_np(fd, acl, acl_type) == 0)
2503 if (acl_set_file(name, acl_type, acl) != 0) {
2504 archive_set_error(&a->archive, errno, "Failed to set %s acl", tname);
2514 * Restore extended attributes - Linux implementation
2517 set_xattrs(struct archive_write_disk *a)
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);
2528 archive_entry_xattr_next(entry, &name, &value, &size);
2530 strncmp(name, "xfsroot.", 8) != 0 &&
2531 strncmp(name, "system.", 7) != 0) {
2535 e = fsetxattr(a->fd, name, value, size, 0);
2539 e = lsetxattr(archive_entry_pathname(entry),
2540 name, value, size, 0);
2543 if (errno == ENOTSUP) {
2544 if (!warning_done) {
2546 archive_set_error(&a->archive, errno,
2547 "Cannot restore extended "
2548 "attributes on this file "
2552 archive_set_error(&a->archive, errno,
2553 "Failed to set extended attribute");
2557 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2558 "Invalid extended attribute encountered");
2564 #elif HAVE_EXTATTR_SET_FILE && HAVE_DECL_EXTATTR_NAMESPACE_USER
2566 * Restore extended attributes - FreeBSD implementation
2569 set_xattrs(struct archive_write_disk *a)
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);
2580 archive_entry_xattr_next(entry, &name, &value, &size);
2585 if (strncmp(name, "user.", 5) == 0) {
2586 /* "user." attributes go to user namespace */
2588 namespace = EXTATTR_NAMESPACE_USER;
2590 /* Warn about other extended attributes. */
2591 archive_set_error(&a->archive,
2592 ARCHIVE_ERRNO_FILE_FORMAT,
2593 "Can't restore extended attribute ``%s''",
2599 #if HAVE_EXTATTR_SET_FD
2601 e = extattr_set_fd(a->fd, namespace, name, value, size);
2604 /* TODO: should we use extattr_set_link() instead? */
2606 e = extattr_set_file(archive_entry_pathname(entry),
2607 namespace, name, value, size);
2609 if (e != (int)size) {
2610 if (errno == ENOTSUP) {
2611 if (!warning_done) {
2613 archive_set_error(&a->archive, errno,
2614 "Cannot restore extended "
2615 "attributes on this file "
2619 archive_set_error(&a->archive, errno,
2620 "Failed to set extended attribute");
2631 * Restore extended attributes - stub implementation for unsupported systems
2634 set_xattrs(struct archive_write_disk *a)
2636 static int warning_done = 0;
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) {
2642 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2643 "Cannot restore extended attributes on this system");
2644 return (ARCHIVE_WARN);
2646 /* Warning was already emitted; suppress further warnings. */
2647 return (ARCHIVE_OK);
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.
2658 trivial_lookup_gid(void *private_data, const char *gname, gid_t gid)
2660 (void)private_data; /* UNUSED */
2661 (void)gname; /* UNUSED */
2666 trivial_lookup_uid(void *private_data, const char *uname, uid_t uid)
2668 (void)private_data; /* UNUSED */
2669 (void)uname; /* UNUSED */
2674 * Test if file on disk is older than entry.
2677 older(struct stat *st, struct archive_entry *entry)
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))
2683 /* Definitely younger. */
2684 if (st->st_mtime > archive_entry_mtime(entry))
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))
2691 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
2692 /* Definitely older. */
2693 if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry))
2695 #elif HAVE_STRUCT_STAT_ST_MTIME_N
2697 if (st->st_mtime_n < archive_entry_mtime_nsec(entry))
2699 #elif HAVE_STRUCT_STAT_ST_UMTIME
2701 if (st->st_umtime * 1000 < archive_entry_mtime_nsec(entry))
2703 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
2705 if (st->st_mtime_usec * 1000 < archive_entry_mtime_nsec(entry))
2708 /* This system doesn't have high-res timestamps. */
2710 /* Same age or newer, so not older. */