2 * Copyright (c) 2003-2005 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: src/lib/libarchive/archive_read_extract.c,v 1.47 2006/09/05 05:59:45 kientzle Exp $");
30 #include <sys/types.h>
34 #ifdef HAVE_ATTR_XATTR_H
35 #include <attr/xattr.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
43 #ifdef HAVE_EXT2FS_EXT2_FS_H
44 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
49 #ifdef HAVE_LINUX_EXT2_FS_H
50 #include <linux/ext2_fs.h> /* for Linux file flags */
60 #include "archive_string.h"
61 #include "archive_entry.h"
62 #include "archive_private.h"
65 struct fixup_entry
*next
;
69 unsigned long mtime_nanos
;
70 unsigned long atime_nanos
;
71 unsigned long fflags_set
;
72 int fixup
; /* bitmask of what needs fixing */
78 #define FIXUP_FFLAGS 4
88 mode_t default_dir_mode
;
89 struct archive_string create_parent_dir
;
90 struct fixup_entry
*fixup_list
;
91 struct fixup_entry
*current_fixup
;
93 struct bucket ucache
[127];
94 struct bucket gcache
[127];
97 * Cached stat data from disk for the current entry.
98 * If this is valid, pst points to st. Otherwise,
105 /* Default mode for dirs created automatically (will be modified by umask). */
106 #define DEFAULT_DIR_MODE 0777
108 * Mode to use for newly-created dirs during extraction; the correct
109 * mode will be set at the end of the extraction.
111 #define SECURE_DIR_MODE 0700
113 static int archive_extract_cleanup(struct archive
*);
114 static int extract_block_device(struct archive
*,
115 struct archive_entry
*, int);
116 static int extract_char_device(struct archive
*,
117 struct archive_entry
*, int);
118 static int extract_device(struct archive
*,
119 struct archive_entry
*, int flags
, mode_t mode
);
120 static int extract_dir(struct archive
*, struct archive_entry
*, int);
121 static int extract_fifo(struct archive
*, struct archive_entry
*, int);
122 static int extract_file(struct archive
*, struct archive_entry
*, int);
123 static int extract_hard_link(struct archive
*, struct archive_entry
*, int);
124 static int extract_symlink(struct archive
*, struct archive_entry
*, int);
125 static unsigned int hash(const char *);
126 static gid_t
lookup_gid(struct archive
*, const char *uname
, gid_t
);
127 static uid_t
lookup_uid(struct archive
*, const char *uname
, uid_t
);
128 static int create_dir(struct archive
*, const char *, int flags
);
129 static int create_dir_mutable(struct archive
*, char *, int flags
);
130 static int create_dir_recursive(struct archive
*, char *, int flags
);
131 static int create_parent_dir(struct archive
*, const char *, int flags
);
132 static int create_parent_dir_mutable(struct archive
*, char *, int flags
);
133 static int restore_metadata(struct archive
*, int fd
,
134 struct archive_entry
*, int flags
);
135 #ifdef HAVE_POSIX_ACL
136 static int set_acl(struct archive
*, int fd
, struct archive_entry
*,
137 acl_type_t
, int archive_entry_acl_type
, const char *tn
);
139 static int set_acls(struct archive
*, int fd
, struct archive_entry
*);
140 static int set_xattrs(struct archive
*, int fd
, struct archive_entry
*);
141 static int set_fflags(struct archive
*, int fd
, const char *name
, mode_t
,
142 unsigned long fflags_set
, unsigned long fflags_clear
);
143 static int set_ownership(struct archive
*, int fd
, struct archive_entry
*,
145 static int set_perm(struct archive
*, int fd
, struct archive_entry
*,
146 int mode
, int flags
);
147 static int set_time(struct archive
*, int fd
, struct archive_entry
*, int);
148 static struct fixup_entry
*sort_dir_list(struct fixup_entry
*p
);
152 * Extract this entry to disk.
154 * TODO: Validate hardlinks. According to the standards, we're
155 * supposed to check each extracted hardlink and squawk if it refers
156 * to a file that we didn't restore. I'm not entirely convinced this
157 * is a good idea, but more importantly: Is there any way to validate
158 * hardlinks without keeping a complete list of filenames from the
159 * entire archive?? Ugh.
163 archive_read_extract(struct archive
*a
, struct archive_entry
*entry
, int flags
)
166 struct extract
*extract
;
169 char *original_filename
;
171 if (a
->extract
== NULL
) {
172 a
->extract
= malloc(sizeof(*a
->extract
));
173 if (a
->extract
== NULL
) {
174 archive_set_error(a
, ENOMEM
, "Can't extract");
175 return (ARCHIVE_FATAL
);
177 a
->cleanup_archive_extract
= archive_extract_cleanup
;
178 memset(a
->extract
, 0, sizeof(*a
->extract
));
180 extract
= a
->extract
;
181 umask(extract
->umask
= umask(0)); /* Read the current umask. */
182 extract
->default_dir_mode
= DEFAULT_DIR_MODE
& ~extract
->umask
;
184 extract
->current_fixup
= NULL
;
186 original_filename
= NULL
;
188 /* The following is not possible without fchdir. <sigh> */
191 * If pathname is longer than PATH_MAX, record starting directory
192 * and chdir to a suitable intermediate dir.
194 if (strlen(archive_entry_pathname(entry
)) > PATH_MAX
) {
197 restore_pwd
= open(".", O_RDONLY
);
198 if (restore_pwd
< 0) {
199 archive_set_error(a
, errno
,
200 "Unable to restore long pathname");
201 return (ARCHIVE_WARN
);
205 * Yes, the copy here is necessary because we edit
206 * the pathname in-place to create intermediate dirnames.
208 original_filename
= strdup(archive_entry_pathname(entry
));
211 * "intdir" points to the initial dir section we're going
212 * to remove, "tail" points to the remainder of the path.
214 intdir
= tail
= original_filename
;
215 while (strlen(tail
) > PATH_MAX
) {
218 /* Locate a dir prefix shorter than PATH_MAX. */
219 tail
= intdir
+ PATH_MAX
- 8;
220 while (tail
> intdir
&& *tail
!= '/')
222 if (tail
<= intdir
) {
223 archive_set_error(a
, EPERM
,
224 "Path element too long");
229 /* Create intdir and chdir to it. */
230 *tail
= '\0'; /* Terminate dir portion */
231 ret
= create_dir(a
, intdir
, flags
);
232 if (ret
== ARCHIVE_OK
&& chdir(intdir
) != 0) {
233 archive_set_error(a
, errno
, "Couldn't chdir");
236 *tail
= '/'; /* Restore the / we removed. */
237 if (ret
!= ARCHIVE_OK
)
241 archive_entry_set_pathname(entry
, tail
);
245 if (stat(archive_entry_pathname(entry
), &extract
->st
) == 0)
246 extract
->pst
= &extract
->st
;
248 if (extract
->pst
!= NULL
&&
249 extract
->pst
->st_dev
== a
->skip_file_dev
&&
250 extract
->pst
->st_ino
== a
->skip_file_ino
) {
251 archive_set_error(a
, 0, "Refusing to overwrite archive");
253 } else if (archive_entry_hardlink(entry
) != NULL
)
254 ret
= extract_hard_link(a
, entry
, flags
);
256 mode
= archive_entry_mode(entry
);
257 switch (mode
& S_IFMT
) {
259 /* Fall through, as required by POSIX. */
261 ret
= extract_file(a
, entry
, flags
);
263 case S_IFLNK
: /* Symlink */
264 ret
= extract_symlink(a
, entry
, flags
);
267 ret
= extract_char_device(a
, entry
, flags
);
270 ret
= extract_block_device(a
, entry
, flags
);
273 ret
= extract_dir(a
, entry
, flags
);
276 ret
= extract_fifo(a
, entry
, flags
);
284 /* If we changed directory above, restore it here. */
285 if (restore_pwd
>= 0 && original_filename
!= NULL
) {
288 archive_entry_copy_pathname(entry
, original_filename
);
289 free(original_filename
);
297 * Cleanup function for archive_extract. Mostly, this involves processing
298 * the fixup list, which is used to address a number of problems:
299 * * Dir permissions might prevent us from restoring a file in that
300 * dir, so we restore the dir 0700 first, then correct the
302 * * Similarly, the act of restoring a file touches the directory
303 * and changes the timestamp on the dir, so we have to touch-up dir
304 * timestamps at the end as well.
305 * * Some file flags can interfere with the restore by, for example,
306 * preventing the creation of hardlinks to those files.
308 * Note that tar/cpio do not require that archives be in a particular
309 * order; there is no way to know when the last file has been restored
310 * within a directory, so there's no way to optimize the memory usage
311 * here by fixing up the directory any earlier than the
314 * XXX TODO: Directory ACLs should be restored here, for the same
315 * reason we set directory perms here. XXX
317 * Registering this function (rather than calling it explicitly by
318 * name from archive_read_finish) reduces static link pollution, since
319 * applications that don't use this API won't get this file linked in.
322 archive_extract_cleanup(struct archive
*a
)
324 struct fixup_entry
*next
, *p
;
325 struct extract
*extract
;
327 /* Sort dir list so directories are fixed up in depth-first order. */
328 extract
= a
->extract
;
329 p
= sort_dir_list(extract
->fixup_list
);
332 extract
->pst
= NULL
; /* Mark stat cache as out-of-date. */
333 if (p
->fixup
& FIXUP_TIMES
) {
334 struct timeval times
[2];
335 times
[1].tv_sec
= p
->mtime
;
336 times
[1].tv_usec
= p
->mtime_nanos
/ 1000;
337 times
[0].tv_sec
= p
->atime
;
338 times
[0].tv_usec
= p
->atime_nanos
/ 1000;
339 utimes(p
->name
, times
);
341 if (p
->fixup
& FIXUP_MODE
)
342 chmod(p
->name
, p
->mode
);
344 if (p
->fixup
& FIXUP_FFLAGS
)
345 set_fflags(a
, -1, p
->name
, p
->mode
, p
->fflags_set
, 0);
352 extract
->fixup_list
= NULL
;
353 archive_string_free(&extract
->create_parent_dir
);
360 * Simple O(n log n) merge sort to order the fixup list. In
361 * particular, we want to restore dir timestamps depth-first.
363 static struct fixup_entry
*
364 sort_dir_list(struct fixup_entry
*p
)
366 struct fixup_entry
*a
, *b
, *t
;
370 /* A one-item list is already sorted. */
374 /* Step 1: split the list. */
378 /* Step a twice, t once. */
384 /* Now, t is at the mid-point, so break the list here. */
389 /* Step 2: Recursively sort the two sub-lists. */
390 a
= sort_dir_list(a
);
391 b
= sort_dir_list(b
);
393 /* Step 3: Merge the returned lists. */
394 /* Pick the first element for the merged list. */
395 if (strcmp(a
->name
, b
->name
) > 0) {
403 /* Always put the later element on the list first. */
404 while (a
!= NULL
&& b
!= NULL
) {
405 if (strcmp(a
->name
, b
->name
) > 0) {
415 /* Only one list is non-empty, so just splice it on. */
425 * Returns a new, initialized fixup entry.
427 * TODO: Reduce the memory requirements for this list by using a tree
428 * structure rather than a simple list of names.
430 static struct fixup_entry
*
431 new_fixup(struct archive
*a
, const char *pathname
)
433 struct extract
*extract
;
434 struct fixup_entry
*fe
;
436 extract
= a
->extract
;
437 fe
= malloc(sizeof(struct fixup_entry
));
440 fe
->next
= extract
->fixup_list
;
441 extract
->fixup_list
= fe
;
443 fe
->name
= strdup(pathname
);
448 * Returns a fixup structure for the current entry.
450 static struct fixup_entry
*
451 current_fixup(struct archive
*a
, const char *pathname
)
453 struct extract
*extract
;
455 extract
= a
->extract
;
456 if (extract
->current_fixup
== NULL
)
457 extract
->current_fixup
= new_fixup(a
, pathname
);
458 return (extract
->current_fixup
);
462 extract_file(struct archive
*a
, struct archive_entry
*entry
, int flags
)
464 struct extract
*extract
;
469 extract
= a
->extract
;
470 name
= archive_entry_pathname(entry
);
471 mode
= archive_entry_mode(entry
) & 0777;
475 * If we're not supposed to overwrite pre-existing files,
476 * use O_EXCL. Otherwise, use O_TRUNC.
478 if (flags
& (ARCHIVE_EXTRACT_UNLINK
| ARCHIVE_EXTRACT_NO_OVERWRITE
))
479 fd
= open(name
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
);
481 fd
= open(name
, O_WRONLY
| O_CREAT
| O_TRUNC
, mode
);
483 /* Try removing a pre-existing file. */
484 if (fd
< 0 && !(flags
& ARCHIVE_EXTRACT_NO_OVERWRITE
)) {
486 fd
= open(name
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
);
489 /* Might be a non-existent parent dir; try fixing that. */
491 create_parent_dir(a
, name
, flags
);
492 fd
= open(name
, O_WRONLY
| O_CREAT
| O_EXCL
, mode
);
495 archive_set_error(a
, errno
, "Can't open '%s'", name
);
496 return (ARCHIVE_WARN
);
498 r
= archive_read_data_into_fd(a
, fd
);
499 extract
->pst
= NULL
; /* Cached stat data no longer valid. */
500 r2
= restore_metadata(a
, fd
, entry
, flags
);
502 return (err_combine(r
, r2
));
506 extract_dir(struct archive
*a
, struct archive_entry
*entry
, int flags
)
508 struct extract
*extract
;
509 struct fixup_entry
*fe
;
512 extract
= a
->extract
;
513 extract
->pst
= NULL
; /* Invalidate cached stat data. */
515 /* Copy path to mutable storage. */
516 archive_strcpy(&(extract
->create_parent_dir
),
517 archive_entry_pathname(entry
));
518 path
= extract
->create_parent_dir
.s
;
521 archive_set_error(a
, ARCHIVE_ERRNO_MISC
,
522 "Invalid empty pathname");
523 return (ARCHIVE_WARN
);
526 /* Deal with any troublesome trailing path elements. */
527 /* TODO: Someday, generalize this to remove '//' or '/./' from
528 * the middle of paths. But, it should not compress '..' from
529 * the middle of paths. It's a feature that restoring
530 * "a/../b" creates both 'a' and 'b' directories. */
532 /* Locate last element. */
533 p
= strrchr(path
, '/');
538 /* Trim trailing '/' unless that's the entire path. */
539 if (p
[0] == '\0' && p
- 1 > path
) {
543 /* Trim trailing '.' unless that's the entire path. */
544 if (p
> path
&& p
[0] == '.' && p
[1] == '\0') {
548 /* Just exit on trailing '..'. */
549 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '\0') {
550 archive_set_error(a
, ARCHIVE_ERRNO_MISC
,
551 "Can't restore directory '..'");
552 return (ARCHIVE_WARN
);
557 if (mkdir(path
, SECURE_DIR_MODE
) == 0)
560 if (extract
->pst
== NULL
&& stat(path
, &extract
->st
) == 0)
561 extract
->pst
= &extract
->st
;
563 if (extract
->pst
!= NULL
) {
564 extract
->pst
= &extract
->st
;
565 /* If dir already exists, don't reset permissions. */
566 if (S_ISDIR(extract
->pst
->st_mode
))
568 /* It exists but isn't a dir. */
569 if ((flags
& ARCHIVE_EXTRACT_UNLINK
))
572 /* Doesn't already exist; try building the parent path. */
573 if (create_parent_dir_mutable(a
, path
, flags
) != ARCHIVE_OK
)
574 return (ARCHIVE_WARN
);
577 /* One final attempt to create the dir. */
578 if (mkdir(path
, SECURE_DIR_MODE
) != 0) {
579 archive_set_error(a
, errno
, "Can't create directory");
580 return (ARCHIVE_WARN
);
584 /* Add this dir to the fixup list. */
585 fe
= current_fixup(a
, path
);
586 fe
->fixup
|= FIXUP_MODE
;
587 fe
->mode
= archive_entry_mode(entry
);
588 if ((flags
& ARCHIVE_EXTRACT_PERM
) == 0)
589 fe
->mode
&= ~extract
->umask
;
590 if (flags
& ARCHIVE_EXTRACT_TIME
) {
591 fe
->fixup
|= FIXUP_TIMES
;
592 fe
->mtime
= archive_entry_mtime(entry
);
593 fe
->mtime_nanos
= archive_entry_mtime_nsec(entry
);
594 fe
->atime
= archive_entry_atime(entry
);
595 fe
->atime_nanos
= archive_entry_atime_nsec(entry
);
597 /* For now, set the mode to SECURE_DIR_MODE. */
598 archive_entry_set_mode(entry
, SECURE_DIR_MODE
);
599 return (restore_metadata(a
, -1, entry
, flags
));
604 * Create the parent of the specified path. Copy the provided
605 * path into mutable storage first.
608 create_parent_dir(struct archive
*a
, const char *path
, int flags
)
612 /* Copy path to mutable storage. */
613 archive_strcpy(&(a
->extract
->create_parent_dir
), path
);
614 r
= create_parent_dir_mutable(a
, a
->extract
->create_parent_dir
.s
, flags
);
619 * Like create_parent_dir, but creates the dir actually requested, not
623 create_dir(struct archive
*a
, const char *path
, int flags
)
626 /* Copy path to mutable storage. */
627 archive_strcpy(&(a
->extract
->create_parent_dir
), path
);
628 r
= create_dir_mutable(a
, a
->extract
->create_parent_dir
.s
, flags
);
633 * Create the parent directory of the specified path, assuming path
634 * is already in mutable storage.
637 create_parent_dir_mutable(struct archive
*a
, char *path
, int flags
)
642 /* Remove tail element to obtain parent name. */
643 slash
= strrchr(path
, '/');
647 r
= create_dir_mutable(a
, path
, flags
);
653 * Create the specified dir, assuming path is already in
657 create_dir_mutable(struct archive
*a
, char *path
, int flags
)
662 old_umask
= umask(~SECURE_DIR_MODE
);
663 r
= create_dir_recursive(a
, path
, flags
);
669 * Create the specified dir, recursing to create parents as necessary.
671 * Returns ARCHIVE_OK if the path exists when we're done here.
672 * Otherwise, returns ARCHIVE_WARN.
675 create_dir_recursive(struct archive
*a
, char *path
, int flags
)
678 struct extract
*extract
;
679 struct fixup_entry
*le
;
683 extract
= a
->extract
;
686 /* Check for special names and just skip them. */
687 slash
= strrchr(path
, '/');
688 base
= strrchr(path
, '/');
694 if (base
[0] == '\0' ||
695 (base
[0] == '.' && base
[1] == '\0') ||
696 (base
[0] == '.' && base
[1] == '.' && base
[2] == '\0')) {
697 /* Don't bother trying to create null path, '.', or '..'. */
700 r
= create_dir_recursive(a
, path
, flags
);
708 * Yes, this should be stat() and not lstat(). Using lstat()
709 * here loses the ability to extract through symlinks. Also note
710 * that this should not use the extract->st cache.
712 if (stat(path
, &st
) == 0) {
713 if (S_ISDIR(st
.st_mode
))
715 if ((flags
& ARCHIVE_EXTRACT_NO_OVERWRITE
)) {
716 archive_set_error(a
, EEXIST
,
717 "Can't create directory '%s'", path
);
718 return (ARCHIVE_WARN
);
720 if (unlink(path
) != 0) {
721 archive_set_error(a
, errno
,
722 "Can't create directory '%s': "
723 "Conflicting file cannot be removed");
724 return (ARCHIVE_WARN
);
726 } else if (errno
!= ENOENT
&& errno
!= ENOTDIR
) {
728 archive_set_error(a
, errno
, "Can't test directory '%s'", path
);
729 return (ARCHIVE_WARN
);
730 } else if (slash
!= NULL
) {
732 r
= create_dir_recursive(a
, path
, flags
);
738 if (mkdir(path
, SECURE_DIR_MODE
) == 0) {
739 le
= new_fixup(a
, path
);
740 le
->fixup
|= FIXUP_MODE
;
741 le
->mode
= extract
->default_dir_mode
;
746 * Without the following check, a/b/../b/c/d fails at the
747 * second visit to 'b', so 'd' can't be created. Note that we
748 * don't add it to the fixup list here, as it's already been
751 if (stat(path
, &st
) == 0 && S_ISDIR(st
.st_mode
))
754 archive_set_error(a
, errno
, "Failed to create dir '%s'", path
);
755 return (ARCHIVE_WARN
);
759 extract_hard_link(struct archive
*a
, struct archive_entry
*entry
, int flags
)
761 struct extract
*extract
;
763 const char *pathname
;
764 const char *linkname
;
766 extract
= a
->extract
;
767 pathname
= archive_entry_pathname(entry
);
768 linkname
= archive_entry_hardlink(entry
);
770 /* Just remove any pre-existing file with this name. */
771 if (!(flags
& ARCHIVE_EXTRACT_NO_OVERWRITE
))
774 r
= link(linkname
, pathname
);
775 extract
->pst
= NULL
; /* Invalidate cached stat data. */
778 /* Might be a non-existent parent dir; try fixing that. */
779 create_parent_dir(a
, pathname
, flags
);
780 r
= link(linkname
, pathname
);
784 /* XXX Better error message here XXX */
785 archive_set_error(a
, errno
,
786 "Can't restore hardlink to '%s'", linkname
);
787 return (ARCHIVE_WARN
);
790 /* Set ownership, time, permission information. */
791 r
= restore_metadata(a
, -1, entry
, flags
);
796 extract_symlink(struct archive
*a
, struct archive_entry
*entry
, int flags
)
798 struct extract
*extract
;
800 const char *pathname
;
801 const char *linkname
;
803 extract
= a
->extract
;
804 pathname
= archive_entry_pathname(entry
);
805 linkname
= archive_entry_symlink(entry
);
807 /* Just remove any pre-existing file with this name. */
808 if (!(flags
& ARCHIVE_EXTRACT_NO_OVERWRITE
))
811 r
= symlink(linkname
, pathname
);
812 extract
->pst
= NULL
; /* Invalidate cached stat data. */
815 /* Might be a non-existent parent dir; try fixing that. */
816 create_parent_dir(a
, pathname
, flags
);
817 r
= symlink(linkname
, pathname
);
821 /* XXX Better error message here XXX */
822 archive_set_error(a
, errno
,
823 "Can't restore symlink to '%s'", linkname
);
824 return (ARCHIVE_WARN
);
827 r
= restore_metadata(a
, -1, entry
, flags
);
832 extract_device(struct archive
*a
, struct archive_entry
*entry
,
833 int flags
, mode_t mode
)
835 struct extract
*extract
;
838 extract
= a
->extract
;
839 /* Just remove any pre-existing file with this name. */
840 if (!(flags
& ARCHIVE_EXTRACT_NO_OVERWRITE
))
841 unlink(archive_entry_pathname(entry
));
843 r
= mknod(archive_entry_pathname(entry
), mode
,
844 archive_entry_rdev(entry
));
845 extract
->pst
= NULL
; /* Invalidate cached stat data. */
847 /* Might be a non-existent parent dir; try fixing that. */
848 if (r
!= 0 && errno
== ENOENT
) {
849 create_parent_dir(a
, archive_entry_pathname(entry
), flags
);
850 r
= mknod(archive_entry_pathname(entry
), mode
,
851 archive_entry_rdev(entry
));
855 archive_set_error(a
, errno
, "Can't restore device node");
856 return (ARCHIVE_WARN
);
859 r
= restore_metadata(a
, -1, entry
, flags
);
864 extract_char_device(struct archive
*a
, struct archive_entry
*entry
, int flags
)
868 mode
= (archive_entry_mode(entry
) & ~S_IFMT
) | S_IFCHR
;
869 return (extract_device(a
, entry
, flags
, mode
));
873 extract_block_device(struct archive
*a
, struct archive_entry
*entry
, int flags
)
877 mode
= (archive_entry_mode(entry
) & ~S_IFMT
) | S_IFBLK
;
878 return (extract_device(a
, entry
, flags
, mode
));
882 extract_fifo(struct archive
*a
, struct archive_entry
*entry
, int flags
)
884 struct extract
*extract
;
887 extract
= a
->extract
;
888 /* Just remove any pre-existing file with this name. */
889 if (!(flags
& ARCHIVE_EXTRACT_NO_OVERWRITE
))
890 unlink(archive_entry_pathname(entry
));
892 r
= mkfifo(archive_entry_pathname(entry
),
893 archive_entry_mode(entry
));
894 extract
->pst
= NULL
; /* Invalidate cached stat data. */
896 /* Might be a non-existent parent dir; try fixing that. */
897 if (r
!= 0 && errno
== ENOENT
) {
898 create_parent_dir(a
, archive_entry_pathname(entry
), flags
);
899 r
= mkfifo(archive_entry_pathname(entry
),
900 archive_entry_mode(entry
));
904 archive_set_error(a
, errno
, "Can't restore fifo");
905 return (ARCHIVE_WARN
);
908 r
= restore_metadata(a
, -1, entry
, flags
);
913 restore_metadata(struct archive
*a
, int fd
, struct archive_entry
*entry
, int flags
)
917 r
= set_ownership(a
, fd
, entry
, flags
);
918 r2
= set_time(a
, fd
, entry
, flags
);
919 r
= err_combine(r
, r2
);
920 r2
= set_perm(a
, fd
, entry
, archive_entry_mode(entry
), flags
);
921 return (err_combine(r
, r2
));
925 set_ownership(struct archive
*a
, int fd
,
926 struct archive_entry
*entry
, int flags
)
932 if ((flags
& ARCHIVE_EXTRACT_OWNER
) == 0)
935 uid
= lookup_uid(a
, archive_entry_uname(entry
),
936 archive_entry_uid(entry
));
937 gid
= lookup_gid(a
, archive_entry_gname(entry
),
938 archive_entry_gid(entry
));
940 /* If we know we can't change it, don't bother trying. */
941 if (a
->user_uid
!= 0 && a
->user_uid
!= uid
)
945 if (fd
>= 0 && fchown(fd
, uid
, gid
) == 0)
950 if (lchown(archive_entry_pathname(entry
), uid
, gid
))
952 if (!S_ISLNK(archive_entry_mode(entry
))
953 && chown(archive_entry_pathname(entry
), uid
, gid
) != 0)
956 archive_set_error(a
, errno
,
957 "Can't set user=%d/group=%d for %s", uid
, gid
,
958 archive_entry_pathname(entry
));
959 return (ARCHIVE_WARN
);
965 set_time(struct archive
*a
, int fd
, struct archive_entry
*entry
, int flags
)
967 const struct stat
*st
;
968 struct timeval times
[2];
970 (void)a
; /* UNUSED */
971 st
= archive_entry_stat(entry
);
973 if ((flags
& ARCHIVE_EXTRACT_TIME
) == 0)
975 /* It's a waste of time to mess with dir timestamps here. */
976 if (S_ISDIR(archive_entry_mode(entry
)))
979 times
[1].tv_sec
= st
->st_mtime
;
980 times
[1].tv_usec
= ARCHIVE_STAT_MTIME_NANOS(st
) / 1000;
982 times
[0].tv_sec
= st
->st_atime
;
983 times
[0].tv_usec
= ARCHIVE_STAT_ATIME_NANOS(st
) / 1000;
986 if (fd
>= 0 && futimes(fd
, times
) == 0)
991 if (lutimes(archive_entry_pathname(entry
), times
) != 0) {
993 if ((archive_entry_mode(entry
) & S_IFMT
) != S_IFLNK
&&
994 utimes(archive_entry_pathname(entry
), times
) != 0) {
996 archive_set_error(a
, errno
, "Can't update time for %s",
997 archive_entry_pathname(entry
));
998 return (ARCHIVE_WARN
);
1002 * Note: POSIX does not provide a portable way to restore ctime.
1003 * (Apart from resetting the system clock, which is distasteful.)
1004 * So, any restoration of ctime will necessarily be OS-specific.
1007 /* XXX TODO: Can FreeBSD restore ctime? XXX */
1009 return (ARCHIVE_OK
);
1013 set_perm(struct archive
*a
, int fd
, struct archive_entry
*entry
,
1014 int mode
, int flags
)
1016 struct extract
*extract
;
1017 struct fixup_entry
*le
;
1019 unsigned long set
, clear
;
1023 extract
= a
->extract
;
1025 /* Obey umask unless ARCHIVE_EXTRACT_PERM. */
1026 if ((flags
& ARCHIVE_EXTRACT_PERM
) == 0)
1027 mode
&= ~extract
->umask
; /* Enforce umask. */
1028 name
= archive_entry_pathname(entry
);
1030 if (mode
& (S_ISUID
| S_ISGID
)) {
1031 if (extract
->pst
!= NULL
) {
1032 /* Already have stat() data available. */
1034 } else if (fd
>= 0 && fstat(fd
, &extract
->st
) == 0) {
1035 extract
->pst
= &extract
->st
;
1037 } else if (stat(name
, &extract
->st
) == 0) {
1038 extract
->pst
= &extract
->st
;
1040 archive_set_error(a
, errno
,
1041 "Couldn't stat file");
1042 return (ARCHIVE_WARN
);
1046 * TODO: Use the uid/gid looked up in set_ownership
1047 * above rather than the uid/gid stored in the entry.
1049 if (extract
->pst
->st_uid
!= archive_entry_uid(entry
))
1051 if (extract
->pst
->st_gid
!= archive_entry_gid(entry
))
1056 * Ensure we change permissions on the object we extracted,
1057 * and not any incidental symlink that might have gotten in
1060 if (!S_ISLNK(archive_entry_mode(entry
))) {
1063 if (fchmod(fd
, mode
) != 0) {
1064 archive_set_error(a
, errno
,
1065 "Can't set permissions");
1066 return (ARCHIVE_WARN
);
1070 if (chmod(name
, mode
) != 0) {
1071 archive_set_error(a
, errno
, "Can't set permissions");
1072 return (ARCHIVE_WARN
);
1077 * If lchmod() isn't supported, it's no big deal.
1078 * Permissions on symlinks are actually ignored on
1081 if (lchmod(name
, mode
) != 0) {
1082 archive_set_error(a
, errno
, "Can't set permissions");
1083 return (ARCHIVE_WARN
);
1088 if (flags
& ARCHIVE_EXTRACT_ACL
) {
1089 r
= set_acls(a
, fd
, entry
);
1090 if (r
!= ARCHIVE_OK
)
1094 if (flags
& ARCHIVE_EXTRACT_XATTR
) {
1095 r
= set_xattrs(a
, fd
, entry
);
1096 if (r
!= ARCHIVE_OK
)
1101 * Make 'critical_flags' hold all file flags that can't be
1102 * immediately restored. For example, on BSD systems,
1103 * SF_IMMUTABLE prevents hardlinks from being created, so
1104 * should not be set until after any hardlinks are created. To
1105 * preserve some semblance of portability, this uses #ifdef
1106 * extensively. Ugly, but it works.
1108 * Yes, Virginia, this does create a security race. It's mitigated
1109 * somewhat by the practice of creating dirs 0700 until the extract
1110 * is done, but it would be nice if we could do more than that.
1111 * People restoring critical file systems should be wary of
1112 * other programs that might try to muck with files as they're
1115 /* Hopefully, the compiler will optimize this mess into a constant. */
1118 critical_flags
|= SF_IMMUTABLE
;
1121 critical_flags
|= UF_IMMUTABLE
;
1124 critical_flags
|= SF_APPEND
;
1127 critical_flags
|= UF_APPEND
;
1129 #ifdef EXT2_APPEND_FL
1130 critical_flags
|= EXT2_APPEND_FL
;
1132 #ifdef EXT2_IMMUTABLE_FL
1133 critical_flags
|= EXT2_IMMUTABLE_FL
;
1136 if (flags
& ARCHIVE_EXTRACT_FFLAGS
) {
1137 archive_entry_fflags(entry
, &set
, &clear
);
1140 * The first test encourages the compiler to eliminate
1141 * all of this if it's not necessary.
1143 if ((critical_flags
!= 0) && (set
& critical_flags
)) {
1144 le
= current_fixup(a
, archive_entry_pathname(entry
));
1145 le
->fixup
|= FIXUP_FFLAGS
;
1146 le
->fflags_set
= set
;
1147 /* Store the mode if it's not already there. */
1148 if ((le
->fixup
& FIXUP_MODE
) == 0)
1151 r
= set_fflags(a
, fd
, archive_entry_pathname(entry
),
1153 if (r
!= ARCHIVE_OK
)
1157 return (ARCHIVE_OK
);
1161 #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && !defined(__linux)
1163 set_fflags(struct archive
*a
, int fd
, const char *name
, mode_t mode
,
1164 unsigned long set
, unsigned long clear
)
1166 struct extract
*extract
;
1168 extract
= a
->extract
;
1169 if (set
== 0 && clear
== 0)
1170 return (ARCHIVE_OK
);
1172 (void)mode
; /* UNUSED */
1174 * XXX Is the stat here really necessary? Or can I just use
1175 * the 'set' flags directly? In particular, I'm not sure
1176 * about the correct approach if we're overwriting an existing
1177 * file that already has flags on it. XXX
1179 if (extract
->pst
!= NULL
) {
1180 /* Already have stat() data available. */
1181 } else if (fd
>= 0 && fstat(fd
, &extract
->st
) == 0)
1182 extract
->pst
= &extract
->st
;
1183 else if (stat(name
, &extract
->st
) == 0)
1184 extract
->pst
= &extract
->st
;
1186 archive_set_error(a
, errno
,
1187 "Couldn't stat file");
1188 return (ARCHIVE_WARN
);
1191 extract
->st
.st_flags
&= ~clear
;
1192 extract
->st
.st_flags
|= set
;
1193 #ifdef HAVE_FCHFLAGS
1194 /* If platform has fchflags() and we were given an fd, use it. */
1195 if (fd
>= 0 && fchflags(fd
, extract
->st
.st_flags
) == 0)
1196 return (ARCHIVE_OK
);
1199 * If we can't use the fd to set the flags, we'll use the
1200 * pathname to set flags. We prefer lchflags() but will use
1201 * chflags() if we must.
1203 #ifdef HAVE_LCHFLAGS
1204 if (lchflags(name
, extract
->st
.st_flags
) == 0)
1205 return (ARCHIVE_OK
);
1206 #elif defined(HAVE_CHFLAGS)
1207 if (chflags(name
, extract
->st
.st_flags
) == 0)
1208 return (ARCHIVE_OK
);
1210 archive_set_error(a
, errno
,
1211 "Failed to set file flags");
1212 return (ARCHIVE_WARN
);
1215 #elif defined(__linux) && defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS)
1218 * Linux has flags too, but uses ioctl() to access them instead of
1219 * having a separate chflags() system call.
1222 set_fflags(struct archive
*a
, int fd
, const char *name
, mode_t mode
,
1223 unsigned long set
, unsigned long clear
)
1225 struct extract
*extract
;
1228 unsigned long newflags
, oldflags
;
1229 unsigned long sf_mask
= 0;
1231 extract
= a
->extract
;
1232 if (set
== 0 && clear
== 0)
1233 return (ARCHIVE_OK
);
1234 /* Only regular files and dirs can have flags. */
1235 if (!S_ISREG(mode
) && !S_ISDIR(mode
))
1236 return (ARCHIVE_OK
);
1238 /* If we weren't given an fd, open it ourselves. */
1240 myfd
= open(name
, O_RDONLY
|O_NONBLOCK
);
1242 return (ARCHIVE_OK
);
1245 * Linux has no define for the flags that are only settable by
1246 * the root user. This code may seem a little complex, but
1247 * there seem to be some Linux systems that lack these
1248 * defines. (?) The code below degrades reasonably gracefully
1249 * if sf_mask is incomplete.
1251 #ifdef EXT2_IMMUTABLE_FL
1252 sf_mask
|= EXT2_IMMUTABLE_FL
;
1254 #ifdef EXT2_APPEND_FL
1255 sf_mask
|= EXT2_APPEND_FL
;
1258 * XXX As above, this would be way simpler if we didn't have
1259 * to read the current flags from disk. XXX
1262 /* Try setting the flags as given. */
1263 if (ioctl(myfd
, EXT2_IOC_GETFLAGS
, &oldflags
) >= 0) {
1264 newflags
= (oldflags
& ~clear
) | set
;
1265 if (ioctl(myfd
, EXT2_IOC_SETFLAGS
, &newflags
) >= 0)
1270 /* If we couldn't set all the flags, try again with a subset. */
1271 if (ioctl(myfd
, EXT2_IOC_GETFLAGS
, &oldflags
) >= 0) {
1272 newflags
&= ~sf_mask
;
1273 oldflags
&= sf_mask
;
1274 newflags
|= oldflags
;
1275 if (ioctl(myfd
, EXT2_IOC_SETFLAGS
, &newflags
) >= 0)
1278 /* We couldn't set the flags, so report the failure. */
1280 archive_set_error(a
, errno
,
1281 "Failed to set file flags");
1289 #else /* Not HAVE_CHFLAGS && Not __linux */
1292 * Of course, some systems have neither BSD chflags() nor Linux' flags
1293 * support through ioctl().
1296 set_fflags(struct archive
*a
, int fd
, const char *name
, mode_t mode
,
1297 unsigned long set
, unsigned long clear
)
1305 return (ARCHIVE_OK
);
1308 #endif /* __linux */
1310 #ifndef HAVE_POSIX_ACL
1311 /* Default empty function body to satisfy mainline code. */
1313 set_acls(struct archive
*a
, int fd
, struct archive_entry
*entry
)
1319 return (ARCHIVE_OK
);
1325 * XXX TODO: What about ACL types other than ACCESS and DEFAULT?
1328 set_acls(struct archive
*a
, int fd
, struct archive_entry
*entry
)
1332 ret
= set_acl(a
, fd
, entry
, ACL_TYPE_ACCESS
,
1333 ARCHIVE_ENTRY_ACL_TYPE_ACCESS
, "access");
1334 if (ret
!= ARCHIVE_OK
)
1336 ret
= set_acl(a
, fd
, entry
, ACL_TYPE_DEFAULT
,
1337 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT
, "default");
1343 set_acl(struct archive
*a
, int fd
, struct archive_entry
*entry
,
1344 acl_type_t acl_type
, int ae_requested_type
, const char *typename
)
1347 acl_entry_t acl_entry
;
1348 acl_permset_t acl_permset
;
1350 int ae_type
, ae_permset
, ae_tag
, ae_id
;
1353 const char *ae_name
;
1358 entries
= archive_entry_acl_reset(entry
, ae_requested_type
);
1360 return (ARCHIVE_OK
);
1361 acl
= acl_init(entries
);
1362 while (archive_entry_acl_next(entry
, ae_requested_type
, &ae_type
,
1363 &ae_permset
, &ae_tag
, &ae_id
, &ae_name
) == ARCHIVE_OK
) {
1364 acl_create_entry(&acl
, &acl_entry
);
1367 case ARCHIVE_ENTRY_ACL_USER
:
1368 acl_set_tag_type(acl_entry
, ACL_USER
);
1369 ae_uid
= lookup_uid(a
, ae_name
, ae_id
);
1370 acl_set_qualifier(acl_entry
, &ae_uid
);
1372 case ARCHIVE_ENTRY_ACL_GROUP
:
1373 acl_set_tag_type(acl_entry
, ACL_GROUP
);
1374 ae_gid
= lookup_gid(a
, ae_name
, ae_id
);
1375 acl_set_qualifier(acl_entry
, &ae_gid
);
1377 case ARCHIVE_ENTRY_ACL_USER_OBJ
:
1378 acl_set_tag_type(acl_entry
, ACL_USER_OBJ
);
1380 case ARCHIVE_ENTRY_ACL_GROUP_OBJ
:
1381 acl_set_tag_type(acl_entry
, ACL_GROUP_OBJ
);
1383 case ARCHIVE_ENTRY_ACL_MASK
:
1384 acl_set_tag_type(acl_entry
, ACL_MASK
);
1386 case ARCHIVE_ENTRY_ACL_OTHER
:
1387 acl_set_tag_type(acl_entry
, ACL_OTHER
);
1394 acl_get_permset(acl_entry
, &acl_permset
);
1395 acl_clear_perms(acl_permset
);
1396 if (ae_permset
& ARCHIVE_ENTRY_ACL_EXECUTE
)
1397 acl_add_perm(acl_permset
, ACL_EXECUTE
);
1398 if (ae_permset
& ARCHIVE_ENTRY_ACL_WRITE
)
1399 acl_add_perm(acl_permset
, ACL_WRITE
);
1400 if (ae_permset
& ARCHIVE_ENTRY_ACL_READ
)
1401 acl_add_perm(acl_permset
, ACL_READ
);
1404 name
= archive_entry_pathname(entry
);
1406 /* Try restoring the ACL through 'fd' if we can. */
1408 if (fd
>= 0 && acl_type
== ACL_TYPE_ACCESS
&& acl_set_fd(fd
, acl
) == 0)
1412 #if HAVE_ACL_SET_FD_NP
1413 if (fd
>= 0 && acl_set_fd_np(fd
, acl
, acl_type
) == 0)
1418 if (acl_set_file(name
, acl_type
, acl
) != 0) {
1419 archive_set_error(a
, errno
, "Failed to set %s acl", typename
);
1429 * Restore extended attributes - Linux implementation
1432 set_xattrs(struct archive
*a
, int fd
, struct archive_entry
*entry
)
1434 static int warning_done
= 0;
1435 int ret
= ARCHIVE_OK
;
1436 int i
= archive_entry_xattr_reset(entry
);
1442 archive_entry_xattr_next(entry
, &name
, &value
, &size
);
1444 strncmp(name
, "xfsroot.", 8) != 0 &&
1445 strncmp(name
, "system.", 7) != 0) {
1449 e
= fsetxattr(fd
, name
, value
, size
, 0);
1453 e
= lsetxattr(archive_entry_pathname(entry
),
1454 name
, value
, size
, 0);
1457 if (errno
== ENOTSUP
) {
1458 if (!warning_done
) {
1460 archive_set_error(a
, errno
,
1461 "Cannot restore extended "
1462 "attributes on this file "
1466 archive_set_error(a
, errno
,
1467 "Failed to set extended attribute");
1471 archive_set_error(a
, ARCHIVE_ERRNO_FILE_FORMAT
,
1472 "Invalid extended attribute encountered");
1480 * Restore extended attributes - stub implementation for unsupported systems
1483 set_xattrs(struct archive
*a
, int fd
, struct archive_entry
*entry
)
1485 static int warning_done
= 0;
1486 (void)a
; /* UNUSED */
1487 (void)fd
; /* UNUSED */
1489 /* If there aren't any extended attributes, then it's okay not
1490 * to extract them, otherwise, issue a single warning. */
1491 if (archive_entry_xattr_count(entry
) != 0 && !warning_done
) {
1493 archive_set_error(a
, ARCHIVE_ERRNO_FILE_FORMAT
,
1494 "Cannot restore extended attributes on this system");
1495 return (ARCHIVE_WARN
);
1497 /* Warning was already emitted; suppress further warnings. */
1498 return (ARCHIVE_OK
);
1503 * The following routines do some basic caching of uname/gname
1504 * lookups. All such lookups go through these routines, including ACL
1505 * conversions. Even a small cache here provides an enormous speedup,
1506 * especially on systems using NIS, LDAP, or a similar networked
1509 * TODO: Provide an API for clients to override these routines.
1512 lookup_gid(struct archive
*a
, const char *gname
, gid_t gid
)
1514 struct group
*grent
;
1515 struct extract
*extract
;
1520 extract
= a
->extract
;
1521 cache_size
= sizeof(extract
->gcache
) / sizeof(extract
->gcache
[0]);
1523 /* If no gname, just use the gid provided. */
1524 if (gname
== NULL
|| *gname
== '\0')
1527 /* Try to find gname in the cache. */
1529 b
= &extract
->gcache
[h
% cache_size
];
1530 if (b
->name
!= NULL
&& b
->hash
== h
&& strcmp(gname
, b
->name
) == 0)
1531 return ((gid_t
)b
->id
);
1533 /* Free the cache slot for a new entry. */
1534 if (b
->name
!= NULL
)
1536 b
->name
= strdup(gname
);
1537 /* Note: If strdup fails, that's okay; we just won't cache. */
1539 grent
= getgrnam(gname
);
1541 gid
= grent
->gr_gid
;
1548 lookup_uid(struct archive
*a
, const char *uname
, uid_t uid
)
1550 struct passwd
*pwent
;
1551 struct extract
*extract
;
1556 extract
= a
->extract
;
1557 cache_size
= sizeof(extract
->ucache
) / sizeof(extract
->ucache
[0]);
1559 /* If no uname, just use the uid provided. */
1560 if (uname
== NULL
|| *uname
== '\0')
1563 /* Try to find uname in the cache. */
1565 b
= &extract
->ucache
[h
% cache_size
];
1566 if (b
->name
!= NULL
&& b
->hash
== h
&& strcmp(uname
, b
->name
) == 0)
1567 return ((uid_t
)b
->id
);
1569 /* Free the cache slot for a new entry. */
1570 if (b
->name
!= NULL
)
1572 b
->name
= strdup(uname
);
1573 /* Note: If strdup fails, that's okay; we just won't cache. */
1575 pwent
= getpwnam(uname
);
1577 uid
= pwent
->pw_uid
;
1586 /* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
1587 as used by ELF for hashing function names. */
1589 while (*p
!= '\0') {
1590 h
= ( h
<< 4 ) + *p
++;
1591 if (( g
= h
& 0xF0000000 )) {
1600 archive_read_extract_set_progress_callback(struct archive
*a
,
1601 void (*progress_func
)(void *), void *user_data
)
1603 a
->extract_progress
= progress_func
;
1604 a
->extract_progress_user_data
= user_data
;