2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
36 #ifdef HAVE_SYS_EXTATTR_H
37 #include <sys/extattr.h>
39 #ifdef HAVE_ATTR_XATTR_H
40 #include <attr/xattr.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 #include <sys/ioctl.h>
45 #ifdef HAVE_SYS_STAT_H
48 #ifdef HAVE_SYS_TIME_H
51 #ifdef HAVE_SYS_UTIME_H
52 #include <sys/utime.h>
63 #ifdef HAVE_LINUX_FS_H
64 #include <linux/fs.h> /* for Linux file flags */
67 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
68 * As the include guards don't agree, the order of include is important.
70 #ifdef HAVE_LINUX_EXT2_FS_H
71 #include <linux/ext2_fs.h> /* for Linux file flags */
73 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
74 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
97 #include "archive_string.h"
98 #include "archive_entry.h"
99 #include "archive_private.h"
106 struct fixup_entry *next;
111 unsigned long atime_nanos;
112 unsigned long birthtime_nanos;
113 unsigned long mtime_nanos;
114 unsigned long fflags_set;
115 int fixup; /* bitmask of what needs fixing */
120 * We use a bitmask to track which operations remain to be done for
121 * this file. In particular, this helps us avoid unnecessary
122 * operations when it's possible to take care of one step as a
123 * side-effect of another. For example, mkdir() can specify the mode
124 * for the newly-created object but symlink() cannot. This means we
125 * can skip chmod() if mkdir() succeeded, but we must explicitly
126 * chmod() if we're trying to create a directory that already exists
127 * (mkdir() failed) or if we're restoring a symlink. Similarly, we
128 * need to verify UID/GID before trying to restore SUID/SGID bits;
129 * that verification can occur explicitly through a stat() call or
130 * implicitly because of a successful chown() call.
132 #define TODO_MODE_FORCE 0x40000000
133 #define TODO_MODE_BASE 0x20000000
134 #define TODO_SUID 0x10000000
135 #define TODO_SUID_CHECK 0x08000000
136 #define TODO_SGID 0x04000000
137 #define TODO_SGID_CHECK 0x02000000
138 #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
139 #define TODO_TIMES ARCHIVE_EXTRACT_TIME
140 #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
141 #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
142 #define TODO_ACLS ARCHIVE_EXTRACT_ACL
143 #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
145 struct archive_write_disk {
146 struct archive archive;
149 struct fixup_entry *fixup_list;
150 struct fixup_entry *current_fixup;
156 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid);
157 void (*cleanup_gid)(void *private);
158 void *lookup_gid_data;
159 uid_t (*lookup_uid)(void *private, const char *gname, gid_t gid);
160 void (*cleanup_uid)(void *private);
161 void *lookup_uid_data;
164 * Full path of last file to satisfy symlink checks.
166 struct archive_string path_safe;
169 * Cached stat data from disk for the current entry.
170 * If this is valid, pst points to st. Otherwise,
176 /* Information about the object being restored right now. */
177 struct archive_entry *entry; /* Entry being extracted. */
178 char *name; /* Name of entry, possibly edited. */
179 struct archive_string _name_data; /* backing store for 'name' */
180 /* Tasks remaining for this object. */
182 /* Tasks deferred until end-of-archive. */
184 /* Options requested by the client. */
186 /* Handle for the file we're restoring. */
188 /* Current offset for writing data to the file. */
190 /* Last offset actually written to disk. */
192 /* Maximum size of file, -1 if unknown. */
194 /* Dir we were in before this restore; only for deep paths. */
196 /* Mode we should use for this entry; affected by _PERM and umask. */
198 /* UID/GID to use in restoring this entry. */
204 * Default mode for dirs created automatically (will be modified by umask).
205 * Note that POSIX specifies 0777 for implicity-created dirs, "modified
206 * by the process' file creation mask."
208 #define DEFAULT_DIR_MODE 0777
210 * Dir modes are restored in two steps: During the extraction, the permissions
211 * in the archive are modified to match the following limits. During
212 * the post-extract fixup pass, the permissions from the archive are
215 #define MINIMUM_DIR_MODE 0700
216 #define MAXIMUM_DIR_MODE 0775
218 static int check_symlinks(struct archive_write_disk *);
219 static int create_filesystem_object(struct archive_write_disk *);
220 static struct fixup_entry *current_fixup(struct archive_write_disk *, const char *pathname);
222 static void edit_deep_directories(struct archive_write_disk *ad);
224 static int cleanup_pathname(struct archive_write_disk *);
225 static int create_dir(struct archive_write_disk *, char *);
226 static int create_parent_dir(struct archive_write_disk *, char *);
227 static int older(struct stat *, struct archive_entry *);
228 static int restore_entry(struct archive_write_disk *);
229 #ifdef HAVE_POSIX_ACL
230 static int set_acl(struct archive_write_disk *, int fd, struct archive_entry *,
231 acl_type_t, int archive_entry_acl_type, const char *tn);
233 static int set_acls(struct archive_write_disk *);
234 static int set_xattrs(struct archive_write_disk *);
235 static int set_fflags(struct archive_write_disk *);
236 static int set_fflags_platform(struct archive_write_disk *, int fd,
237 const char *name, mode_t mode,
238 unsigned long fflags_set, unsigned long fflags_clear);
239 static int set_ownership(struct archive_write_disk *);
240 static int set_mode(struct archive_write_disk *, int mode);
241 static int set_time(int, int, const char *, time_t, long, time_t, long);
242 static int set_times(struct archive_write_disk *);
243 static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
244 static gid_t trivial_lookup_gid(void *, const char *, gid_t);
245 static uid_t trivial_lookup_uid(void *, const char *, uid_t);
246 static ssize_t write_data_block(struct archive_write_disk *,
247 const char *, size_t);
249 static struct archive_vtable *archive_write_disk_vtable(void);
251 static int _archive_write_close(struct archive *);
252 static int _archive_write_finish(struct archive *);
253 static int _archive_write_header(struct archive *, struct archive_entry *);
254 static int _archive_write_finish_entry(struct archive *);
255 static ssize_t _archive_write_data(struct archive *, const void *, size_t);
256 static ssize_t _archive_write_data_block(struct archive *, const void *, size_t, off_t);
259 _archive_write_disk_lazy_stat(struct archive_write_disk *a)
261 if (a->pst != NULL) {
262 /* Already have stat() data available. */
266 if (a->fd >= 0 && fstat(a->fd, &a->st) == 0) {
272 * XXX At this point, symlinks should not be hit, otherwise
273 * XXX a race occured. Do we want to check explicitly for that?
275 if (lstat(a->name, &a->st) == 0) {
279 archive_set_error(&a->archive, errno, "Couldn't stat file");
280 return (ARCHIVE_WARN);
283 static struct archive_vtable *
284 archive_write_disk_vtable(void)
286 static struct archive_vtable av;
287 static int inited = 0;
290 av.archive_close = _archive_write_close;
291 av.archive_finish = _archive_write_finish;
292 av.archive_write_header = _archive_write_header;
293 av.archive_write_finish_entry = _archive_write_finish_entry;
294 av.archive_write_data = _archive_write_data;
295 av.archive_write_data_block = _archive_write_data_block;
302 archive_write_disk_set_options(struct archive *_a, int flags)
304 struct archive_write_disk *a = (struct archive_write_disk *)_a;
312 * Extract this entry to disk.
314 * TODO: Validate hardlinks. According to the standards, we're
315 * supposed to check each extracted hardlink and squawk if it refers
316 * to a file that we didn't restore. I'm not entirely convinced this
317 * is a good idea, but more importantly: Is there any way to validate
318 * hardlinks without keeping a complete list of filenames from the
319 * entire archive?? Ugh.
323 _archive_write_header(struct archive *_a, struct archive_entry *entry)
325 struct archive_write_disk *a = (struct archive_write_disk *)_a;
326 struct fixup_entry *fe;
329 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
330 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
331 "archive_write_disk_header");
332 archive_clear_error(&a->archive);
333 if (a->archive.state & ARCHIVE_STATE_DATA) {
334 r = _archive_write_finish_entry(&a->archive);
335 if (r == ARCHIVE_FATAL)
339 /* Set up for this particular entry. */
341 a->current_fixup = NULL;
344 archive_entry_free(a->entry);
347 a->entry = archive_entry_clone(entry);
351 a->uid = a->user_uid;
352 a->mode = archive_entry_mode(a->entry);
353 if (archive_entry_size_is_set(a->entry))
354 a->filesize = archive_entry_size(a->entry);
357 archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry));
358 a->name = a->_name_data.s;
359 archive_clear_error(&a->archive);
362 * Clean up the requested path. This is necessary for correct
363 * dir restores; the dir restore logic otherwise gets messed
364 * up by nonsense like "dir/.".
366 ret = cleanup_pathname(a);
367 if (ret != ARCHIVE_OK)
371 * Set the umask to zero so we get predictable mode settings.
372 * This gets done on every call to _write_header in case the
373 * user edits their umask during the extraction for some
374 * reason. This will be reset before we return. Note that we
375 * don't need to do this in _finish_entry, as the chmod(), etc,
376 * system calls don't obey umask.
378 a->user_umask = umask(0);
379 /* From here on, early exit requires "goto done" to clean up. */
381 /* Figure out what we need to do for this entry. */
382 a->todo = TODO_MODE_BASE;
383 if (a->flags & ARCHIVE_EXTRACT_PERM) {
384 a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
386 * SGID requires an extra "check" step because we
387 * cannot easily predict the GID that the system will
388 * assign. (Different systems assign GIDs to files
389 * based on a variety of criteria, including process
390 * credentials and the gid of the enclosing
391 * directory.) We can only restore the SGID bit if
392 * the file has the right GID, and we only know the
393 * GID if we either set it (see set_ownership) or if
394 * we've actually called stat() on the file after it
395 * was restored. Since there are several places at
396 * which we might verify the GID, we need a TODO bit
399 if (a->mode & S_ISGID)
400 a->todo |= TODO_SGID | TODO_SGID_CHECK;
402 * Verifying the SUID is simpler, but can still be
403 * done in multiple ways, hence the separate "check" bit.
405 if (a->mode & S_ISUID)
406 a->todo |= TODO_SUID | TODO_SUID_CHECK;
409 * User didn't request full permissions, so don't
410 * restore SUID, SGID bits and obey umask.
415 a->mode &= ~a->user_umask;
418 if (a->flags & ARCHIVE_EXTRACT_OWNER)
419 a->todo |= TODO_OWNER;
421 if (a->flags & ARCHIVE_EXTRACT_TIME)
422 a->todo |= TODO_TIMES;
423 if (a->flags & ARCHIVE_EXTRACT_ACL)
424 a->todo |= TODO_ACLS;
425 if (a->flags & ARCHIVE_EXTRACT_XATTR)
426 a->todo |= TODO_XATTR;
427 if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
428 a->todo |= TODO_FFLAGS;
429 if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
430 ret = check_symlinks(a);
431 if (ret != ARCHIVE_OK)
435 /* If path exceeds PATH_MAX, shorten the path. */
436 edit_deep_directories(a);
439 ret = restore_entry(a);
442 * On the GNU tar mailing list, some people working with new
443 * Linux filesystems observed that system xattrs used as
444 * layout hints need to be restored before the file contents
445 * are written, so this can't be done at file close.
447 if (a->todo & TODO_XATTR) {
448 int r2 = set_xattrs(a);
449 if (r2 < ret) ret = r2;
453 /* If we changed directory above, restore it here. */
454 if (a->restore_pwd >= 0) {
455 fchdir(a->restore_pwd);
456 close(a->restore_pwd);
462 * Fixup uses the unedited pathname from archive_entry_pathname(),
463 * because it is relative to the base dir and the edited path
464 * might be relative to some intermediate dir as a result of the
465 * deep restore logic.
467 if (a->deferred & TODO_MODE) {
468 fe = current_fixup(a, archive_entry_pathname(entry));
469 fe->fixup |= TODO_MODE_BASE;
473 if ((a->deferred & TODO_TIMES)
474 && (archive_entry_mtime_is_set(entry)
475 || archive_entry_atime_is_set(entry))) {
476 fe = current_fixup(a, archive_entry_pathname(entry));
477 fe->fixup |= TODO_TIMES;
478 if (archive_entry_atime_is_set(entry)) {
479 fe->atime = archive_entry_atime(entry);
480 fe->atime_nanos = archive_entry_atime_nsec(entry);
482 /* If atime is unset, use start time. */
483 fe->atime = a->start_time;
486 if (archive_entry_mtime_is_set(entry)) {
487 fe->mtime = archive_entry_mtime(entry);
488 fe->mtime_nanos = archive_entry_mtime_nsec(entry);
490 /* If mtime is unset, use start time. */
491 fe->mtime = a->start_time;
494 if (archive_entry_birthtime_is_set(entry)) {
495 fe->birthtime = archive_entry_birthtime(entry);
496 fe->birthtime_nanos = archive_entry_birthtime_nsec(entry);
498 /* If birthtime is unset, use mtime. */
499 fe->birthtime = fe->mtime;
500 fe->birthtime_nanos = fe->mtime_nanos;
504 if (a->deferred & TODO_FFLAGS) {
505 fe = current_fixup(a, archive_entry_pathname(entry));
506 fe->fixup |= TODO_FFLAGS;
507 /* TODO: Complete this.. defer fflags from below. */
510 /* We've created the object and are ready to pour data into it. */
511 if (ret >= ARCHIVE_WARN)
512 a->archive.state = ARCHIVE_STATE_DATA;
514 * If it's not open, tell our client not to try writing.
515 * In particular, dirs, links, etc, don't get written to.
518 archive_entry_set_size(entry, 0);
522 /* Restore the user's umask before returning. */
523 umask(a->user_umask);
529 archive_write_disk_set_skip_file(struct archive *_a, dev_t d, ino_t i)
531 struct archive_write_disk *a = (struct archive_write_disk *)_a;
532 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
533 ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
534 a->skip_file_dev = d;
535 a->skip_file_ino = i;
540 write_data_block(struct archive_write_disk *a, const char *buff, size_t size)
542 uint64_t start_size = size;
543 ssize_t bytes_written = 0;
544 ssize_t block_size = 0, bytes_to_write;
549 if (a->filesize == 0 || a->fd < 0) {
550 archive_set_error(&a->archive, 0,
551 "Attempt to write to an empty file");
552 return (ARCHIVE_WARN);
555 if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
556 #if HAVE_STRUCT_STAT_ST_BLKSIZE
558 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
560 block_size = a->pst->st_blksize;
562 /* XXX TODO XXX Is there a more appropriate choice here ? */
563 /* This needn't match the filesystem allocation size. */
564 block_size = 16*1024;
568 /* If this write would run beyond the file size, truncate it. */
569 if (a->filesize >= 0 && (off_t)(a->offset + size) > a->filesize)
570 start_size = size = (size_t)(a->filesize - a->offset);
572 /* Write the data. */
574 if (block_size == 0) {
575 bytes_to_write = size;
577 /* We're sparsifying the file. */
581 /* Skip leading zero bytes. */
582 for (p = buff, end = buff + size; p < end; ++p) {
586 a->offset += p - buff;
592 /* Calculate next block boundary after offset. */
594 = (a->offset / block_size + 1) * block_size;
596 /* If the adjusted write would cross block boundary,
597 * truncate it to the block boundary. */
598 bytes_to_write = size;
599 if (a->offset + bytes_to_write > block_end)
600 bytes_to_write = block_end - a->offset;
602 /* Seek if necessary to the specified offset. */
603 if (a->offset != a->fd_offset) {
604 if (lseek(a->fd, a->offset, SEEK_SET) < 0) {
605 archive_set_error(&a->archive, errno,
607 return (ARCHIVE_FATAL);
609 a->fd_offset = a->offset;
610 a->archive.file_position = a->offset;
611 a->archive.raw_position = a->offset;
613 bytes_written = write(a->fd, buff, bytes_to_write);
614 if (bytes_written < 0) {
615 archive_set_error(&a->archive, errno, "Write failed");
616 return (ARCHIVE_WARN);
618 buff += bytes_written;
619 size -= bytes_written;
620 a->offset += bytes_written;
621 a->archive.file_position += bytes_written;
622 a->archive.raw_position += bytes_written;
623 a->fd_offset = a->offset;
625 return (start_size - size);
629 _archive_write_data_block(struct archive *_a,
630 const void *buff, size_t size, off_t offset)
632 struct archive_write_disk *a = (struct archive_write_disk *)_a;
635 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
636 ARCHIVE_STATE_DATA, "archive_write_disk_block");
639 r = write_data_block(a, buff, size);
642 if ((size_t)r < size) {
643 archive_set_error(&a->archive, 0,
644 "Write request too large");
645 return (ARCHIVE_WARN);
651 _archive_write_data(struct archive *_a, const void *buff, size_t size)
653 struct archive_write_disk *a = (struct archive_write_disk *)_a;
655 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
656 ARCHIVE_STATE_DATA, "archive_write_data");
658 return (write_data_block(a, buff, size));
662 _archive_write_finish_entry(struct archive *_a)
664 struct archive_write_disk *a = (struct archive_write_disk *)_a;
665 int ret = ARCHIVE_OK;
667 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
668 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
669 "archive_write_finish_entry");
670 if (a->archive.state & ARCHIVE_STATE_HEADER)
672 archive_clear_error(&a->archive);
674 /* Pad or truncate file to the right size. */
676 /* There's no file. */
677 } else if (a->filesize < 0) {
678 /* File size is unknown, so we can't set the size. */
679 } else if (a->fd_offset == a->filesize) {
680 /* Last write ended at exactly the filesize; we're done. */
681 /* Hopefully, this is the common case. */
684 if (ftruncate(a->fd, a->filesize) == -1 &&
686 archive_set_error(&a->archive, errno,
687 "File size could not be restored");
688 return (ARCHIVE_FAILED);
692 * Explicitly stat the file as some platforms might not
693 * implement the XSI option to extend files via ftruncate.
696 if ((ret = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
698 if (a->st.st_size != a->filesize) {
699 const char nul = '\0';
700 if (lseek(a->fd, a->st.st_size - 1, SEEK_SET) < 0) {
701 archive_set_error(&a->archive, errno,
703 return (ARCHIVE_FATAL);
705 if (write(a->fd, &nul, 1) < 0) {
706 archive_set_error(&a->archive, errno,
707 "Write to restore size failed");
708 return (ARCHIVE_FATAL);
714 /* Restore metadata. */
717 * Look up the "real" UID only if we're going to need it.
718 * TODO: the TODO_SGID condition can be dropped here, can't it?
720 if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
721 a->uid = a->lookup_uid(a->lookup_uid_data,
722 archive_entry_uname(a->entry),
723 archive_entry_uid(a->entry));
725 /* Look up the "real" GID only if we're going to need it. */
726 /* TODO: the TODO_SUID condition can be dropped here, can't it? */
727 if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
728 a->gid = a->lookup_gid(a->lookup_gid_data,
729 archive_entry_gname(a->entry),
730 archive_entry_gid(a->entry));
733 * If restoring ownership, do it before trying to restore suid/sgid
734 * bits. If we set the owner, we know what it is and can skip
735 * a stat() call to examine the ownership of the file on disk.
737 if (a->todo & TODO_OWNER)
738 ret = set_ownership(a);
739 if (a->todo & TODO_MODE) {
740 int r2 = set_mode(a, a->mode);
741 if (r2 < ret) ret = r2;
743 if (a->todo & TODO_ACLS) {
744 int r2 = set_acls(a);
745 if (r2 < ret) ret = r2;
748 * Some flags prevent file modification; they must be restored after
749 * file contents are written.
751 if (a->todo & TODO_FFLAGS) {
752 int r2 = set_fflags(a);
753 if (r2 < ret) ret = r2;
756 * Time has to be restored after all other metadata;
757 * otherwise atime will get changed.
759 if (a->todo & TODO_TIMES) {
760 int r2 = set_times(a);
761 if (r2 < ret) ret = r2;
764 /* If there's an fd, we can close it now. */
769 /* If there's an entry, we can release it now. */
771 archive_entry_free(a->entry);
774 a->archive.state = ARCHIVE_STATE_HEADER;
779 archive_write_disk_set_group_lookup(struct archive *_a,
781 gid_t (*lookup_gid)(void *private, const char *gname, gid_t gid),
782 void (*cleanup_gid)(void *private))
784 struct archive_write_disk *a = (struct archive_write_disk *)_a;
785 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
786 ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
788 a->lookup_gid = lookup_gid;
789 a->cleanup_gid = cleanup_gid;
790 a->lookup_gid_data = private_data;
795 archive_write_disk_set_user_lookup(struct archive *_a,
797 uid_t (*lookup_uid)(void *private, const char *uname, uid_t uid),
798 void (*cleanup_uid)(void *private))
800 struct archive_write_disk *a = (struct archive_write_disk *)_a;
801 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
802 ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
804 a->lookup_uid = lookup_uid;
805 a->cleanup_uid = cleanup_uid;
806 a->lookup_uid_data = private_data;
812 * Create a new archive_write_disk object and initialize it with global state.
815 archive_write_disk_new(void)
817 struct archive_write_disk *a;
819 a = (struct archive_write_disk *)malloc(sizeof(*a));
822 memset(a, 0, sizeof(*a));
823 a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
824 /* We're ready to write a header immediately. */
825 a->archive.state = ARCHIVE_STATE_HEADER;
826 a->archive.vtable = archive_write_disk_vtable();
827 a->lookup_uid = trivial_lookup_uid;
828 a->lookup_gid = trivial_lookup_gid;
829 a->start_time = time(NULL);
831 a->user_uid = geteuid();
832 #endif /* HAVE_GETEUID */
833 if (archive_string_ensure(&a->path_safe, 512) == NULL) {
837 return (&a->archive);
842 * If pathname is longer than PATH_MAX, chdir to a suitable
843 * intermediate dir and edit the path down to a shorter suffix. Note
844 * that this routine never returns an error; if the chdir() attempt
845 * fails for any reason, we just go ahead with the long pathname. The
846 * object creation is likely to fail, but any error will get handled
851 edit_deep_directories(struct archive_write_disk *a)
854 char *tail = a->name;
858 /* If path is short, avoid the open() below. */
859 if (strlen(tail) <= PATH_MAX)
862 /* Try to record our starting dir. */
863 a->restore_pwd = open(".", O_RDONLY | O_BINARY);
864 if (a->restore_pwd < 0)
867 /* As long as the path is too long... */
868 while (strlen(tail) > PATH_MAX) {
869 /* Locate a dir prefix shorter than PATH_MAX. */
870 tail += PATH_MAX - 8;
871 while (tail > a->name && *tail != '/')
873 /* Exit if we find a too-long path component. */
876 /* Create the intermediate dir and chdir to it. */
877 *tail = '\0'; /* Terminate dir portion */
878 ret = create_dir(a, a->name);
879 if (ret == ARCHIVE_OK && chdir(a->name) != 0)
880 ret = ARCHIVE_FAILED;
881 *tail = '/'; /* Restore the / we removed. */
882 if (ret != ARCHIVE_OK)
885 /* The chdir() succeeded; we've now shortened the path. */
893 * The main restore function.
896 restore_entry(struct archive_write_disk *a)
898 int ret = ARCHIVE_OK, en;
900 if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
902 * TODO: Fix this. Apparently, there are platforms
903 * that still allow root to hose the entire filesystem
904 * by unlinking a dir. The S_ISDIR() test above
905 * prevents us from using unlink() here if the new
906 * object is a dir, but that doesn't mean the old
907 * object isn't a dir.
909 if (unlink(a->name) == 0) {
910 /* We removed it, reset cached stat. */
912 } else if (errno == ENOENT) {
913 /* File didn't exist, that's just as good. */
914 } else if (rmdir(a->name) == 0) {
915 /* It was a dir, but now it's gone. */
918 /* We tried, but couldn't get rid of it. */
919 archive_set_error(&a->archive, errno,
921 return(ARCHIVE_FAILED);
925 /* Try creating it first; if this fails, we'll try to recover. */
926 en = create_filesystem_object(a);
928 if ((en == ENOTDIR || en == ENOENT)
929 && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
930 /* If the parent dir doesn't exist, try creating it. */
931 create_parent_dir(a, a->name);
932 /* Now try to create the object again. */
933 en = create_filesystem_object(a);
936 if ((en == EISDIR || en == EEXIST)
937 && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
938 /* If we're not overwriting, we're done. */
939 archive_set_error(&a->archive, en, "Already exists");
940 return (ARCHIVE_FAILED);
944 * Some platforms return EISDIR if you call
945 * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
946 * return EEXIST. POSIX is ambiguous, requiring EISDIR
947 * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
948 * on an existing item.
951 /* A dir is in the way of a non-dir, rmdir it. */
952 if (rmdir(a->name) != 0) {
953 archive_set_error(&a->archive, errno,
954 "Can't remove already-existing dir");
955 return (ARCHIVE_FAILED);
959 en = create_filesystem_object(a);
960 } else if (en == EEXIST) {
962 * We know something is in the way, but we don't know what;
963 * we need to find out before we go any further.
967 * The SECURE_SYMLINK logic has already removed a
968 * symlink to a dir if the client wants that. So
969 * follow the symlink if we're creating a dir.
971 if (S_ISDIR(a->mode))
972 r = stat(a->name, &a->st);
974 * If it's not a dir (or it's a broken symlink),
975 * then don't follow it.
977 if (r != 0 || !S_ISDIR(a->mode))
978 r = lstat(a->name, &a->st);
980 archive_set_error(&a->archive, errno,
981 "Can't stat existing object");
982 return (ARCHIVE_FAILED);
986 * NO_OVERWRITE_NEWER doesn't apply to directories.
988 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
989 && !S_ISDIR(a->st.st_mode)) {
990 if (!older(&(a->st), a->entry)) {
991 archive_set_error(&a->archive, 0,
992 "File on disk is not older; skipping.");
993 return (ARCHIVE_FAILED);
997 /* If it's our archive, we're done. */
998 if (a->skip_file_dev > 0 &&
999 a->skip_file_ino > 0 &&
1000 a->st.st_dev == a->skip_file_dev &&
1001 a->st.st_ino == a->skip_file_ino) {
1002 archive_set_error(&a->archive, 0, "Refusing to overwrite archive");
1003 return (ARCHIVE_FAILED);
1006 if (!S_ISDIR(a->st.st_mode)) {
1007 /* A non-dir is in the way, unlink it. */
1008 if (unlink(a->name) != 0) {
1009 archive_set_error(&a->archive, errno,
1010 "Can't unlink already-existing object");
1011 return (ARCHIVE_FAILED);
1015 en = create_filesystem_object(a);
1016 } else if (!S_ISDIR(a->mode)) {
1017 /* A dir is in the way of a non-dir, rmdir it. */
1018 if (rmdir(a->name) != 0) {
1019 archive_set_error(&a->archive, errno,
1020 "Can't remove already-existing dir");
1021 return (ARCHIVE_FAILED);
1024 en = create_filesystem_object(a);
1027 * There's a dir in the way of a dir. Don't
1028 * waste time with rmdir()/mkdir(), just fix
1029 * up the permissions on the existing dir.
1030 * Note that we don't change perms on existing
1031 * dirs unless _EXTRACT_PERM is specified.
1033 if ((a->mode != a->st.st_mode)
1034 && (a->todo & TODO_MODE_FORCE))
1035 a->deferred |= (a->todo & TODO_MODE);
1036 /* Ownership doesn't need deferred fixup. */
1037 en = 0; /* Forget the EEXIST. */
1042 /* Everything failed; give up here. */
1043 archive_set_error(&a->archive, en, "Can't create '%s'",
1045 return (ARCHIVE_FAILED);
1048 a->pst = NULL; /* Cached stat data no longer valid. */
1053 * Returns 0 if creation succeeds, or else returns errno value from
1054 * the failed system call. Note: This function should only ever perform
1055 * a single system call.
1058 create_filesystem_object(struct archive_write_disk *a)
1060 /* Create the entry. */
1061 const char *linkname;
1062 mode_t final_mode, mode;
1065 /* We identify hard/symlinks according to the link names. */
1066 /* Since link(2) and symlink(2) don't handle modes, we're done here. */
1067 linkname = archive_entry_hardlink(a->entry);
1068 if (linkname != NULL) {
1069 r = link(linkname, a->name) ? errno : 0;
1071 * New cpio and pax formats allow hardlink entries
1072 * to carry data, so we may have to open the file
1073 * for hardlink entries.
1075 * If the hardlink was successfully created and
1076 * the archive doesn't have carry data for it,
1077 * consider it to be non-authoritive for meta data.
1078 * This is consistent with GNU tar and BSD pax.
1079 * If the hardlink does carry data, let the last
1080 * archive entry decide ownership.
1082 if (r == 0 && a->filesize <= 0) {
1085 } if (r == 0 && a->filesize > 0) {
1086 a->fd = open(a->name, O_WRONLY | O_TRUNC | O_BINARY);
1092 linkname = archive_entry_symlink(a->entry);
1093 if (linkname != NULL)
1094 return symlink(linkname, a->name) ? errno : 0;
1097 * The remaining system calls all set permissions, so let's
1098 * try to take advantage of that to avoid an extra chmod()
1099 * call. (Recall that umask is set to zero right now!)
1102 /* Mode we want for the final restored object (w/o file type bits). */
1103 final_mode = a->mode & 07777;
1105 * The mode that will actually be restored in this step. Note
1106 * that SUID, SGID, etc, require additional work to ensure
1107 * security, so we never restore them at this point.
1109 mode = final_mode & 0777;
1111 switch (a->mode & AE_IFMT) {
1113 /* POSIX requires that we fall through here. */
1116 a->fd = open(a->name,
1117 O_WRONLY | O_CREAT | O_EXCL | O_BINARY, mode);
1122 /* Note: we use AE_IFCHR for the case label, and
1123 * S_IFCHR for the mknod() call. This is correct. */
1124 r = mknod(a->name, mode | S_IFCHR,
1125 archive_entry_rdev(a->entry));
1127 /* TODO: Find a better way to warn about our inability
1128 * to restore a char device node. */
1130 #endif /* HAVE_MKNOD */
1134 r = mknod(a->name, mode | S_IFBLK,
1135 archive_entry_rdev(a->entry));
1137 /* TODO: Find a better way to warn about our inability
1138 * to restore a block device node. */
1140 #endif /* HAVE_MKNOD */
1143 mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
1144 r = mkdir(a->name, mode);
1146 /* Defer setting dir times. */
1147 a->deferred |= (a->todo & TODO_TIMES);
1148 a->todo &= ~TODO_TIMES;
1149 /* Never use an immediate chmod(). */
1150 /* We can't avoid the chmod() entirely if EXTRACT_PERM
1151 * because of SysV SGID inheritance. */
1152 if ((mode != final_mode)
1153 || (a->flags & ARCHIVE_EXTRACT_PERM))
1154 a->deferred |= (a->todo & TODO_MODE);
1155 a->todo &= ~TODO_MODE;
1160 r = mkfifo(a->name, mode);
1162 /* TODO: Find a better way to warn about our inability
1163 * to restore a fifo. */
1165 #endif /* HAVE_MKFIFO */
1169 /* All the system calls above set errno on failure. */
1173 /* If we managed to set the final mode, we've avoided a chmod(). */
1174 if (mode == final_mode)
1175 a->todo &= ~TODO_MODE;
1180 * Cleanup function for archive_extract. Mostly, this involves processing
1181 * the fixup list, which is used to address a number of problems:
1182 * * Dir permissions might prevent us from restoring a file in that
1183 * dir, so we restore the dir with minimum 0700 permissions first,
1184 * then correct the mode at the end.
1185 * * Similarly, the act of restoring a file touches the directory
1186 * and changes the timestamp on the dir, so we have to touch-up dir
1187 * timestamps at the end as well.
1188 * * Some file flags can interfere with the restore by, for example,
1189 * preventing the creation of hardlinks to those files.
1191 * Note that tar/cpio do not require that archives be in a particular
1192 * order; there is no way to know when the last file has been restored
1193 * within a directory, so there's no way to optimize the memory usage
1194 * here by fixing up the directory any earlier than the
1197 * XXX TODO: Directory ACLs should be restored here, for the same
1198 * reason we set directory perms here. XXX
1201 _archive_write_close(struct archive *_a)
1203 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1204 struct fixup_entry *next, *p;
1207 __archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1208 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1209 "archive_write_disk_close");
1210 ret = _archive_write_finish_entry(&a->archive);
1212 /* Sort dir list so directories are fixed up in depth-first order. */
1213 p = sort_dir_list(a->fixup_list);
1216 a->pst = NULL; /* Mark stat cache as out-of-date. */
1217 if (p->fixup & TODO_TIMES) {
1219 /* {f,l,}utimes() are preferred, when available. */
1221 struct __timeval times[2];
1223 struct timeval times[2];
1225 times[0].tv_sec = p->atime;
1226 times[0].tv_usec = p->atime_nanos / 1000;
1227 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1228 /* if it's valid and not mtime, push the birthtime first */
1229 if (((times[1].tv_sec = p->birthtime) < p->mtime) &&
1232 times[1].tv_usec = p->birthtime_nanos / 1000;
1233 utimes(p->name, times);
1236 times[1].tv_sec = p->mtime;
1237 times[1].tv_usec = p->mtime_nanos / 1000;
1239 lutimes(p->name, times);
1241 utimes(p->name, times);
1244 /* utime() is more portable, but less precise. */
1245 struct utimbuf times;
1246 times.modtime = p->mtime;
1247 times.actime = p->atime;
1249 utime(p->name, ×);
1252 if (p->fixup & TODO_MODE_BASE)
1253 chmod(p->name, p->mode);
1255 if (p->fixup & TODO_FFLAGS)
1256 set_fflags_platform(a, -1, p->name,
1257 p->mode, p->fflags_set, 0);
1264 a->fixup_list = NULL;
1269 _archive_write_finish(struct archive *_a)
1271 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1273 ret = _archive_write_close(&a->archive);
1274 if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
1275 (a->cleanup_gid)(a->lookup_gid_data);
1276 if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
1277 (a->cleanup_uid)(a->lookup_uid_data);
1278 archive_string_free(&a->_name_data);
1279 archive_string_free(&a->archive.error_string);
1280 archive_string_free(&a->path_safe);
1286 * Simple O(n log n) merge sort to order the fixup list. In
1287 * particular, we want to restore dir timestamps depth-first.
1289 static struct fixup_entry *
1290 sort_dir_list(struct fixup_entry *p)
1292 struct fixup_entry *a, *b, *t;
1296 /* A one-item list is already sorted. */
1297 if (p->next == NULL)
1300 /* Step 1: split the list. */
1304 /* Step a twice, t once. */
1310 /* Now, t is at the mid-point, so break the list here. */
1315 /* Step 2: Recursively sort the two sub-lists. */
1316 a = sort_dir_list(a);
1317 b = sort_dir_list(b);
1319 /* Step 3: Merge the returned lists. */
1320 /* Pick the first element for the merged list. */
1321 if (strcmp(a->name, b->name) > 0) {
1329 /* Always put the later element on the list first. */
1330 while (a != NULL && b != NULL) {
1331 if (strcmp(a->name, b->name) > 0) {
1341 /* Only one list is non-empty, so just splice it on. */
1351 * Returns a new, initialized fixup entry.
1353 * TODO: Reduce the memory requirements for this list by using a tree
1354 * structure rather than a simple list of names.
1356 static struct fixup_entry *
1357 new_fixup(struct archive_write_disk *a, const char *pathname)
1359 struct fixup_entry *fe;
1361 fe = (struct fixup_entry *)malloc(sizeof(struct fixup_entry));
1364 fe->next = a->fixup_list;
1367 fe->name = strdup(pathname);
1372 * Returns a fixup structure for the current entry.
1374 static struct fixup_entry *
1375 current_fixup(struct archive_write_disk *a, const char *pathname)
1377 if (a->current_fixup == NULL)
1378 a->current_fixup = new_fixup(a, pathname);
1379 return (a->current_fixup);
1382 /* TODO: Make this work. */
1384 * TODO: The deep-directory support bypasses this; disable deep directory
1385 * support if we're doing symlink checks.
1388 * TODO: Someday, integrate this with the deep dir support; they both
1389 * scan the path and both can be optimized by comparing against other
1393 check_symlinks(struct archive_write_disk *a)
1401 * Guard against symlink tricks. Reject any archive entry whose
1402 * destination would be altered by a symlink.
1404 /* Whatever we checked last time doesn't need to be re-checked. */
1407 while ((*pn != '\0') && (*p == *pn))
1410 /* Keep going until we've checked the entire name. */
1411 while (pn[0] != '\0' && (pn[0] != '/' || pn[1] != '\0')) {
1412 /* Skip the next path element. */
1413 while (*pn != '\0' && *pn != '/')
1417 /* Check that we haven't hit a symlink. */
1418 r = lstat(a->name, &st);
1420 /* We've hit a dir that doesn't exist; stop now. */
1421 if (errno == ENOENT)
1423 } else if (S_ISLNK(st.st_mode)) {
1426 * Last element is symlink; remove it
1427 * so we can overwrite it with the
1428 * item being extracted.
1430 if (unlink(a->name)) {
1431 archive_set_error(&a->archive, errno,
1432 "Could not remove symlink %s",
1435 return (ARCHIVE_FAILED);
1439 * Even if we did remove it, a warning
1440 * is in order. The warning is silly,
1441 * though, if we're just replacing one
1442 * symlink with another symlink.
1444 if (!S_ISLNK(a->mode)) {
1445 archive_set_error(&a->archive, 0,
1446 "Removing symlink %s",
1449 /* Symlink gone. No more problem! */
1452 } else if (a->flags & ARCHIVE_EXTRACT_UNLINK) {
1453 /* User asked us to remove problems. */
1454 if (unlink(a->name) != 0) {
1455 archive_set_error(&a->archive, 0,
1456 "Cannot remove intervening symlink %s",
1459 return (ARCHIVE_FAILED);
1463 archive_set_error(&a->archive, 0,
1464 "Cannot extract through symlink %s",
1467 return (ARCHIVE_FAILED);
1472 /* We've checked and/or cleaned the whole path, so remember it. */
1473 archive_strcpy(&a->path_safe, a->name);
1474 return (ARCHIVE_OK);
1479 * 1. Convert a path separator from '\' to '/' .
1480 * We shouldn't check multi-byte character directly because some
1481 * character-set have been using the '\' character for a part of
1482 * its multibyte character code.
1483 * 2. Replace unusable characters in Windows with underscore('_').
1484 * See also : http://msdn.microsoft.com/en-us/library/aa365247.aspx
1487 cleanup_pathname_win(struct archive_write_disk *a)
1495 for (p = a->name; *p != '\0'; p++) {
1499 /* Rewrite the path name if its character is a unusable. */
1500 if (*p == ':' || *p == '*' || *p == '?' || *p == '"' ||
1501 *p == '<' || *p == '>' || *p == '|')
1504 if (alen == 0 || l == 0)
1507 * Convert path separator.
1510 while (*p != '\0' && alen) {
1511 l = mbtowc(&wc, p, alen);
1513 while (*p != '\0') {
1520 if (l == 1 && wc == L'\\')
1529 * Canonicalize the pathname. In particular, this strips duplicate
1530 * '/' characters, '.' elements, and trailing '/'. It also raises an
1531 * error for an empty path, a trailing '..' or (if _SECURE_NODOTDOT is
1532 * set) any '..' in the path.
1535 cleanup_pathname(struct archive_write_disk *a)
1538 char separator = '\0';
1540 dest = src = a->name;
1542 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1543 "Invalid empty pathname");
1544 return (ARCHIVE_FAILED);
1548 cleanup_pathname_win(a);
1550 /* Skip leading '/'. */
1554 /* Scan the pathname one element at a time. */
1556 /* src points to first char after '/' */
1557 if (src[0] == '\0') {
1559 } else if (src[0] == '/') {
1560 /* Found '//', ignore second one. */
1563 } else if (src[0] == '.') {
1564 if (src[1] == '\0') {
1565 /* Ignore trailing '.' */
1567 } else if (src[1] == '/') {
1571 } else if (src[1] == '.') {
1572 if (src[2] == '/' || src[2] == '\0') {
1573 /* Conditionally warn about '..' */
1574 if (a->flags & ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
1575 archive_set_error(&a->archive,
1577 "Path contains '..'");
1578 return (ARCHIVE_FAILED);
1582 * Note: Under no circumstances do we
1583 * remove '..' elements. In
1584 * particular, restoring
1585 * '/foo/../bar/' should create the
1586 * 'foo' dir as a side-effect.
1591 /* Copy current element, including leading '/'. */
1594 while (*src != '\0' && *src != '/') {
1601 /* Skip '/' separator. */
1605 * We've just copied zero or more path elements, not including the
1608 if (dest == a->name) {
1610 * Nothing got copied. The path must have been something
1611 * like '.' or '/' or './' or '/././././/./'.
1618 /* Terminate the result. */
1620 return (ARCHIVE_OK);
1624 * Create the parent directory of the specified path, assuming path
1625 * is already in mutable storage.
1628 create_parent_dir(struct archive_write_disk *a, char *path)
1633 /* Remove tail element to obtain parent name. */
1634 slash = strrchr(path, '/');
1636 return (ARCHIVE_OK);
1638 r = create_dir(a, path);
1644 * Create the specified dir, recursing to create parents as necessary.
1646 * Returns ARCHIVE_OK if the path exists when we're done here.
1647 * Otherwise, returns ARCHIVE_FAILED.
1648 * Assumes path is in mutable storage; path is unchanged on exit.
1651 create_dir(struct archive_write_disk *a, char *path)
1654 struct fixup_entry *le;
1656 mode_t mode_final, mode;
1661 /* Check for special names and just skip them. */
1662 slash = strrchr(path, '/');
1668 if (base[0] == '\0' ||
1669 (base[0] == '.' && base[1] == '\0') ||
1670 (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
1671 /* Don't bother trying to create null path, '.', or '..'. */
1672 if (slash != NULL) {
1674 r = create_dir(a, path);
1678 return (ARCHIVE_OK);
1682 * Yes, this should be stat() and not lstat(). Using lstat()
1683 * here loses the ability to extract through symlinks. Also note
1684 * that this should not use the a->st cache.
1686 if (stat(path, &st) == 0) {
1687 if (S_ISDIR(st.st_mode))
1688 return (ARCHIVE_OK);
1689 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
1690 archive_set_error(&a->archive, EEXIST,
1691 "Can't create directory '%s'", path);
1692 return (ARCHIVE_FAILED);
1694 if (unlink(path) != 0) {
1695 archive_set_error(&a->archive, errno,
1696 "Can't create directory '%s': "
1697 "Conflicting file cannot be removed");
1698 return (ARCHIVE_FAILED);
1700 } else if (errno != ENOENT && errno != ENOTDIR) {
1702 archive_set_error(&a->archive, errno, "Can't test directory '%s'", path);
1703 return (ARCHIVE_FAILED);
1704 } else if (slash != NULL) {
1706 r = create_dir(a, path);
1708 if (r != ARCHIVE_OK)
1713 * Mode we want for the final restored directory. Per POSIX,
1714 * implicitly-created dirs must be created obeying the umask.
1715 * There's no mention whether this is different for privileged
1716 * restores (which the rest of this code handles by pretending
1717 * umask=0). I've chosen here to always obey the user's umask for
1718 * implicit dirs, even if _EXTRACT_PERM was specified.
1720 mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
1721 /* Mode we want on disk during the restore process. */
1723 mode |= MINIMUM_DIR_MODE;
1724 mode &= MAXIMUM_DIR_MODE;
1725 if (mkdir(path, mode) == 0) {
1726 if (mode != mode_final) {
1727 le = new_fixup(a, path);
1728 le->fixup |=TODO_MODE_BASE;
1729 le->mode = mode_final;
1731 return (ARCHIVE_OK);
1735 * Without the following check, a/b/../b/c/d fails at the
1736 * second visit to 'b', so 'd' can't be created. Note that we
1737 * don't add it to the fixup list here, as it's already been
1740 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
1741 return (ARCHIVE_OK);
1743 archive_set_error(&a->archive, errno, "Failed to create dir '%s'",
1745 return (ARCHIVE_FAILED);
1749 * Note: Although we can skip setting the user id if the desired user
1750 * id matches the current user, we cannot skip setting the group, as
1751 * many systems set the gid based on the containing directory. So
1752 * we have to perform a chown syscall if we want to set the SGID
1753 * bit. (The alternative is to stat() and then possibly chown(); it's
1754 * more efficient to skip the stat() and just always chown().) Note
1755 * that a successful chown() here clears the TODO_SGID_CHECK bit, which
1756 * allows set_mode to skip the stat() check for the GID.
1759 set_ownership(struct archive_write_disk *a)
1761 /* If we know we can't change it, don't bother trying. */
1762 if (a->user_uid != 0 && a->user_uid != a->uid) {
1763 archive_set_error(&a->archive, errno,
1764 "Can't set UID=%d", a->uid);
1765 return (ARCHIVE_WARN);
1769 /* If we have an fd, we can avoid a race. */
1770 if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0) {
1771 /* We've set owner and know uid/gid are correct. */
1772 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
1773 return (ARCHIVE_OK);
1777 /* We prefer lchown() but will use chown() if that's all we have. */
1778 /* Of course, if we have neither, this will always fail. */
1780 if (lchown(a->name, a->uid, a->gid) == 0) {
1781 /* We've set owner and know uid/gid are correct. */
1782 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
1783 return (ARCHIVE_OK);
1786 if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0) {
1787 /* We've set owner and know uid/gid are correct. */
1788 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
1789 return (ARCHIVE_OK);
1793 archive_set_error(&a->archive, errno,
1794 "Can't set user=%d/group=%d for %s", a->uid, a->gid,
1796 return (ARCHIVE_WARN);
1801 * The utimes()-family functions provide high resolution and
1802 * a way to set time on an fd or a symlink. We prefer them
1803 * when they're available.
1806 set_time(int fd, int mode, const char *name,
1807 time_t atime, long atime_nsec,
1808 time_t mtime, long mtime_nsec)
1811 struct __timeval times[2];
1813 struct timeval times[2];
1816 times[0].tv_sec = atime;
1817 times[0].tv_usec = atime_nsec / 1000;
1818 times[1].tv_sec = mtime;
1819 times[1].tv_usec = mtime_nsec / 1000;
1823 return (futimes(fd, times));
1825 (void)fd; /* UNUSED */
1828 (void)mode; /* UNUSED */
1829 return (lutimes(name, times));
1833 return (utimes(name, times));
1836 #elif defined(HAVE_UTIME)
1838 * utime() is an older, more standard interface that we'll use
1839 * if utimes() isn't available.
1842 set_time(int fd, int mode, const char *name,
1843 time_t atime, long atime_nsec,
1844 time_t mtime, long mtime_nsec)
1846 struct utimbuf times;
1847 (void)fd; /* UNUSED */
1848 (void)name; /* UNUSED */
1849 (void)atime_nsec; /* UNUSED */
1850 (void)mtime_nsec; /* UNUSED */
1851 times.actime = atime;
1852 times.modtime = mtime;
1854 return (ARCHIVE_OK);
1855 return (utime(name, ×));
1859 set_time(int fd, int mode, const char *name,
1860 time_t atime, long atime_nsec,
1861 time_t mtime, long mtime_nsec)
1863 return (ARCHIVE_WARN);
1868 set_times(struct archive_write_disk *a)
1870 time_t atime = a->start_time, mtime = a->start_time;
1871 long atime_nsec = 0, mtime_nsec = 0;
1873 /* If no time was provided, we're done. */
1874 if (!archive_entry_atime_is_set(a->entry)
1875 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
1876 && !archive_entry_birthtime_is_set(a->entry)
1878 && !archive_entry_mtime_is_set(a->entry))
1879 return (ARCHIVE_OK);
1881 /* If no atime was specified, use start time instead. */
1882 /* In theory, it would be marginally more correct to use
1883 * time(NULL) here, but that would cost us an extra syscall
1884 * for little gain. */
1885 if (archive_entry_atime_is_set(a->entry)) {
1886 atime = archive_entry_atime(a->entry);
1887 atime_nsec = archive_entry_atime_nsec(a->entry);
1891 * If you have struct stat.st_birthtime, we assume BSD birthtime
1892 * semantics, in which {f,l,}utimes() updates birthtime to earliest
1893 * mtime. So we set the time twice, first using the birthtime,
1894 * then using the mtime.
1896 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
1897 /* If birthtime is set, flush that through to disk first. */
1898 if (archive_entry_birthtime_is_set(a->entry))
1899 if (set_time(a->fd, a->mode, a->name, atime, atime_nsec,
1900 archive_entry_birthtime(a->entry),
1901 archive_entry_birthtime_nsec(a->entry))) {
1902 archive_set_error(&a->archive, errno,
1903 "Can't update time for %s",
1905 return (ARCHIVE_WARN);
1909 if (archive_entry_mtime_is_set(a->entry)) {
1910 mtime = archive_entry_mtime(a->entry);
1911 mtime_nsec = archive_entry_mtime_nsec(a->entry);
1913 if (set_time(a->fd, a->mode, a->name,
1914 atime, atime_nsec, mtime, mtime_nsec)) {
1915 archive_set_error(&a->archive, errno,
1916 "Can't update time for %s",
1918 return (ARCHIVE_WARN);
1922 * Note: POSIX does not provide a portable way to restore ctime.
1923 * (Apart from resetting the system clock, which is distasteful.)
1924 * So, any restoration of ctime will necessarily be OS-specific.
1927 return (ARCHIVE_OK);
1931 set_mode(struct archive_write_disk *a, int mode)
1934 mode &= 07777; /* Strip off file type bits. */
1936 if (a->todo & TODO_SGID_CHECK) {
1938 * If we don't know the GID is right, we must stat()
1939 * to verify it. We can't just check the GID of this
1940 * process, since systems sometimes set GID from
1941 * the enclosing dir or based on ACLs.
1943 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
1945 if (a->pst->st_gid != a->gid) {
1948 if (a->flags & ARCHIVE_EXTRACT_OWNER) {
1950 * This is only an error if you
1951 * requested owner restore. If you
1952 * didn't, we'll try to restore
1953 * sgid/suid, but won't consider it a
1954 * problem if we can't.
1956 archive_set_error(&a->archive, -1,
1957 "Can't restore SGID bit");
1962 /* While we're here, double-check the UID. */
1963 if (a->pst->st_uid != a->uid
1964 && (a->todo & TODO_SUID)) {
1967 if (a->flags & ARCHIVE_EXTRACT_OWNER) {
1968 archive_set_error(&a->archive, -1,
1969 "Can't restore SUID bit");
1974 a->todo &= ~TODO_SGID_CHECK;
1975 a->todo &= ~TODO_SUID_CHECK;
1976 } else if (a->todo & TODO_SUID_CHECK) {
1978 * If we don't know the UID is right, we can just check
1979 * the user, since all systems set the file UID from
1982 if (a->user_uid != a->uid) {
1985 if (a->flags & ARCHIVE_EXTRACT_OWNER) {
1986 archive_set_error(&a->archive, -1,
1987 "Can't make file SUID");
1992 a->todo &= ~TODO_SUID_CHECK;
1995 if (S_ISLNK(a->mode)) {
1998 * If this is a symlink, use lchmod(). If the
1999 * platform doesn't support lchmod(), just skip it. A
2000 * platform that doesn't provide a way to set
2001 * permissions on symlinks probably ignores
2002 * permissions on symlinks, so a failure here has no
2005 if (lchmod(a->name, mode) != 0) {
2006 archive_set_error(&a->archive, errno,
2007 "Can't set permissions to 0%o", (int)mode);
2011 } else if (!S_ISDIR(a->mode)) {
2013 * If it's not a symlink and not a dir, then use
2014 * fchmod() or chmod(), depending on whether we have
2015 * an fd. Dirs get their perms set during the
2016 * post-extract fixup, which is handled elsewhere.
2020 if (fchmod(a->fd, mode) != 0) {
2021 archive_set_error(&a->archive, errno,
2022 "Can't set permissions to 0%o", (int)mode);
2027 /* If this platform lacks fchmod(), then
2028 * we'll just use chmod(). */
2029 if (chmod(a->name, mode) != 0) {
2030 archive_set_error(&a->archive, errno,
2031 "Can't set permissions to 0%o", (int)mode);
2039 set_fflags(struct archive_write_disk *a)
2041 struct fixup_entry *le;
2042 unsigned long set, clear;
2045 mode_t mode = archive_entry_mode(a->entry);
2048 * Make 'critical_flags' hold all file flags that can't be
2049 * immediately restored. For example, on BSD systems,
2050 * SF_IMMUTABLE prevents hardlinks from being created, so
2051 * should not be set until after any hardlinks are created. To
2052 * preserve some semblance of portability, this uses #ifdef
2053 * extensively. Ugly, but it works.
2055 * Yes, Virginia, this does create a security race. It's mitigated
2056 * somewhat by the practice of creating dirs 0700 until the extract
2057 * is done, but it would be nice if we could do more than that.
2058 * People restoring critical file systems should be wary of
2059 * other programs that might try to muck with files as they're
2062 /* Hopefully, the compiler will optimize this mess into a constant. */
2065 critical_flags |= SF_IMMUTABLE;
2068 critical_flags |= UF_IMMUTABLE;
2071 critical_flags |= SF_APPEND;
2074 critical_flags |= UF_APPEND;
2076 #ifdef EXT2_APPEND_FL
2077 critical_flags |= EXT2_APPEND_FL;
2079 #ifdef EXT2_IMMUTABLE_FL
2080 critical_flags |= EXT2_IMMUTABLE_FL;
2083 if (a->todo & TODO_FFLAGS) {
2084 archive_entry_fflags(a->entry, &set, &clear);
2087 * The first test encourages the compiler to eliminate
2088 * all of this if it's not necessary.
2090 if ((critical_flags != 0) && (set & critical_flags)) {
2091 le = current_fixup(a, a->name);
2092 le->fixup |= TODO_FFLAGS;
2093 le->fflags_set = set;
2094 /* Store the mode if it's not already there. */
2095 if ((le->fixup & TODO_MODE) == 0)
2098 r = set_fflags_platform(a, a->fd,
2099 a->name, mode, set, clear);
2100 if (r != ARCHIVE_OK)
2104 return (ARCHIVE_OK);
2108 #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && defined(HAVE_STRUCT_STAT_ST_FLAGS)
2110 * BSD reads flags using stat() and sets them with one of {f,l,}chflags()
2113 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
2114 mode_t mode, unsigned long set, unsigned long clear)
2118 (void)mode; /* UNUSED */
2119 if (set == 0 && clear == 0)
2120 return (ARCHIVE_OK);
2123 * XXX Is the stat here really necessary? Or can I just use
2124 * the 'set' flags directly? In particular, I'm not sure
2125 * about the correct approach if we're overwriting an existing
2126 * file that already has flags on it. XXX
2128 if ((r = _archive_write_disk_lazy_stat(a)) != ARCHIVE_OK)
2131 a->st.st_flags &= ~clear;
2132 a->st.st_flags |= set;
2133 #ifdef HAVE_FCHFLAGS
2134 /* If platform has fchflags() and we were given an fd, use it. */
2135 if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0)
2136 return (ARCHIVE_OK);
2139 * If we can't use the fd to set the flags, we'll use the
2140 * pathname to set flags. We prefer lchflags() but will use
2141 * chflags() if we must.
2143 #ifdef HAVE_LCHFLAGS
2144 if (lchflags(name, a->st.st_flags) == 0)
2145 return (ARCHIVE_OK);
2146 #elif defined(HAVE_CHFLAGS)
2147 if (S_ISLNK(a->st.st_mode)) {
2148 archive_set_error(&a->archive, errno,
2149 "Can't set file flags on symlink.");
2150 return (ARCHIVE_WARN);
2152 if (chflags(name, a->st.st_flags) == 0)
2153 return (ARCHIVE_OK);
2155 archive_set_error(&a->archive, errno,
2156 "Failed to set file flags");
2157 return (ARCHIVE_WARN);
2160 #elif defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS)
2162 * Linux uses ioctl() to read and write file flags.
2165 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
2166 mode_t mode, unsigned long set, unsigned long clear)
2170 unsigned long newflags, oldflags;
2171 unsigned long sf_mask = 0;
2173 if (set == 0 && clear == 0)
2174 return (ARCHIVE_OK);
2175 /* Only regular files and dirs can have flags. */
2176 if (!S_ISREG(mode) && !S_ISDIR(mode))
2177 return (ARCHIVE_OK);
2179 /* If we weren't given an fd, open it ourselves. */
2181 myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY);
2183 return (ARCHIVE_OK);
2186 * Linux has no define for the flags that are only settable by
2187 * the root user. This code may seem a little complex, but
2188 * there seem to be some Linux systems that lack these
2189 * defines. (?) The code below degrades reasonably gracefully
2190 * if sf_mask is incomplete.
2192 #ifdef EXT2_IMMUTABLE_FL
2193 sf_mask |= EXT2_IMMUTABLE_FL;
2195 #ifdef EXT2_APPEND_FL
2196 sf_mask |= EXT2_APPEND_FL;
2199 * XXX As above, this would be way simpler if we didn't have
2200 * to read the current flags from disk. XXX
2203 /* Try setting the flags as given. */
2204 if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
2205 newflags = (oldflags & ~clear) | set;
2206 if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
2211 /* If we couldn't set all the flags, try again with a subset. */
2212 if (ioctl(myfd, EXT2_IOC_GETFLAGS, &oldflags) >= 0) {
2213 newflags &= ~sf_mask;
2214 oldflags &= sf_mask;
2215 newflags |= oldflags;
2216 if (ioctl(myfd, EXT2_IOC_SETFLAGS, &newflags) >= 0)
2219 /* We couldn't set the flags, so report the failure. */
2221 archive_set_error(&a->archive, errno,
2222 "Failed to set file flags");
2233 * Of course, some systems have neither BSD chflags() nor Linux' flags
2234 * support through ioctl().
2237 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
2238 mode_t mode, unsigned long set, unsigned long clear)
2240 (void)a; /* UNUSED */
2241 (void)fd; /* UNUSED */
2242 (void)name; /* UNUSED */
2243 (void)mode; /* UNUSED */
2244 (void)set; /* UNUSED */
2245 (void)clear; /* UNUSED */
2246 return (ARCHIVE_OK);
2249 #endif /* __linux */
2251 #ifndef HAVE_POSIX_ACL
2252 /* Default empty function body to satisfy mainline code. */
2254 set_acls(struct archive_write_disk *a)
2256 (void)a; /* UNUSED */
2257 return (ARCHIVE_OK);
2263 * XXX TODO: What about ACL types other than ACCESS and DEFAULT?
2266 set_acls(struct archive_write_disk *a)
2270 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_ACCESS,
2271 ARCHIVE_ENTRY_ACL_TYPE_ACCESS, "access");
2272 if (ret != ARCHIVE_OK)
2274 ret = set_acl(a, a->fd, a->entry, ACL_TYPE_DEFAULT,
2275 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, "default");
2281 set_acl(struct archive_write_disk *a, int fd, struct archive_entry *entry,
2282 acl_type_t acl_type, int ae_requested_type, const char *tname)
2285 acl_entry_t acl_entry;
2286 acl_permset_t acl_permset;
2288 int ae_type, ae_permset, ae_tag, ae_id;
2291 const char *ae_name;
2296 entries = archive_entry_acl_reset(entry, ae_requested_type);
2298 return (ARCHIVE_OK);
2299 acl = acl_init(entries);
2300 while (archive_entry_acl_next(entry, ae_requested_type, &ae_type,
2301 &ae_permset, &ae_tag, &ae_id, &ae_name) == ARCHIVE_OK) {
2302 acl_create_entry(&acl, &acl_entry);
2305 case ARCHIVE_ENTRY_ACL_USER:
2306 acl_set_tag_type(acl_entry, ACL_USER);
2307 ae_uid = a->lookup_uid(a->lookup_uid_data,
2309 acl_set_qualifier(acl_entry, &ae_uid);
2311 case ARCHIVE_ENTRY_ACL_GROUP:
2312 acl_set_tag_type(acl_entry, ACL_GROUP);
2313 ae_gid = a->lookup_gid(a->lookup_gid_data,
2315 acl_set_qualifier(acl_entry, &ae_gid);
2317 case ARCHIVE_ENTRY_ACL_USER_OBJ:
2318 acl_set_tag_type(acl_entry, ACL_USER_OBJ);
2320 case ARCHIVE_ENTRY_ACL_GROUP_OBJ:
2321 acl_set_tag_type(acl_entry, ACL_GROUP_OBJ);
2323 case ARCHIVE_ENTRY_ACL_MASK:
2324 acl_set_tag_type(acl_entry, ACL_MASK);
2326 case ARCHIVE_ENTRY_ACL_OTHER:
2327 acl_set_tag_type(acl_entry, ACL_OTHER);
2334 acl_get_permset(acl_entry, &acl_permset);
2335 acl_clear_perms(acl_permset);
2336 if (ae_permset & ARCHIVE_ENTRY_ACL_EXECUTE)
2337 acl_add_perm(acl_permset, ACL_EXECUTE);
2338 if (ae_permset & ARCHIVE_ENTRY_ACL_WRITE)
2339 acl_add_perm(acl_permset, ACL_WRITE);
2340 if (ae_permset & ARCHIVE_ENTRY_ACL_READ)
2341 acl_add_perm(acl_permset, ACL_READ);
2344 name = archive_entry_pathname(entry);
2346 /* Try restoring the ACL through 'fd' if we can. */
2348 if (fd >= 0 && acl_type == ACL_TYPE_ACCESS && acl_set_fd(fd, acl) == 0)
2352 #if HAVE_ACL_SET_FD_NP
2353 if (fd >= 0 && acl_set_fd_np(fd, acl, acl_type) == 0)
2358 if (acl_set_file(name, acl_type, acl) != 0) {
2359 archive_set_error(&a->archive, errno, "Failed to set %s acl", tname);
2369 * Restore extended attributes - Linux implementation
2372 set_xattrs(struct archive_write_disk *a)
2374 struct archive_entry *entry = a->entry;
2375 static int warning_done = 0;
2376 int ret = ARCHIVE_OK;
2377 int i = archive_entry_xattr_reset(entry);
2383 archive_entry_xattr_next(entry, &name, &value, &size);
2385 strncmp(name, "xfsroot.", 8) != 0 &&
2386 strncmp(name, "system.", 7) != 0) {
2390 e = fsetxattr(a->fd, name, value, size, 0);
2394 e = lsetxattr(archive_entry_pathname(entry),
2395 name, value, size, 0);
2398 if (errno == ENOTSUP) {
2399 if (!warning_done) {
2401 archive_set_error(&a->archive, errno,
2402 "Cannot restore extended "
2403 "attributes on this file "
2407 archive_set_error(&a->archive, errno,
2408 "Failed to set extended attribute");
2412 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2413 "Invalid extended attribute encountered");
2419 #elif HAVE_EXTATTR_SET_FILE
2421 * Restore extended attributes - FreeBSD implementation
2424 set_xattrs(struct archive_write_disk *a)
2426 struct archive_entry *entry = a->entry;
2427 static int warning_done = 0;
2428 int ret = ARCHIVE_OK;
2429 int i = archive_entry_xattr_reset(entry);
2435 archive_entry_xattr_next(entry, &name, &value, &size);
2440 if (strncmp(name, "user.", 5) == 0) {
2441 /* "user." attributes go to user namespace */
2443 namespace = EXTATTR_NAMESPACE_USER;
2445 /* Warn about other extended attributes. */
2446 archive_set_error(&a->archive,
2447 ARCHIVE_ERRNO_FILE_FORMAT,
2448 "Can't restore extended attribute ``%s''",
2454 #if HAVE_EXTATTR_SET_FD
2456 e = extattr_set_fd(a->fd, namespace, name, value, size);
2459 /* TODO: should we use extattr_set_link() instead? */
2461 e = extattr_set_file(archive_entry_pathname(entry),
2462 namespace, name, value, size);
2464 if (e != (int)size) {
2465 if (errno == ENOTSUP) {
2466 if (!warning_done) {
2468 archive_set_error(&a->archive, errno,
2469 "Cannot restore extended "
2470 "attributes on this file "
2474 archive_set_error(&a->archive, errno,
2475 "Failed to set extended attribute");
2486 * Restore extended attributes - stub implementation for unsupported systems
2489 set_xattrs(struct archive_write_disk *a)
2491 static int warning_done = 0;
2493 /* If there aren't any extended attributes, then it's okay not
2494 * to extract them, otherwise, issue a single warning. */
2495 if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
2497 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2498 "Cannot restore extended attributes on this system");
2499 return (ARCHIVE_WARN);
2501 /* Warning was already emitted; suppress further warnings. */
2502 return (ARCHIVE_OK);
2508 * Trivial implementations of gid/uid lookup functions.
2509 * These are normally overridden by the client, but these stub
2510 * versions ensure that we always have something that works.
2513 trivial_lookup_gid(void *private_data, const char *gname, gid_t gid)
2515 (void)private_data; /* UNUSED */
2516 (void)gname; /* UNUSED */
2521 trivial_lookup_uid(void *private_data, const char *uname, uid_t uid)
2523 (void)private_data; /* UNUSED */
2524 (void)uname; /* UNUSED */
2529 * Test if file on disk is older than entry.
2532 older(struct stat *st, struct archive_entry *entry)
2534 /* First, test the seconds and return if we have a definite answer. */
2535 /* Definitely older. */
2536 if (st->st_mtime < archive_entry_mtime(entry))
2538 /* Definitely younger. */
2539 if (st->st_mtime > archive_entry_mtime(entry))
2541 /* If this platform supports fractional seconds, try those. */
2542 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
2543 /* Definitely older. */
2544 if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry))
2546 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
2547 /* Definitely older. */
2548 if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry))
2550 #elif HAVE_STRUCT_STAT_ST_MTIME_N
2552 if (st->st_mtime_n < archive_entry_mtime_nsec(entry))
2554 #elif HAVE_STRUCT_STAT_ST_UMTIME
2556 if (st->st_umtime * 1000 < archive_entry_mtime_nsec(entry))
2558 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
2560 if (st->st_mtime_usec * 1000 < archive_entry_mtime_nsec(entry))
2563 /* This system doesn't have high-res timestamps. */
2565 /* Same age or newer, so not older. */