2 * Copyright (c) 2003-2010 Tim Kientzle
3 * Copyright (c) 2012 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD$");
31 #if !defined(_WIN32) || defined(__CYGWIN__)
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
39 #ifdef HAVE_SYS_EXTATTR_H
40 #include <sys/extattr.h>
43 #include <sys/xattr.h>
44 #elif HAVE_ATTR_XATTR_H
45 #include <attr/xattr.h>
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_SYS_STAT_H
56 #ifdef HAVE_SYS_TIME_H
59 #ifdef HAVE_SYS_UTIME_H
60 #include <sys/utime.h>
62 #ifdef HAVE_COPYFILE_H
74 #ifdef HAVE_LANGINFO_H
77 #ifdef HAVE_LINUX_FS_H
78 #include <linux/fs.h> /* for Linux file flags */
81 * Some Linux distributions have both linux/ext2_fs.h and ext2fs/ext2_fs.h.
82 * As the include guards don't agree, the order of include is important.
84 #ifdef HAVE_LINUX_EXT2_FS_H
85 #include <linux/ext2_fs.h> /* for Linux file flags */
87 #if defined(HAVE_EXT2FS_EXT2_FS_H) && !defined(__CYGWIN__)
88 #include <ext2fs/ext2_fs.h> /* Linux file flags, broken on Cygwin */
109 #ifdef F_GETTIMES /* Tru64 specific */
110 #include <sys/fcntl1.h>
114 * Macro to cast st_mtime and time_t to an int64 so that 2 numbers can reliably be compared.
116 * It assumes that the input is an integer type of no more than 64 bits.
117 * If the number is less than zero, t must be a signed type, so it fits in
118 * int64_t. Otherwise, it's a nonnegative value so we can cast it to uint64_t
119 * without loss. But it could be a large unsigned value, so we have to clip it
122 #define to_int64_time(t) \
123 ((t) < 0 ? (int64_t)(t) : (uint64_t)(t) > (uint64_t)INT64_MAX ? INT64_MAX : (int64_t)(t))
126 #include <TargetConditionals.h>
127 #if TARGET_OS_MAC && !TARGET_OS_EMBEDDED && HAVE_QUARANTINE_H
128 #include <quarantine.h>
129 #define HAVE_QUARANTINE 1
137 /* TODO: Support Mac OS 'quarantine' feature. This is really just a
138 * standard tag to mark files that have been downloaded as "tainted".
139 * On Mac OS, we should mark the extracted files as tainted if the
140 * archive being read was tainted. Windows has a similar feature; we
141 * should investigate ways to support this generically. */
144 #include "archive_acl_private.h"
145 #include "archive_string.h"
146 #include "archive_endian.h"
147 #include "archive_entry.h"
148 #include "archive_private.h"
149 #include "archive_write_disk_private.h"
158 /* Ignore non-int O_NOFOLLOW constant. */
159 /* gnulib's fcntl.h does this on AIX, but it seems practical everywhere */
160 #if defined O_NOFOLLOW && !(INT_MIN <= O_NOFOLLOW && O_NOFOLLOW <= INT_MAX)
169 struct fixup_entry *next;
170 struct archive_acl acl;
176 unsigned long atime_nanos;
177 unsigned long birthtime_nanos;
178 unsigned long mtime_nanos;
179 unsigned long ctime_nanos;
180 unsigned long fflags_set;
181 size_t mac_metadata_size;
183 int fixup; /* bitmask of what needs fixing */
188 * We use a bitmask to track which operations remain to be done for
189 * this file. In particular, this helps us avoid unnecessary
190 * operations when it's possible to take care of one step as a
191 * side-effect of another. For example, mkdir() can specify the mode
192 * for the newly-created object but symlink() cannot. This means we
193 * can skip chmod() if mkdir() succeeded, but we must explicitly
194 * chmod() if we're trying to create a directory that already exists
195 * (mkdir() failed) or if we're restoring a symlink. Similarly, we
196 * need to verify UID/GID before trying to restore SUID/SGID bits;
197 * that verification can occur explicitly through a stat() call or
198 * implicitly because of a successful chown() call.
200 #define TODO_MODE_FORCE 0x40000000
201 #define TODO_MODE_BASE 0x20000000
202 #define TODO_SUID 0x10000000
203 #define TODO_SUID_CHECK 0x08000000
204 #define TODO_SGID 0x04000000
205 #define TODO_SGID_CHECK 0x02000000
206 #define TODO_APPLEDOUBLE 0x01000000
207 #define TODO_MODE (TODO_MODE_BASE|TODO_SUID|TODO_SGID)
208 #define TODO_TIMES ARCHIVE_EXTRACT_TIME
209 #define TODO_OWNER ARCHIVE_EXTRACT_OWNER
210 #define TODO_FFLAGS ARCHIVE_EXTRACT_FFLAGS
211 #define TODO_ACLS ARCHIVE_EXTRACT_ACL
212 #define TODO_XATTR ARCHIVE_EXTRACT_XATTR
213 #define TODO_MAC_METADATA ARCHIVE_EXTRACT_MAC_METADATA
214 #define TODO_HFS_COMPRESSION ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED
216 struct archive_write_disk {
217 struct archive archive;
220 struct fixup_entry *fixup_list;
221 struct fixup_entry *current_fixup;
224 int64_t skip_file_dev;
225 int64_t skip_file_ino;
228 int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid);
229 void (*cleanup_gid)(void *private);
230 void *lookup_gid_data;
231 int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid);
232 void (*cleanup_uid)(void *private);
233 void *lookup_uid_data;
236 * Full path of last file to satisfy symlink checks.
238 struct archive_string path_safe;
241 * Cached stat data from disk for the current entry.
242 * If this is valid, pst points to st. Otherwise,
248 /* Information about the object being restored right now. */
249 struct archive_entry *entry; /* Entry being extracted. */
250 char *name; /* Name of entry, possibly edited. */
251 struct archive_string _name_data; /* backing store for 'name' */
252 /* Tasks remaining for this object. */
254 /* Tasks deferred until end-of-archive. */
256 /* Options requested by the client. */
258 /* Handle for the file we're restoring. */
260 /* Current offset for writing data to the file. */
262 /* Last offset actually written to disk. */
264 /* Total bytes actually written to files. */
265 int64_t total_bytes_written;
266 /* Maximum size of file, -1 if unknown. */
268 /* Dir we were in before this restore; only for deep paths. */
270 /* Mode we should use for this entry; affected by _PERM and umask. */
272 /* UID/GID to use in restoring this entry. */
278 /* Xattr "com.apple.decmpfs". */
279 uint32_t decmpfs_attr_size;
280 unsigned char *decmpfs_header_p;
281 /* ResourceFork set options used for fsetxattr. */
282 int rsrc_xattr_options;
283 /* Xattr "com.apple.ResourceFork". */
284 unsigned char *resource_fork;
285 size_t resource_fork_allocated_size;
286 unsigned int decmpfs_block_count;
287 uint32_t *decmpfs_block_info;
288 /* Buffer for compressed data. */
289 unsigned char *compressed_buffer;
290 size_t compressed_buffer_size;
291 size_t compressed_buffer_remaining;
292 /* The offset of the ResourceFork where compressed data will
294 uint32_t compressed_rsrc_position;
295 uint32_t compressed_rsrc_position_v;
296 /* Buffer for uncompressed data. */
297 char *uncompressed_buffer;
298 size_t block_remaining_bytes;
299 size_t file_remaining_bytes;
303 int decmpfs_compression_level;
308 * Default mode for dirs created automatically (will be modified by umask).
309 * Note that POSIX specifies 0777 for implicitly-created dirs, "modified
310 * by the process' file creation mask."
312 #define DEFAULT_DIR_MODE 0777
314 * Dir modes are restored in two steps: During the extraction, the permissions
315 * in the archive are modified to match the following limits. During
316 * the post-extract fixup pass, the permissions from the archive are
319 #define MINIMUM_DIR_MODE 0700
320 #define MAXIMUM_DIR_MODE 0775
323 * Maximum uncompressed size of a decmpfs block.
325 #define MAX_DECMPFS_BLOCK_SIZE (64 * 1024)
327 * HFS+ compression type.
329 #define CMP_XATTR 3/* Compressed data in xattr. */
330 #define CMP_RESOURCE_FORK 4/* Compressed data in resource fork. */
332 * HFS+ compression resource fork.
334 #define RSRC_H_SIZE 260 /* Base size of Resource fork header. */
335 #define RSRC_F_SIZE 50 /* Size of Resource fork footer. */
336 /* Size to write compressed data to resource fork. */
337 #define COMPRESSED_W_SIZE (64 * 1024)
338 /* decmpfs definitions. */
339 #define MAX_DECMPFS_XATTR_SIZE 3802
340 #ifndef DECMPFS_XATTR_NAME
341 #define DECMPFS_XATTR_NAME "com.apple.decmpfs"
343 #define DECMPFS_MAGIC 0x636d7066
344 #define DECMPFS_COMPRESSION_MAGIC 0
345 #define DECMPFS_COMPRESSION_TYPE 4
346 #define DECMPFS_UNCOMPRESSED_SIZE 8
347 #define DECMPFS_HEADER_SIZE 16
349 #define HFS_BLOCKS(s) ((s) >> 12)
351 static void fsobj_error(int *, struct archive_string *, int, const char *,
353 static int check_symlinks_fsobj(char *, int *, struct archive_string *,
355 static int check_symlinks(struct archive_write_disk *);
356 static int create_filesystem_object(struct archive_write_disk *);
357 static struct fixup_entry *current_fixup(struct archive_write_disk *,
358 const char *pathname);
359 #if defined(HAVE_FCHDIR) && defined(PATH_MAX)
360 static void edit_deep_directories(struct archive_write_disk *ad);
362 static int cleanup_pathname_fsobj(char *, int *, struct archive_string *,
364 static int cleanup_pathname(struct archive_write_disk *);
365 static int create_dir(struct archive_write_disk *, char *);
366 static int create_parent_dir(struct archive_write_disk *, char *);
367 static ssize_t hfs_write_data_block(struct archive_write_disk *,
368 const char *, size_t);
369 static int fixup_appledouble(struct archive_write_disk *, const char *);
370 static int older(struct stat *, struct archive_entry *);
371 static int restore_entry(struct archive_write_disk *);
372 static int set_mac_metadata(struct archive_write_disk *, const char *,
373 const void *, size_t);
374 static int set_xattrs(struct archive_write_disk *);
375 static int clear_nochange_fflags(struct archive_write_disk *);
376 static int set_fflags(struct archive_write_disk *);
377 static int set_fflags_platform(struct archive_write_disk *, int fd,
378 const char *name, mode_t mode,
379 unsigned long fflags_set, unsigned long fflags_clear);
380 static int set_ownership(struct archive_write_disk *);
381 static int set_mode(struct archive_write_disk *, int mode);
382 static int set_time(int, int, const char *, time_t, long, time_t, long);
383 static int set_times(struct archive_write_disk *, int, int, const char *,
384 time_t, long, time_t, long, time_t, long, time_t, long);
385 static int set_times_from_entry(struct archive_write_disk *);
386 static struct fixup_entry *sort_dir_list(struct fixup_entry *p);
387 static ssize_t write_data_block(struct archive_write_disk *,
388 const char *, size_t);
390 static struct archive_vtable *archive_write_disk_vtable(void);
392 static int _archive_write_disk_close(struct archive *);
393 static int _archive_write_disk_free(struct archive *);
394 static int _archive_write_disk_header(struct archive *,
395 struct archive_entry *);
396 static int64_t _archive_write_disk_filter_bytes(struct archive *, int);
397 static int _archive_write_disk_finish_entry(struct archive *);
398 static ssize_t _archive_write_disk_data(struct archive *, const void *,
400 static ssize_t _archive_write_disk_data_block(struct archive *, const void *,
404 lazy_stat(struct archive_write_disk *a)
406 if (a->pst != NULL) {
407 /* Already have stat() data available. */
411 if (a->fd >= 0 && fstat(a->fd, &a->st) == 0) {
417 * XXX At this point, symlinks should not be hit, otherwise
418 * XXX a race occurred. Do we want to check explicitly for that?
420 if (lstat(a->name, &a->st) == 0) {
424 archive_set_error(&a->archive, errno, "Couldn't stat file");
425 return (ARCHIVE_WARN);
428 static struct archive_vtable *
429 archive_write_disk_vtable(void)
431 static struct archive_vtable av;
432 static int inited = 0;
435 av.archive_close = _archive_write_disk_close;
436 av.archive_filter_bytes = _archive_write_disk_filter_bytes;
437 av.archive_free = _archive_write_disk_free;
438 av.archive_write_header = _archive_write_disk_header;
439 av.archive_write_finish_entry
440 = _archive_write_disk_finish_entry;
441 av.archive_write_data = _archive_write_disk_data;
442 av.archive_write_data_block = _archive_write_disk_data_block;
449 _archive_write_disk_filter_bytes(struct archive *_a, int n)
451 struct archive_write_disk *a = (struct archive_write_disk *)_a;
452 (void)n; /* UNUSED */
453 if (n == -1 || n == 0)
454 return (a->total_bytes_written);
460 archive_write_disk_set_options(struct archive *_a, int flags)
462 struct archive_write_disk *a = (struct archive_write_disk *)_a;
470 * Extract this entry to disk.
472 * TODO: Validate hardlinks. According to the standards, we're
473 * supposed to check each extracted hardlink and squawk if it refers
474 * to a file that we didn't restore. I'm not entirely convinced this
475 * is a good idea, but more importantly: Is there any way to validate
476 * hardlinks without keeping a complete list of filenames from the
477 * entire archive?? Ugh.
481 _archive_write_disk_header(struct archive *_a, struct archive_entry *entry)
483 struct archive_write_disk *a = (struct archive_write_disk *)_a;
484 struct fixup_entry *fe;
487 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
488 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
489 "archive_write_disk_header");
490 archive_clear_error(&a->archive);
491 if (a->archive.state & ARCHIVE_STATE_DATA) {
492 r = _archive_write_disk_finish_entry(&a->archive);
493 if (r == ARCHIVE_FATAL)
497 /* Set up for this particular entry. */
499 a->current_fixup = NULL;
502 archive_entry_free(a->entry);
505 a->entry = archive_entry_clone(entry);
510 a->uid = a->user_uid;
511 a->mode = archive_entry_mode(a->entry);
512 if (archive_entry_size_is_set(a->entry))
513 a->filesize = archive_entry_size(a->entry);
516 archive_strcpy(&(a->_name_data), archive_entry_pathname(a->entry));
517 a->name = a->_name_data.s;
518 archive_clear_error(&a->archive);
521 * Clean up the requested path. This is necessary for correct
522 * dir restores; the dir restore logic otherwise gets messed
523 * up by nonsense like "dir/.".
525 ret = cleanup_pathname(a);
526 if (ret != ARCHIVE_OK)
530 * Query the umask so we get predictable mode settings.
531 * This gets done on every call to _write_header in case the
532 * user edits their umask during the extraction for some
535 umask(a->user_umask = umask(0));
537 /* Figure out what we need to do for this entry. */
538 a->todo = TODO_MODE_BASE;
539 if (a->flags & ARCHIVE_EXTRACT_PERM) {
540 a->todo |= TODO_MODE_FORCE; /* Be pushy about permissions. */
542 * SGID requires an extra "check" step because we
543 * cannot easily predict the GID that the system will
544 * assign. (Different systems assign GIDs to files
545 * based on a variety of criteria, including process
546 * credentials and the gid of the enclosing
547 * directory.) We can only restore the SGID bit if
548 * the file has the right GID, and we only know the
549 * GID if we either set it (see set_ownership) or if
550 * we've actually called stat() on the file after it
551 * was restored. Since there are several places at
552 * which we might verify the GID, we need a TODO bit
555 if (a->mode & S_ISGID)
556 a->todo |= TODO_SGID | TODO_SGID_CHECK;
558 * Verifying the SUID is simpler, but can still be
559 * done in multiple ways, hence the separate "check" bit.
561 if (a->mode & S_ISUID)
562 a->todo |= TODO_SUID | TODO_SUID_CHECK;
565 * User didn't request full permissions, so don't
566 * restore SUID, SGID bits and obey umask.
571 a->mode &= ~a->user_umask;
573 if (a->flags & ARCHIVE_EXTRACT_OWNER)
574 a->todo |= TODO_OWNER;
575 if (a->flags & ARCHIVE_EXTRACT_TIME)
576 a->todo |= TODO_TIMES;
577 if (a->flags & ARCHIVE_EXTRACT_ACL) {
578 #if ARCHIVE_ACL_DARWIN
580 * On MacOS, platform ACLs get stored in mac_metadata, too.
581 * If we intend to extract mac_metadata and it is present
582 * we skip extracting libarchive NFSv4 ACLs.
584 size_t metadata_size;
586 if ((a->flags & ARCHIVE_EXTRACT_MAC_METADATA) == 0 ||
587 archive_entry_mac_metadata(a->entry,
588 &metadata_size) == NULL || metadata_size == 0)
590 #if ARCHIVE_ACL_LIBRICHACL
592 * RichACLs are stored in an extended attribute.
593 * If we intend to extract extended attributes and have this
594 * attribute we skip extracting libarchive NFSv4 ACLs.
596 short extract_acls = 1;
597 if (a->flags & ARCHIVE_EXTRACT_XATTR && (
598 archive_entry_acl_types(a->entry) &
599 ARCHIVE_ENTRY_ACL_TYPE_NFS4)) {
600 const char *attr_name;
601 const void *attr_value;
603 int i = archive_entry_xattr_reset(a->entry);
605 archive_entry_xattr_next(a->entry, &attr_name,
606 &attr_value, &attr_size);
607 if (attr_name != NULL && attr_value != NULL &&
608 attr_size > 0 && strcmp(attr_name,
609 "trusted.richacl") == 0) {
617 #if ARCHIVE_ACL_DARWIN || ARCHIVE_ACL_LIBRICHACL
620 if (archive_entry_filetype(a->entry) == AE_IFDIR)
621 a->deferred |= TODO_ACLS;
623 a->todo |= TODO_ACLS;
624 #if ARCHIVE_ACL_DARWIN || ARCHIVE_ACL_LIBRICHACL
628 if (a->flags & ARCHIVE_EXTRACT_MAC_METADATA) {
629 if (archive_entry_filetype(a->entry) == AE_IFDIR)
630 a->deferred |= TODO_MAC_METADATA;
632 a->todo |= TODO_MAC_METADATA;
634 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H)
635 if ((a->flags & ARCHIVE_EXTRACT_NO_HFS_COMPRESSION) == 0) {
636 unsigned long set, clear;
637 archive_entry_fflags(a->entry, &set, &clear);
638 if ((set & ~clear) & UF_COMPRESSED) {
639 a->todo |= TODO_HFS_COMPRESSION;
640 a->decmpfs_block_count = (unsigned)-1;
643 if ((a->flags & ARCHIVE_EXTRACT_HFS_COMPRESSION_FORCED) != 0 &&
644 (a->mode & AE_IFMT) == AE_IFREG && a->filesize > 0) {
645 a->todo |= TODO_HFS_COMPRESSION;
646 a->decmpfs_block_count = (unsigned)-1;
651 /* Check if the current file name is a type of the
652 * resource fork file. */
653 p = strrchr(a->name, '/');
658 if (p[0] == '.' && p[1] == '_') {
659 /* Do not compress "._XXX" files. */
660 a->todo &= ~TODO_HFS_COMPRESSION;
662 a->todo |= TODO_APPLEDOUBLE;
667 if (a->flags & ARCHIVE_EXTRACT_XATTR) {
668 #if ARCHIVE_XATTR_DARWIN
670 * On MacOS, extended attributes get stored in mac_metadata,
671 * too. If we intend to extract mac_metadata and it is present
672 * we skip extracting extended attributes.
674 size_t metadata_size;
676 if ((a->flags & ARCHIVE_EXTRACT_MAC_METADATA) == 0 ||
677 archive_entry_mac_metadata(a->entry,
678 &metadata_size) == NULL || metadata_size == 0)
680 a->todo |= TODO_XATTR;
682 if (a->flags & ARCHIVE_EXTRACT_FFLAGS)
683 a->todo |= TODO_FFLAGS;
684 if (a->flags & ARCHIVE_EXTRACT_SECURE_SYMLINKS) {
685 ret = check_symlinks(a);
686 if (ret != ARCHIVE_OK)
689 #if defined(HAVE_FCHDIR) && defined(PATH_MAX)
690 /* If path exceeds PATH_MAX, shorten the path. */
691 edit_deep_directories(a);
694 ret = restore_entry(a);
696 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H)
698 * Check if the filesystem the file is restoring on supports
699 * HFS+ Compression. If not, cancel HFS+ Compression.
701 if (a->todo | TODO_HFS_COMPRESSION) {
703 * NOTE: UF_COMPRESSED is ignored even if the filesystem
704 * supports HFS+ Compression because the file should
705 * have at least an extended attribute "com.apple.decmpfs"
706 * before the flag is set to indicate that the file have
707 * been compressed. If the filesystem does not support
708 * HFS+ Compression the system call will fail.
710 if (a->fd < 0 || fchflags(a->fd, UF_COMPRESSED) != 0)
711 a->todo &= ~TODO_HFS_COMPRESSION;
716 * TODO: There are rumours that some extended attributes must
717 * be restored before file data is written. If this is true,
718 * then we either need to write all extended attributes both
719 * before and after restoring the data, or find some rule for
720 * determining which must go first and which last. Due to the
721 * many ways people are using xattrs, this may prove to be an
722 * intractable problem.
726 /* If we changed directory above, restore it here. */
727 if (a->restore_pwd >= 0) {
728 r = fchdir(a->restore_pwd);
730 archive_set_error(&a->archive, errno,
734 close(a->restore_pwd);
740 * Fixup uses the unedited pathname from archive_entry_pathname(),
741 * because it is relative to the base dir and the edited path
742 * might be relative to some intermediate dir as a result of the
743 * deep restore logic.
745 if (a->deferred & TODO_MODE) {
746 fe = current_fixup(a, archive_entry_pathname(entry));
748 return (ARCHIVE_FATAL);
749 fe->fixup |= TODO_MODE_BASE;
753 if ((a->deferred & TODO_TIMES)
754 && (archive_entry_mtime_is_set(entry)
755 || archive_entry_atime_is_set(entry))) {
756 fe = current_fixup(a, archive_entry_pathname(entry));
758 return (ARCHIVE_FATAL);
760 fe->fixup |= TODO_TIMES;
761 if (archive_entry_atime_is_set(entry)) {
762 fe->atime = archive_entry_atime(entry);
763 fe->atime_nanos = archive_entry_atime_nsec(entry);
765 /* If atime is unset, use start time. */
766 fe->atime = a->start_time;
769 if (archive_entry_mtime_is_set(entry)) {
770 fe->mtime = archive_entry_mtime(entry);
771 fe->mtime_nanos = archive_entry_mtime_nsec(entry);
773 /* If mtime is unset, use start time. */
774 fe->mtime = a->start_time;
777 if (archive_entry_birthtime_is_set(entry)) {
778 fe->birthtime = archive_entry_birthtime(entry);
779 fe->birthtime_nanos = archive_entry_birthtime_nsec(
782 /* If birthtime is unset, use mtime. */
783 fe->birthtime = fe->mtime;
784 fe->birthtime_nanos = fe->mtime_nanos;
788 if (a->deferred & TODO_ACLS) {
789 fe = current_fixup(a, archive_entry_pathname(entry));
791 return (ARCHIVE_FATAL);
792 fe->fixup |= TODO_ACLS;
793 archive_acl_copy(&fe->acl, archive_entry_acl(entry));
796 if (a->deferred & TODO_MAC_METADATA) {
797 const void *metadata;
798 size_t metadata_size;
799 metadata = archive_entry_mac_metadata(a->entry, &metadata_size);
800 if (metadata != NULL && metadata_size > 0) {
801 fe = current_fixup(a, archive_entry_pathname(entry));
803 return (ARCHIVE_FATAL);
804 fe->mac_metadata = malloc(metadata_size);
805 if (fe->mac_metadata != NULL) {
806 memcpy(fe->mac_metadata, metadata,
808 fe->mac_metadata_size = metadata_size;
809 fe->fixup |= TODO_MAC_METADATA;
814 if (a->deferred & TODO_FFLAGS) {
815 fe = current_fixup(a, archive_entry_pathname(entry));
817 return (ARCHIVE_FATAL);
818 fe->fixup |= TODO_FFLAGS;
819 /* TODO: Complete this.. defer fflags from below. */
822 /* We've created the object and are ready to pour data into it. */
823 if (ret >= ARCHIVE_WARN)
824 a->archive.state = ARCHIVE_STATE_DATA;
826 * If it's not open, tell our client not to try writing.
827 * In particular, dirs, links, etc, don't get written to.
830 archive_entry_set_size(entry, 0);
838 archive_write_disk_set_skip_file(struct archive *_a, int64_t d, int64_t i)
840 struct archive_write_disk *a = (struct archive_write_disk *)_a;
841 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
842 ARCHIVE_STATE_ANY, "archive_write_disk_set_skip_file");
843 a->skip_file_set = 1;
844 a->skip_file_dev = d;
845 a->skip_file_ino = i;
850 write_data_block(struct archive_write_disk *a, const char *buff, size_t size)
852 uint64_t start_size = size;
853 ssize_t bytes_written = 0;
854 ssize_t block_size = 0, bytes_to_write;
859 if (a->filesize == 0 || a->fd < 0) {
860 archive_set_error(&a->archive, 0,
861 "Attempt to write to an empty file");
862 return (ARCHIVE_WARN);
865 if (a->flags & ARCHIVE_EXTRACT_SPARSE) {
866 #if HAVE_STRUCT_STAT_ST_BLKSIZE
868 if ((r = lazy_stat(a)) != ARCHIVE_OK)
870 block_size = a->pst->st_blksize;
872 /* XXX TODO XXX Is there a more appropriate choice here ? */
873 /* This needn't match the filesystem allocation size. */
874 block_size = 16*1024;
878 /* If this write would run beyond the file size, truncate it. */
879 if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize)
880 start_size = size = (size_t)(a->filesize - a->offset);
882 /* Write the data. */
884 if (block_size == 0) {
885 bytes_to_write = size;
887 /* We're sparsifying the file. */
891 /* Skip leading zero bytes. */
892 for (p = buff, end = buff + size; p < end; ++p) {
896 a->offset += p - buff;
902 /* Calculate next block boundary after offset. */
904 = (a->offset / block_size + 1) * block_size;
906 /* If the adjusted write would cross block boundary,
907 * truncate it to the block boundary. */
908 bytes_to_write = size;
909 if (a->offset + bytes_to_write > block_end)
910 bytes_to_write = block_end - a->offset;
912 /* Seek if necessary to the specified offset. */
913 if (a->offset != a->fd_offset) {
914 if (lseek(a->fd, a->offset, SEEK_SET) < 0) {
915 archive_set_error(&a->archive, errno,
917 return (ARCHIVE_FATAL);
919 a->fd_offset = a->offset;
921 bytes_written = write(a->fd, buff, bytes_to_write);
922 if (bytes_written < 0) {
923 archive_set_error(&a->archive, errno, "Write failed");
924 return (ARCHIVE_WARN);
926 buff += bytes_written;
927 size -= bytes_written;
928 a->total_bytes_written += bytes_written;
929 a->offset += bytes_written;
930 a->fd_offset = a->offset;
932 return (start_size - size);
935 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
936 && defined(HAVE_ZLIB_H)
939 * Set UF_COMPRESSED file flag.
940 * This have to be called after hfs_write_decmpfs() because if the
941 * file does not have "com.apple.decmpfs" xattr the flag is ignored.
944 hfs_set_compressed_fflag(struct archive_write_disk *a)
948 if ((r = lazy_stat(a)) != ARCHIVE_OK)
951 a->st.st_flags |= UF_COMPRESSED;
952 if (fchflags(a->fd, a->st.st_flags) != 0) {
953 archive_set_error(&a->archive, errno,
954 "Failed to set UF_COMPRESSED file flag");
955 return (ARCHIVE_WARN);
961 * HFS+ Compression decmpfs
963 * +------------------------------+ +0
964 * | Magic(LE 4 bytes) |
965 * +------------------------------+
966 * | Type(LE 4 bytes) |
967 * +------------------------------+
968 * | Uncompressed size(LE 8 bytes)|
969 * +------------------------------+ +16
971 * | Compressed data |
972 * | (Placed only if Type == 3) |
974 * +------------------------------+ +3802 = MAX_DECMPFS_XATTR_SIZE
976 * Type is 3: decmpfs has compressed data.
977 * Type is 4: Resource Fork has compressed data.
980 * Write "com.apple.decmpfs"
983 hfs_write_decmpfs(struct archive_write_disk *a)
986 uint32_t compression_type;
988 r = fsetxattr(a->fd, DECMPFS_XATTR_NAME, a->decmpfs_header_p,
989 a->decmpfs_attr_size, 0, 0);
991 archive_set_error(&a->archive, errno,
992 "Cannot restore xattr:%s", DECMPFS_XATTR_NAME);
993 compression_type = archive_le32dec(
994 &a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE]);
995 if (compression_type == CMP_RESOURCE_FORK)
996 fremovexattr(a->fd, XATTR_RESOURCEFORK_NAME,
997 XATTR_SHOWCOMPRESSION);
998 return (ARCHIVE_WARN);
1000 return (ARCHIVE_OK);
1004 * HFS+ Compression Resource Fork
1006 * +-----------------------------+
1007 * | Header(260 bytes) |
1008 * +-----------------------------+
1009 * | Block count(LE 4 bytes) |
1010 * +-----------------------------+ --+
1011 * +-- | Offset (LE 4 bytes) | |
1012 * | | [distance from Block count] | | Block 0
1013 * | +-----------------------------+ |
1014 * | | Compressed size(LE 4 bytes) | |
1015 * | +-----------------------------+ --+
1017 * | | .................. |
1019 * | +-----------------------------+ --+
1020 * | | Offset (LE 4 bytes) | |
1021 * | +-----------------------------+ | Block (Block count -1)
1022 * | | Compressed size(LE 4 bytes) | |
1023 * +-> +-----------------------------+ --+
1024 * | Compressed data(n bytes) | Block 0
1025 * +-----------------------------+
1027 * | .................. |
1029 * +-----------------------------+
1030 * | Compressed data(n bytes) | Block (Block count -1)
1031 * +-----------------------------+
1032 * | Footer(50 bytes) |
1033 * +-----------------------------+
1037 * Write the header of "com.apple.ResourceFork"
1040 hfs_write_resource_fork(struct archive_write_disk *a, unsigned char *buff,
1041 size_t bytes, uint32_t position)
1045 ret = fsetxattr(a->fd, XATTR_RESOURCEFORK_NAME, buff, bytes,
1046 position, a->rsrc_xattr_options);
1048 archive_set_error(&a->archive, errno,
1049 "Cannot restore xattr: %s at %u pos %u bytes",
1050 XATTR_RESOURCEFORK_NAME,
1053 return (ARCHIVE_WARN);
1055 a->rsrc_xattr_options &= ~XATTR_CREATE;
1056 return (ARCHIVE_OK);
1060 hfs_write_compressed_data(struct archive_write_disk *a, size_t bytes_compressed)
1064 ret = hfs_write_resource_fork(a, a->compressed_buffer,
1065 bytes_compressed, a->compressed_rsrc_position);
1066 if (ret == ARCHIVE_OK)
1067 a->compressed_rsrc_position += bytes_compressed;
1072 hfs_write_resource_fork_header(struct archive_write_disk *a)
1074 unsigned char *buff;
1075 uint32_t rsrc_bytes;
1076 uint32_t rsrc_header_bytes;
1079 * Write resource fork header + block info.
1081 buff = a->resource_fork;
1082 rsrc_bytes = a->compressed_rsrc_position - RSRC_F_SIZE;
1084 RSRC_H_SIZE + /* Header base size. */
1085 4 + /* Block count. */
1086 (a->decmpfs_block_count * 8);/* Block info */
1087 archive_be32enc(buff, 0x100);
1088 archive_be32enc(buff + 4, rsrc_bytes);
1089 archive_be32enc(buff + 8, rsrc_bytes - 256);
1090 archive_be32enc(buff + 12, 0x32);
1091 memset(buff + 16, 0, 240);
1092 archive_be32enc(buff + 256, rsrc_bytes - 260);
1093 return hfs_write_resource_fork(a, buff, rsrc_header_bytes, 0);
1097 hfs_set_resource_fork_footer(unsigned char *buff, size_t buff_size)
1099 static const char rsrc_footer[RSRC_F_SIZE] = {
1100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1102 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1103 0x00, 0x1c, 0x00, 0x32, 0x00, 0x00, 'c', 'm',
1104 'p', 'f', 0x00, 0x00, 0x00, 0x0a, 0x00, 0x01,
1105 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1108 if (buff_size < sizeof(rsrc_footer))
1110 memcpy(buff, rsrc_footer, sizeof(rsrc_footer));
1111 return (sizeof(rsrc_footer));
1115 hfs_reset_compressor(struct archive_write_disk *a)
1119 if (a->stream_valid)
1120 ret = deflateReset(&a->stream);
1122 ret = deflateInit(&a->stream, a->decmpfs_compression_level);
1125 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1126 "Failed to initialize compressor");
1127 return (ARCHIVE_FATAL);
1129 a->stream_valid = 1;
1131 return (ARCHIVE_OK);
1135 hfs_decompress(struct archive_write_disk *a)
1137 uint32_t *block_info;
1138 unsigned int block_count;
1139 uint32_t data_pos, data_size;
1141 ssize_t bytes_written, bytes_to_write;
1144 block_info = (uint32_t *)(a->resource_fork + RSRC_H_SIZE);
1145 block_count = archive_le32dec(block_info++);
1146 while (block_count--) {
1147 data_pos = RSRC_H_SIZE + archive_le32dec(block_info++);
1148 data_size = archive_le32dec(block_info++);
1149 r = fgetxattr(a->fd, XATTR_RESOURCEFORK_NAME,
1150 a->compressed_buffer, data_size, data_pos, 0);
1151 if (r != data_size) {
1152 archive_set_error(&a->archive,
1153 (r < 0)?errno:ARCHIVE_ERRNO_MISC,
1154 "Failed to read resource fork");
1155 return (ARCHIVE_WARN);
1157 if (a->compressed_buffer[0] == 0xff) {
1158 bytes_to_write = data_size -1;
1159 b = a->compressed_buffer + 1;
1161 uLong dest_len = MAX_DECMPFS_BLOCK_SIZE;
1164 zr = uncompress((Bytef *)a->uncompressed_buffer,
1165 &dest_len, a->compressed_buffer, data_size);
1167 archive_set_error(&a->archive,
1169 "Failed to decompress resource fork");
1170 return (ARCHIVE_WARN);
1172 bytes_to_write = dest_len;
1173 b = (unsigned char *)a->uncompressed_buffer;
1176 bytes_written = write(a->fd, b, bytes_to_write);
1177 if (bytes_written < 0) {
1178 archive_set_error(&a->archive, errno,
1180 return (ARCHIVE_WARN);
1182 bytes_to_write -= bytes_written;
1184 } while (bytes_to_write > 0);
1186 r = fremovexattr(a->fd, XATTR_RESOURCEFORK_NAME, 0);
1188 archive_set_error(&a->archive, errno,
1189 "Failed to remove resource fork");
1190 return (ARCHIVE_WARN);
1192 return (ARCHIVE_OK);
1196 hfs_drive_compressor(struct archive_write_disk *a, const char *buff,
1199 unsigned char *buffer_compressed;
1200 size_t bytes_compressed;
1204 ret = hfs_reset_compressor(a);
1205 if (ret != ARCHIVE_OK)
1208 if (a->compressed_buffer == NULL) {
1211 block_size = COMPRESSED_W_SIZE + RSRC_F_SIZE +
1212 + compressBound(MAX_DECMPFS_BLOCK_SIZE);
1213 a->compressed_buffer = malloc(block_size);
1214 if (a->compressed_buffer == NULL) {
1215 archive_set_error(&a->archive, ENOMEM,
1216 "Can't allocate memory for Resource Fork");
1217 return (ARCHIVE_FATAL);
1219 a->compressed_buffer_size = block_size;
1220 a->compressed_buffer_remaining = block_size;
1223 buffer_compressed = a->compressed_buffer +
1224 a->compressed_buffer_size - a->compressed_buffer_remaining;
1225 a->stream.next_in = (Bytef *)(uintptr_t)(const void *)buff;
1226 a->stream.avail_in = size;
1227 a->stream.next_out = buffer_compressed;
1228 a->stream.avail_out = a->compressed_buffer_remaining;
1230 ret = deflate(&a->stream, Z_FINISH);
1236 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1237 "Failed to compress data");
1238 return (ARCHIVE_FAILED);
1240 } while (ret == Z_OK);
1241 bytes_compressed = a->compressed_buffer_remaining - a->stream.avail_out;
1244 * If the compressed size is larger than the original size,
1245 * throw away compressed data, use uncompressed data instead.
1247 if (bytes_compressed > size) {
1248 buffer_compressed[0] = 0xFF;/* uncompressed marker. */
1249 memcpy(buffer_compressed + 1, buff, size);
1250 bytes_compressed = size + 1;
1252 a->compressed_buffer_remaining -= bytes_compressed;
1255 * If the compressed size is smaller than MAX_DECMPFS_XATTR_SIZE
1256 * and the block count in the file is only one, store compressed
1257 * data to decmpfs xattr instead of the resource fork.
1259 if (a->decmpfs_block_count == 1 &&
1260 (a->decmpfs_attr_size + bytes_compressed)
1261 <= MAX_DECMPFS_XATTR_SIZE) {
1262 archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE],
1264 memcpy(a->decmpfs_header_p + DECMPFS_HEADER_SIZE,
1265 buffer_compressed, bytes_compressed);
1266 a->decmpfs_attr_size += bytes_compressed;
1267 a->compressed_buffer_remaining = a->compressed_buffer_size;
1269 * Finish HFS+ Compression.
1270 * - Write the decmpfs xattr.
1271 * - Set the UF_COMPRESSED file flag.
1273 ret = hfs_write_decmpfs(a);
1274 if (ret == ARCHIVE_OK)
1275 ret = hfs_set_compressed_fflag(a);
1279 /* Update block info. */
1280 archive_le32enc(a->decmpfs_block_info++,
1281 a->compressed_rsrc_position_v - RSRC_H_SIZE);
1282 archive_le32enc(a->decmpfs_block_info++, bytes_compressed);
1283 a->compressed_rsrc_position_v += bytes_compressed;
1286 * Write the compressed data to the resource fork.
1288 bytes_used = a->compressed_buffer_size - a->compressed_buffer_remaining;
1289 while (bytes_used >= COMPRESSED_W_SIZE) {
1290 ret = hfs_write_compressed_data(a, COMPRESSED_W_SIZE);
1291 if (ret != ARCHIVE_OK)
1293 bytes_used -= COMPRESSED_W_SIZE;
1294 if (bytes_used > COMPRESSED_W_SIZE)
1295 memmove(a->compressed_buffer,
1296 a->compressed_buffer + COMPRESSED_W_SIZE,
1299 memcpy(a->compressed_buffer,
1300 a->compressed_buffer + COMPRESSED_W_SIZE,
1303 a->compressed_buffer_remaining = a->compressed_buffer_size - bytes_used;
1306 * If the current block is the last block, write the remaining
1307 * compressed data and the resource fork footer.
1309 if (a->file_remaining_bytes == 0) {
1313 /* Append the resource footer. */
1314 rsrc_size = hfs_set_resource_fork_footer(
1315 a->compressed_buffer + bytes_used,
1316 a->compressed_buffer_remaining);
1317 ret = hfs_write_compressed_data(a, bytes_used + rsrc_size);
1318 a->compressed_buffer_remaining = a->compressed_buffer_size;
1320 /* If the compressed size is not enough smaller than
1321 * the uncompressed size. cancel HFS+ compression.
1322 * TODO: study a behavior of ditto utility and improve
1323 * the condition to fall back into no HFS+ compression. */
1324 bk = HFS_BLOCKS(a->compressed_rsrc_position);
1326 if (bk > HFS_BLOCKS(a->filesize))
1327 return hfs_decompress(a);
1329 * Write the resourcefork header.
1331 if (ret == ARCHIVE_OK)
1332 ret = hfs_write_resource_fork_header(a);
1334 * Finish HFS+ Compression.
1335 * - Write the decmpfs xattr.
1336 * - Set the UF_COMPRESSED file flag.
1338 if (ret == ARCHIVE_OK)
1339 ret = hfs_write_decmpfs(a);
1340 if (ret == ARCHIVE_OK)
1341 ret = hfs_set_compressed_fflag(a);
1347 hfs_write_decmpfs_block(struct archive_write_disk *a, const char *buff,
1350 const char *buffer_to_write;
1351 size_t bytes_to_write;
1354 if (a->decmpfs_block_count == (unsigned)-1) {
1357 unsigned int block_count;
1359 if (a->decmpfs_header_p == NULL) {
1360 new_block = malloc(MAX_DECMPFS_XATTR_SIZE
1361 + sizeof(uint32_t));
1362 if (new_block == NULL) {
1363 archive_set_error(&a->archive, ENOMEM,
1364 "Can't allocate memory for decmpfs");
1365 return (ARCHIVE_FATAL);
1367 a->decmpfs_header_p = new_block;
1369 a->decmpfs_attr_size = DECMPFS_HEADER_SIZE;
1370 archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_MAGIC],
1372 archive_le32enc(&a->decmpfs_header_p[DECMPFS_COMPRESSION_TYPE],
1374 archive_le64enc(&a->decmpfs_header_p[DECMPFS_UNCOMPRESSED_SIZE],
1377 /* Calculate a block count of the file. */
1379 (a->filesize + MAX_DECMPFS_BLOCK_SIZE -1) /
1380 MAX_DECMPFS_BLOCK_SIZE;
1382 * Allocate buffer for resource fork.
1383 * Set up related pointers;
1386 RSRC_H_SIZE + /* header */
1387 4 + /* Block count */
1388 (block_count * sizeof(uint32_t) * 2) +
1389 RSRC_F_SIZE; /* footer */
1390 if (new_size > a->resource_fork_allocated_size) {
1391 new_block = realloc(a->resource_fork, new_size);
1392 if (new_block == NULL) {
1393 archive_set_error(&a->archive, ENOMEM,
1394 "Can't allocate memory for ResourceFork");
1395 return (ARCHIVE_FATAL);
1397 a->resource_fork_allocated_size = new_size;
1398 a->resource_fork = new_block;
1401 /* Allocate uncompressed buffer */
1402 if (a->uncompressed_buffer == NULL) {
1403 new_block = malloc(MAX_DECMPFS_BLOCK_SIZE);
1404 if (new_block == NULL) {
1405 archive_set_error(&a->archive, ENOMEM,
1406 "Can't allocate memory for decmpfs");
1407 return (ARCHIVE_FATAL);
1409 a->uncompressed_buffer = new_block;
1411 a->block_remaining_bytes = MAX_DECMPFS_BLOCK_SIZE;
1412 a->file_remaining_bytes = a->filesize;
1413 a->compressed_buffer_remaining = a->compressed_buffer_size;
1416 * Set up a resource fork.
1418 a->rsrc_xattr_options = XATTR_CREATE;
1419 /* Get the position where we are going to set a bunch
1421 a->decmpfs_block_info =
1422 (uint32_t *)(a->resource_fork + RSRC_H_SIZE);
1423 /* Set the block count to the resource fork. */
1424 archive_le32enc(a->decmpfs_block_info++, block_count);
1425 /* Get the position where we are going to set compressed
1427 a->compressed_rsrc_position =
1428 RSRC_H_SIZE + 4 + (block_count * 8);
1429 a->compressed_rsrc_position_v = a->compressed_rsrc_position;
1430 a->decmpfs_block_count = block_count;
1433 /* Ignore redundant bytes. */
1434 if (a->file_remaining_bytes == 0)
1435 return ((ssize_t)size);
1437 /* Do not overrun a block size. */
1438 if (size > a->block_remaining_bytes)
1439 bytes_to_write = a->block_remaining_bytes;
1441 bytes_to_write = size;
1442 /* Do not overrun the file size. */
1443 if (bytes_to_write > a->file_remaining_bytes)
1444 bytes_to_write = a->file_remaining_bytes;
1446 /* For efficiency, if a copy length is full of the uncompressed
1447 * buffer size, do not copy writing data to it. */
1448 if (bytes_to_write == MAX_DECMPFS_BLOCK_SIZE)
1449 buffer_to_write = buff;
1451 memcpy(a->uncompressed_buffer +
1452 MAX_DECMPFS_BLOCK_SIZE - a->block_remaining_bytes,
1453 buff, bytes_to_write);
1454 buffer_to_write = a->uncompressed_buffer;
1456 a->block_remaining_bytes -= bytes_to_write;
1457 a->file_remaining_bytes -= bytes_to_write;
1459 if (a->block_remaining_bytes == 0 || a->file_remaining_bytes == 0) {
1460 ret = hfs_drive_compressor(a, buffer_to_write,
1461 MAX_DECMPFS_BLOCK_SIZE - a->block_remaining_bytes);
1464 a->block_remaining_bytes = MAX_DECMPFS_BLOCK_SIZE;
1466 /* Ignore redundant bytes. */
1467 if (a->file_remaining_bytes == 0)
1468 return ((ssize_t)size);
1469 return (bytes_to_write);
1473 hfs_write_data_block(struct archive_write_disk *a, const char *buff,
1476 uint64_t start_size = size;
1477 ssize_t bytes_written = 0;
1478 ssize_t bytes_to_write;
1481 return (ARCHIVE_OK);
1483 if (a->filesize == 0 || a->fd < 0) {
1484 archive_set_error(&a->archive, 0,
1485 "Attempt to write to an empty file");
1486 return (ARCHIVE_WARN);
1489 /* If this write would run beyond the file size, truncate it. */
1490 if (a->filesize >= 0 && (int64_t)(a->offset + size) > a->filesize)
1491 start_size = size = (size_t)(a->filesize - a->offset);
1493 /* Write the data. */
1495 bytes_to_write = size;
1496 /* Seek if necessary to the specified offset. */
1497 if (a->offset < a->fd_offset) {
1498 /* Can't support backward move. */
1499 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1501 return (ARCHIVE_FATAL);
1502 } else if (a->offset > a->fd_offset) {
1503 int64_t skip = a->offset - a->fd_offset;
1504 char nullblock[1024];
1506 memset(nullblock, 0, sizeof(nullblock));
1508 if (skip > (int64_t)sizeof(nullblock))
1509 bytes_written = hfs_write_decmpfs_block(
1510 a, nullblock, sizeof(nullblock));
1512 bytes_written = hfs_write_decmpfs_block(
1513 a, nullblock, skip);
1514 if (bytes_written < 0) {
1515 archive_set_error(&a->archive, errno,
1517 return (ARCHIVE_WARN);
1519 skip -= bytes_written;
1522 a->fd_offset = a->offset;
1525 hfs_write_decmpfs_block(a, buff, bytes_to_write);
1526 if (bytes_written < 0)
1527 return (bytes_written);
1528 buff += bytes_written;
1529 size -= bytes_written;
1530 a->total_bytes_written += bytes_written;
1531 a->offset += bytes_written;
1532 a->fd_offset = a->offset;
1534 return (start_size - size);
1538 hfs_write_data_block(struct archive_write_disk *a, const char *buff,
1541 return (write_data_block(a, buff, size));
1546 _archive_write_disk_data_block(struct archive *_a,
1547 const void *buff, size_t size, int64_t offset)
1549 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1552 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1553 ARCHIVE_STATE_DATA, "archive_write_data_block");
1556 if (a->todo & TODO_HFS_COMPRESSION)
1557 r = hfs_write_data_block(a, buff, size);
1559 r = write_data_block(a, buff, size);
1562 if ((size_t)r < size) {
1563 archive_set_error(&a->archive, 0,
1564 "Too much data: Truncating file at %ju bytes",
1565 (uintmax_t)a->filesize);
1566 return (ARCHIVE_WARN);
1568 #if ARCHIVE_VERSION_NUMBER < 3999000
1569 return (ARCHIVE_OK);
1576 _archive_write_disk_data(struct archive *_a, const void *buff, size_t size)
1578 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1580 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1581 ARCHIVE_STATE_DATA, "archive_write_data");
1583 if (a->todo & TODO_HFS_COMPRESSION)
1584 return (hfs_write_data_block(a, buff, size));
1585 return (write_data_block(a, buff, size));
1589 _archive_write_disk_finish_entry(struct archive *_a)
1591 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1592 int ret = ARCHIVE_OK;
1594 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1595 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
1596 "archive_write_finish_entry");
1597 if (a->archive.state & ARCHIVE_STATE_HEADER)
1598 return (ARCHIVE_OK);
1599 archive_clear_error(&a->archive);
1601 /* Pad or truncate file to the right size. */
1603 /* There's no file. */
1604 } else if (a->filesize < 0) {
1605 /* File size is unknown, so we can't set the size. */
1606 } else if (a->fd_offset == a->filesize) {
1607 /* Last write ended at exactly the filesize; we're done. */
1608 /* Hopefully, this is the common case. */
1609 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_ZLIB_H)
1610 } else if (a->todo & TODO_HFS_COMPRESSION) {
1614 if (a->file_remaining_bytes)
1615 memset(null_d, 0, sizeof(null_d));
1616 while (a->file_remaining_bytes) {
1617 if (a->file_remaining_bytes > sizeof(null_d))
1618 r = hfs_write_data_block(
1619 a, null_d, sizeof(null_d));
1621 r = hfs_write_data_block(
1622 a, null_d, a->file_remaining_bytes);
1629 if (ftruncate(a->fd, a->filesize) == -1 &&
1631 archive_set_error(&a->archive, errno,
1632 "File size could not be restored");
1633 return (ARCHIVE_FAILED);
1637 * Not all platforms implement the XSI option to
1638 * extend files via ftruncate. Stat() the file again
1639 * to see what happened.
1642 if ((ret = lazy_stat(a)) != ARCHIVE_OK)
1644 /* We can use lseek()/write() to extend the file if
1645 * ftruncate didn't work or isn't available. */
1646 if (a->st.st_size < a->filesize) {
1647 const char nul = '\0';
1648 if (lseek(a->fd, a->filesize - 1, SEEK_SET) < 0) {
1649 archive_set_error(&a->archive, errno,
1651 return (ARCHIVE_FATAL);
1653 if (write(a->fd, &nul, 1) < 0) {
1654 archive_set_error(&a->archive, errno,
1655 "Write to restore size failed");
1656 return (ARCHIVE_FATAL);
1662 /* Restore metadata. */
1665 * This is specific to Mac OS X.
1666 * If the current file is an AppleDouble file, it should be
1667 * linked with the data fork file and remove it.
1669 if (a->todo & TODO_APPLEDOUBLE) {
1670 int r2 = fixup_appledouble(a, a->name);
1671 if (r2 == ARCHIVE_EOF) {
1672 /* The current file has been successfully linked
1673 * with the data fork file and removed. So there
1674 * is nothing to do on the current file. */
1675 goto finish_metadata;
1677 if (r2 < ret) ret = r2;
1681 * Look up the "real" UID only if we're going to need it.
1682 * TODO: the TODO_SGID condition can be dropped here, can't it?
1684 if (a->todo & (TODO_OWNER | TODO_SUID | TODO_SGID)) {
1685 a->uid = archive_write_disk_uid(&a->archive,
1686 archive_entry_uname(a->entry),
1687 archive_entry_uid(a->entry));
1689 /* Look up the "real" GID only if we're going to need it. */
1690 /* TODO: the TODO_SUID condition can be dropped here, can't it? */
1691 if (a->todo & (TODO_OWNER | TODO_SGID | TODO_SUID)) {
1692 a->gid = archive_write_disk_gid(&a->archive,
1693 archive_entry_gname(a->entry),
1694 archive_entry_gid(a->entry));
1698 * Restore ownership before set_mode tries to restore suid/sgid
1699 * bits. If we set the owner, we know what it is and can skip
1700 * a stat() call to examine the ownership of the file on disk.
1702 if (a->todo & TODO_OWNER) {
1703 int r2 = set_ownership(a);
1704 if (r2 < ret) ret = r2;
1708 * set_mode must precede ACLs on systems such as Solaris and
1709 * FreeBSD where setting the mode implicitly clears extended ACLs
1711 if (a->todo & TODO_MODE) {
1712 int r2 = set_mode(a, a->mode);
1713 if (r2 < ret) ret = r2;
1717 * Security-related extended attributes (such as
1718 * security.capability on Linux) have to be restored last,
1719 * since they're implicitly removed by other file changes.
1721 if (a->todo & TODO_XATTR) {
1722 int r2 = set_xattrs(a);
1723 if (r2 < ret) ret = r2;
1727 * Some flags prevent file modification; they must be restored after
1728 * file contents are written.
1730 if (a->todo & TODO_FFLAGS) {
1731 int r2 = set_fflags(a);
1732 if (r2 < ret) ret = r2;
1736 * Time must follow most other metadata;
1737 * otherwise atime will get changed.
1739 if (a->todo & TODO_TIMES) {
1740 int r2 = set_times_from_entry(a);
1741 if (r2 < ret) ret = r2;
1745 * Mac extended metadata includes ACLs.
1747 if (a->todo & TODO_MAC_METADATA) {
1748 const void *metadata;
1749 size_t metadata_size;
1750 metadata = archive_entry_mac_metadata(a->entry, &metadata_size);
1751 if (metadata != NULL && metadata_size > 0) {
1752 int r2 = set_mac_metadata(a, archive_entry_pathname(
1753 a->entry), metadata, metadata_size);
1754 if (r2 < ret) ret = r2;
1759 * ACLs must be restored after timestamps because there are
1760 * ACLs that prevent attribute changes (including time).
1762 if (a->todo & TODO_ACLS) {
1764 r2 = archive_write_disk_set_acls(&a->archive, a->fd,
1765 archive_entry_pathname(a->entry),
1766 archive_entry_acl(a->entry),
1767 archive_entry_mode(a->entry));
1768 if (r2 < ret) ret = r2;
1772 /* If there's an fd, we can close it now. */
1777 /* If there's an entry, we can release it now. */
1779 archive_entry_free(a->entry);
1782 a->archive.state = ARCHIVE_STATE_HEADER;
1787 archive_write_disk_set_group_lookup(struct archive *_a,
1789 int64_t (*lookup_gid)(void *private, const char *gname, int64_t gid),
1790 void (*cleanup_gid)(void *private))
1792 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1793 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1794 ARCHIVE_STATE_ANY, "archive_write_disk_set_group_lookup");
1796 if (a->cleanup_gid != NULL && a->lookup_gid_data != NULL)
1797 (a->cleanup_gid)(a->lookup_gid_data);
1799 a->lookup_gid = lookup_gid;
1800 a->cleanup_gid = cleanup_gid;
1801 a->lookup_gid_data = private_data;
1802 return (ARCHIVE_OK);
1806 archive_write_disk_set_user_lookup(struct archive *_a,
1808 int64_t (*lookup_uid)(void *private, const char *uname, int64_t uid),
1809 void (*cleanup_uid)(void *private))
1811 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1812 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1813 ARCHIVE_STATE_ANY, "archive_write_disk_set_user_lookup");
1815 if (a->cleanup_uid != NULL && a->lookup_uid_data != NULL)
1816 (a->cleanup_uid)(a->lookup_uid_data);
1818 a->lookup_uid = lookup_uid;
1819 a->cleanup_uid = cleanup_uid;
1820 a->lookup_uid_data = private_data;
1821 return (ARCHIVE_OK);
1825 archive_write_disk_gid(struct archive *_a, const char *name, int64_t id)
1827 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1828 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1829 ARCHIVE_STATE_ANY, "archive_write_disk_gid");
1831 return (a->lookup_gid)(a->lookup_gid_data, name, id);
1836 archive_write_disk_uid(struct archive *_a, const char *name, int64_t id)
1838 struct archive_write_disk *a = (struct archive_write_disk *)_a;
1839 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
1840 ARCHIVE_STATE_ANY, "archive_write_disk_uid");
1842 return (a->lookup_uid)(a->lookup_uid_data, name, id);
1847 * Create a new archive_write_disk object and initialize it with global state.
1850 archive_write_disk_new(void)
1852 struct archive_write_disk *a;
1854 a = (struct archive_write_disk *)calloc(1, sizeof(*a));
1857 a->archive.magic = ARCHIVE_WRITE_DISK_MAGIC;
1858 /* We're ready to write a header immediately. */
1859 a->archive.state = ARCHIVE_STATE_HEADER;
1860 a->archive.vtable = archive_write_disk_vtable();
1861 a->start_time = time(NULL);
1862 /* Query and restore the umask. */
1863 umask(a->user_umask = umask(0));
1865 a->user_uid = geteuid();
1866 #endif /* HAVE_GETEUID */
1867 if (archive_string_ensure(&a->path_safe, 512) == NULL) {
1872 a->decmpfs_compression_level = 5;
1874 return (&a->archive);
1879 * If pathname is longer than PATH_MAX, chdir to a suitable
1880 * intermediate dir and edit the path down to a shorter suffix. Note
1881 * that this routine never returns an error; if the chdir() attempt
1882 * fails for any reason, we just go ahead with the long pathname. The
1883 * object creation is likely to fail, but any error will get handled
1886 #if defined(HAVE_FCHDIR) && defined(PATH_MAX)
1888 edit_deep_directories(struct archive_write_disk *a)
1891 char *tail = a->name;
1893 /* If path is short, avoid the open() below. */
1894 if (strlen(tail) < PATH_MAX)
1897 /* Try to record our starting dir. */
1898 a->restore_pwd = open(".", O_RDONLY | O_BINARY | O_CLOEXEC);
1899 __archive_ensure_cloexec_flag(a->restore_pwd);
1900 if (a->restore_pwd < 0)
1903 /* As long as the path is too long... */
1904 while (strlen(tail) >= PATH_MAX) {
1905 /* Locate a dir prefix shorter than PATH_MAX. */
1906 tail += PATH_MAX - 8;
1907 while (tail > a->name && *tail != '/')
1909 /* Exit if we find a too-long path component. */
1910 if (tail <= a->name)
1912 /* Create the intermediate dir and chdir to it. */
1913 *tail = '\0'; /* Terminate dir portion */
1914 ret = create_dir(a, a->name);
1915 if (ret == ARCHIVE_OK && chdir(a->name) != 0)
1916 ret = ARCHIVE_FAILED;
1917 *tail = '/'; /* Restore the / we removed. */
1918 if (ret != ARCHIVE_OK)
1921 /* The chdir() succeeded; we've now shortened the path. */
1929 * The main restore function.
1932 restore_entry(struct archive_write_disk *a)
1934 int ret = ARCHIVE_OK, en;
1936 if (a->flags & ARCHIVE_EXTRACT_UNLINK && !S_ISDIR(a->mode)) {
1938 * TODO: Fix this. Apparently, there are platforms
1939 * that still allow root to hose the entire filesystem
1940 * by unlinking a dir. The S_ISDIR() test above
1941 * prevents us from using unlink() here if the new
1942 * object is a dir, but that doesn't mean the old
1943 * object isn't a dir.
1945 if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
1946 (void)clear_nochange_fflags(a);
1947 if (unlink(a->name) == 0) {
1948 /* We removed it, reset cached stat. */
1950 } else if (errno == ENOENT) {
1951 /* File didn't exist, that's just as good. */
1952 } else if (rmdir(a->name) == 0) {
1953 /* It was a dir, but now it's gone. */
1956 /* We tried, but couldn't get rid of it. */
1957 archive_set_error(&a->archive, errno,
1958 "Could not unlink");
1959 return(ARCHIVE_FAILED);
1963 /* Try creating it first; if this fails, we'll try to recover. */
1964 en = create_filesystem_object(a);
1966 if ((en == ENOTDIR || en == ENOENT)
1967 && !(a->flags & ARCHIVE_EXTRACT_NO_AUTODIR)) {
1968 /* If the parent dir doesn't exist, try creating it. */
1969 create_parent_dir(a, a->name);
1970 /* Now try to create the object again. */
1971 en = create_filesystem_object(a);
1974 if ((en == ENOENT) && (archive_entry_hardlink(a->entry) != NULL)) {
1975 archive_set_error(&a->archive, en,
1976 "Hard-link target '%s' does not exist.",
1977 archive_entry_hardlink(a->entry));
1978 return (ARCHIVE_FAILED);
1981 if ((en == EISDIR || en == EEXIST)
1982 && (a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
1983 /* If we're not overwriting, we're done. */
1984 archive_entry_unset_size(a->entry);
1985 return (ARCHIVE_OK);
1989 * Some platforms return EISDIR if you call
1990 * open(O_WRONLY | O_EXCL | O_CREAT) on a directory, some
1991 * return EEXIST. POSIX is ambiguous, requiring EISDIR
1992 * for open(O_WRONLY) on a dir and EEXIST for open(O_EXCL | O_CREAT)
1993 * on an existing item.
1996 /* A dir is in the way of a non-dir, rmdir it. */
1997 if (rmdir(a->name) != 0) {
1998 archive_set_error(&a->archive, errno,
1999 "Can't remove already-existing dir");
2000 return (ARCHIVE_FAILED);
2004 en = create_filesystem_object(a);
2005 } else if (en == EEXIST) {
2007 * We know something is in the way, but we don't know what;
2008 * we need to find out before we go any further.
2012 * The SECURE_SYMLINKS logic has already removed a
2013 * symlink to a dir if the client wants that. So
2014 * follow the symlink if we're creating a dir.
2016 if (S_ISDIR(a->mode))
2017 r = stat(a->name, &a->st);
2019 * If it's not a dir (or it's a broken symlink),
2020 * then don't follow it.
2022 if (r != 0 || !S_ISDIR(a->mode))
2023 r = lstat(a->name, &a->st);
2025 archive_set_error(&a->archive, errno,
2026 "Can't stat existing object");
2027 return (ARCHIVE_FAILED);
2031 * NO_OVERWRITE_NEWER doesn't apply to directories.
2033 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE_NEWER)
2034 && !S_ISDIR(a->st.st_mode)) {
2035 if (!older(&(a->st), a->entry)) {
2036 archive_entry_unset_size(a->entry);
2037 return (ARCHIVE_OK);
2041 /* If it's our archive, we're done. */
2042 if (a->skip_file_set &&
2043 a->st.st_dev == (dev_t)a->skip_file_dev &&
2044 a->st.st_ino == (ino_t)a->skip_file_ino) {
2045 archive_set_error(&a->archive, 0,
2046 "Refusing to overwrite archive");
2047 return (ARCHIVE_FAILED);
2050 if (!S_ISDIR(a->st.st_mode)) {
2051 /* A non-dir is in the way, unlink it. */
2052 if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
2053 (void)clear_nochange_fflags(a);
2054 if (unlink(a->name) != 0) {
2055 archive_set_error(&a->archive, errno,
2056 "Can't unlink already-existing object");
2057 return (ARCHIVE_FAILED);
2061 en = create_filesystem_object(a);
2062 } else if (!S_ISDIR(a->mode)) {
2063 /* A dir is in the way of a non-dir, rmdir it. */
2064 if (a->flags & ARCHIVE_EXTRACT_CLEAR_NOCHANGE_FFLAGS)
2065 (void)clear_nochange_fflags(a);
2066 if (rmdir(a->name) != 0) {
2067 archive_set_error(&a->archive, errno,
2068 "Can't replace existing directory with non-directory");
2069 return (ARCHIVE_FAILED);
2072 en = create_filesystem_object(a);
2075 * There's a dir in the way of a dir. Don't
2076 * waste time with rmdir()/mkdir(), just fix
2077 * up the permissions on the existing dir.
2078 * Note that we don't change perms on existing
2079 * dirs unless _EXTRACT_PERM is specified.
2081 if ((a->mode != a->st.st_mode)
2082 && (a->todo & TODO_MODE_FORCE))
2083 a->deferred |= (a->todo & TODO_MODE);
2084 /* Ownership doesn't need deferred fixup. */
2085 en = 0; /* Forget the EEXIST. */
2090 /* Everything failed; give up here. */
2091 if ((&a->archive)->error == NULL)
2092 archive_set_error(&a->archive, en, "Can't create '%s'",
2094 return (ARCHIVE_FAILED);
2097 a->pst = NULL; /* Cached stat data no longer valid. */
2102 * Returns 0 if creation succeeds, or else returns errno value from
2103 * the failed system call. Note: This function should only ever perform
2104 * a single system call.
2107 create_filesystem_object(struct archive_write_disk *a)
2109 /* Create the entry. */
2110 const char *linkname;
2111 mode_t final_mode, mode;
2113 /* these for check_symlinks_fsobj */
2114 char *linkname_copy; /* non-const copy of linkname */
2116 struct archive_string error_string;
2119 /* We identify hard/symlinks according to the link names. */
2120 /* Since link(2) and symlink(2) don't handle modes, we're done here. */
2121 linkname = archive_entry_hardlink(a->entry);
2122 if (linkname != NULL) {
2126 archive_string_init(&error_string);
2127 linkname_copy = strdup(linkname);
2128 if (linkname_copy == NULL) {
2132 * TODO: consider using the cleaned-up path as the link
2135 r = cleanup_pathname_fsobj(linkname_copy, &error_number,
2136 &error_string, a->flags);
2137 if (r != ARCHIVE_OK) {
2138 archive_set_error(&a->archive, error_number, "%s",
2140 free(linkname_copy);
2141 archive_string_free(&error_string);
2143 * EPERM is more appropriate than error_number for our
2148 r = check_symlinks_fsobj(linkname_copy, &error_number,
2149 &error_string, a->flags);
2150 if (r != ARCHIVE_OK) {
2151 archive_set_error(&a->archive, error_number, "%s",
2153 free(linkname_copy);
2154 archive_string_free(&error_string);
2156 * EPERM is more appropriate than error_number for our
2161 free(linkname_copy);
2162 archive_string_free(&error_string);
2163 r = link(linkname, a->name) ? errno : 0;
2165 * New cpio and pax formats allow hardlink entries
2166 * to carry data, so we may have to open the file
2167 * for hardlink entries.
2169 * If the hardlink was successfully created and
2170 * the archive doesn't have carry data for it,
2171 * consider it to be non-authoritative for meta data.
2172 * This is consistent with GNU tar and BSD pax.
2173 * If the hardlink does carry data, let the last
2174 * archive entry decide ownership.
2176 if (r == 0 && a->filesize <= 0) {
2179 } else if (r == 0 && a->filesize > 0) {
2181 r = lstat(a->name, &st);
2183 r = stat(a->name, &st);
2187 else if ((st.st_mode & AE_IFMT) == AE_IFREG) {
2188 a->fd = open(a->name, O_WRONLY | O_TRUNC |
2189 O_BINARY | O_CLOEXEC | O_NOFOLLOW);
2190 __archive_ensure_cloexec_flag(a->fd);
2198 linkname = archive_entry_symlink(a->entry);
2199 if (linkname != NULL) {
2201 return symlink(linkname, a->name) ? errno : 0;
2208 * The remaining system calls all set permissions, so let's
2209 * try to take advantage of that to avoid an extra chmod()
2210 * call. (Recall that umask is set to zero right now!)
2213 /* Mode we want for the final restored object (w/o file type bits). */
2214 final_mode = a->mode & 07777;
2216 * The mode that will actually be restored in this step. Note
2217 * that SUID, SGID, etc, require additional work to ensure
2218 * security, so we never restore them at this point.
2220 mode = final_mode & 0777 & ~a->user_umask;
2222 switch (a->mode & AE_IFMT) {
2224 /* POSIX requires that we fall through here. */
2227 a->fd = open(a->name,
2228 O_WRONLY | O_CREAT | O_EXCL | O_BINARY | O_CLOEXEC, mode);
2229 __archive_ensure_cloexec_flag(a->fd);
2234 /* Note: we use AE_IFCHR for the case label, and
2235 * S_IFCHR for the mknod() call. This is correct. */
2236 r = mknod(a->name, mode | S_IFCHR,
2237 archive_entry_rdev(a->entry));
2240 /* TODO: Find a better way to warn about our inability
2241 * to restore a char device node. */
2243 #endif /* HAVE_MKNOD */
2246 r = mknod(a->name, mode | S_IFBLK,
2247 archive_entry_rdev(a->entry));
2250 /* TODO: Find a better way to warn about our inability
2251 * to restore a block device node. */
2253 #endif /* HAVE_MKNOD */
2255 mode = (mode | MINIMUM_DIR_MODE) & MAXIMUM_DIR_MODE;
2256 r = mkdir(a->name, mode);
2258 /* Defer setting dir times. */
2259 a->deferred |= (a->todo & TODO_TIMES);
2260 a->todo &= ~TODO_TIMES;
2261 /* Never use an immediate chmod(). */
2262 /* We can't avoid the chmod() entirely if EXTRACT_PERM
2263 * because of SysV SGID inheritance. */
2264 if ((mode != final_mode)
2265 || (a->flags & ARCHIVE_EXTRACT_PERM))
2266 a->deferred |= (a->todo & TODO_MODE);
2267 a->todo &= ~TODO_MODE;
2272 r = mkfifo(a->name, mode);
2275 /* TODO: Find a better way to warn about our inability
2276 * to restore a fifo. */
2278 #endif /* HAVE_MKFIFO */
2281 /* All the system calls above set errno on failure. */
2285 /* If we managed to set the final mode, we've avoided a chmod(). */
2286 if (mode == final_mode)
2287 a->todo &= ~TODO_MODE;
2292 * Cleanup function for archive_extract. Mostly, this involves processing
2293 * the fixup list, which is used to address a number of problems:
2294 * * Dir permissions might prevent us from restoring a file in that
2295 * dir, so we restore the dir with minimum 0700 permissions first,
2296 * then correct the mode at the end.
2297 * * Similarly, the act of restoring a file touches the directory
2298 * and changes the timestamp on the dir, so we have to touch-up dir
2299 * timestamps at the end as well.
2300 * * Some file flags can interfere with the restore by, for example,
2301 * preventing the creation of hardlinks to those files.
2302 * * Mac OS extended metadata includes ACLs, so must be deferred on dirs.
2304 * Note that tar/cpio do not require that archives be in a particular
2305 * order; there is no way to know when the last file has been restored
2306 * within a directory, so there's no way to optimize the memory usage
2307 * here by fixing up the directory any earlier than the
2310 * XXX TODO: Directory ACLs should be restored here, for the same
2311 * reason we set directory perms here. XXX
2314 _archive_write_disk_close(struct archive *_a)
2316 struct archive_write_disk *a = (struct archive_write_disk *)_a;
2317 struct fixup_entry *next, *p;
2320 archive_check_magic(&a->archive, ARCHIVE_WRITE_DISK_MAGIC,
2321 ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
2322 "archive_write_disk_close");
2323 ret = _archive_write_disk_finish_entry(&a->archive);
2325 /* Sort dir list so directories are fixed up in depth-first order. */
2326 p = sort_dir_list(a->fixup_list);
2329 a->pst = NULL; /* Mark stat cache as out-of-date. */
2330 if (p->fixup & TODO_TIMES) {
2331 set_times(a, -1, p->mode, p->name,
2332 p->atime, p->atime_nanos,
2333 p->birthtime, p->birthtime_nanos,
2334 p->mtime, p->mtime_nanos,
2335 p->ctime, p->ctime_nanos);
2337 if (p->fixup & TODO_MODE_BASE)
2338 chmod(p->name, p->mode);
2339 if (p->fixup & TODO_ACLS)
2340 archive_write_disk_set_acls(&a->archive, -1, p->name,
2342 if (p->fixup & TODO_FFLAGS)
2343 set_fflags_platform(a, -1, p->name,
2344 p->mode, p->fflags_set, 0);
2345 if (p->fixup & TODO_MAC_METADATA)
2346 set_mac_metadata(a, p->name, p->mac_metadata,
2347 p->mac_metadata_size);
2349 archive_acl_clear(&p->acl);
2350 free(p->mac_metadata);
2355 a->fixup_list = NULL;
2360 _archive_write_disk_free(struct archive *_a)
2362 struct archive_write_disk *a;
2365 return (ARCHIVE_OK);
2366 archive_check_magic(_a, ARCHIVE_WRITE_DISK_MAGIC,
2367 ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_write_disk_free");
2368 a = (struct archive_write_disk *)_a;
2369 ret = _archive_write_disk_close(&a->archive);
2370 archive_write_disk_set_group_lookup(&a->archive, NULL, NULL, NULL);
2371 archive_write_disk_set_user_lookup(&a->archive, NULL, NULL, NULL);
2373 archive_entry_free(a->entry);
2374 archive_string_free(&a->_name_data);
2375 archive_string_free(&a->archive.error_string);
2376 archive_string_free(&a->path_safe);
2377 a->archive.magic = 0;
2378 __archive_clean(&a->archive);
2379 free(a->decmpfs_header_p);
2380 free(a->resource_fork);
2381 free(a->compressed_buffer);
2382 free(a->uncompressed_buffer);
2383 #if defined(__APPLE__) && defined(UF_COMPRESSED) && defined(HAVE_SYS_XATTR_H)\
2384 && defined(HAVE_ZLIB_H)
2385 if (a->stream_valid) {
2386 switch (deflateEnd(&a->stream)) {
2390 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2391 "Failed to clean up compressor");
2392 ret = ARCHIVE_FATAL;
2402 * Simple O(n log n) merge sort to order the fixup list. In
2403 * particular, we want to restore dir timestamps depth-first.
2405 static struct fixup_entry *
2406 sort_dir_list(struct fixup_entry *p)
2408 struct fixup_entry *a, *b, *t;
2412 /* A one-item list is already sorted. */
2413 if (p->next == NULL)
2416 /* Step 1: split the list. */
2420 /* Step a twice, t once. */
2426 /* Now, t is at the mid-point, so break the list here. */
2431 /* Step 2: Recursively sort the two sub-lists. */
2432 a = sort_dir_list(a);
2433 b = sort_dir_list(b);
2435 /* Step 3: Merge the returned lists. */
2436 /* Pick the first element for the merged list. */
2437 if (strcmp(a->name, b->name) > 0) {
2445 /* Always put the later element on the list first. */
2446 while (a != NULL && b != NULL) {
2447 if (strcmp(a->name, b->name) > 0) {
2457 /* Only one list is non-empty, so just splice it on. */
2467 * Returns a new, initialized fixup entry.
2469 * TODO: Reduce the memory requirements for this list by using a tree
2470 * structure rather than a simple list of names.
2472 static struct fixup_entry *
2473 new_fixup(struct archive_write_disk *a, const char *pathname)
2475 struct fixup_entry *fe;
2477 fe = (struct fixup_entry *)calloc(1, sizeof(struct fixup_entry));
2479 archive_set_error(&a->archive, ENOMEM,
2480 "Can't allocate memory for a fixup");
2483 fe->next = a->fixup_list;
2486 fe->name = strdup(pathname);
2491 * Returns a fixup structure for the current entry.
2493 static struct fixup_entry *
2494 current_fixup(struct archive_write_disk *a, const char *pathname)
2496 if (a->current_fixup == NULL)
2497 a->current_fixup = new_fixup(a, pathname);
2498 return (a->current_fixup);
2501 /* Error helper for new *_fsobj functions */
2503 fsobj_error(int *a_eno, struct archive_string *a_estr,
2504 int err, const char *errstr, const char *path)
2509 archive_string_sprintf(a_estr, "%s%s", errstr, path);
2513 * TODO: Someday, integrate this with the deep dir support; they both
2514 * scan the path and both can be optimized by comparing against other
2517 /* TODO: Extend this to support symlinks on Windows Vista and later. */
2520 * Checks the given path to see if any elements along it are symlinks. Returns
2521 * ARCHIVE_OK if there are none, otherwise puts an error in errmsg.
2524 check_symlinks_fsobj(char *path, int *a_eno, struct archive_string *a_estr,
2527 #if !defined(HAVE_LSTAT)
2528 /* Platform doesn't have lstat, so we can't look for symlinks. */
2529 (void)path; /* UNUSED */
2530 (void)error_number; /* UNUSED */
2531 (void)error_string; /* UNUSED */
2532 (void)flags; /* UNUSED */
2533 return (ARCHIVE_OK);
2535 int res = ARCHIVE_OK;
2544 /* Nothing to do here if name is empty */
2546 return (ARCHIVE_OK);
2549 * Guard against symlink tricks. Reject any archive entry whose
2550 * destination would be altered by a symlink.
2552 * Walk the filename in chunks separated by '/'. For each segment:
2553 * - if it doesn't exist, continue
2554 * - if it's symlink, abort or remove it
2555 * - if it's a directory and it's not the last chunk, cd into it
2557 * head points to the current (relative) path
2558 * tail points to the temporary \0 terminating the segment we're
2559 * currently examining
2560 * c holds what used to be in *tail
2561 * last is 1 if this is the last tail
2563 restore_pwd = open(".", O_RDONLY | O_BINARY | O_CLOEXEC);
2564 __archive_ensure_cloexec_flag(restore_pwd);
2565 if (restore_pwd < 0)
2566 return (ARCHIVE_FATAL);
2570 /* TODO: reintroduce a safe cache here? */
2571 /* Skip the root directory if the path is absolute. */
2572 if(tail == path && tail[0] == '/')
2574 /* Keep going until we've checked the entire name.
2575 * head, tail, path all alias the same string, which is
2576 * temporarily zeroed at tail, so be careful restoring the
2577 * stashed (c=tail[0]) for error messages.
2578 * Exiting the loop with break is okay; continue is not.
2582 * Skip the separator we just consumed, plus any adjacent ones
2584 while (*tail == '/')
2586 /* Skip the next path element. */
2587 while (*tail != '\0' && *tail != '/')
2589 /* is this the last path component? */
2590 last = (tail[0] == '\0') || (tail[0] == '/' && tail[1] == '\0');
2591 /* temporarily truncate the string here */
2594 /* Check that we haven't hit a symlink. */
2595 r = lstat(head, &st);
2598 /* We've hit a dir that doesn't exist; stop now. */
2599 if (errno == ENOENT) {
2603 * Treat any other error as fatal - best to be
2605 * Note: This effectively disables deep
2606 * directory support when security checks are
2607 * enabled. Otherwise, very long pathnames that
2608 * trigger an error here could evade the
2610 * TODO: We could do better, but it would
2611 * probably require merging the symlink checks
2612 * with the deep-directory editing.
2614 fsobj_error(a_eno, a_estr, errno,
2615 "Could not stat ", path);
2616 res = ARCHIVE_FAILED;
2619 } else if (S_ISDIR(st.st_mode)) {
2621 if (chdir(head) != 0) {
2623 fsobj_error(a_eno, a_estr, errno,
2624 "Could not chdir ", path);
2625 res = (ARCHIVE_FATAL);
2628 /* Our view is now from inside this dir: */
2631 } else if (S_ISLNK(st.st_mode)) {
2634 * Last element is symlink; remove it
2635 * so we can overwrite it with the
2636 * item being extracted.
2640 fsobj_error(a_eno, a_estr, errno,
2641 "Could not remove symlink ",
2643 res = ARCHIVE_FAILED;
2647 * Even if we did remove it, a warning
2648 * is in order. The warning is silly,
2649 * though, if we're just replacing one
2650 * symlink with another symlink.
2654 * FIXME: not sure how important this is to
2658 if (!S_ISLNK(path)) {
2659 fsobj_error(a_eno, a_estr, 0,
2660 "Removing symlink ", path);
2663 /* Symlink gone. No more problem! */
2666 } else if (flags & ARCHIVE_EXTRACT_UNLINK) {
2667 /* User asked us to remove problems. */
2668 if (unlink(head) != 0) {
2670 fsobj_error(a_eno, a_estr, 0,
2671 "Cannot remove intervening "
2673 res = ARCHIVE_FAILED;
2678 ARCHIVE_EXTRACT_SECURE_SYMLINKS) == 0) {
2680 * We are not the last element and we want to
2681 * follow symlinks if they are a directory.
2683 * This is needed to extract hardlinks over
2686 r = stat(head, &st);
2689 if (errno == ENOENT) {
2692 fsobj_error(a_eno, a_estr,
2694 "Could not stat ", path);
2695 res = (ARCHIVE_FAILED);
2698 } else if (S_ISDIR(st.st_mode)) {
2699 if (chdir(head) != 0) {
2701 fsobj_error(a_eno, a_estr,
2703 "Could not chdir ", path);
2704 res = (ARCHIVE_FATAL);
2708 * Our view is now from inside
2714 fsobj_error(a_eno, a_estr, 0,
2715 "Cannot extract through "
2717 res = ARCHIVE_FAILED;
2722 fsobj_error(a_eno, a_estr, 0,
2723 "Cannot extract through symlink ", path);
2724 res = ARCHIVE_FAILED;
2728 /* be sure to always maintain this */
2730 if (tail[0] != '\0')
2731 tail++; /* Advance to the next segment. */
2733 /* Catches loop exits via break */
2736 /* If we changed directory above, restore it here. */
2737 if (restore_pwd >= 0) {
2738 r = fchdir(restore_pwd);
2740 fsobj_error(a_eno, a_estr, errno,
2741 "chdir() failure", "");
2746 res = (ARCHIVE_FATAL);
2750 /* TODO: reintroduce a safe cache here? */
2756 * Check a->name for symlinks, returning ARCHIVE_OK if its clean, otherwise
2757 * calls archive_set_error and returns ARCHIVE_{FATAL,FAILED}
2760 check_symlinks(struct archive_write_disk *a)
2762 struct archive_string error_string;
2765 archive_string_init(&error_string);
2766 rc = check_symlinks_fsobj(a->name, &error_number, &error_string,
2768 if (rc != ARCHIVE_OK) {
2769 archive_set_error(&a->archive, error_number, "%s",
2772 archive_string_free(&error_string);
2773 a->pst = NULL; /* to be safe */
2778 #if defined(__CYGWIN__)
2780 * 1. Convert a path separator from '\' to '/' .
2781 * We shouldn't check multibyte character directly because some
2782 * character-set have been using the '\' character for a part of
2783 * its multibyte character code.
2784 * 2. Replace unusable characters in Windows with underscore('_').
2785 * See also : http://msdn.microsoft.com/en-us/library/aa365247.aspx
2788 cleanup_pathname_win(char *path)
2793 int mb, complete, utf8;
2798 utf8 = (strcmp(nl_langinfo(CODESET), "UTF-8") == 0)? 1: 0;
2799 for (p = path; *p != '\0'; p++) {
2802 /* If previous byte is smaller than 128,
2803 * this is not second byte of multibyte characters,
2804 * so we can replace '\' with '/'. */
2808 complete = 0;/* uncompleted. */
2809 } else if (*(unsigned char *)p > 127)
2813 /* Rewrite the path name if its next character is unusable. */
2814 if (*p == ':' || *p == '*' || *p == '?' || *p == '"' ||
2815 *p == '<' || *p == '>' || *p == '|')
2822 * Convert path separator in wide-character.
2825 while (*p != '\0' && alen) {
2826 l = mbtowc(&wc, p, alen);
2827 if (l == (size_t)-1) {
2828 while (*p != '\0') {
2835 if (l == 1 && wc == L'\\')
2844 * Canonicalize the pathname. In particular, this strips duplicate
2845 * '/' characters, '.' elements, and trailing '/'. It also raises an
2846 * error for an empty path, a trailing '..', (if _SECURE_NODOTDOT is
2847 * set) any '..' in the path or (if ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS
2848 * is set) if the path is absolute.
2851 cleanup_pathname_fsobj(char *path, int *a_eno, struct archive_string *a_estr,
2855 char separator = '\0';
2859 fsobj_error(a_eno, a_estr, ARCHIVE_ERRNO_MISC,
2860 "Invalid empty ", "pathname");
2861 return (ARCHIVE_FAILED);
2864 #if defined(__CYGWIN__)
2865 cleanup_pathname_win(path);
2867 /* Skip leading '/'. */
2869 if (flags & ARCHIVE_EXTRACT_SECURE_NOABSOLUTEPATHS) {
2870 fsobj_error(a_eno, a_estr, ARCHIVE_ERRNO_MISC,
2871 "Path is ", "absolute");
2872 return (ARCHIVE_FAILED);
2878 /* Scan the pathname one element at a time. */
2880 /* src points to first char after '/' */
2881 if (src[0] == '\0') {
2883 } else if (src[0] == '/') {
2884 /* Found '//', ignore second one. */
2887 } else if (src[0] == '.') {
2888 if (src[1] == '\0') {
2889 /* Ignore trailing '.' */
2891 } else if (src[1] == '/') {
2895 } else if (src[1] == '.') {
2896 if (src[2] == '/' || src[2] == '\0') {
2897 /* Conditionally warn about '..' */
2899 & ARCHIVE_EXTRACT_SECURE_NODOTDOT) {
2900 fsobj_error(a_eno, a_estr,
2902 "Path contains ", "'..'");
2903 return (ARCHIVE_FAILED);
2907 * Note: Under no circumstances do we
2908 * remove '..' elements. In
2909 * particular, restoring
2910 * '/foo/../bar/' should create the
2911 * 'foo' dir as a side-effect.
2916 /* Copy current element, including leading '/'. */
2919 while (*src != '\0' && *src != '/') {
2926 /* Skip '/' separator. */
2930 * We've just copied zero or more path elements, not including the
2935 * Nothing got copied. The path must have been something
2936 * like '.' or '/' or './' or '/././././/./'.
2943 /* Terminate the result. */
2945 return (ARCHIVE_OK);
2949 cleanup_pathname(struct archive_write_disk *a)
2951 struct archive_string error_string;
2954 archive_string_init(&error_string);
2955 rc = cleanup_pathname_fsobj(a->name, &error_number, &error_string,
2957 if (rc != ARCHIVE_OK) {
2958 archive_set_error(&a->archive, error_number, "%s",
2961 archive_string_free(&error_string);
2966 * Create the parent directory of the specified path, assuming path
2967 * is already in mutable storage.
2970 create_parent_dir(struct archive_write_disk *a, char *path)
2975 /* Remove tail element to obtain parent name. */
2976 slash = strrchr(path, '/');
2978 return (ARCHIVE_OK);
2980 r = create_dir(a, path);
2986 * Create the specified dir, recursing to create parents as necessary.
2988 * Returns ARCHIVE_OK if the path exists when we're done here.
2989 * Otherwise, returns ARCHIVE_FAILED.
2990 * Assumes path is in mutable storage; path is unchanged on exit.
2993 create_dir(struct archive_write_disk *a, char *path)
2996 struct fixup_entry *le;
2998 mode_t mode_final, mode;
3001 /* Check for special names and just skip them. */
3002 slash = strrchr(path, '/');
3008 if (base[0] == '\0' ||
3009 (base[0] == '.' && base[1] == '\0') ||
3010 (base[0] == '.' && base[1] == '.' && base[2] == '\0')) {
3011 /* Don't bother trying to create null path, '.', or '..'. */
3012 if (slash != NULL) {
3014 r = create_dir(a, path);
3018 return (ARCHIVE_OK);
3022 * Yes, this should be stat() and not lstat(). Using lstat()
3023 * here loses the ability to extract through symlinks. Also note
3024 * that this should not use the a->st cache.
3026 if (stat(path, &st) == 0) {
3027 if (S_ISDIR(st.st_mode))
3028 return (ARCHIVE_OK);
3029 if ((a->flags & ARCHIVE_EXTRACT_NO_OVERWRITE)) {
3030 archive_set_error(&a->archive, EEXIST,
3031 "Can't create directory '%s'", path);
3032 return (ARCHIVE_FAILED);
3034 if (unlink(path) != 0) {
3035 archive_set_error(&a->archive, errno,
3036 "Can't create directory '%s': "
3037 "Conflicting file cannot be removed",
3039 return (ARCHIVE_FAILED);
3041 } else if (errno != ENOENT && errno != ENOTDIR) {
3043 archive_set_error(&a->archive, errno,
3044 "Can't test directory '%s'", path);
3045 return (ARCHIVE_FAILED);
3046 } else if (slash != NULL) {
3048 r = create_dir(a, path);
3050 if (r != ARCHIVE_OK)
3055 * Mode we want for the final restored directory. Per POSIX,
3056 * implicitly-created dirs must be created obeying the umask.
3057 * There's no mention whether this is different for privileged
3058 * restores (which the rest of this code handles by pretending
3059 * umask=0). I've chosen here to always obey the user's umask for
3060 * implicit dirs, even if _EXTRACT_PERM was specified.
3062 mode_final = DEFAULT_DIR_MODE & ~a->user_umask;
3063 /* Mode we want on disk during the restore process. */
3065 mode |= MINIMUM_DIR_MODE;
3066 mode &= MAXIMUM_DIR_MODE;
3067 if (mkdir(path, mode) == 0) {
3068 if (mode != mode_final) {
3069 le = new_fixup(a, path);
3071 return (ARCHIVE_FATAL);
3072 le->fixup |=TODO_MODE_BASE;
3073 le->mode = mode_final;
3075 return (ARCHIVE_OK);
3079 * Without the following check, a/b/../b/c/d fails at the
3080 * second visit to 'b', so 'd' can't be created. Note that we
3081 * don't add it to the fixup list here, as it's already been
3084 if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
3085 return (ARCHIVE_OK);
3087 archive_set_error(&a->archive, errno, "Failed to create dir '%s'",
3089 return (ARCHIVE_FAILED);
3093 * Note: Although we can skip setting the user id if the desired user
3094 * id matches the current user, we cannot skip setting the group, as
3095 * many systems set the gid based on the containing directory. So
3096 * we have to perform a chown syscall if we want to set the SGID
3097 * bit. (The alternative is to stat() and then possibly chown(); it's
3098 * more efficient to skip the stat() and just always chown().) Note
3099 * that a successful chown() here clears the TODO_SGID_CHECK bit, which
3100 * allows set_mode to skip the stat() check for the GID.
3103 set_ownership(struct archive_write_disk *a)
3106 /* unfortunately, on win32 there is no 'root' user with uid 0,
3107 so we just have to try the chown and see if it works */
3109 /* If we know we can't change it, don't bother trying. */
3110 if (a->user_uid != 0 && a->user_uid != a->uid) {
3111 archive_set_error(&a->archive, errno,
3112 "Can't set UID=%jd", (intmax_t)a->uid);
3113 return (ARCHIVE_WARN);
3118 /* If we have an fd, we can avoid a race. */
3119 if (a->fd >= 0 && fchown(a->fd, a->uid, a->gid) == 0) {
3120 /* We've set owner and know uid/gid are correct. */
3121 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
3122 return (ARCHIVE_OK);
3126 /* We prefer lchown() but will use chown() if that's all we have. */
3127 /* Of course, if we have neither, this will always fail. */
3129 if (lchown(a->name, a->uid, a->gid) == 0) {
3130 /* We've set owner and know uid/gid are correct. */
3131 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
3132 return (ARCHIVE_OK);
3135 if (!S_ISLNK(a->mode) && chown(a->name, a->uid, a->gid) == 0) {
3136 /* We've set owner and know uid/gid are correct. */
3137 a->todo &= ~(TODO_OWNER | TODO_SGID_CHECK | TODO_SUID_CHECK);
3138 return (ARCHIVE_OK);
3142 archive_set_error(&a->archive, errno,
3143 "Can't set user=%jd/group=%jd for %s",
3144 (intmax_t)a->uid, (intmax_t)a->gid, a->name);
3145 return (ARCHIVE_WARN);
3149 * Note: Returns 0 on success, non-zero on failure.
3152 set_time(int fd, int mode, const char *name,
3153 time_t atime, long atime_nsec,
3154 time_t mtime, long mtime_nsec)
3156 /* Select the best implementation for this platform. */
3157 #if defined(HAVE_UTIMENSAT) && defined(HAVE_FUTIMENS)
3159 * utimensat() and futimens() are defined in
3160 * POSIX.1-2008. They support ns resolution and setting times
3161 * on fds and symlinks.
3163 struct timespec ts[2];
3164 (void)mode; /* UNUSED */
3165 ts[0].tv_sec = atime;
3166 ts[0].tv_nsec = atime_nsec;
3167 ts[1].tv_sec = mtime;
3168 ts[1].tv_nsec = mtime_nsec;
3170 return futimens(fd, ts);
3171 return utimensat(AT_FDCWD, name, ts, AT_SYMLINK_NOFOLLOW);
3175 * The utimes()-family functions support µs-resolution and
3176 * setting times fds and symlinks. utimes() is documented as
3177 * LEGACY by POSIX, futimes() and lutimes() are not described
3180 struct timeval times[2];
3182 times[0].tv_sec = atime;
3183 times[0].tv_usec = atime_nsec / 1000;
3184 times[1].tv_sec = mtime;
3185 times[1].tv_usec = mtime_nsec / 1000;
3189 return (futimes(fd, times));
3191 (void)fd; /* UNUSED */
3194 (void)mode; /* UNUSED */
3195 return (lutimes(name, times));
3199 return (utimes(name, times));
3202 #elif defined(HAVE_UTIME)
3204 * utime() is POSIX-standard but only supports 1s resolution and
3205 * does not support fds or symlinks.
3207 struct utimbuf times;
3208 (void)fd; /* UNUSED */
3209 (void)name; /* UNUSED */
3210 (void)atime_nsec; /* UNUSED */
3211 (void)mtime_nsec; /* UNUSED */
3212 times.actime = atime;
3213 times.modtime = mtime;
3215 return (ARCHIVE_OK);
3216 return (utime(name, ×));
3220 * We don't know how to set the time on this platform.
3222 (void)fd; /* UNUSED */
3223 (void)mode; /* UNUSED */
3224 (void)name; /* UNUSED */
3225 (void)atime_nsec; /* UNUSED */
3226 (void)mtime_nsec; /* UNUSED */
3227 return (ARCHIVE_WARN);
3233 set_time_tru64(int fd, int mode, const char *name,
3234 time_t atime, long atime_nsec,
3235 time_t mtime, long mtime_nsec,
3236 time_t ctime, long ctime_nsec)
3238 struct attr_timbuf tstamp;
3239 tstamp.atime.tv_sec = atime;
3240 tstamp.mtime.tv_sec = mtime;
3241 tstamp.ctime.tv_sec = ctime;
3242 #if defined (__hpux) && defined (__ia64)
3243 tstamp.atime.tv_nsec = atime_nsec;
3244 tstamp.mtime.tv_nsec = mtime_nsec;
3245 tstamp.ctime.tv_nsec = ctime_nsec;
3247 tstamp.atime.tv_usec = atime_nsec / 1000;
3248 tstamp.mtime.tv_usec = mtime_nsec / 1000;
3249 tstamp.ctime.tv_usec = ctime_nsec / 1000;
3251 return (fcntl(fd,F_SETTIMES,&tstamp));
3253 #endif /* F_SETTIMES */
3256 set_times(struct archive_write_disk *a,
3257 int fd, int mode, const char *name,
3258 time_t atime, long atime_nanos,
3259 time_t birthtime, long birthtime_nanos,
3260 time_t mtime, long mtime_nanos,
3261 time_t cctime, long ctime_nanos)
3263 /* Note: set_time doesn't use libarchive return conventions!
3264 * It uses syscall conventions. So 0 here instead of ARCHIVE_OK. */
3269 * on Tru64 try own fcntl first which can restore even the
3270 * ctime, fall back to default code path below if it fails
3271 * or if we are not running as root
3273 if (a->user_uid == 0 &&
3274 set_time_tru64(fd, mode, name,
3275 atime, atime_nanos, mtime,
3276 mtime_nanos, cctime, ctime_nanos) == 0) {
3277 return (ARCHIVE_OK);
3280 (void)cctime; /* UNUSED */
3281 (void)ctime_nanos; /* UNUSED */
3284 #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
3286 * If you have struct stat.st_birthtime, we assume BSD
3287 * birthtime semantics, in which {f,l,}utimes() updates
3288 * birthtime to earliest mtime. So we set the time twice,
3289 * first using the birthtime, then using the mtime. If
3290 * birthtime == mtime, this isn't necessary, so we skip it.
3291 * If birthtime > mtime, then this won't work, so we skip it.
3293 if (birthtime < mtime
3294 || (birthtime == mtime && birthtime_nanos < mtime_nanos))
3295 r1 = set_time(fd, mode, name,
3297 birthtime, birthtime_nanos);
3299 (void)birthtime; /* UNUSED */
3300 (void)birthtime_nanos; /* UNUSED */
3302 r2 = set_time(fd, mode, name,
3304 mtime, mtime_nanos);
3305 if (r1 != 0 || r2 != 0) {
3306 archive_set_error(&a->archive, errno,
3307 "Can't restore time");
3308 return (ARCHIVE_WARN);
3310 return (ARCHIVE_OK);
3314 set_times_from_entry(struct archive_write_disk *a)
3316 time_t atime, birthtime, mtime, cctime;
3317 long atime_nsec, birthtime_nsec, mtime_nsec, ctime_nsec;
3319 /* Suitable defaults. */
3320 atime = birthtime = mtime = cctime = a->start_time;
3321 atime_nsec = birthtime_nsec = mtime_nsec = ctime_nsec = 0;
3323 /* If no time was provided, we're done. */
3324 if (!archive_entry_atime_is_set(a->entry)
3325 #if HAVE_STRUCT_STAT_ST_BIRTHTIME
3326 && !archive_entry_birthtime_is_set(a->entry)
3328 && !archive_entry_mtime_is_set(a->entry))
3329 return (ARCHIVE_OK);
3331 if (archive_entry_atime_is_set(a->entry)) {
3332 atime = archive_entry_atime(a->entry);
3333 atime_nsec = archive_entry_atime_nsec(a->entry);
3335 if (archive_entry_birthtime_is_set(a->entry)) {
3336 birthtime = archive_entry_birthtime(a->entry);
3337 birthtime_nsec = archive_entry_birthtime_nsec(a->entry);
3339 if (archive_entry_mtime_is_set(a->entry)) {
3340 mtime = archive_entry_mtime(a->entry);
3341 mtime_nsec = archive_entry_mtime_nsec(a->entry);
3343 if (archive_entry_ctime_is_set(a->entry)) {
3344 cctime = archive_entry_ctime(a->entry);
3345 ctime_nsec = archive_entry_ctime_nsec(a->entry);
3348 return set_times(a, a->fd, a->mode, a->name,
3350 birthtime, birthtime_nsec,
3352 cctime, ctime_nsec);
3356 set_mode(struct archive_write_disk *a, int mode)
3359 mode &= 07777; /* Strip off file type bits. */
3361 if (a->todo & TODO_SGID_CHECK) {
3363 * If we don't know the GID is right, we must stat()
3364 * to verify it. We can't just check the GID of this
3365 * process, since systems sometimes set GID from
3366 * the enclosing dir or based on ACLs.
3368 if ((r = lazy_stat(a)) != ARCHIVE_OK)
3370 if (a->pst->st_gid != a->gid) {
3372 if (a->flags & ARCHIVE_EXTRACT_OWNER) {
3374 * This is only an error if you
3375 * requested owner restore. If you
3376 * didn't, we'll try to restore
3377 * sgid/suid, but won't consider it a
3378 * problem if we can't.
3380 archive_set_error(&a->archive, -1,
3381 "Can't restore SGID bit");
3385 /* While we're here, double-check the UID. */
3386 if (a->pst->st_uid != a->uid
3387 && (a->todo & TODO_SUID)) {
3389 if (a->flags & ARCHIVE_EXTRACT_OWNER) {
3390 archive_set_error(&a->archive, -1,
3391 "Can't restore SUID bit");
3395 a->todo &= ~TODO_SGID_CHECK;
3396 a->todo &= ~TODO_SUID_CHECK;
3397 } else if (a->todo & TODO_SUID_CHECK) {
3399 * If we don't know the UID is right, we can just check
3400 * the user, since all systems set the file UID from
3403 if (a->user_uid != a->uid) {
3405 if (a->flags & ARCHIVE_EXTRACT_OWNER) {
3406 archive_set_error(&a->archive, -1,
3407 "Can't make file SUID");
3411 a->todo &= ~TODO_SUID_CHECK;
3414 if (S_ISLNK(a->mode)) {
3417 * If this is a symlink, use lchmod(). If the
3418 * platform doesn't support lchmod(), just skip it. A
3419 * platform that doesn't provide a way to set
3420 * permissions on symlinks probably ignores
3421 * permissions on symlinks, so a failure here has no
3424 if (lchmod(a->name, mode) != 0) {
3428 #if ENOTSUP != EOPNOTSUPP
3432 * if lchmod is defined but the platform
3433 * doesn't support it, silently ignore
3438 archive_set_error(&a->archive, errno,
3439 "Can't set permissions to 0%o", (int)mode);
3444 } else if (!S_ISDIR(a->mode)) {
3446 * If it's not a symlink and not a dir, then use
3447 * fchmod() or chmod(), depending on whether we have
3448 * an fd. Dirs get their perms set during the
3449 * post-extract fixup, which is handled elsewhere.
3453 if (fchmod(a->fd, mode) != 0) {
3454 archive_set_error(&a->archive, errno,
3455 "Can't set permissions to 0%o", (int)mode);
3460 /* If this platform lacks fchmod(), then
3461 * we'll just use chmod(). */
3462 if (chmod(a->name, mode) != 0) {
3463 archive_set_error(&a->archive, errno,
3464 "Can't set permissions to 0%o", (int)mode);
3472 set_fflags(struct archive_write_disk *a)
3474 struct fixup_entry *le;
3475 unsigned long set, clear;
3478 mode_t mode = archive_entry_mode(a->entry);
3481 * Make 'critical_flags' hold all file flags that can't be
3482 * immediately restored. For example, on BSD systems,
3483 * SF_IMMUTABLE prevents hardlinks from being created, so
3484 * should not be set until after any hardlinks are created. To
3485 * preserve some semblance of portability, this uses #ifdef
3486 * extensively. Ugly, but it works.
3488 * Yes, Virginia, this does create a security race. It's mitigated
3489 * somewhat by the practice of creating dirs 0700 until the extract
3490 * is done, but it would be nice if we could do more than that.
3491 * People restoring critical file systems should be wary of
3492 * other programs that might try to muck with files as they're
3495 /* Hopefully, the compiler will optimize this mess into a constant. */
3498 critical_flags |= SF_IMMUTABLE;
3501 critical_flags |= UF_IMMUTABLE;
3504 critical_flags |= SF_APPEND;
3507 critical_flags |= UF_APPEND;
3509 #if defined(FS_APPEND_FL)
3510 critical_flags |= FS_APPEND_FL;
3511 #elif defined(EXT2_APPEND_FL)
3512 critical_flags |= EXT2_APPEND_FL;
3514 #if defined(FS_IMMUTABLE_FL)
3515 critical_flags |= FS_IMMUTABLE_FL;
3516 #elif defined(EXT2_IMMUTABLE_FL)
3517 critical_flags |= EXT2_IMMUTABLE_FL;
3519 #ifdef FS_JOURNAL_DATA_FL
3520 critical_flags |= FS_JOURNAL_DATA_FL;
3523 if (a->todo & TODO_FFLAGS) {
3524 archive_entry_fflags(a->entry, &set, &clear);
3527 * The first test encourages the compiler to eliminate
3528 * all of this if it's not necessary.
3530 if ((critical_flags != 0) && (set & critical_flags)) {
3531 le = current_fixup(a, a->name);
3533 return (ARCHIVE_FATAL);
3534 le->fixup |= TODO_FFLAGS;
3535 le->fflags_set = set;
3536 /* Store the mode if it's not already there. */
3537 if ((le->fixup & TODO_MODE) == 0)
3540 r = set_fflags_platform(a, a->fd,
3541 a->name, mode, set, clear);
3542 if (r != ARCHIVE_OK)
3546 return (ARCHIVE_OK);
3550 clear_nochange_fflags(struct archive_write_disk *a)
3553 mode_t mode = archive_entry_mode(a->entry);
3555 /* Hopefully, the compiler will optimize this mess into a constant. */
3558 nochange_flags |= SF_IMMUTABLE;
3561 nochange_flags |= UF_IMMUTABLE;
3564 nochange_flags |= SF_APPEND;
3567 nochange_flags |= UF_APPEND;
3569 #ifdef EXT2_APPEND_FL
3570 nochange_flags |= EXT2_APPEND_FL;
3572 #ifdef EXT2_IMMUTABLE_FL
3573 nochange_flags |= EXT2_IMMUTABLE_FL;
3576 return (set_fflags_platform(a, a->fd, a->name, mode, 0,
3581 #if ( defined(HAVE_LCHFLAGS) || defined(HAVE_CHFLAGS) || defined(HAVE_FCHFLAGS) ) && defined(HAVE_STRUCT_STAT_ST_FLAGS)
3583 * BSD reads flags using stat() and sets them with one of {f,l,}chflags()
3586 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
3587 mode_t mode, unsigned long set, unsigned long clear)
3591 (void)mode; /* UNUSED */
3592 if (set == 0 && clear == 0)
3593 return (ARCHIVE_OK);
3596 * XXX Is the stat here really necessary? Or can I just use
3597 * the 'set' flags directly? In particular, I'm not sure
3598 * about the correct approach if we're overwriting an existing
3599 * file that already has flags on it. XXX
3601 if ((r = lazy_stat(a)) != ARCHIVE_OK)
3604 a->st.st_flags &= ~clear;
3605 a->st.st_flags |= set;
3606 #ifdef HAVE_FCHFLAGS
3607 /* If platform has fchflags() and we were given an fd, use it. */
3608 if (fd >= 0 && fchflags(fd, a->st.st_flags) == 0)
3609 return (ARCHIVE_OK);
3612 * If we can't use the fd to set the flags, we'll use the
3613 * pathname to set flags. We prefer lchflags() but will use
3614 * chflags() if we must.
3616 #ifdef HAVE_LCHFLAGS
3617 if (lchflags(name, a->st.st_flags) == 0)
3618 return (ARCHIVE_OK);
3619 #elif defined(HAVE_CHFLAGS)
3620 if (S_ISLNK(a->st.st_mode)) {
3621 archive_set_error(&a->archive, errno,
3622 "Can't set file flags on symlink.");
3623 return (ARCHIVE_WARN);
3625 if (chflags(name, a->st.st_flags) == 0)
3626 return (ARCHIVE_OK);
3628 archive_set_error(&a->archive, errno,
3629 "Failed to set file flags");
3630 return (ARCHIVE_WARN);
3633 #elif (defined(FS_IOC_GETFLAGS) && defined(FS_IOC_SETFLAGS) && \
3634 defined(HAVE_WORKING_FS_IOC_GETFLAGS)) || \
3635 (defined(EXT2_IOC_GETFLAGS) && defined(EXT2_IOC_SETFLAGS) && \
3636 defined(HAVE_WORKING_EXT2_IOC_GETFLAGS))
3638 * Linux uses ioctl() to read and write file flags.
3641 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
3642 mode_t mode, unsigned long set, unsigned long clear)
3646 int newflags, oldflags;
3649 if (set == 0 && clear == 0)
3650 return (ARCHIVE_OK);
3651 /* Only regular files and dirs can have flags. */
3652 if (!S_ISREG(mode) && !S_ISDIR(mode))
3653 return (ARCHIVE_OK);
3655 /* If we weren't given an fd, open it ourselves. */
3657 myfd = open(name, O_RDONLY | O_NONBLOCK | O_BINARY | O_CLOEXEC);
3658 __archive_ensure_cloexec_flag(myfd);
3661 return (ARCHIVE_OK);
3664 * Linux has no define for the flags that are only settable by
3665 * the root user. This code may seem a little complex, but
3666 * there seem to be some Linux systems that lack these
3667 * defines. (?) The code below degrades reasonably gracefully
3668 * if sf_mask is incomplete.
3670 #if defined(FS_IMMUTABLE_FL)
3671 sf_mask |= FS_IMMUTABLE_FL;
3672 #elif defined(EXT2_IMMUTABLE_FL)
3673 sf_mask |= EXT2_IMMUTABLE_FL;
3675 #if defined(FS_APPEND_FL)
3676 sf_mask |= FS_APPEND_FL;
3677 #elif defined(EXT2_APPEND_FL)
3678 sf_mask |= EXT2_APPEND_FL;
3680 #if defined(FS_JOURNAL_DATA_FL)
3681 sf_mask |= FS_JOURNAL_DATA_FL;
3684 * XXX As above, this would be way simpler if we didn't have
3685 * to read the current flags from disk. XXX
3689 /* Read the current file flags. */
3691 #ifdef FS_IOC_GETFLAGS
3699 /* Try setting the flags as given. */
3700 newflags = (oldflags & ~clear) | set;
3702 #ifdef FS_IOC_SETFLAGS
3712 /* If we couldn't set all the flags, try again with a subset. */
3713 newflags &= ~sf_mask;
3714 oldflags &= sf_mask;
3715 newflags |= oldflags;
3717 #ifdef FS_IOC_SETFLAGS
3725 /* We couldn't set the flags, so report the failure. */
3727 archive_set_error(&a->archive, errno,
3728 "Failed to set file flags");
3739 * Of course, some systems have neither BSD chflags() nor Linux' flags
3740 * support through ioctl().
3743 set_fflags_platform(struct archive_write_disk *a, int fd, const char *name,
3744 mode_t mode, unsigned long set, unsigned long clear)
3746 (void)a; /* UNUSED */
3747 (void)fd; /* UNUSED */
3748 (void)name; /* UNUSED */
3749 (void)mode; /* UNUSED */
3750 (void)set; /* UNUSED */
3751 (void)clear; /* UNUSED */
3752 return (ARCHIVE_OK);
3755 #endif /* __linux */
3757 #ifndef HAVE_COPYFILE_H
3758 /* Default is to simply drop Mac extended metadata. */
3760 set_mac_metadata(struct archive_write_disk *a, const char *pathname,
3761 const void *metadata, size_t metadata_size)
3763 (void)a; /* UNUSED */
3764 (void)pathname; /* UNUSED */
3765 (void)metadata; /* UNUSED */
3766 (void)metadata_size; /* UNUSED */
3767 return (ARCHIVE_OK);
3771 fixup_appledouble(struct archive_write_disk *a, const char *pathname)
3773 (void)a; /* UNUSED */
3774 (void)pathname; /* UNUSED */
3775 return (ARCHIVE_OK);
3780 * On Mac OS, we use copyfile() to unpack the metadata and
3781 * apply it to the target file.
3784 #if defined(HAVE_SYS_XATTR_H)
3786 copy_xattrs(struct archive_write_disk *a, int tmpfd, int dffd)
3789 char *xattr_names = NULL, *xattr_val = NULL;
3790 int ret = ARCHIVE_OK, xattr_i;
3792 xattr_size = flistxattr(tmpfd, NULL, 0, 0);
3793 if (xattr_size == -1) {
3794 archive_set_error(&a->archive, errno,
3795 "Failed to read metadata(xattr)");
3799 xattr_names = malloc(xattr_size);
3800 if (xattr_names == NULL) {
3801 archive_set_error(&a->archive, ENOMEM,
3802 "Can't allocate memory for metadata(xattr)");
3803 ret = ARCHIVE_FATAL;
3806 xattr_size = flistxattr(tmpfd, xattr_names, xattr_size, 0);
3807 if (xattr_size == -1) {
3808 archive_set_error(&a->archive, errno,
3809 "Failed to read metadata(xattr)");
3813 for (xattr_i = 0; xattr_i < xattr_size;
3814 xattr_i += strlen(xattr_names + xattr_i) + 1) {
3815 char *xattr_val_saved;
3819 s = fgetxattr(tmpfd, xattr_names + xattr_i, NULL, 0, 0, 0);
3821 archive_set_error(&a->archive, errno,
3822 "Failed to get metadata(xattr)");
3826 xattr_val_saved = xattr_val;
3827 xattr_val = realloc(xattr_val, s);
3828 if (xattr_val == NULL) {
3829 archive_set_error(&a->archive, ENOMEM,
3830 "Failed to get metadata(xattr)");
3832 free(xattr_val_saved);
3835 s = fgetxattr(tmpfd, xattr_names + xattr_i, xattr_val, s, 0, 0);
3837 archive_set_error(&a->archive, errno,
3838 "Failed to get metadata(xattr)");
3842 f = fsetxattr(dffd, xattr_names + xattr_i, xattr_val, s, 0, 0);
3844 archive_set_error(&a->archive, errno,
3845 "Failed to get metadata(xattr)");
3858 copy_acls(struct archive_write_disk *a, int tmpfd, int dffd)
3860 #ifndef HAVE_SYS_ACL_H
3863 acl_t acl, dfacl = NULL;
3864 int acl_r, ret = ARCHIVE_OK;
3866 acl = acl_get_fd(tmpfd);
3868 if (errno == ENOENT)
3869 /* There are not any ACLs. */
3871 archive_set_error(&a->archive, errno,
3872 "Failed to get metadata(acl)");
3876 dfacl = acl_dup(acl);
3877 acl_r = acl_set_fd(dffd, dfacl);
3879 archive_set_error(&a->archive, errno,
3880 "Failed to get metadata(acl)");
3894 create_tempdatafork(struct archive_write_disk *a, const char *pathname)
3896 struct archive_string tmpdatafork;
3899 archive_string_init(&tmpdatafork);
3900 archive_strcpy(&tmpdatafork, "tar.md.XXXXXX");
3901 tmpfd = mkstemp(tmpdatafork.s);
3903 archive_set_error(&a->archive, errno,
3904 "Failed to mkstemp");
3905 archive_string_free(&tmpdatafork);
3908 if (copyfile(pathname, tmpdatafork.s, 0,
3909 COPYFILE_UNPACK | COPYFILE_NOFOLLOW
3910 | COPYFILE_ACL | COPYFILE_XATTR) < 0) {
3911 archive_set_error(&a->archive, errno,
3912 "Failed to restore metadata");
3916 unlink(tmpdatafork.s);
3917 archive_string_free(&tmpdatafork);
3922 copy_metadata(struct archive_write_disk *a, const char *metadata,
3923 const char *datafork, int datafork_compressed)
3925 int ret = ARCHIVE_OK;
3927 if (datafork_compressed) {
3930 tmpfd = create_tempdatafork(a, metadata);
3932 return (ARCHIVE_WARN);
3935 * Do not open the data fork compressed by HFS+ compression
3936 * with at least a writing mode(O_RDWR or O_WRONLY). it
3937 * makes the data fork uncompressed.
3939 dffd = open(datafork, 0);
3941 archive_set_error(&a->archive, errno,
3942 "Failed to open the data fork for metadata");
3944 return (ARCHIVE_WARN);
3947 #if defined(HAVE_SYS_XATTR_H)
3948 ret = copy_xattrs(a, tmpfd, dffd);
3949 if (ret == ARCHIVE_OK)
3951 ret = copy_acls(a, tmpfd, dffd);
3955 if (copyfile(metadata, datafork, 0,
3956 COPYFILE_UNPACK | COPYFILE_NOFOLLOW
3957 | COPYFILE_ACL | COPYFILE_XATTR) < 0) {
3958 archive_set_error(&a->archive, errno,
3959 "Failed to restore metadata");
3967 set_mac_metadata(struct archive_write_disk *a, const char *pathname,
3968 const void *metadata, size_t metadata_size)
3970 struct archive_string tmp;
3973 int ret = ARCHIVE_OK;
3975 /* This would be simpler if copyfile() could just accept the
3976 * metadata as a block of memory; then we could sidestep this
3977 * silly dance of writing the data to disk just so that
3978 * copyfile() can read it back in again. */
3979 archive_string_init(&tmp);
3980 archive_strcpy(&tmp, pathname);
3981 archive_strcat(&tmp, ".XXXXXX");
3982 fd = mkstemp(tmp.s);
3985 archive_set_error(&a->archive, errno,
3986 "Failed to restore metadata");
3987 archive_string_free(&tmp);
3988 return (ARCHIVE_WARN);
3990 written = write(fd, metadata, metadata_size);
3992 if ((size_t)written != metadata_size) {
3993 archive_set_error(&a->archive, errno,
3994 "Failed to restore metadata");
3999 #if defined(UF_COMPRESSED)
4000 if ((a->todo & TODO_HFS_COMPRESSION) != 0 &&
4001 (ret = lazy_stat(a)) == ARCHIVE_OK)
4002 compressed = a->st.st_flags & UF_COMPRESSED;
4006 ret = copy_metadata(a, tmp.s, pathname, compressed);
4009 archive_string_free(&tmp);
4014 fixup_appledouble(struct archive_write_disk *a, const char *pathname)
4019 struct archive_string datafork;
4020 int fd = -1, ret = ARCHIVE_OK;
4022 archive_string_init(&datafork);
4023 /* Check if the current file name is a type of the resource
4025 p = strrchr(pathname, '/');
4030 if (p[0] != '.' || p[1] != '_')
4031 goto skip_appledouble;
4034 * Check if the data fork file exists.
4036 * TODO: Check if this write disk object has handled it.
4038 archive_strncpy(&datafork, pathname, p - pathname);
4039 archive_strcat(&datafork, p + 2);
4040 if (lstat(datafork.s, &st) == -1 ||
4041 (st.st_mode & AE_IFMT) != AE_IFREG)
4042 goto skip_appledouble;
4045 * Check if the file is in the AppleDouble form.
4047 fd = open(pathname, O_RDONLY | O_BINARY | O_CLOEXEC);
4048 __archive_ensure_cloexec_flag(fd);
4050 archive_set_error(&a->archive, errno,
4051 "Failed to open a restoring file");
4053 goto skip_appledouble;
4055 if (read(fd, buff, 8) == -1) {
4056 archive_set_error(&a->archive, errno,
4057 "Failed to read a restoring file");
4060 goto skip_appledouble;
4063 /* Check AppleDouble Magic Code. */
4064 if (archive_be32dec(buff) != 0x00051607)
4065 goto skip_appledouble;
4066 /* Check AppleDouble Version. */
4067 if (archive_be32dec(buff+4) != 0x00020000)
4068 goto skip_appledouble;
4070 ret = copy_metadata(a, pathname, datafork.s,
4071 #if defined(UF_COMPRESSED)
4072 st.st_flags & UF_COMPRESSED);
4076 if (ret == ARCHIVE_OK) {
4081 archive_string_free(&datafork);
4086 #if ARCHIVE_XATTR_LINUX || ARCHIVE_XATTR_DARWIN || ARCHIVE_XATTR_AIX
4088 * Restore extended attributes - Linux, Darwin and AIX implementations:
4089 * AIX' ea interface is syntaxwise identical to the Linux xattr interface.
4092 set_xattrs(struct archive_write_disk *a)
4094 struct archive_entry *entry = a->entry;
4095 struct archive_string errlist;
4096 int ret = ARCHIVE_OK;
4097 int i = archive_entry_xattr_reset(entry);
4100 archive_string_init(&errlist);
4108 archive_entry_xattr_next(entry, &name, &value, &size);
4112 #if ARCHIVE_XATTR_LINUX
4113 /* Linux: quietly skip POSIX.1e ACL extended attributes */
4114 if (strncmp(name, "system.", 7) == 0 &&
4115 (strcmp(name + 7, "posix_acl_access") == 0 ||
4116 strcmp(name + 7, "posix_acl_default") == 0))
4118 if (strncmp(name, "trusted.SGI_", 12) == 0 &&
4119 (strcmp(name + 12, "ACL_DEFAULT") == 0 ||
4120 strcmp(name + 12, "ACL_FILE") == 0))
4123 /* Linux: xfsroot namespace is obsolete and unsupported */
4124 if (strncmp(name, "xfsroot.", 8) == 0) {
4126 archive_strcat(&errlist, name);
4127 archive_strappend_char(&errlist, ' ');
4133 #if ARCHIVE_XATTR_LINUX
4134 e = fsetxattr(a->fd, name, value, size, 0);
4135 #elif ARCHIVE_XATTR_DARWIN
4136 e = fsetxattr(a->fd, name, value, size, 0, 0);
4137 #elif ARCHIVE_XATTR_AIX
4138 e = fsetea(a->fd, name, value, size, 0);
4141 #if ARCHIVE_XATTR_LINUX
4142 e = lsetxattr(archive_entry_pathname(entry),
4143 name, value, size, 0);
4144 #elif ARCHIVE_XATTR_DARWIN
4145 e = setxattr(archive_entry_pathname(entry),
4146 name, value, size, 0, XATTR_NOFOLLOW);
4147 #elif ARCHIVE_XATTR_AIX
4148 e = lsetea(archive_entry_pathname(entry),
4149 name, value, size, 0);
4154 archive_strcat(&errlist, name);
4155 archive_strappend_char(&errlist, ' ');
4156 if (errno != ENOTSUP && errno != ENOSYS)
4161 if (ret == ARCHIVE_WARN) {
4162 if (fail && errlist.length > 0) {
4164 errlist.s[errlist.length] = '\0';
4165 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
4166 "Cannot restore extended attributes: %s",
4169 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
4170 "Cannot restore extended "
4171 "attributes on this file system.");
4174 archive_string_free(&errlist);
4177 #elif ARCHIVE_XATTR_FREEBSD
4179 * Restore extended attributes - FreeBSD implementation
4182 set_xattrs(struct archive_write_disk *a)
4184 struct archive_entry *entry = a->entry;
4185 struct archive_string errlist;
4186 int ret = ARCHIVE_OK;
4187 int i = archive_entry_xattr_reset(entry);
4190 archive_string_init(&errlist);
4196 archive_entry_xattr_next(entry, &name, &value, &size);
4201 if (strncmp(name, "user.", 5) == 0) {
4202 /* "user." attributes go to user namespace */
4204 namespace = EXTATTR_NAMESPACE_USER;
4206 /* Other namespaces are unsupported */
4207 archive_strcat(&errlist, name);
4208 archive_strappend_char(&errlist, ' ');
4215 e = extattr_set_fd(a->fd, namespace, name,
4218 e = extattr_set_link(
4219 archive_entry_pathname(entry), namespace,
4222 if (e != (ssize_t)size) {
4223 archive_strcat(&errlist, name);
4224 archive_strappend_char(&errlist, ' ');
4226 if (errno != ENOTSUP && errno != ENOSYS)
4232 if (ret == ARCHIVE_WARN) {
4233 if (fail && errlist.length > 0) {
4235 errlist.s[errlist.length] = '\0';
4237 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
4238 "Cannot restore extended attributes: %s",
4241 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
4242 "Cannot restore extended "
4243 "attributes on this file system.");
4246 archive_string_free(&errlist);
4251 * Restore extended attributes - stub implementation for unsupported systems
4254 set_xattrs(struct archive_write_disk *a)
4256 static int warning_done = 0;
4258 /* If there aren't any extended attributes, then it's okay not
4259 * to extract them, otherwise, issue a single warning. */
4260 if (archive_entry_xattr_count(a->entry) != 0 && !warning_done) {
4262 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
4263 "Cannot restore extended attributes on this system");
4264 return (ARCHIVE_WARN);
4266 /* Warning was already emitted; suppress further warnings. */
4267 return (ARCHIVE_OK);
4272 * Test if file on disk is older than entry.
4275 older(struct stat *st, struct archive_entry *entry)
4277 /* First, test the seconds and return if we have a definite answer. */
4278 /* Definitely older. */
4279 if (to_int64_time(st->st_mtime) < to_int64_time(archive_entry_mtime(entry)))
4281 /* Definitely younger. */
4282 if (to_int64_time(st->st_mtime) > to_int64_time(archive_entry_mtime(entry)))
4284 /* If this platform supports fractional seconds, try those. */
4285 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
4286 /* Definitely older. */
4287 if (st->st_mtimespec.tv_nsec < archive_entry_mtime_nsec(entry))
4289 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
4290 /* Definitely older. */
4291 if (st->st_mtim.tv_nsec < archive_entry_mtime_nsec(entry))
4293 #elif HAVE_STRUCT_STAT_ST_MTIME_N
4295 if (st->st_mtime_n < archive_entry_mtime_nsec(entry))
4297 #elif HAVE_STRUCT_STAT_ST_UMTIME
4299 if (st->st_umtime * 1000 < archive_entry_mtime_nsec(entry))
4301 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
4303 if (st->st_mtime_usec * 1000 < archive_entry_mtime_nsec(entry))
4306 /* This system doesn't have high-res timestamps. */
4308 /* Same age or newer, so not older. */
4312 #ifndef ARCHIVE_ACL_SUPPORT
4314 archive_write_disk_set_acls(struct archive *a, int fd, const char *name,
4315 struct archive_acl *abstract_acl, __LA_MODE_T mode)
4317 (void)a; /* UNUSED */
4318 (void)fd; /* UNUSED */
4319 (void)name; /* UNUSED */
4320 (void)abstract_acl; /* UNUSED */
4321 (void)mode; /* UNUSED */
4322 return (ARCHIVE_OK);
4326 #endif /* !_WIN32 || __CYGWIN__ */