Make sure UFS disallows mknod()'s with type VDIR.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_write_disk.c
blob8010c1330ea09ff9a25b00de9003120b4eb01ee1
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
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_write_disk.c,v 1.24 2008/03/15 04:20:50 kientzle Exp $");
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
32 #endif
33 #ifdef HAVE_SYS_ACL_H
34 #include <sys/acl.h>
35 #endif
36 #ifdef HAVE_ATTR_XATTR_H
37 #include <attr/xattr.h>
38 #endif
39 #ifdef HAVE_SYS_IOCTL_H
40 #include <sys/ioctl.h>
41 #endif
42 #ifdef HAVE_SYS_STAT_H
43 #include <sys/stat.h>
44 #endif
45 #ifdef HAVE_SYS_TIME_H
46 #include <sys/time.h>
47 #endif
48 #ifdef HAVE_SYS_UTIME_H
49 #include <sys/utime.h>
50 #endif
52 #ifdef HAVE_EXT2FS_EXT2_FS_H
53 #include <ext2fs/ext2_fs.h> /* for Linux file flags */
54 #endif
55 #ifdef HAVE_ERRNO_H
56 #include <errno.h>
57 #endif
58 #ifdef HAVE_FCNTL_H
59 #include <fcntl.h>
60 #endif
61 #ifdef HAVE_GRP_H
62 #include <grp.h>
63 #endif
64 #ifdef HAVE_LINUX_FS_H
65 #include <linux/fs.h> /* for Linux file flags */
66 #endif
67 #ifdef HAVE_LINUX_EXT2_FS_H
68 #include <linux/ext2_fs.h> /* for Linux file flags */
69 #endif
70 #ifdef HAVE_LIMITS_H
71 #include <limits.h>
72 #endif
73 #ifdef HAVE_PWD_H
74 #include <pwd.h>
75 #endif
76 #include <stdio.h>
77 #ifdef HAVE_STDLIB_H
78 #include <stdlib.h>
79 #endif
80 #ifdef HAVE_STRING_H
81 #include <string.h>
82 #endif
83 #ifdef HAVE_UNISTD_H
84 #include <unistd.h>
85 #endif
86 #ifdef HAVE_UTIME_H
87 #include <utime.h>
88 #endif
90 #include "archive.h"
91 #include "archive_string.h"
92 #include "archive_entry.h"
93 #include "archive_private.h"
95 #ifndef O_BINARY
96 #define O_BINARY 0
97 #endif
99 struct fixup_entry {
100 struct fixup_entry *next;
101 mode_t mode;
102 int64_t mtime;
103 int64_t atime;
104 unsigned long mtime_nanos;
105 unsigned long atime_nanos;
106 unsigned long fflags_set;
107 int fixup; /* bitmask of what needs fixing */
108 char *name;
112 * We use a bitmask to track which operations remain to be done for
113 * this file. In particular, this helps us avoid unnecessary
114 * operations when it's possible to take care of one step as a
115 * side-effect of another. For example, mkdir() can specify the mode
116 * for the newly-created object but symlink() cannot. This means we
117 * can skip chmod() if mkdir() succeeded, but we must explicitly
118 * chmod() if we're trying to create a directory that already exists
119 * (mkdir() failed) or if we're restoring a symlink. Similarly, we
120 * need to verify UID/GID before trying to restore SUID/SGID bits;
121 * that verification can occur explicitly through a stat() call or
122 * implicitly because of a successful chown() call.
124 #define TODO_MODE_FORCE 0x40000000
125 #define TODO_MODE_BASE 0x20000000
126 #define TODO_SUID 0x10000000
127 #define TODO_SUID_CHECK 0x08000000
128 #define TODO_SGID 0x04000000
129 #define TODO_SGID_CHECK 0x02000000
130 #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
131 #define TODO_TIMES ARCHIVE_EXTRACT_TIME
132 #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
133 #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
134 #define TODO_ACLS ARCHIVE_EXTRACT_ACL
135 #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
137 struct archive_write_disk {
138 struct archive archive;
140 mode_t user_umask;
141 struct fixup_entry *fixup_list;
142 struct fixup_entry *current_fixup;
143 uid_t user_uid;
144 dev_t skip_file_dev;
145 ino_t skip_file_ino;
147 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid);
148 void (*cleanup_gid)(void *private);
149 void *lookup_gid_data;
150 uid_t (*lookup_uid)(void *private, const char *gname, gid_t gid);
151 void (*cleanup_uid)(void *private);
152 void *lookup_uid_data;
155 * Full path of last file to satisfy symlink checks.
157 struct archive_string path_safe;
160 * Cached stat data from disk for the current entry.
161 * If this is valid, pst points to st. Otherwise,
162 * pst is null.
164 struct stat st;
165 struct stat *pst;
167 /* Information about the object being restored right now. */
168 struct archive_entry *entry; /* Entry being extracted. */
169 char *name; /* Name of entry, possibly edited. */
170 struct archive_string _name_data; /* backing store for 'name' */
171 /* Tasks remaining for this object. */
172 int todo;
173 /* Tasks deferred until end-of-archive. */
174 int deferred;
175 /* Options requested by the client. */
176 int flags;
177 /* Handle for the file we're restoring. */
178 int fd;
179 /* Current offset for writing data to the file. */
180 off_t offset;
181 /* Maximum size of file. */
182 off_t filesize;
183 /* Dir we were in before this restore; only for deep paths. */
184 int restore_pwd;
185 /* Mode we should use for this entry; affected by _PERM and umask. */
186 mode_t mode;
187 /* UID/GID to use in restoring this entry. */
188 uid_t uid;
189 gid_t gid;
190 /* Last offset written to disk. */
191 off_t last_offset;
195 * Default mode for dirs created automatically (will be modified by umask).
196 * Note that POSIX specifies 0777 for implicity-created dirs, "modified
197 * by the process' file creation mask."
199 #define DEFAULT_DIR_MODE 0777
201 * Dir modes are restored in two steps: During the extraction, the permissions
202 * in the archive are modified to match the following limits. During
203 * the post-extract fixup pass, the permissions from the archive are
204 * applied.
206 #define MINIMUM_DIR_MODE 0700
207 #define MAXIMUM_DIR_MODE 0775
209 static int check_symlinks(struct archive_write_disk *);
210 static int create_filesystem_object(struct archive_write_disk *);
211 static struct fixup_entry *current_fixup(struct archive_write_disk *, const char *pathname);
212 #ifdef HAVE_FCHDIR
213 static void edit_deep_directories(struct archive_write_disk *ad);
214 #endif
215 static int cleanup_pathname(struct archive_write_disk *);
216 static int create_dir(struct archive_write_disk *, char *);
217 static int create_parent_dir(struct archive_write_disk *, char *);
218 static int older(struct stat *, struct archive_entry *);
219 static int restore_entry(struct archive_write_disk *);
220 #ifdef HAVE_POSIX_ACL
221 static int set_acl(struct archive_write_disk *, int fd, struct archive_entry *,
222 acl_type_t, int archive_entry_acl_type, const char *tn);
223 #endif
224 static int set_acls(struct archive_write_disk *);
225 static int set_xattrs(struct archive_write_disk *);
226 static int set_fflags(struct archive_write_disk *);
227 static int set_fflags_platform(struct archive_write_disk *, int fd,
228 const char *name, mode_t mode,
229 unsigned long fflags_set, unsigned long fflags_clear);
230 static int set_ownership(struct archive_write_disk *);
231 static int set_mode(struct archive_write_disk *, int mode);
232 static int set_time(struct archive_write_disk *);
233 static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
234 static gid_t trivial_lookup_gid(void *, const char *, gid_t);
235 static uid_t trivial_lookup_uid(void *, const char *, uid_t);
238 static struct archive_vtable *archive_write_disk_vtable(void);
240 static int _archive_write_close(struct archive *);
241 static int _archive_write_finish(struct archive *);
242 static int _archive_write_header(struct archive *, struct archive_entry *);
243 static int _archive_write_finish_entry(struct archive *);
244 static ssize_t _archive_write_data(struct archive *, const void *, size_t);
245 static ssize_t _archive_write_data_block(struct archive *, const void *, size_t, off_t);
247 static int
248 _archive_write_disk_lazy_stat(struct archive_write_disk *a)
250 if (a->pst != NULL) {
251 /* Already have stat() data available. */
252 return (ARCHIVE_OK);
254 #ifdef HAVE_FSTAT
255 if (a->fd >= 0 && fstat(a->fd, &a->st) == 0) {
256 a->pst = &a->st;
257 return (ARCHIVE_OK);
259 #endif
261 * XXX At this point, symlinks should not be hit, otherwise
262 * XXX a race occured. Do we want to check explicitly for that?
264 if (lstat(a->name, &a->st) == 0) {
265 a->pst = &a->st;
266 return (ARCHIVE_OK);
268 archive_set_error(&a->archive, errno, "Couldn't stat file");
269 return (ARCHIVE_WARN);
272 static struct archive_vtable *
273 archive_write_disk_vtable(void)
275 static struct archive_vtable av;
276 static int inited = 0;
278 if (!inited) {
279 av.archive_write_close = _archive_write_close;
280 av.archive_write_finish = _archive_write_finish;
281 av.archive_write_header = _archive_write_header;
282 av.archive_write_finish_entry = _archive_write_finish_entry;
283 av.archive_write_data = _archive_write_data;
284 av.archive_write_data_block = _archive_write_data_block;
286 return (&av);
291 archive_write_disk_set_options(struct archive *_a, int flags)
293 struct archive_write_disk *a = (struct archive_write_disk *)_a;
295 a->flags = flags;
296 return (ARCHIVE_OK);
301 * Extract this entry to disk.
303 * TODO: Validate hardlinks. According to the standards, we're
304 * supposed to check each extracted hardlink and squawk if it refers
305 * to a file that we didn't restore. I'm not entirely convinced this
306 * is a good idea, but more importantly: Is there any way to validate
307 * hardlinks without keeping a complete list of filenames from the
308 * entire archive?? Ugh.
311 static int
312 _archive_write_header(struct archive *_a, struct archive_entry *entry)
314 struct archive_write_disk *a = (struct archive_write_disk *)_a;
315 struct fixup_entry *fe;
316 int ret, r;
318 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
319 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
320 "archive_write_disk_header");
321 archive_clear_error(&a->archive);
322 if (a->archive.state & ARCHIVE_STATE_DATA) {
323 r = _archive_write_finish_entry(&a->archive);
324 if (r == ARCHIVE_FATAL)
325 return (r);
328 /* Set up for this particular entry. */
329 a->pst = NULL;
330 a->current_fixup = NULL;
331 a->deferred = 0;
332 if (a->entry) {
333 archive_entry_free(a->entry);
334 a->entry = NULL;
336 a->entry = archive_entry_clone(entry);
337 a->fd = -1;
338 a->last_offset = 0;
339 a->offset = 0;
340 a->uid = a->user_uid;
341 a->mode = archive_entry_mode(a->entry);
342 a->filesize = archive_entry_size(a->entry);
343 archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry));
344 a->name = a->_name_data.s;
345 archive_clear_error(&a->archive);
348 * Clean up the requested path. This is necessary for correct
349 * dir restores; the dir restore logic otherwise gets messed
350 * up by nonsense like "dir/.".
352 ret = cleanup_pathname(a);
353 if (ret != ARCHIVE_OK)
354 return (ret);
357 * Set the umask to zero so we get predictable mode settings.
358 * This gets done on every call to _write_header in case the
359 * user edits their umask during the extraction for some
360 * reason. This will be reset before we return. Note that we
361 * don't need to do this in _finish_entry, as the chmod(), etc,
362 * system calls don't obey umask.
364 a->user_umask = umask(0);
365 /* From here on, early exit requires "goto done" to clean up. */
367 /* Figure out what we need to do for this entry. */
368 a->todo = TODO_MODE_BASE;
369 if (a->flags & ARCHIVE_EXTRACT_PERM) {
370 a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
372 * SGID requires an extra "check" step because we
373 * cannot easily predict the GID that the system will
374 * assign. (Different systems assign GIDs to files
375 * based on a variety of criteria, including process
376 * credentials and the gid of the enclosing
377 * directory.) We can only restore the SGID bit if
378 * the file has the right GID, and we only know the
379 * GID if we either set it (see set_ownership) or if
380 * we've actually called stat() on the file after it
381 * was restored. Since there are several places at
382 * which we might verify the GID, we need a TODO bit
383 * to keep track.
385 if (a->mode & S_ISGID)
386 a->todo |= TODO_SGID | TODO_SGID_CHECK;
388 * Verifying the SUID is simpler, but can still be
389 * done in multiple ways, hence the separate "check" bit.
391 if (a->mode & S_ISUID)
392 a->todo |= TODO_SUID | TODO_SUID_CHECK;
393 } else {
395 * User didn't request full permissions, so don't
396 * restore SUID, SGID bits and obey umask.
398 a->mode &= ~S_ISUID;
399 a->mode &= ~S_ISGID;
400 a->mode &= ~S_ISVTX;
401 a->mode &= ~a->user_umask;
403 if (a->flags & ARCHIVE_EXTRACT_OWNER)
404 a->todo |= TODO_OWNER;
405 if (a->flags & ARCHIVE_EXTRACT_TIME)
406 a->todo |= TODO_TIMES;
407 if (a->flags & ARCHIVE_EXTRACT_ACL)
408 a->todo |= TODO_ACLS;
409 if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
410 a->todo |= TODO_FFLAGS;
411 if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
412 ret = check_symlinks(a);
413 if (ret != ARCHIVE_OK)
414 goto done;
416 #ifdef HAVE_FCHDIR
417 /* If path exceeds PATH_MAX, shorten the path. */
418 edit_deep_directories(a);
419 #endif
421 ret = restore_entry(a);
423 #ifdef HAVE_FCHDIR
424 /* If we changed directory above, restore it here. */
425 if (a->restore_pwd >= 0) {
426 fchdir(a->restore_pwd);
427 close(a->restore_pwd);
428 a->restore_pwd = -1;
430 #endif
433 * Fixup uses the unedited pathname from archive_entry_pathname(),
434 * because it is relative to the base dir and the edited path
435 * might be relative to some intermediate dir as a result of the
436 * deep restore logic.
438 if (a->deferred & TODO_MODE) {
439 fe = current_fixup(a, archive_entry_pathname(entry));
440 fe->fixup |= TODO_MODE_BASE;
441 fe->mode = a->mode;
444 if (a->deferred & TODO_TIMES) {
445 fe = current_fixup(a, archive_entry_pathname(entry));
446 fe->fixup |= TODO_TIMES;
447 fe->mtime = archive_entry_mtime(entry);
448 fe->mtime_nanos = archive_entry_mtime_nsec(entry);
449 fe->atime = archive_entry_atime(entry);
450 fe->atime_nanos = archive_entry_atime_nsec(entry);
453 if (a->deferred & TODO_FFLAGS) {
454 fe = current_fixup(a, archive_entry_pathname(entry));
455 fe->fixup |= TODO_FFLAGS;
456 /* TODO: Complete this.. defer fflags from below. */
459 /* We've created the object and are ready to pour data into it. */
460 if (ret == ARCHIVE_OK)
461 a->archive.state = ARCHIVE_STATE_DATA;
463 * If it's not open, tell our client not to try writing.
464 * In particular, dirs, links, etc, don't get written to.
466 if (a->fd < 0) {
467 archive_entry_set_size(entry, 0);
468 a->filesize = 0;
470 done:
471 /* Restore the user's umask before returning. */
472 umask(a->user_umask);
474 return (ret);
478 archive_write_disk_set_skip_file(struct archive *_a, dev_t d, ino_t i)
480 struct archive_write_disk *a = (struct archive_write_disk *)_a;
481 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
482 ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
483 a->skip_file_dev = d;
484 a->skip_file_ino = i;
485 return (ARCHIVE_OK);
488 static ssize_t
489 _archive_write_data_block(struct archive *_a,
490 const void *buff, size_t size, off_t offset)
492 struct archive_write_disk *a = (struct archive_write_disk *)_a;
493 ssize_t bytes_written = 0;
494 ssize_t block_size, bytes_to_write;
495 int r = ARCHIVE_OK;
497 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
498 ARCHIVE_STATE_DATA, "archive_write_disk_block");
499 if (a->fd < 0) {
500 archive_set_error(&a->archive, 0, "File not open");
501 return (ARCHIVE_WARN);
503 archive_clear_error(&a->archive);
505 if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
506 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
507 return (r);
508 block_size = a->pst->st_blksize;
509 } else
510 block_size = -1;
512 if ((off_t)(offset + size) > a->filesize) {
513 size = (size_t)(a->filesize - a->offset);
514 archive_set_error(&a->archive, 0,
515 "Write request too large");
516 r = ARCHIVE_WARN;
519 /* Write the data. */
520 while (size > 0) {
521 if (block_size != -1) {
522 const char *buf;
524 for (buf = buff; size; ++buf, --size, ++offset) {
525 if (*buf != '\0')
526 break;
528 if (size == 0)
529 break;
530 bytes_to_write = block_size - offset % block_size;
531 buff = buf;
532 } else
533 bytes_to_write = size;
534 /* Seek if necessary to the specified offset. */
535 if (offset != a->last_offset) {
536 if (lseek(a->fd, offset, SEEK_SET) < 0) {
537 archive_set_error(&a->archive, errno, "Seek failed");
538 return (ARCHIVE_FATAL);
541 bytes_written = write(a->fd, buff, size);
542 if (bytes_written < 0) {
543 archive_set_error(&a->archive, errno, "Write failed");
544 return (ARCHIVE_WARN);
546 buff = (const char *)buff + bytes_written;
547 size -= bytes_written;
548 offset += bytes_written;
549 a->last_offset = a->offset = offset;
551 a->offset = offset;
552 return (r);
555 static ssize_t
556 _archive_write_data(struct archive *_a, const void *buff, size_t size)
558 struct archive_write_disk *a = (struct archive_write_disk *)_a;
559 int r;
561 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
562 ARCHIVE_STATE_DATA, "archive_write_data");
563 if (a->fd < 0)
564 return (ARCHIVE_OK);
566 r = _archive_write_data_block(_a, buff, size, a->offset);
567 if (r < ARCHIVE_OK)
568 return (r);
569 return size;
572 static int
573 _archive_write_finish_entry(struct archive *_a)
575 struct archive_write_disk *a = (struct archive_write_disk *)_a;
576 int ret = ARCHIVE_OK;
578 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
579 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
580 "archive_write_finish_entry");
581 if (a->archive.state & ARCHIVE_STATE_HEADER)
582 return (ARCHIVE_OK);
583 archive_clear_error(&a->archive);
585 if (a->last_offset != a->filesize && a->fd >= 0) {
586 if (ftruncate(a->fd, a->filesize) == -1 &&
587 a->filesize == 0) {
588 archive_set_error(&a->archive, errno,
589 "File size could not be restored");
590 return (ARCHIVE_FAILED);
593 * Explicitly stat the file as some platforms might not
594 * implement the XSI option to extend files via ftruncate.
596 a->pst = NULL;
597 if ((ret = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
598 return (ret);
599 if (a->st.st_size != a->filesize) {
600 const char nul = '\0';
601 if (lseek(a->fd, a->st.st_size - 1, SEEK_SET) < 0) {
602 archive_set_error(&a->archive, errno, "Seek failed");
603 return (ARCHIVE_FATAL);
605 if (write(a->fd, &nul, 1) < 0) {
606 archive_set_error(&a->archive, errno,
607 "Write to restore size failed");
608 return (ARCHIVE_FATAL);
613 /* Restore metadata. */
616 * Look up the "real" UID only if we're going to need it. We
617 * need this for TODO_SGID because chown() requires both.
619 if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
620 a->uid = a->lookup_uid(a->lookup_uid_data,
621 archive_entry_uname(a->entry),
622 archive_entry_uid(a->entry));
624 /* Look up the "real" GID only if we're going to need it. */
625 if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
626 a->gid = a->lookup_gid(a->lookup_gid_data,
627 archive_entry_gname(a->entry),
628 archive_entry_gid(a->entry));
631 * If restoring ownership, do it before trying to restore suid/sgid
632 * bits. If we set the owner, we know what it is and can skip
633 * a stat() call to examine the ownership of the file on disk.
635 if (a->todo & TODO_OWNER)
636 ret = set_ownership(a);
637 if (a->todo & TODO_MODE) {
638 int r2 = set_mode(a, a->mode);
639 if (r2 < ret) ret = r2;
641 if (a->todo & TODO_TIMES) {
642 int r2 = set_time(a);
643 if (r2 < ret) ret = r2;
645 if (a->todo & TODO_ACLS) {
646 int r2 = set_acls(a);
647 if (r2 < ret) ret = r2;
649 if (a->todo & TODO_XATTR) {
650 int r2 = set_xattrs(a);
651 if (r2 < ret) ret = r2;
653 if (a->todo & TODO_FFLAGS) {
654 int r2 = set_fflags(a);
655 if (r2 < ret) ret = r2;
658 /* If there's an fd, we can close it now. */
659 if (a->fd >= 0) {
660 close(a->fd);
661 a->fd = -1;
663 /* If there's an entry, we can release it now. */
664 if (a->entry) {
665 archive_entry_free(a->entry);
666 a->entry = NULL;
668 a->archive.state = ARCHIVE_STATE_HEADER;
669 return (ret);
673 archive_write_disk_set_group_lookup(struct archive *_a,
674 void *private_data,
675 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid),
676 void (*cleanup_gid)(void *private))
678 struct archive_write_disk *a = (struct archive_write_disk *)_a;
679 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
680 ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
682 a->lookup_gid = lookup_gid;
683 a->cleanup_gid = cleanup_gid;
684 a->lookup_gid_data = private_data;
685 return (ARCHIVE_OK);
689 archive_write_disk_set_user_lookup(struct archive *_a,
690 void *private_data,
691 uid_t (*lookup_uid)(void *private, const char *uname, uid_t uid),
692 void (*cleanup_uid)(void *private))
694 struct archive_write_disk *a = (struct archive_write_disk *)_a;
695 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
696 ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
698 a->lookup_uid = lookup_uid;
699 a->cleanup_uid = cleanup_uid;
700 a->lookup_uid_data = private_data;
701 return (ARCHIVE_OK);
706 * Create a new archive_write_disk object and initialize it with global state.
708 struct archive *
709 archive_write_disk_new(void)
711 struct archive_write_disk *a;
713 a = (struct archive_write_disk *)malloc(sizeof(*a));
714 if (a == NULL)
715 return (NULL);
716 memset(a, 0, sizeof(*a));
717 a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
718 /* We're ready to write a header immediately. */
719 a->archive.state = ARCHIVE_STATE_HEADER;
720 a->archive.vtable = archive_write_disk_vtable();
721 a->lookup_uid = trivial_lookup_uid;
722 a->lookup_gid = trivial_lookup_gid;
723 #ifdef HAVE_GETEUID
724 a->user_uid = geteuid();
725 #endif /* HAVE_GETEUID */
726 if (archive_string_ensure(&a->path_safe, 512) == NULL) {
727 free(a);
728 return (NULL);
730 return (&a->archive);
735 * If pathname is longer than PATH_MAX, chdir to a suitable
736 * intermediate dir and edit the path down to a shorter suffix. Note
737 * that this routine never returns an error; if the chdir() attempt
738 * fails for any reason, we just go ahead with the long pathname. The
739 * object creation is likely to fail, but any error will get handled
740 * at that time.
742 #ifdef HAVE_FCHDIR
743 static void
744 edit_deep_directories(struct archive_write_disk *a)
746 int ret;
747 char *tail = a->name;
749 a->restore_pwd = -1;
751 /* If path is short, avoid the open() below. */
752 if (strlen(tail) <= PATH_MAX)
753 return;
755 /* Try to record our starting dir. */
756 a->restore_pwd = open(".", O_RDONLY | O_BINARY);
757 if (a->restore_pwd < 0)
758 return;
760 /* As long as the path is too long... */
761 while (strlen(tail) > PATH_MAX) {
762 /* Locate a dir prefix shorter than PATH_MAX. */
763 tail += PATH_MAX - 8;
764 while (tail > a->name && *tail != '/')
765 tail--;
766 /* Exit if we find a too-long path component. */
767 if (tail <= a->name)
768 return;
769 /* Create the intermediate dir and chdir to it. */
770 *tail = '\0'; /* Terminate dir portion */
771 ret = create_dir(a, a->name);
772 if (ret == ARCHIVE_OK && chdir(a->name) != 0)
773 ret = ARCHIVE_WARN;
774 *tail = '/'; /* Restore the / we removed. */
775 if (ret != ARCHIVE_OK)
776 return;
777 tail++;
778 /* The chdir() succeeded; we've now shortened the path. */
779 a->name = tail;
781 return;
783 #endif
786 * The main restore function.
788 static int
789 restore_entry(struct archive_write_disk *a)
791 int ret = ARCHIVE_OK, en;
793 if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
795 * TODO: Fix this. Apparently, there are platforms
796 * that still allow root to hose the entire filesystem
797 * by unlinking a dir. The S_ISDIR() test above
798 * prevents us from using unlink() here if the new
799 * object is a dir, but that doesn't mean the old
800 * object isn't a dir.
802 if (unlink(a->name) == 0) {
803 /* We removed it, reset cached stat. */
804 a->pst = NULL;
805 } else if (errno == ENOENT) {
806 /* File didn't exist, that's just as good. */
807 } else if (rmdir(a->name) == 0) {
808 /* It was a dir, but now it's gone. */
809 a->pst = NULL;
810 } else {
811 /* We tried, but couldn't get rid of it. */
812 archive_set_error(&a->archive, errno,
813 "Could not unlink");
814 return(ARCHIVE_WARN);
818 /* Try creating it first; if this fails, we'll try to recover. */
819 en = create_filesystem_object(a);
821 if ((en == ENOTDIR || en == ENOENT)
822 && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
823 /* If the parent dir doesn't exist, try creating it. */
824 create_parent_dir(a, a->name);
825 /* Now try to create the object again. */
826 en = create_filesystem_object(a);
829 if ((en == EISDIR || en == EEXIST)
830 && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
831 /* If we're not overwriting, we're done. */
832 archive_set_error(&a->archive, en, "Already exists");
833 return (ARCHIVE_WARN);
837 * Some platforms return EISDIR if you call
838 * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
839 * return EEXIST. POSIX is ambiguous, requiring EISDIR
840 * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
841 * on an existing item.
843 if (en == EISDIR) {
844 /* A dir is in the way of a non-dir, rmdir it. */
845 if (rmdir(a->name) != 0) {
846 archive_set_error(&a->archive, errno,
847 "Can't remove already-existing dir");
848 return (ARCHIVE_WARN);
850 a->pst = NULL;
851 /* Try again. */
852 en = create_filesystem_object(a);
853 } else if (en == EEXIST) {
855 * We know something is in the way, but we don't know what;
856 * we need to find out before we go any further.
858 if (lstat(a->name, &a->st) != 0) {
859 archive_set_error(&a->archive, errno,
860 "Can't stat existing object");
861 return (ARCHIVE_WARN);
864 /* TODO: if it's a symlink... */
866 if (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER) {
867 if (!older(&(a->st), a->entry)) {
868 archive_set_error(&a->archive, 0,
869 "File on disk is not older; skipping.");
870 return (ARCHIVE_FAILED);
874 /* If it's our archive, we're done. */
875 if (a->skip_file_dev > 0 &&
876 a->skip_file_ino > 0 &&
877 a->st.st_dev == a->skip_file_dev &&
878 a->st.st_ino == a->skip_file_ino) {
879 archive_set_error(&a->archive, 0, "Refusing to overwrite archive");
880 return (ARCHIVE_FAILED);
883 if (!S_ISDIR(a->st.st_mode)) {
884 /* A non-dir is in the way, unlink it. */
885 if (unlink(a->name) != 0) {
886 archive_set_error(&a->archive, errno,
887 "Can't unlink already-existing object");
888 return (ARCHIVE_WARN);
890 a->pst = NULL;
891 /* Try again. */
892 en = create_filesystem_object(a);
893 } else if (!S_ISDIR(a->mode)) {
894 /* A dir is in the way of a non-dir, rmdir it. */
895 if (rmdir(a->name) != 0) {
896 archive_set_error(&a->archive, errno,
897 "Can't remove already-existing dir");
898 return (ARCHIVE_WARN);
900 /* Try again. */
901 en = create_filesystem_object(a);
902 } else {
904 * There's a dir in the way of a dir. Don't
905 * waste time with rmdir()/mkdir(), just fix
906 * up the permissions on the existing dir.
907 * Note that we don't change perms on existing
908 * dirs unless _EXTRACT_PERM is specified.
910 if ((a->mode != a->st.st_mode)
911 && (a->todo & TODO_MODE_FORCE))
912 a->deferred |= (a->todo & TODO_MODE);
913 /* Ownership doesn't need deferred fixup. */
914 en = 0; /* Forget the EEXIST. */
918 if (en) {
919 /* Everything failed; give up here. */
920 archive_set_error(&a->archive, en, "Can't create '%s'", a->name);
921 return (ARCHIVE_WARN);
924 a->pst = NULL; /* Cached stat data no longer valid. */
925 return (ret);
929 * Returns 0 if creation succeeds, or else returns errno value from
930 * the failed system call. Note: This function should only ever perform
931 * a single system call.
934 create_filesystem_object(struct archive_write_disk *a)
936 /* Create the entry. */
937 const char *linkname;
938 mode_t final_mode, mode;
939 int r;
941 /* We identify hard/symlinks according to the link names. */
942 /* Since link(2) and symlink(2) don't handle modes, we're done here. */
943 linkname = archive_entry_hardlink(a->entry);
944 if (linkname != NULL) {
945 r = link(linkname, a->name) ? errno : 0;
947 * New cpio and pax formats allow hardlink entries
948 * to carry data, so we may have to open the file
949 * for hardlink entries.
951 * If the hardlink was successfully created and
952 * the archive doesn't have carry data for it,
953 * consider it to be non-authoritive for meta data.
954 * This is consistent with GNU tar and BSD pax.
955 * If the hardlink does carry data, let the last
956 * archive entry decide ownership.
958 if (r == 0 && a->filesize == 0) {
959 a->todo = 0;
960 a->deferred = 0;
961 } if (r == 0 && a->filesize > 0) {
962 a->fd = open(a->name, O_WRONLY | O_TRUNC | O_BINARY);
963 if (a->fd < 0)
964 r = errno;
966 return (r);
968 linkname = archive_entry_symlink(a->entry);
969 if (linkname != NULL)
970 return symlink(linkname, a->name) ? errno : 0;
973 * The remaining system calls all set permissions, so let's
974 * try to take advantage of that to avoid an extra chmod()
975 * call. (Recall that umask is set to zero right now!)
978 /* Mode we want for the final restored object (w/o file type bits). */
979 final_mode = a->mode & 07777;
981 * The mode that will actually be restored in this step. Note
982 * that SUID, SGID, etc, require additional work to ensure
983 * security, so we never restore them at this point.
985 mode = final_mode & 0777;
987 switch (a->mode & AE_IFMT) {
988 default:
989 /* POSIX requires that we fall through here. */
990 /* FALLTHROUGH */
991 case AE_IFREG:
992 a->fd = open(a->name,
993 O_WRONLY | O_CREAT | O_EXCL | O_BINARY, mode);
994 r = (a->fd < 0);
995 break;
996 case AE_IFCHR:
997 #ifdef HAVE_MKNOD
998 /* Note: we use AE_IFCHR for the case label, and
999 * S_IFCHR for the mknod() call. This is correct. */
1000 r = mknod(a->name, mode | S_IFCHR,
1001 archive_entry_rdev(a->entry));
1002 #else
1003 /* TODO: Find a better way to warn about our inability
1004 * to restore a char device node. */
1005 return (EINVAL);
1006 #endif /* HAVE_MKNOD */
1007 break;
1008 case AE_IFBLK:
1009 #ifdef HAVE_MKNOD
1010 r = mknod(a->name, mode | S_IFBLK,
1011 archive_entry_rdev(a->entry));
1012 #else
1013 /* TODO: Find a better way to warn about our inability
1014 * to restore a block device node. */
1015 return (EINVAL);
1016 #endif /* HAVE_MKNOD */
1017 break;
1018 case AE_IFDIR:
1019 mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
1020 r = mkdir(a->name, mode);
1021 if (r == 0) {
1022 /* Defer setting dir times. */
1023 a->deferred |= (a->todo & TODO_TIMES);
1024 a->todo &= ~TODO_TIMES;
1025 /* Never use an immediate chmod(). */
1026 if (mode != final_mode)
1027 a->deferred |= (a->todo & TODO_MODE);
1028 a->todo &= ~TODO_MODE;
1030 break;
1031 case AE_IFIFO:
1032 #ifdef HAVE_MKFIFO
1033 r = mkfifo(a->name, mode);
1034 #else
1035 /* TODO: Find a better way to warn about our inability
1036 * to restore a fifo. */
1037 return (EINVAL);
1038 #endif /* HAVE_MKFIFO */
1039 break;
1042 /* All the system calls above set errno on failure. */
1043 if (r)
1044 return (errno);
1046 /* If we managed to set the final mode, we've avoided a chmod(). */
1047 if (mode == final_mode)
1048 a->todo &= ~TODO_MODE;
1049 return (0);
1053 * Cleanup function for archive_extract. Mostly, this involves processing
1054 * the fixup list, which is used to address a number of problems:
1055 * * Dir permissions might prevent us from restoring a file in that
1056 * dir, so we restore the dir with minimum 0700 permissions first,
1057 * then correct the mode at the end.
1058 * * Similarly, the act of restoring a file touches the directory
1059 * and changes the timestamp on the dir, so we have to touch-up dir
1060 * timestamps at the end as well.
1061 * * Some file flags can interfere with the restore by, for example,
1062 * preventing the creation of hardlinks to those files.
1064 * Note that tar/cpio do not require that archives be in a particular
1065 * order; there is no way to know when the last file has been restored
1066 * within a directory, so there's no way to optimize the memory usage
1067 * here by fixing up the directory any earlier than the
1068 * end-of-archive.
1070 * XXX TODO: Directory ACLs should be restored here, for the same
1071 * reason we set directory perms here. XXX
1073 static int
1074 _archive_write_close(struct archive *_a)
1076 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1077 struct fixup_entry *next, *p;
1078 int ret;
1080 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1081 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1082 "archive_write_disk_close");
1083 ret = _archive_write_finish_entry(&a->archive);
1085 /* Sort dir list so directories are fixed up in depth-first order. */
1086 p = sort_dir_list(a->fixup_list);
1088 while (p != NULL) {
1089 a->pst = NULL; /* Mark stat cache as out-of-date. */
1090 if (p->fixup & TODO_TIMES) {
1091 #ifdef HAVE_UTIMES
1092 /* {f,l,}utimes() are preferred, when available. */
1093 struct timeval times[2];
1094 times[1].tv_sec = p->mtime;
1095 times[1].tv_usec = p->mtime_nanos / 1000;
1096 times[0].tv_sec = p->atime;
1097 times[0].tv_usec = p->atime_nanos / 1000;
1098 #ifdef HAVE_LUTIMES
1099 lutimes(p->name, times);
1100 #else
1101 utimes(p->name, times);
1102 #endif
1103 #else
1104 /* utime() is more portable, but less precise. */
1105 struct utimbuf times;
1106 times.modtime = p->mtime;
1107 times.actime = p->atime;
1109 utime(p->name, &times);
1110 #endif
1112 if (p->fixup & TODO_MODE_BASE)
1113 chmod(p->name, p->mode);
1115 if (p->fixup & TODO_FFLAGS)
1116 set_fflags_platform(a, -1, p->name,
1117 p->mode, p->fflags_set, 0);
1119 next = p->next;
1120 free(p->name);
1121 free(p);
1122 p = next;
1124 a->fixup_list = NULL;
1125 return (ret);
1128 static int
1129 _archive_write_finish(struct archive *_a)
1131 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1132 int ret;
1133 ret = _archive_write_close(&a->archive);
1134 if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
1135 (a->cleanup_gid)(a->lookup_gid_data);
1136 if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
1137 (a->cleanup_uid)(a->lookup_uid_data);
1138 archive_string_free(&a->_name_data);
1139 archive_string_free(&a->archive.error_string);
1140 archive_string_free(&a->path_safe);
1141 free(a);
1142 return (ret);
1146 * Simple O(n log n) merge sort to order the fixup list. In
1147 * particular, we want to restore dir timestamps depth-first.
1149 static struct fixup_entry *
1150 sort_dir_list(struct fixup_entry *p)
1152 struct fixup_entry *a, *b, *t;
1154 if (p == NULL)
1155 return (NULL);
1156 /* A one-item list is already sorted. */
1157 if (p->next == NULL)
1158 return (p);
1160 /* Step 1: split the list. */
1161 t = p;
1162 a = p->next->next;
1163 while (a != NULL) {
1164 /* Step a twice, t once. */
1165 a = a->next;
1166 if (a != NULL)
1167 a = a->next;
1168 t = t->next;
1170 /* Now, t is at the mid-point, so break the list here. */
1171 b = t->next;
1172 t->next = NULL;
1173 a = p;
1175 /* Step 2: Recursively sort the two sub-lists. */
1176 a = sort_dir_list(a);
1177 b = sort_dir_list(b);
1179 /* Step 3: Merge the returned lists. */
1180 /* Pick the first element for the merged list. */
1181 if (strcmp(a->name, b->name) > 0) {
1182 t = p = a;
1183 a = a->next;
1184 } else {
1185 t = p = b;
1186 b = b->next;
1189 /* Always put the later element on the list first. */
1190 while (a != NULL && b != NULL) {
1191 if (strcmp(a->name, b->name) > 0) {
1192 t->next = a;
1193 a = a->next;
1194 } else {
1195 t->next = b;
1196 b = b->next;
1198 t = t->next;
1201 /* Only one list is non-empty, so just splice it on. */
1202 if (a != NULL)
1203 t->next = a;
1204 if (b != NULL)
1205 t->next = b;
1207 return (p);
1211 * Returns a new, initialized fixup entry.
1213 * TODO: Reduce the memory requirements for this list by using a tree
1214 * structure rather than a simple list of names.
1216 static struct fixup_entry *
1217 new_fixup(struct archive_write_disk *a, const char *pathname)
1219 struct fixup_entry *fe;
1221 fe = (struct fixup_entry *)malloc(sizeof(struct fixup_entry));
1222 if (fe == NULL)
1223 return (NULL);
1224 fe->next = a->fixup_list;
1225 a->fixup_list = fe;
1226 fe->fixup = 0;
1227 fe->name = strdup(pathname);
1228 return (fe);
1232 * Returns a fixup structure for the current entry.
1234 static struct fixup_entry *
1235 current_fixup(struct archive_write_disk *a, const char *pathname)
1237 if (a->current_fixup == NULL)
1238 a->current_fixup = new_fixup(a, pathname);
1239 return (a->current_fixup);
1242 /* TODO: Make this work. */
1244 * TODO: The deep-directory support bypasses this; disable deep directory
1245 * support if we're doing symlink checks.
1248 * TODO: Someday, integrate this with the deep dir support; they both
1249 * scan the path and both can be optimized by comparing against other
1250 * recent paths.
1252 static int
1253 check_symlinks(struct archive_write_disk *a)
1255 char *pn, *p;
1256 char c;
1257 int r;
1258 struct stat st;
1261 * Guard against symlink tricks. Reject any archive entry whose
1262 * destination would be altered by a symlink.
1264 /* Whatever we checked last time doesn't need to be re-checked. */
1265 pn = a->name;
1266 p = a->path_safe.s;
1267 while ((*pn != '\0') && (*p == *pn))
1268 ++p, ++pn;
1269 c = pn[0];
1270 /* Keep going until we've checked the entire name. */
1271 while (pn[0] != '\0' && (pn[0] != '/' || pn[1] != '\0')) {
1272 /* Skip the next path element. */
1273 while (*pn != '\0' && *pn != '/')
1274 ++pn;
1275 c = pn[0];
1276 pn[0] = '\0';
1277 /* Check that we haven't hit a symlink. */
1278 r = lstat(a->name, &st);
1279 if (r != 0) {
1280 /* We've hit a dir that doesn't exist; stop now. */
1281 if (errno == ENOENT)
1282 break;
1283 } else if (S_ISLNK(st.st_mode)) {
1284 if (c == '\0') {
1286 * Last element is symlink; remove it
1287 * so we can overwrite it with the
1288 * item being extracted.
1290 if (unlink(a->name)) {
1291 archive_set_error(&a->archive, errno,
1292 "Could not remove symlink %s",
1293 a->name);
1294 pn[0] = c;
1295 return (ARCHIVE_WARN);
1297 a->pst = NULL;
1299 * Even if we did remove it, a warning
1300 * is in order. The warning is silly,
1301 * though, if we're just replacing one
1302 * symlink with another symlink.
1304 if (!S_ISLNK(a->mode)) {
1305 archive_set_error(&a->archive, 0,
1306 "Removing symlink %s",
1307 a->name);
1309 /* Symlink gone. No more problem! */
1310 pn[0] = c;
1311 return (0);
1312 } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
1313 /* User asked us to remove problems. */
1314 if (unlink(a->name) != 0) {
1315 archive_set_error(&a->archive, 0,
1316 "Cannot remove intervening symlink %s",
1317 a->name);
1318 pn[0] = c;
1319 return (ARCHIVE_WARN);
1321 a->pst = NULL;
1322 } else {
1323 archive_set_error(&a->archive, 0,
1324 "Cannot extract through symlink %s",
1325 a->name);
1326 pn[0] = c;
1327 return (ARCHIVE_WARN);
1331 pn[0] = c;
1332 /* We've checked and/or cleaned the whole path, so remember it. */
1333 archive_strcpy(&a->path_safe, a->name);
1334 return (ARCHIVE_OK);
1338 * Canonicalize the pathname. In particular, this strips duplicate
1339 * '/' characters, '.' elements, and trailing '/'. It also raises an
1340 * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
1341 * set) any '..' in the path.
1343 static int
1344 cleanup_pathname(struct archive_write_disk *a)
1346 char *dest, *src;
1347 char separator = '\0';
1348 int lastdotdot = 0; /* True if last elt copied was '..' */
1350 dest = src = a->name;
1351 if (*src == '\0') {
1352 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1353 "Invalid empty pathname");
1354 return (ARCHIVE_WARN);
1357 /* Skip leading '/'. */
1358 if (*src == '/')
1359 separator = *src++;
1361 /* Scan the pathname one element at a time. */
1362 for (;;) {
1363 /* src points to first char after '/' */
1364 if (src[0] == '\0') {
1365 break;
1366 } else if (src[0] == '/') {
1367 /* Found '//', ignore second one. */
1368 src++;
1369 continue;
1370 } else if (src[0] == '.') {
1371 if (src[1] == '\0') {
1372 /* Ignore trailing '.' */
1373 break;
1374 } else if (src[1] == '/') {
1375 /* Skip './'. */
1376 src += 2;
1377 continue;
1378 } else if (src[1] == '.') {
1379 if (src[2] == '/' || src[2] == '\0') {
1380 /* Conditionally warn about '..' */
1381 if (a->flags & ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
1382 archive_set_error(&a->archive,
1383 ARCHIVE_ERRNO_MISC,
1384 "Path contains '..'");
1385 return (ARCHIVE_WARN);
1387 lastdotdot = 1;
1388 } else
1389 lastdotdot = 0;
1391 * Note: Under no circumstances do we
1392 * remove '..' elements. In
1393 * particular, restoring
1394 * '/foo/../bar/' should create the
1395 * 'foo' dir as a side-effect.
1397 } else
1398 lastdotdot = 0;
1399 } else
1400 lastdotdot = 0;
1402 /* Copy current element, including leading '/'. */
1403 if (separator)
1404 *dest++ = '/';
1405 while (*src != '\0' && *src != '/') {
1406 *dest++ = *src++;
1409 if (*src == '\0')
1410 break;
1412 /* Skip '/' separator. */
1413 separator = *src++;
1416 * We've just copied zero or more path elements, not including the
1417 * final '/'.
1419 if (lastdotdot) {
1420 /* Trailing '..' is always wrong. */
1421 archive_set_error(&a->archive,
1422 ARCHIVE_ERRNO_MISC,
1423 "Path contains trailing '..'");
1424 return (ARCHIVE_WARN);
1426 if (dest == a->name) {
1428 * Nothing got copied. The path must have been something
1429 * like '.' or '/' or './' or '/././././/./'.
1431 if (separator)
1432 *dest++ = '/';
1433 else
1434 *dest++ = '.';
1436 /* Terminate the result. */
1437 *dest = '\0';
1438 return (ARCHIVE_OK);
1442 * Create the parent directory of the specified path, assuming path
1443 * is already in mutable storage.
1445 static int
1446 create_parent_dir(struct archive_write_disk *a, char *path)
1448 char *slash;
1449 int r;
1451 /* Remove tail element to obtain parent name. */
1452 slash = strrchr(path, '/');
1453 if (slash == NULL)
1454 return (ARCHIVE_OK);
1455 *slash = '\0';
1456 r = create_dir(a, path);
1457 *slash = '/';
1458 return (r);
1462 * Create the specified dir, recursing to create parents as necessary.
1464 * Returns ARCHIVE_OK if the path exists when we're done here.
1465 * Otherwise, returns ARCHIVE_WARN.
1466 * Assumes path is in mutable storage; path is unchanged on exit.
1468 static int
1469 create_dir(struct archive_write_disk *a, char *path)
1471 struct stat st;
1472 struct fixup_entry *le;
1473 char *slash, *base;
1474 mode_t mode_final, mode;
1475 int r;
1477 r = ARCHIVE_OK;
1479 /* Check for special names and just skip them. */
1480 slash = strrchr(path, '/');
1481 if (slash == NULL)
1482 base = path;
1483 else
1484 base = slash + 1;
1486 if (base[0] == '\0' ||
1487 (base[0] == '.' && base[1] == '\0') ||
1488 (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
1489 /* Don't bother trying to create null path, '.', or '..'. */
1490 if (slash != NULL) {
1491 *slash = '\0';
1492 r = create_dir(a, path);
1493 *slash = '/';
1494 return (r);
1496 return (ARCHIVE_OK);
1500 * Yes, this should be stat() and not lstat(). Using lstat()
1501 * here loses the ability to extract through symlinks. Also note
1502 * that this should not use the a->st cache.
1504 if (stat(path, &st) == 0) {
1505 if (S_ISDIR(st.st_mode))
1506 return (ARCHIVE_OK);
1507 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
1508 archive_set_error(&a->archive, EEXIST,
1509 "Can't create directory '%s'", path);
1510 return (ARCHIVE_WARN);
1512 if (unlink(path) != 0) {
1513 archive_set_error(&a->archive, errno,
1514 "Can't create directory '%s': "
1515 "Conflicting file cannot be removed");
1516 return (ARCHIVE_WARN);
1518 } else if (errno != ENOENT && errno != ENOTDIR) {
1519 /* Stat failed? */
1520 archive_set_error(&a->archive, errno, "Can't test directory '%s'", path);
1521 return (ARCHIVE_WARN);
1522 } else if (slash != NULL) {
1523 *slash = '\0';
1524 r = create_dir(a, path);
1525 *slash = '/';
1526 if (r != ARCHIVE_OK)
1527 return (r);
1531 * Mode we want for the final restored directory. Per POSIX,
1532 * implicitly-created dirs must be created obeying the umask.
1533 * There's no mention whether this is different for privileged
1534 * restores (which the rest of this code handles by pretending
1535 * umask=0). I've chosen here to always obey the user's umask for
1536 * implicit dirs, even if _EXTRACT_PERM was specified.
1538 mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
1539 /* Mode we want on disk during the restore process. */
1540 mode = mode_final;
1541 mode |= MINIMUM_DIR_MODE;
1542 mode &= MAXIMUM_DIR_MODE;
1543 if (mkdir(path, mode) == 0) {
1544 if (mode != mode_final) {
1545 le = new_fixup(a, path);
1546 le->fixup |=TODO_MODE_BASE;
1547 le->mode = mode_final;
1549 return (ARCHIVE_OK);
1553 * Without the following check, a/b/../b/c/d fails at the
1554 * second visit to 'b', so 'd' can't be created. Note that we
1555 * don't add it to the fixup list here, as it's already been
1556 * added.
1558 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1559 return (ARCHIVE_OK);
1561 archive_set_error(&a->archive, errno, "Failed to create dir '%s'", path);
1562 return (ARCHIVE_WARN);
1566 * Note: Although we can skip setting the user id if the desired user
1567 * id matches the current user, we cannot skip setting the group, as
1568 * many systems set the gid bit based on the containing directory. So
1569 * we have to perform a chown syscall if we want to restore the SGID
1570 * bit. (The alternative is to stat() and then possibly chown(); it's
1571 * more efficient to skip the stat() and just always chown().) Note
1572 * that a successful chown() here clears the TODO_SGID_CHECK bit, which
1573 * allows set_mode to skip the stat() check for the GID.
1575 static int
1576 set_ownership(struct archive_write_disk *a)
1578 /* If we know we can't change it, don't bother trying. */
1579 if (a->user_uid != 0 && a->user_uid != a->uid) {
1580 archive_set_error(&a->archive, errno,
1581 "Can't set UID=%d", a->uid);
1582 return (ARCHIVE_WARN);
1585 #ifdef HAVE_FCHOWN
1586 /* If we have an fd, we can avoid a race. */
1587 if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0) {
1588 /* We've set owner and know uid/gid are correct. */
1589 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
1590 return (ARCHIVE_OK);
1592 #endif
1594 /* We prefer lchown() but will use chown() if that's all we have. */
1595 /* Of course, if we have neither, this will always fail. */
1596 #ifdef HAVE_LCHOWN
1597 if (lchown(a->name, a->uid, a->gid) == 0) {
1598 /* We've set owner and know uid/gid are correct. */
1599 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
1600 return (ARCHIVE_OK);
1602 #elif HAVE_CHOWN
1603 if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0) {
1604 /* We've set owner and know uid/gid are correct. */
1605 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
1606 return (ARCHIVE_OK);
1608 #endif
1610 archive_set_error(&a->archive, errno,
1611 "Can't set user=%d/group=%d for %s", a->uid, a->gid,
1612 a->name);
1613 return (ARCHIVE_WARN);
1616 #ifdef HAVE_UTIMES
1618 * The utimes()-family functions provide high resolution and
1619 * a way to set time on an fd or a symlink. We prefer them
1620 * when they're available.
1622 static int
1623 set_time(struct archive_write_disk *a)
1625 struct timeval times[2];
1627 times[1].tv_sec = archive_entry_mtime(a->entry);
1628 times[1].tv_usec = archive_entry_mtime_nsec(a->entry) / 1000;
1630 times[0].tv_sec = archive_entry_atime(a->entry);
1631 times[0].tv_usec = archive_entry_atime_nsec(a->entry) / 1000;
1633 #ifdef HAVE_FUTIMES
1634 if (a->fd >= 0 && futimes(a->fd, times) == 0) {
1635 return (ARCHIVE_OK);
1637 #endif
1639 #ifdef HAVE_LUTIMES
1640 if (lutimes(a->name, times) != 0)
1641 #else
1642 if (!S_ISLNK(a->mode) && utimes(a->name, times) != 0)
1643 #endif
1645 archive_set_error(&a->archive, errno, "Can't update time for %s",
1646 a->name);
1647 return (ARCHIVE_WARN);
1651 * Note: POSIX does not provide a portable way to restore ctime.
1652 * (Apart from resetting the system clock, which is distasteful.)
1653 * So, any restoration of ctime will necessarily be OS-specific.
1656 /* XXX TODO: Can FreeBSD restore ctime? XXX */
1657 return (ARCHIVE_OK);
1659 #elif defined(HAVE_UTIME)
1661 * utime() is an older, more standard interface that we'll use
1662 * if utimes() isn't available.
1664 static int
1665 set_time(struct archive_write_disk *a)
1667 struct utimbuf times;
1669 times.modtime = archive_entry_mtime(a->entry);
1670 times.actime = archive_entry_atime(a->entry);
1671 if (!S_ISLNK(a->mode) && utime(a->name, &times) != 0) {
1672 archive_set_error(&a->archive, errno,
1673 "Can't update time for %s", a->name);
1674 return (ARCHIVE_WARN);
1676 return (ARCHIVE_OK);
1678 #else
1679 /* This platform doesn't give us a way to restore the time. */
1680 static int
1681 set_time(struct archive_write_disk *a)
1683 (void)a; /* UNUSED */
1684 archive_set_error(&a->archive, errno,
1685 "Can't update time for %s", a->name);
1686 return (ARCHIVE_WARN);
1688 #endif
1691 static int
1692 set_mode(struct archive_write_disk *a, int mode)
1694 int r = ARCHIVE_OK;
1695 mode &= 07777; /* Strip off file type bits. */
1697 if (a->todo & TODO_SGID_CHECK) {
1699 * If we don't know the GID is right, we must stat()
1700 * to verify it. We can't just check the GID of this
1701 * process, since systems sometimes set GID from
1702 * the enclosing dir or based on ACLs.
1704 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
1705 return (r);
1706 if (a->pst->st_gid != a->gid) {
1707 mode &= ~ S_ISGID;
1708 if (a->flags & ARCHIVE_EXTRACT_OWNER) {
1710 * This is only an error if you
1711 * requested owner restore. If you
1712 * didn't, we'll try to restore
1713 * sgid/suid, but won't consider it a
1714 * problem if we can't.
1716 archive_set_error(&a->archive, -1,
1717 "Can't restore SGID bit");
1718 r = ARCHIVE_WARN;
1721 /* While we're here, double-check the UID. */
1722 if (a->pst->st_uid != a->uid
1723 && (a->todo & TODO_SUID)) {
1724 mode &= ~ S_ISUID;
1725 if (a->flags & ARCHIVE_EXTRACT_OWNER) {
1726 archive_set_error(&a->archive, -1,
1727 "Can't restore SUID bit");
1728 r = ARCHIVE_WARN;
1731 a->todo &= ~TODO_SGID_CHECK;
1732 a->todo &= ~TODO_SUID_CHECK;
1733 } else if (a->todo & TODO_SUID_CHECK) {
1735 * If we don't know the UID is right, we can just check
1736 * the user, since all systems set the file UID from
1737 * the process UID.
1739 if (a->user_uid != a->uid) {
1740 mode &= ~ S_ISUID;
1741 if (a->flags & ARCHIVE_EXTRACT_OWNER) {
1742 archive_set_error(&a->archive, -1,
1743 "Can't make file SUID");
1744 r = ARCHIVE_WARN;
1747 a->todo &= ~TODO_SUID_CHECK;
1750 if (S_ISLNK(a->mode)) {
1751 #ifdef HAVE_LCHMOD
1753 * If this is a symlink, use lchmod(). If the
1754 * platform doesn't support lchmod(), just skip it. A
1755 * platform that doesn't provide a way to set
1756 * permissions on symlinks probably ignores
1757 * permissions on symlinks, so a failure here has no
1758 * impact.
1760 if (lchmod(a->name, mode) != 0) {
1761 archive_set_error(&a->archive, errno,
1762 "Can't set permissions to 0%o", (int)mode);
1763 r = ARCHIVE_WARN;
1765 #endif
1766 } else if (!S_ISDIR(a->mode)) {
1768 * If it's not a symlink and not a dir, then use
1769 * fchmod() or chmod(), depending on whether we have
1770 * an fd. Dirs get their perms set during the
1771 * post-extract fixup, which is handled elsewhere.
1773 #ifdef HAVE_FCHMOD
1774 if (a->fd >= 0) {
1775 if (fchmod(a->fd, mode) != 0) {
1776 archive_set_error(&a->archive, errno,
1777 "Can't set permissions to 0%o", (int)mode);
1778 r = ARCHIVE_WARN;
1780 } else
1781 #endif
1782 /* If this platform lacks fchmod(), then
1783 * we'll just use chmod(). */
1784 if (chmod(a->name, mode) != 0) {
1785 archive_set_error(&a->archive, errno,
1786 "Can't set permissions to 0%o", (int)mode);
1787 r = ARCHIVE_WARN;
1790 return (r);
1793 static int
1794 set_fflags(struct archive_write_disk *a)
1796 struct fixup_entry *le;
1797 unsigned long set, clear;
1798 int r;
1799 int critical_flags;
1800 mode_t mode = archive_entry_mode(a->entry);
1803 * Make 'critical_flags' hold all file flags that can't be
1804 * immediately restored. For example, on BSD systems,
1805 * SF_IMMUTABLE prevents hardlinks from being created, so
1806 * should not be set until after any hardlinks are created. To
1807 * preserve some semblance of portability, this uses #ifdef
1808 * extensively. Ugly, but it works.
1810 * Yes, Virginia, this does create a security race. It's mitigated
1811 * somewhat by the practice of creating dirs 0700 until the extract
1812 * is done, but it would be nice if we could do more than that.
1813 * People restoring critical file systems should be wary of
1814 * other programs that might try to muck with files as they're
1815 * being restored.
1817 /* Hopefully, the compiler will optimize this mess into a constant. */
1818 critical_flags = 0;
1819 #ifdef SF_IMMUTABLE
1820 critical_flags |= SF_IMMUTABLE;
1821 #endif
1822 #ifdef UF_IMMUTABLE
1823 critical_flags |= UF_IMMUTABLE;
1824 #endif
1825 #ifdef SF_APPEND
1826 critical_flags |= SF_APPEND;
1827 #endif
1828 #ifdef UF_APPEND
1829 critical_flags |= UF_APPEND;
1830 #endif
1831 #ifdef EXT2_APPEND_FL
1832 critical_flags |= EXT2_APPEND_FL;
1833 #endif
1834 #ifdef EXT2_IMMUTABLE_FL
1835 critical_flags |= EXT2_IMMUTABLE_FL;
1836 #endif
1838 if (a->todo & TODO_FFLAGS) {
1839 archive_entry_fflags(a->entry, &set, &clear);
1842 * The first test encourages the compiler to eliminate
1843 * all of this if it's not necessary.
1845 if ((critical_flags != 0) && (set & critical_flags)) {
1846 le = current_fixup(a, a->name);
1847 le->fixup |= TODO_FFLAGS;
1848 le->fflags_set = set;
1849 /* Store the mode if it's not already there. */
1850 if ((le->fixup & TODO_MODE) == 0)
1851 le->mode = mode;
1852 } else {
1853 r = set_fflags_platform(a, a->fd,
1854 a->name, mode, set, clear);
1855 if (r != ARCHIVE_OK)
1856 return (r);
1859 return (ARCHIVE_OK);
1863 #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && !defined(__linux)
1864 static int
1865 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
1866 mode_t mode, unsigned long set, unsigned long clear)
1868 int r;
1870 (void)mode; /* UNUSED */
1871 if (set == 0 && clear == 0)
1872 return (ARCHIVE_OK);
1875 * XXX Is the stat here really necessary? Or can I just use
1876 * the 'set' flags directly? In particular, I'm not sure
1877 * about the correct approach if we're overwriting an existing
1878 * file that already has flags on it. XXX
1880 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
1881 return (r);
1883 a->st.st_flags &= ~clear;
1884 a->st.st_flags |= set;
1885 #ifdef HAVE_FCHFLAGS
1886 /* If platform has fchflags() and we were given an fd, use it. */
1887 if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0)
1888 return (ARCHIVE_OK);
1889 #endif
1891 * If we can't use the fd to set the flags, we'll use the
1892 * pathname to set flags. We prefer lchflags() but will use
1893 * chflags() if we must.
1895 #ifdef HAVE_LCHFLAGS
1896 if (lchflags(name, a->st.st_flags) == 0)
1897 return (ARCHIVE_OK);
1898 #elif defined(HAVE_CHFLAGS)
1899 if (S_ISLNK(a->st.st_mode)) {
1900 archive_set_error(&a->archive, errno,
1901 "Can't set file flags on symlink.");
1902 return (ARCHIVE_WARN);
1904 if (chflags(name, a->st.st_flags) == 0)
1905 return (ARCHIVE_OK);
1906 #endif
1907 archive_set_error(&a->archive, errno,
1908 "Failed to set file flags");
1909 return (ARCHIVE_WARN);
1912 #elif defined(__linux) && defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS)
1915 * Linux has flags too, but uses ioctl() to access them instead of
1916 * having a separate chflags() system call.
1918 static int
1919 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
1920 mode_t mode, unsigned long set, unsigned long clear)
1922 int ret;
1923 int myfd = fd;
1924 unsigned long newflags, oldflags;
1925 unsigned long sf_mask = 0;
1927 if (set == 0 && clear == 0)
1928 return (ARCHIVE_OK);
1929 /* Only regular files and dirs can have flags. */
1930 if (!S_ISREG(mode) && !S_ISDIR(mode))
1931 return (ARCHIVE_OK);
1933 /* If we weren't given an fd, open it ourselves. */
1934 if (myfd < 0)
1935 myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY);
1936 if (myfd < 0)
1937 return (ARCHIVE_OK);
1940 * Linux has no define for the flags that are only settable by
1941 * the root user. This code may seem a little complex, but
1942 * there seem to be some Linux systems that lack these
1943 * defines. (?) The code below degrades reasonably gracefully
1944 * if sf_mask is incomplete.
1946 #ifdef EXT2_IMMUTABLE_FL
1947 sf_mask |= EXT2_IMMUTABLE_FL;
1948 #endif
1949 #ifdef EXT2_APPEND_FL
1950 sf_mask |= EXT2_APPEND_FL;
1951 #endif
1953 * XXX As above, this would be way simpler if we didn't have
1954 * to read the current flags from disk. XXX
1956 ret = ARCHIVE_OK;
1957 /* Try setting the flags as given. */
1958 if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
1959 newflags = (oldflags & ~clear) | set;
1960 if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
1961 goto cleanup;
1962 if (errno != EPERM)
1963 goto fail;
1965 /* If we couldn't set all the flags, try again with a subset. */
1966 if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
1967 newflags &= ~sf_mask;
1968 oldflags &= sf_mask;
1969 newflags |= oldflags;
1970 if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
1971 goto cleanup;
1973 /* We couldn't set the flags, so report the failure. */
1974 fail:
1975 archive_set_error(&a->archive, errno,
1976 "Failed to set file flags");
1977 ret = ARCHIVE_WARN;
1978 cleanup:
1979 if (fd < 0)
1980 close(myfd);
1981 return (ret);
1984 #else /* Not HAVE_CHFLAGS && Not __linux */
1987 * Of course, some systems have neither BSD chflags() nor Linux' flags
1988 * support through ioctl().
1990 static int
1991 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
1992 mode_t mode, unsigned long set, unsigned long clear)
1994 (void)a; /* UNUSED */
1995 (void)fd; /* UNUSED */
1996 (void)name; /* UNUSED */
1997 (void)mode; /* UNUSED */
1998 (void)set; /* UNUSED */
1999 (void)clear; /* UNUSED */
2000 return (ARCHIVE_OK);
2003 #endif /* __linux */
2005 #ifndef HAVE_POSIX_ACL
2006 /* Default empty function body to satisfy mainline code. */
2007 static int
2008 set_acls(struct archive_write_disk *a)
2010 (void)a; /* UNUSED */
2011 return (ARCHIVE_OK);
2014 #else
2017 * XXX TODO: What about ACL types other than ACCESS and DEFAULT?
2019 static int
2020 set_acls(struct archive_write_disk *a)
2022 int ret;
2024 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_ACCESS,
2025 ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
2026 if (ret != ARCHIVE_OK)
2027 return (ret);
2028 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_DEFAULT,
2029 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
2030 return (ret);
2034 static int
2035 set_acl(struct archive_write_disk *a, int fd, struct archive_entry *entry,
2036 acl_type_t acl_type, int ae_requested_type, const char *tname)
2038 acl_t acl;
2039 acl_entry_t acl_entry;
2040 acl_permset_t acl_permset;
2041 int ret;
2042 int ae_type, ae_permset, ae_tag, ae_id;
2043 uid_t ae_uid;
2044 gid_t ae_gid;
2045 const char *ae_name;
2046 int entries;
2047 const char *name;
2049 ret = ARCHIVE_OK;
2050 entries = archive_entry_acl_reset(entry, ae_requested_type);
2051 if (entries == 0)
2052 return (ARCHIVE_OK);
2053 acl = acl_init(entries);
2054 while (archive_entry_acl_next(entry, ae_requested_type, &ae_type,
2055 &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
2056 acl_create_entry(&acl, &acl_entry);
2058 switch (ae_tag) {
2059 case ARCHIVE_ENTRY_ACL_USER:
2060 acl_set_tag_type(acl_entry, ACL_USER);
2061 ae_uid = a->lookup_uid(a->lookup_uid_data,
2062 ae_name, ae_id);
2063 acl_set_qualifier(acl_entry, &ae_uid);
2064 break;
2065 case ARCHIVE_ENTRY_ACL_GROUP:
2066 acl_set_tag_type(acl_entry, ACL_GROUP);
2067 ae_gid = a->lookup_gid(a->lookup_gid_data,
2068 ae_name, ae_id);
2069 acl_set_qualifier(acl_entry, &ae_gid);
2070 break;
2071 case ARCHIVE_ENTRY_ACL_USER_OBJ:
2072 acl_set_tag_type(acl_entry, ACL_USER_OBJ);
2073 break;
2074 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
2075 acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
2076 break;
2077 case ARCHIVE_ENTRY_ACL_MASK:
2078 acl_set_tag_type(acl_entry, ACL_MASK);
2079 break;
2080 case ARCHIVE_ENTRY_ACL_OTHER:
2081 acl_set_tag_type(acl_entry, ACL_OTHER);
2082 break;
2083 default:
2084 /* XXX */
2085 break;
2088 acl_get_permset(acl_entry, &acl_permset);
2089 acl_clear_perms(acl_permset);
2090 if (ae_permset & ARCHIVE_ENTRY_ACL_EXECUTE)
2091 acl_add_perm(acl_permset, ACL_EXECUTE);
2092 if (ae_permset & ARCHIVE_ENTRY_ACL_WRITE)
2093 acl_add_perm(acl_permset, ACL_WRITE);
2094 if (ae_permset & ARCHIVE_ENTRY_ACL_READ)
2095 acl_add_perm(acl_permset, ACL_READ);
2098 name = archive_entry_pathname(entry);
2100 /* Try restoring the ACL through 'fd' if we can. */
2101 #if HAVE_ACL_SET_FD
2102 if (fd >= 0 && acl_type == ACL_TYPE_ACCESS && acl_set_fd(fd, acl) == 0)
2103 ret = ARCHIVE_OK;
2104 else
2105 #else
2106 #if HAVE_ACL_SET_FD_NP
2107 if (fd >= 0 && acl_set_fd_np(fd, acl, acl_type) == 0)
2108 ret = ARCHIVE_OK;
2109 else
2110 #endif
2111 #endif
2112 if (acl_set_file(name, acl_type, acl) != 0) {
2113 archive_set_error(&a->archive, errno, "Failed to set %s acl", tname);
2114 ret = ARCHIVE_WARN;
2116 acl_free(acl);
2117 return (ret);
2119 #endif
2121 #if HAVE_LSETXATTR
2123 * Restore extended attributes - Linux implementation
2125 static int
2126 set_xattrs(struct archive_write_disk *a)
2128 struct archive_entry *entry = a->entry;
2129 static int warning_done = 0;
2130 int ret = ARCHIVE_OK;
2131 int i = archive_entry_xattr_reset(entry);
2133 while (i--) {
2134 const char *name;
2135 const void *value;
2136 size_t size;
2137 archive_entry_xattr_next(entry, &name, &value, &size);
2138 if (name != NULL &&
2139 strncmp(name, "xfsroot.", 8) != 0 &&
2140 strncmp(name, "system.", 7) != 0) {
2141 int e;
2142 #if HAVE_FSETXATTR
2143 if (a->fd >= 0)
2144 e = fsetxattr(a->fd, name, value, size, 0);
2145 else
2146 #endif
2148 e = lsetxattr(archive_entry_pathname(entry),
2149 name, value, size, 0);
2151 if (e == -1) {
2152 if (errno == ENOTSUP) {
2153 if (!warning_done) {
2154 warning_done = 1;
2155 archive_set_error(&a->archive, errno,
2156 "Cannot restore extended "
2157 "attributes on this file "
2158 "system");
2160 } else
2161 archive_set_error(&a->archive, errno,
2162 "Failed to set extended attribute");
2163 ret = ARCHIVE_WARN;
2165 } else {
2166 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2167 "Invalid extended attribute encountered");
2168 ret = ARCHIVE_WARN;
2171 return (ret);
2173 #else
2175 * Restore extended attributes - stub implementation for unsupported systems
2177 static int
2178 set_xattrs(struct archive_write_disk *a)
2180 static int warning_done = 0;
2182 /* If there aren't any extended attributes, then it's okay not
2183 * to extract them, otherwise, issue a single warning. */
2184 if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
2185 warning_done = 1;
2186 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2187 "Cannot restore extended attributes on this system");
2188 return (ARCHIVE_WARN);
2190 /* Warning was already emitted; suppress further warnings. */
2191 return (ARCHIVE_OK);
2193 #endif
2197 * Trivial implementations of gid/uid lookup functions.
2198 * These are normally overridden by the client, but these stub
2199 * versions ensure that we always have something that works.
2201 static gid_t
2202 trivial_lookup_gid(void *private_data, const char *gname, gid_t gid)
2204 (void)private_data; /* UNUSED */
2205 (void)gname; /* UNUSED */
2206 return (gid);
2209 static uid_t
2210 trivial_lookup_uid(void *private_data, const char *uname, uid_t uid)
2212 (void)private_data; /* UNUSED */
2213 (void)uname; /* UNUSED */
2214 return (uid);
2218 * Test if file on disk is older than entry.
2220 static int
2221 older(struct stat *st, struct archive_entry *entry)
2223 /* First, test the seconds and return if we have a definite answer. */
2224 /* Definitely older. */
2225 if (st->st_mtime < archive_entry_mtime(entry))
2226 return (1);
2227 /* Definitely younger. */
2228 if (st->st_mtime > archive_entry_mtime(entry))
2229 return (0);
2230 /* If this platform supports fractional seconds, try those. */
2231 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
2232 /* Definitely older. */
2233 if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry))
2234 return (1);
2235 /* Definitely younger. */
2236 if (st->st_mtimespec.tv_nsec > archive_entry_mtime_nsec(entry))
2237 return (0);
2238 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
2239 /* Definitely older. */
2240 if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry))
2241 return (1);
2242 /* Definitely older. */
2243 if (st->st_mtim.tv_nsec > archive_entry_mtime_nsec(entry))
2244 return (0);
2245 #else
2246 /* This system doesn't have high-res timestamps. */
2247 #endif
2248 /* Same age, so not older. */
2249 return (0);