2 * Copyright (c) 2004-2010 Apple, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #include <sys/types.h>
35 #include <sys/errno.h>
38 #include <sys/xattr.h>
40 #include <sys/syscall.h>
41 #include <sys/param.h>
42 #include <sys/mount.h>
44 #include <libkern/OSByteOrder.h>
45 #include <membership.h>
48 #include <sys/clonefile.h>
49 #include <System/sys/content_protection.h>
51 #ifdef VOL_CAP_FMT_DECMPFS_COMPRESSION
52 # include <Kernel/sys/decmpfs.h>
55 #include <TargetConditionals.h>
57 #include <quarantine.h>
59 #define XATTR_QUARANTINE_NAME qtn_xattr_name
60 #else /* TARGET_OS_IPHONE */
61 #define qtn_file_t void *
62 #define QTN_SERIALIZED_DATA_MAX 0
63 static void * qtn_file_alloc(void) { return NULL
; }
64 static int qtn_file_init_with_fd(void *x
, int y
) { return -1; }
65 static int qtn_file_init_with_path(void *x
, const char *path
) { return -1; }
66 static int qtn_file_init_with_data(void *x
, const void *data
, size_t len
) { return -1; }
67 static void qtn_file_free(void *x
) { return; }
68 static int qtn_file_apply_to_fd(void *x
, int y
) { return 0; }
69 static char *qtn_error(int x
) { return NULL
; }
70 static int qtn_file_to_data(void *x
, char *y
, size_t z
) { return -1; }
71 static void *qtn_file_clone(void *x
) { return NULL
; }
72 static uint32_t qtn_file_get_flags(void *x
) { return 0; }
73 static int qtn_file_set_flags(void *x
, uint32_t flags
) { return 0; }
74 #define XATTR_QUARANTINE_NAME "figgledidiggledy"
75 #define QTN_FLAG_DO_NOT_TRANSLOCATE 0
76 #endif /* TARGET_OS_IPHONE */
79 #include "copyfile_private.h"
80 #include "xattr_flags.h"
82 #define XATTR_ROOT_INSTALLED_NAME "com.apple.root.installed"
84 enum cfInternalFlags
{
85 cfDelayAce
= 1 << 0, /* set if ACE shouldn't be set until post-order traversal */
86 cfMakeFileInvisible
= 1 << 1, /* set if kFinderInvisibleMask is on src */
87 cfSawDecmpEA
= 1 << 2, /* set if we've seen a com.apple.decmpfs xattr */
88 cfSrcProtSupportValid
= 1 << 3, /* set if cfSrcSupportsCProtect is valid */
89 cfSrcSupportsCProtect
= 1 << 4, /* set if src supports MNT_CPROTECT */
90 cfDstProtSupportValid
= 1 << 5, /* set if cfDstSupportsCProtect is valid */
91 cfDstSupportsCProtect
= 1 << 6, /* set if dst supports MNT_CPROTECT */
94 #define COPYFILE_MNT_CPROTECT_MASK (cfSrcProtSupportValid | cfSrcSupportsCProtect | cfDstProtSupportValid | cfDstSupportsCProtect)
97 * The state structure keeps track of
98 * the source filename, the destination filename, their
99 * associated file-descriptors, the stat information for the
100 * source file, the security information for the source file,
101 * the flags passed in for the copy, a pointer to place statistics
102 * (not currently implemented), debug flags, and a pointer to callbacks
103 * (not currently implemented).
105 struct _copyfile_state
113 copyfile_flags_t flags
;
114 unsigned int internal_flags
;
117 copyfile_callback_t statuscb
;
119 qtn_file_t qinfo
; /* Quarantine information -- probably NULL */
120 filesec_t original_fsec
;
121 filesec_t permissive_fsec
;
125 xattr_operation_intent_t copyIntent
;
129 #define GET_PROT_CLASS(fd) fcntl((fd), F_GETPROTECTIONCLASS)
130 #define SET_PROT_CLASS(fd, prot_class) fcntl((fd), F_SETPROTECTIONCLASS, (prot_class))
134 #define _ACL_ENTRY_MAGIC 0xac1ac101
136 guid_t ae_applicable
;
143 struct acl_entry *__t = (struct acl_entry*)(ace); \
144 fprintf(stderr, "%s(%d): " #ace " = { flags = %#x, perms = %#x }\n", __FUNCTION__, __LINE__, __t->ae_flags, __t->ae_perms); \
149 ssize_t __l; char *__cp = acl_to_text(ace, &__l); \
150 fprintf(stderr, "%s(%d): " #ace " = %s\n", __FUNCTION__, __LINE__, __cp ? __cp : "(null)"); \
154 acl_compare_permset_np(acl_permset_t p1
, acl_permset_t p2
)
156 struct pm
{ u_int32_t ap_perms
; } *ps1
, *ps2
;
157 ps1
= (struct pm
*) p1
;
158 ps2
= (struct pm
*) p2
;
160 return ((ps1
->ap_perms
== ps2
->ap_perms
) ? 1 : 0);
165 doesdecmpfs(int fd
) {
166 #ifdef DECMPFS_XATTR_NAME
168 struct attrlist attrs
;
169 char volroot
[MAXPATHLEN
+ 1];
173 vol_capabilities_attr_t volAttrs
;
176 (void)fstatfs(fd
, &sfs
);
177 strlcpy(volroot
, sfs
.f_mntonname
, sizeof(volroot
));
179 memset(&attrs
, 0, sizeof(attrs
));
180 attrs
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
181 attrs
.volattr
= ATTR_VOL_CAPABILITIES
;
183 rv
= getattrlist(volroot
, &attrs
, &volattrs
, sizeof(volattrs
), 0);
186 (volattrs
.volAttrs
.capabilities
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_DECMPFS_COMPRESSION
) &&
187 (volattrs
.volAttrs
.valid
[VOL_CAPABILITIES_FORMAT
] & VOL_CAP_FMT_DECMPFS_COMPRESSION
)) {
195 does_copy_protection(int fd
)
199 if (fstatfs(fd
, &sfs
) == -1)
202 return ((sfs
.f_flags
& MNT_CPROTECT
) == MNT_CPROTECT
);
206 sort_xattrname_list(void *start
, size_t length
)
213 /* If it's not a proper C string at the end, don't do anything */
214 if (((char*)start
)[length
] != 0)
217 * In order to sort the list of names, we need to
218 * make a list of pointers to strings. To do that,
219 * we need to run through the buffer, and find the
220 * beginnings of strings.
222 nel
= 10; // Most files don't have many EAs
223 ptrs
= (char**)calloc(nel
, sizeof(char*));
230 char *curPtr
= start
;
231 while (curPtr
< (char*)start
+ length
) {
232 printf("%s\n", curPtr
);
233 curPtr
+= strlen(curPtr
) + 1;
238 tmp
= ptrs
[indx
++] = (char*)start
;
240 while (tmp
= memchr(tmp
, 0, ((char*)start
+ length
) - tmp
)) {
243 ptrs
= realloc(ptrs
, sizeof(char**) * nel
);
247 ptrs
[indx
++] = ++tmp
;
250 printf("Unsorted:\n");
251 for (nel
= 0; nel
< indx
-1; nel
++) {
252 printf("\tEA %d = `%s'\n", nel
, ptrs
[nel
]);
255 qsort_b(ptrs
, indx
-1, sizeof(char*), ^(const void *left
, const void *right
) {
257 char *lstr
= *(char**)left
, *rstr
= *(char**)right
;
258 rv
= strcmp(lstr
, rstr
);
263 for (nel
= 0; nel
< indx
-1; nel
++) {
264 printf("\tEA %d = `%s'\n", nel
, ptrs
[nel
]);
268 * Now that it's sorted, we need to make a copy, so we can
269 * move the strings around into the new order. Then we
270 * copy that on top of the old buffer, and we're done.
272 char *copy
= malloc(length
);
277 for (i
= 0; i
< indx
-1; i
++) {
278 size_t len
= strlen(ptrs
[i
]);
279 memcpy(curPtr
, ptrs
[i
], len
+1);
282 memcpy(start
, copy
, length
);
293 * Internally, the process is broken into a series of
296 static int copyfile_open (copyfile_state_t
);
297 static int copyfile_close (copyfile_state_t
);
298 static int copyfile_data (copyfile_state_t
);
299 static int copyfile_stat (copyfile_state_t
);
300 static int copyfile_security (copyfile_state_t
);
301 static int copyfile_xattr (copyfile_state_t
);
302 static int copyfile_pack (copyfile_state_t
);
303 static int copyfile_unpack (copyfile_state_t
);
305 static copyfile_flags_t
copyfile_check (copyfile_state_t
);
306 static filesec_t
copyfile_fix_perms(copyfile_state_t
, filesec_t
*);
307 static int copyfile_preamble(copyfile_state_t
*s
, copyfile_flags_t flags
);
308 static int copyfile_internal(copyfile_state_t state
, copyfile_flags_t flags
);
309 static int copyfile_unset_posix_fsec(filesec_t
);
310 static int copyfile_quarantine(copyfile_state_t
);
312 #define COPYFILE_DEBUG (1<<31)
313 #define COPYFILE_DEBUG_VAR "COPYFILE_DEBUG"
315 // These macros preserve the value of errno.
316 #ifndef _COPYFILE_TEST
317 # define copyfile_warn(str, ...) \
319 errno_t _errsv = errno; \
320 syslog(LOG_WARNING, str ": %m", ## __VA_ARGS__); \
323 # define copyfile_debug(d, str, ...) \
325 if (s && (d <= s->debug)) {\
326 errno_t _errsv = errno; \
327 syslog(LOG_DEBUG, "%s:%d:%s() " str "\n", __FILE__, __LINE__ , __FUNCTION__, ## __VA_ARGS__); \
332 #define copyfile_warn(str, ...) \
334 errno_t _errsv = errno; \
335 fprintf(stderr, "%s:%d:%s() " str ": %s\n", __FILE__, __LINE__ , __FUNCTION__, ## __VA_ARGS__, (errno) ? strerror(errno) : ""); \
338 # define copyfile_debug(d, str, ...) \
340 if (s && (d <= s->debug)) {\
341 errno_t _errsv = errno; \
342 fprintf(stderr, "%s:%d:%s() " str "\n", __FILE__, __LINE__ , __FUNCTION__, ## __VA_ARGS__); \
348 static int copyfile_quarantine(copyfile_state_t s
)
351 if (s
->qinfo
== NULL
)
354 s
->qinfo
= qtn_file_alloc();
355 if (s
->qinfo
== NULL
)
360 if ((error
= qtn_file_init_with_fd(s
->qinfo
, s
->src_fd
)) != 0)
362 qtn_file_free(s
->qinfo
);
373 add_uberace(acl_t
*acl
)
376 acl_permset_t permset
;
379 if (mbr_uid_to_uuid(getuid(), qual
) != 0)
383 * First, we create an entry, and give it the special name
384 * of ACL_FIRST_ENTRY, thus guaranteeing it will be first.
385 * After that, we clear out all the permissions in it, and
386 * add three permissions: WRITE_DATA, WRITE_ATTRIBUTES, and
387 * WRITE_EXTATTRIBUTES. We put these into an ACE that allows
388 * the functionality, and put this into the ACL.
390 if (acl_create_entry_np(acl
, &entry
, ACL_FIRST_ENTRY
) == -1)
392 if (acl_get_permset(entry
, &permset
) == -1) {
393 copyfile_warn("acl_get_permset");
396 if (acl_clear_perms(permset
) == -1) {
397 copyfile_warn("acl_clear_permset");
400 if (acl_add_perm(permset
, ACL_WRITE_DATA
) == -1) {
401 copyfile_warn("add ACL_WRITE_DATA");
404 if (acl_add_perm(permset
, ACL_WRITE_ATTRIBUTES
) == -1) {
405 copyfile_warn("add ACL_WRITE_ATTRIBUTES");
408 if (acl_add_perm(permset
, ACL_WRITE_EXTATTRIBUTES
) == -1) {
409 copyfile_warn("add ACL_WRITE_EXTATTRIBUTES");
412 if (acl_add_perm(permset
, ACL_APPEND_DATA
) == -1) {
413 copyfile_warn("add ACL_APPEND_DATA");
416 if (acl_add_perm(permset
, ACL_WRITE_SECURITY
) == -1) {
417 copyfile_warn("add ACL_WRITE_SECURITY");
420 if (acl_set_tag_type(entry
, ACL_EXTENDED_ALLOW
) == -1) {
421 copyfile_warn("set ACL_EXTENDED_ALLOW");
425 if(acl_set_permset(entry
, permset
) == -1) {
426 copyfile_warn("acl_set_permset");
429 if(acl_set_qualifier(entry
, qual
) == -1) {
430 copyfile_warn("acl_set_qualifier");
440 is_uberace(acl_entry_t ace
)
443 acl_permset_t perms
, tperms
;
450 // Who am I, and who is the ACE for?
451 mbr_uid_to_uuid(geteuid(), myuuid
);
452 qual
= (guid_t
*)acl_get_qualifier(ace
);
454 // Need to create a temporary acl, so I can get the uberace template.
460 if (acl_get_entry(tacl
, ACL_FIRST_ENTRY
, &tentry
) != 0) {
463 acl_get_permset(tentry
, &tperms
);
466 acl_get_tag_type(ace
, &tag
);
467 acl_get_permset(ace
, &perms
);
469 if (tag
== ACL_EXTENDED_ALLOW
&&
470 (memcmp(qual
, myuuid
, sizeof(myuuid
)) == 0) &&
471 acl_compare_permset_np(tperms
, perms
))
486 remove_uberace(int fd
, struct stat
*sbuf
)
488 filesec_t fsec
= NULL
;
493 fsec
= filesec_init();
498 if (fstatx_np(fd
, &sb
, fsec
) != 0) {
499 if (errno
== ENOTSUP
)
504 if (filesec_get_property(fsec
, FILESEC_ACL
, &acl
) != 0) {
508 if (acl_get_entry(acl
, ACL_FIRST_ENTRY
, &entry
) == 0) {
509 if (is_uberace(entry
))
511 mode_t m
= sbuf
->st_mode
& ~S_IFMT
;
513 if (acl_delete_entry(acl
, entry
) != 0 ||
514 filesec_set_property(fsec
, FILESEC_ACL
, &acl
) != 0 ||
515 filesec_set_property(fsec
, FILESEC_MODE
, &m
) != 0 ||
516 fchmodx_np(fd
, fsec
) != 0)
529 fchmod(fd
, sbuf
->st_mode
& ~S_IFMT
);
534 reset_security(copyfile_state_t s
)
536 /* If we haven't reset the file security information
537 * (COPYFILE_SECURITY is not set in flags)
538 * restore back the permissions the file had originally
540 * One of the reasons this seems so complicated is that
541 * it is partially at odds with copyfile_security().
543 * Simplisticly, we are simply trying to make sure we
544 * only copy what was requested, and that we don't stomp
545 * on what wasn't requested.
548 #ifdef COPYFILE_RECURSIVE
549 if (s
->dst_fd
> -1) {
552 if (s
->src_fd
> -1 && (s
->flags
& COPYFILE_STAT
))
553 fstat(s
->src_fd
, &sbuf
);
555 fstat(s
->dst_fd
, &sbuf
);
557 if (!(s
->internal_flags
& cfDelayAce
))
558 remove_uberace(s
->dst_fd
, &sbuf
);
561 if (s
->permissive_fsec
&& (s
->flags
& COPYFILE_SECURITY
) != COPYFILE_SECURITY
) {
562 if (s
->flags
& COPYFILE_ACL
) {
563 /* Just need to reset the BSD information -- mode, owner, group */
564 (void)fchown(s
->dst_fd
, s
->dst_sb
.st_uid
, s
->dst_sb
.st_gid
);
565 (void)fchmod(s
->dst_fd
, s
->dst_sb
.st_mode
);
568 * flags is either COPYFILE_STAT, or neither; if it's
569 * neither, then we restore both ACL and POSIX permissions;
570 * if it's STAT, however, then we only want to restore the
571 * ACL (which may be empty). We do that by removing the
572 * POSIX information from the filesec object.
574 if (s
->flags
& COPYFILE_STAT
) {
575 copyfile_unset_posix_fsec(s
->original_fsec
);
577 if (fchmodx_np(s
->dst_fd
, s
->original_fsec
) < 0 && errno
!= ENOTSUP
)
578 copyfile_warn("restoring security information");
582 if (s
->permissive_fsec
) {
583 filesec_free(s
->permissive_fsec
);
584 s
->permissive_fsec
= NULL
;
587 if (s
->original_fsec
) {
588 filesec_free(s
->original_fsec
);
589 s
->original_fsec
= NULL
;
597 * copytree -- recursively copy a hierarchy.
599 * Unlike normal copyfile(), copytree() can copy an entire hierarchy.
600 * Care is taken to keep the ACLs set up correctly, in addition to the
601 * normal copying that is done. (When copying a hierarchy, we can't
602 * get rid of the "allow-all-writes" ACE on a directory until we're done
603 * copying the *contents* of the directory.)
605 * The other big difference from copyfile (for the moment) is that copytree()
606 * will use a call-back function to pass along information about what is
607 * about to be copied, and whether or not it succeeded.
609 * copytree() is called from copyfile() -- but copytree() itself then calls
610 * copyfile() to copy each individual object.
612 * If COPYFILE_CLONE is passed, copytree() will clone (instead of copy)
613 * regular files and symbolic links found in each directory.
614 * Directories will still be copied normally.
616 * XXX - no effort is made to handle overlapping hierarchies at the moment.
621 copytree(copyfile_state_t s
)
625 int (*sfunc
)(const char *, struct stat
*);
626 copyfile_callback_t status
= NULL
;
627 char srcisdir
= 0, dstisdir
= 0, dstexists
= 0;
630 const char *dstpathsep
= "";
632 char srcpath
[PATH_MAX
* 2 + 1], dstpath
[PATH_MAX
* 2 + 1];
638 const char *paths
[2] = { 0 };
639 unsigned int flags
= 0;
640 int fts_flags
= FTS_NOCHDIR
;
641 dev_t last_dev
= s
->sb
.st_dev
;
648 if (s
->flags
& (COPYFILE_MOVE
| COPYFILE_UNLINK
| COPYFILE_CHECK
| COPYFILE_PACK
| COPYFILE_UNPACK
)) {
654 flags
= s
->flags
& (COPYFILE_ALL
| COPYFILE_NOFOLLOW
| COPYFILE_VERBOSE
| COPYFILE_EXCL
| COPYFILE_CLONE
| COPYFILE_DATA_SPARSE
);
656 paths
[0] = src
= s
->src
;
659 if (src
== NULL
|| dst
== NULL
) {
665 sfunc
= (flags
& COPYFILE_NOFOLLOW_SRC
) ? lstat
: stat
;
666 if ((sfunc
)(src
, &sbuf
) == -1) {
670 if ((sbuf
.st_mode
& S_IFMT
) == S_IFDIR
) {
674 sfunc
= (flags
& COPYFILE_NOFOLLOW_DST
) ? lstat
: stat
;
675 if ((sfunc
)(dst
, &sbuf
) == -1) {
676 if (errno
!= ENOENT
) {
682 if ((sbuf
.st_mode
& S_IFMT
) == S_IFDIR
) {
688 // This doesn't handle filesystem crossing and case sensitivity
689 // So there's got to be a better way
691 if (realpath(src
, srcpath
) == NULL
) {
696 if (realpath(dst
, dstpath
) == NULL
&&
697 (errno
== ENOENT
&& realpath(dirname(dst
), dstpath
) == NULL
)) {
701 if (strstr(srcpath
, dstpath
) != NULL
) {
707 srcroot
= basename((char*)src
);
708 if (srcroot
== NULL
) {
714 * To work on as well:
715 * We have a few cases when copying a hierarchy:
716 * 1) src is a non-directory, dst is a directory;
717 * 2) src is a non-directory, dst is a non-directory;
718 * 3) src is a non-directory, dst does not exist;
719 * 4) src is a directory, dst is a directory;
720 * 5) src is a directory, dst is a non-directory;
721 * 6) src is a directory, dst does not exist
723 * (1) copies src to dst/basename(src).
724 * (2) fails if COPYFILE_EXCL is set, otherwise copies src to dst.
725 * (3) and (6) copy src to the name dst.
726 * (4) copies the contents of src to the contents of dst.
731 // copy /path/to/src to /path/to/dst/src
732 // Append "/" and (fts_path - strlen(basename(src))) to dst?
734 slash
= strrchr(src
, '/');
738 offset
= slash
- src
+ 1;
740 // copy /path/to/src to /path/to/dst
741 // append (fts_path + strlen(src)) to dst?
743 offset
= strlen(src
);
746 // COPYFILE_RECURSIVE is always done physically: see 11717978.
747 fts_flags
|= FTS_PHYSICAL
;
748 if (!(s
->flags
& (COPYFILE_NOFOLLOW_SRC
|COPYFILE_CLONE
))) {
749 // Follow 'src', even if it's a symlink, unless instructed not to
750 // or we're cloning, where we never follow symlinks.
751 fts_flags
|= FTS_COMFOLLOW
;
754 fts
= fts_open((char * const *)paths
, fts_flags
, NULL
);
756 status
= s
->statuscb
;
757 while ((ftsent
= fts_read(fts
)) != NULL
) {
759 char *dstfile
= NULL
;
761 copyfile_state_t tstate
= copyfile_state_alloc();
762 if (tstate
== NULL
) {
767 tstate
->statuscb
= s
->statuscb
;
768 tstate
->ctx
= s
->ctx
;
769 if (last_dev
== ftsent
->fts_dev
) {
770 tstate
->internal_flags
|= (s
->internal_flags
& COPYFILE_MNT_CPROTECT_MASK
);
772 last_dev
= ftsent
->fts_dev
;
774 asprintf(&dstfile
, "%s%s%s", dst
, dstpathsep
, ftsent
->fts_path
+ offset
);
775 if (dstfile
== NULL
) {
776 copyfile_state_free(tstate
);
781 switch (ftsent
->fts_info
) {
783 tstate
->internal_flags
|= cfDelayAce
;
784 cmd
= COPYFILE_RECURSE_DIR
;
790 cmd
= COPYFILE_RECURSE_FILE
;
793 cmd
= COPYFILE_RECURSE_DIR_CLEANUP
;
800 errno
= ftsent
->fts_errno
;
802 rv
= (*status
)(COPYFILE_RECURSE_ERROR
, COPYFILE_ERR
, tstate
, ftsent
->fts_path
, dstfile
, s
->ctx
);
803 if (rv
== COPYFILE_SKIP
|| rv
== COPYFILE_CONTINUE
) {
807 if (rv
== COPYFILE_QUIT
) {
820 if (cmd
== COPYFILE_RECURSE_DIR
|| cmd
== COPYFILE_RECURSE_FILE
) {
822 rv
= (*status
)(cmd
, COPYFILE_START
, tstate
, ftsent
->fts_path
, dstfile
, s
->ctx
);
823 if (rv
== COPYFILE_SKIP
) {
824 if (cmd
== COPYFILE_RECURSE_DIR
) {
825 rv
= fts_set(fts
, ftsent
, FTS_SKIP
);
827 rv
= (*status
)(0, COPYFILE_ERR
, tstate
, ftsent
->fts_path
, dstfile
, s
->ctx
);
828 if (rv
== COPYFILE_QUIT
)
834 if (rv
== COPYFILE_QUIT
) {
835 retval
= -1; errno
= 0;
839 // Since we don't support cloning directories this code depends on copyfile()
840 // falling back to a regular directory copy.
841 int tmp_flags
= (cmd
== COPYFILE_RECURSE_DIR
) ? (flags
& ~COPYFILE_STAT
) : flags
;
842 rv
= copyfile(ftsent
->fts_path
, dstfile
, tstate
, tmp_flags
);
845 rv
= (*status
)(cmd
, COPYFILE_ERR
, tstate
, ftsent
->fts_path
, dstfile
, s
->ctx
);
846 if (rv
== COPYFILE_QUIT
) {
858 rv
= (*status
)(cmd
, COPYFILE_FINISH
, tstate
, ftsent
->fts_path
, dstfile
, s
->ctx
);
859 if (rv
== COPYFILE_QUIT
) {
860 retval
= -1; errno
= 0;
864 } else if (cmd
== COPYFILE_RECURSE_DIR_CLEANUP
) {
866 rv
= (*status
)(cmd
, COPYFILE_START
, tstate
, ftsent
->fts_path
, dstfile
, s
->ctx
);
867 if (rv
== COPYFILE_QUIT
) {
868 retval
= -1; errno
= 0;
870 } else if (rv
== COPYFILE_SKIP
) {
875 rv
= copyfile(ftsent
->fts_path
, dstfile
, tstate
, (flags
& COPYFILE_NOFOLLOW
) | COPYFILE_STAT
);
878 rv
= (*status
)(COPYFILE_RECURSE_DIR_CLEANUP
, COPYFILE_ERR
, tstate
, ftsent
->fts_path
, dstfile
, s
->ctx
);
879 if (rv
== COPYFILE_QUIT
) {
882 } else if (rv
== COPYFILE_SKIP
|| rv
== COPYFILE_CONTINUE
) {
883 if (rv
== COPYFILE_CONTINUE
)
894 rv
= (*status
)(COPYFILE_RECURSE_DIR_CLEANUP
, COPYFILE_FINISH
, tstate
, ftsent
->fts_path
, dstfile
, s
->ctx
);
895 if (rv
== COPYFILE_QUIT
) {
896 retval
= -1; errno
= 0;
906 s
->internal_flags
&= ~COPYFILE_MNT_CPROTECT_MASK
;
907 s
->internal_flags
|= (tstate
->internal_flags
& COPYFILE_MNT_CPROTECT_MASK
);
909 copyfile_state_free(tstate
);
919 copyfile_debug(1, "returning: %d errno %d\n", retval
, errno
);
924 * fcopyfile() is used to copy a source file descriptor to a destination file
925 * descriptor. This allows an application to figure out how it wants to open
926 * the files (doing various security checks, perhaps), and then just pass in
927 * the file descriptors.
929 int fcopyfile(int src_fd
, int dst_fd
, copyfile_state_t state
, copyfile_flags_t flags
)
932 copyfile_state_t s
= state
;
935 if (src_fd
< 0 || dst_fd
< 0)
941 if (copyfile_preamble(&s
, flags
) < 0)
944 copyfile_debug(2, "set src_fd <- %d", src_fd
);
945 if (s
->src_fd
== -2 && src_fd
> -1)
948 if (fstatx_np(s
->src_fd
, &s
->sb
, s
->fsec
) != 0)
950 if (errno
== ENOTSUP
|| errno
== EPERM
)
951 fstat(s
->src_fd
, &s
->sb
);
954 copyfile_warn("fstatx_np on src fd %d", s
->src_fd
);
960 /* prevent copying on unsupported types */
961 switch (s
->sb
.st_mode
& S_IFMT
)
972 copyfile_debug(2, "set dst_fd <- %d", dst_fd
);
973 if (s
->dst_fd
== -2 && dst_fd
> -1)
976 (void)fstat(s
->dst_fd
, &dst_sb
);
977 (void)fchmod(s
->dst_fd
, (dst_sb
.st_mode
& ~S_IFMT
) | (S_IRUSR
| S_IWUSR
));
979 (void)copyfile_quarantine(s
);
981 ret
= copyfile_internal(s
, flags
);
983 if (ret
>= 0 && !(s
->flags
& COPYFILE_STAT
))
985 (void)fchmod(s
->dst_fd
, dst_sb
.st_mode
& ~S_IFMT
);
994 copyfile_state_free(s
);
1005 * This routine implements the clonefileat functionality
1006 * for copyfile. There are 2 kinds of clone flags, namely
1007 * 1. COPYFILE_CLONE_FORCE which is a 'force' clone flag.
1008 * 2. COPYFILE_CLONE which is a 'best try' flag.
1009 * In both cases, we inherit the flags provided
1010 * to copyfile call and clone the file.
1011 * Both these flags are equivalent to
1012 * (COPYFILE_EXCL | COPYFILE_ACL | COPYFILE_STAT | COPYFILE_XATTR | COPYFILE_DATA)
1013 * With force clone flag set, we return failure if cloning fails,
1014 * however, in case of best try flag, we fallback to the copy method.
1017 static int copyfile_clone(copyfile_state_t state
)
1020 // Since we don't allow cloning of directories, we must also forbid
1021 // cloning the target of symlinks (since that may be a directory).
1022 int cloneFlags
= CLONE_NOFOLLOW
;
1025 if (lstat(state
->src
, &src_sb
) != 0)
1032 * Support only for files and symbolic links.
1033 * TODO:Remove this check when support for directories is added.
1035 if (S_ISREG(src_sb
.st_mode
) || S_ISLNK(src_sb
.st_mode
))
1038 * COPYFILE_UNLINK tells us to try removing the destination
1039 * before we create it. We don't care if the file doesn't
1040 * exist, so we ignore ENOENT.
1042 if (state
->flags
& COPYFILE_UNLINK
)
1044 if (remove(state
->dst
) < 0 && errno
!= ENOENT
)
1049 ret
= clonefileat(AT_FDCWD
, state
->src
, AT_FDCWD
, state
->dst
, cloneFlags
);
1052 * We could also report the size of the single
1053 * object that was cloned. However, that's a lot
1054 * more difficult when we eventually support
1055 * cloning directories. It seems reasonable to NOT
1056 * report any bytes being "copied" in this scenario,
1057 * and let the caller figure out how they want to
1060 state
->was_cloned
= true;
1063 * COPYFILE_MOVE tells us to attempt removing
1064 * the source file after the copy, and to
1065 * ignore any errors returned by remove(3).
1067 if (state
->flags
& COPYFILE_MOVE
) {
1068 (void)remove(state
->src
);
1081 * the original copyfile() routine; this copies a source file to a destination
1082 * file. Note that because we need to set the names in the state variable, this
1083 * is not just the same as opening the two files, and then calling fcopyfile().
1084 * Oh, if only life were that simple!
1086 int copyfile(const char *src
, const char *dst
, copyfile_state_t state
, copyfile_flags_t flags
)
1090 copyfile_state_t s
= state
;
1093 if (src
== NULL
&& dst
== NULL
)
1099 if (copyfile_preamble(&s
, flags
) < 0)
1105 * This macro is... well, it's not the worst thing you can do with cpp, not
1106 * by a long shot. Essentially, we are setting the filename (src or dst)
1107 * in the state structure; since the structure may not have been cleared out
1108 * before being used again, we do some of the cleanup here: if the given
1109 * filename (e.g., src) is set, and state->src is not equal to that, then
1110 * we need to check to see if the file descriptor had been opened, and if so,
1111 * close it. After that, we set state->src to be a copy of the given filename,
1112 * releasing the old copy if necessary.
1114 #define COPYFILE_SET_FNAME(NAME, S) \
1116 if (NAME != NULL) { \
1117 if (S->NAME != NULL && strncmp(NAME, S->NAME, MAXPATHLEN)) { \
1118 copyfile_debug(2, "replacing string %s (%s) -> (%s)", #NAME, NAME, S->NAME);\
1119 if (S->NAME##_fd != -2 && S->NAME##_fd > -1) { \
1120 copyfile_debug(4, "closing %s fd: %d", #NAME, S->NAME##_fd); \
1121 close(S->NAME##_fd); \
1122 S->NAME##_fd = -2; \
1129 if ((NAME) && (S->NAME = strdup(NAME)) == NULL) \
1134 COPYFILE_SET_FNAME(src
, s
);
1135 COPYFILE_SET_FNAME(dst
, s
);
1137 if (s
->flags
& COPYFILE_RECURSIVE
) {
1142 if (s
->flags
& (COPYFILE_CLONE_FORCE
| COPYFILE_CLONE
))
1144 ret
= copyfile_clone(s
);
1147 } else if (s
->flags
& COPYFILE_CLONE_FORCE
) {
1150 // cloning failed. Inherit clonefile flags required for
1151 // falling back to copyfile.
1152 s
->flags
|= (COPYFILE_ACL
| COPYFILE_EXCL
| COPYFILE_NOFOLLOW_SRC
|
1153 COPYFILE_STAT
| COPYFILE_XATTR
| COPYFILE_DATA
);
1155 s
->flags
&= ~COPYFILE_CLONE
;
1161 * Get a copy of the source file's security settings
1163 if (s
->original_fsec
) {
1164 filesec_free(s
->original_fsec
);
1165 s
->original_fsec
= NULL
;
1167 if ((s
->original_fsec
= filesec_init()) == NULL
)
1170 if ((s
->flags
& COPYFILE_NOFOLLOW_DST
) && lstat(s
->dst
, &dst_sb
) == 0 &&
1171 ((dst_sb
.st_mode
& S_IFMT
) == S_IFLNK
)) {
1172 if (s
->permissive_fsec
)
1173 free(s
->permissive_fsec
);
1174 s
->permissive_fsec
= NULL
;
1175 } else if(statx_np(s
->dst
, &dst_sb
, s
->original_fsec
) == 0)
1178 * copyfile_fix_perms() will make a copy of the permission set,
1179 * and insert at the beginning an ACE that ensures we can write
1180 * to the file and set attributes.
1183 if((s
->permissive_fsec
= copyfile_fix_perms(s
, &s
->original_fsec
)) != NULL
)
1186 * Set the permissions for the destination to our copy.
1187 * We should get ENOTSUP from any filesystem that simply
1188 * doesn't support it.
1190 if (chmodx_np(s
->dst
, s
->permissive_fsec
) < 0 && errno
!= ENOTSUP
)
1192 copyfile_warn("setting security information");
1193 filesec_free(s
->permissive_fsec
);
1194 s
->permissive_fsec
= NULL
;
1197 } else if (errno
== ENOENT
) {
1202 * If COPYFILE_CHECK is set in flags, then all we are going to do
1203 * is see what kinds of things WOULD have been copied (see
1204 * copyfile_check() below). We return that value.
1206 if (COPYFILE_CHECK
& flags
)
1208 ret
= copyfile_check(s
);
1210 } else if ((ret
= copyfile_open(s
)) < 0)
1213 (void)fcntl(s
->src_fd
, F_NOCACHE
, 1);
1214 (void)fcntl(s
->dst_fd
, F_NOCACHE
, 1);
1215 #ifdef F_SINGLE_WRITER
1216 (void)fcntl(s
->dst_fd
, F_SINGLE_WRITER
, 1);
1219 ret
= copyfile_internal(s
, flags
);
1223 #ifdef COPYFILE_RECURSIVE
1224 if (!(flags
& COPYFILE_STAT
)) {
1227 /* Just need to reset the BSD information -- mode, owner, group */
1228 (void)fchown(s
->dst_fd
, dst_sb
.st_uid
, dst_sb
.st_gid
);
1229 (void)fchmod(s
->dst_fd
, dst_sb
.st_mode
);
1236 if (s
->src
&& (flags
& COPYFILE_MOVE
))
1237 (void)remove(s
->src
);
1243 copyfile_debug(5, "returning %d errno %d\n", ret
, errno
);
1245 if (state
== NULL
) {
1247 copyfile_state_free(s
);
1262 * Shared prelude to the {f,}copyfile(). This initializes the
1263 * state variable, if necessary, and also checks for both debugging
1264 * and disabling environment variables.
1266 static int copyfile_preamble(copyfile_state_t
*state
, copyfile_flags_t flags
)
1272 if ((*state
= copyfile_state_alloc()) == NULL
)
1278 if (COPYFILE_DEBUG
& flags
)
1281 if ((e
= getenv(COPYFILE_DEBUG_VAR
)))
1284 s
->debug
= (uint32_t)strtol(e
, NULL
, 0);
1286 /* clamp s->debug to 1 if the environment variable is not parsable */
1287 if (s
->debug
== 0 && errno
!= 0)
1290 copyfile_debug(2, "debug value set to: %d", s
->debug
);
1294 /* Temporarily disabled */
1295 if (getenv(COPYFILE_DISABLE_VAR
) != NULL
)
1297 copyfile_debug(1, "copyfile disabled");
1301 copyfile_debug(2, "setting flags: %d", s
->flags
);
1308 * The guts of {f,}copyfile().
1309 * This looks through the flags in a particular order, and calls the
1310 * associated functions.
1312 static int copyfile_internal(copyfile_state_t s
, copyfile_flags_t flags
)
1316 if (s
->dst_fd
< 0 || s
->src_fd
< 0)
1318 copyfile_debug(1, "file descriptors not open (src: %d, dst: %d)", s
->src_fd
, s
->dst_fd
);
1324 * COPYFILE_PACK causes us to create an Apple Double version of the
1325 * source file, and puts it into the destination file. See
1326 * copyfile_pack() below for all the gory details.
1328 if (COPYFILE_PACK
& flags
)
1330 if ((ret
= copyfile_pack(s
)) < 0)
1332 if (s
->dst
) unlink(s
->dst
);
1339 * COPYFILE_UNPACK is the undoing of COPYFILE_PACK, obviously.
1340 * The goal there is to take an Apple Double file, and turn it
1341 * into a normal file (with data fork, resource fork, modes,
1342 * extended attributes, ACLs, etc.).
1344 if (COPYFILE_UNPACK
& flags
)
1346 if ((ret
= copyfile_unpack(s
)) < 0)
1354 * If we have quarantine info set, we attempt
1355 * to apply it to dst_fd. We don't care if
1356 * it fails, not yet anyway.
1364 * If COPYFILE_RUN_IN_PLACE is set, we need to add
1365 * QTN_FLAG_DO_NOT_TRANSLOCATE to the qinfo flags.
1367 * On iOS, qtn_file_get_flags & qtn_file_set_flags
1368 * don't modify anything, always return 0, per static
1369 * defines at top of this file, though we should never
1370 * get here in that case as qinfo will always be NULL.
1372 if (COPYFILE_RUN_IN_PLACE
& flags
)
1376 q_flags
= qtn_file_get_flags(s
->qinfo
);
1377 q_flags
|= QTN_FLAG_DO_NOT_TRANSLOCATE
;
1379 if (qtn_file_set_flags(s
->qinfo
, q_flags
) != 0) {
1380 s
->err
= errno
= EINVAL
;
1385 qr
= qtn_file_apply_to_fd(s
->qinfo
, s
->dst_fd
);
1390 s
->xattr_name
= (char*)XATTR_QUARANTINE_NAME
;
1391 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
1392 s
->xattr_name
= NULL
;
1393 if (rv
== COPYFILE_QUIT
) {
1394 s
->err
= errno
= (qr
< 0 ? ENOTSUP
: qr
);
1398 s
->err
= errno
= (qr
< 0 ? ENOTSUP
: qr
);
1405 * COPYFILE_XATTR tells us to copy the extended attributes;
1406 * this is seperate from the extended security (aka ACLs),
1407 * however. If we succeed in this, we continue to the next
1408 * stage; if we fail, we return with an error value. Note
1409 * that we fail if the errno is ENOTSUP, but we don't print
1410 * a warning in that case.
1412 if (COPYFILE_XATTR
& flags
)
1414 if ((ret
= copyfile_xattr(s
)) < 0)
1416 if (errno
!= ENOTSUP
&& errno
!= EPERM
)
1417 copyfile_warn("error processing extended attributes");
1423 * Similar to above, this tells us whether or not to copy
1424 * the non-meta data portion of the file. We attempt to
1425 * remove (via unlink) the destination file if we fail.
1427 if ((COPYFILE_DATA
|COPYFILE_DATA_SPARSE
) & flags
)
1429 if ((ret
= copyfile_data(s
)) < 0)
1431 copyfile_warn("error processing data");
1432 if (s
->dst
&& unlink(s
->dst
))
1433 copyfile_warn("%s: remove", s
->src
? s
->src
: "(null src)");
1439 * COPYFILE_SECURITY requests that we copy the security, both
1440 * extended and mundane (that is, ACLs and POSIX).
1442 if (COPYFILE_SECURITY
& flags
)
1444 if ((ret
= copyfile_security(s
)) < 0)
1446 copyfile_warn("error processing security information");
1451 if (COPYFILE_STAT
& flags
)
1453 if ((ret
= copyfile_stat(s
)) < 0)
1455 copyfile_warn("error processing POSIX information");
1469 * A publicly-visible routine, copyfile_state_alloc() sets up the state variable.
1471 copyfile_state_t
copyfile_state_alloc(void)
1473 copyfile_state_t s
= (copyfile_state_t
) calloc(1, sizeof(struct _copyfile_state
));
1480 filesec_free(s
->fsec
);
1483 s
->fsec
= filesec_init();
1491 * copyfile_state_free() returns the memory allocated to the state structure.
1492 * It also closes the file descriptors, if they've been opened.
1494 int copyfile_state_free(copyfile_state_t s
)
1499 filesec_free(s
->fsec
);
1501 if (s
->original_fsec
)
1502 filesec_free(s
->original_fsec
);
1504 if (s
->permissive_fsec
)
1505 filesec_free(s
->permissive_fsec
);
1508 qtn_file_free(s
->qinfo
);
1510 if (copyfile_close(s
) < 0)
1512 copyfile_warn("error closing files");
1516 free(s
->xattr_name
);
1527 * Should we worry if we can't close the source? NFS says we
1528 * should, but it's pretty late for us at this point.
1530 static int copyfile_close(copyfile_state_t s
)
1532 if (s
->src
&& s
->src_fd
>= 0)
1535 if (s
->dst
&& s
->dst_fd
>= 0) {
1536 if (close(s
->dst_fd
))
1544 * The purpose of this function is to set up a set of permissions
1545 * (ACL and traditional) that lets us write to the file. In the
1546 * case of ACLs, we do this by putting in a first entry that lets
1547 * us write data, attributes, and extended attributes. In the case
1548 * of traditional permissions, we set the S_IWUSR (user-write)
1551 static filesec_t
copyfile_fix_perms(copyfile_state_t s __unused
, filesec_t
*fsec
)
1553 filesec_t ret_fsec
= NULL
;
1557 if ((ret_fsec
= filesec_dup(*fsec
)) == NULL
)
1560 if (filesec_get_property(ret_fsec
, FILESEC_ACL
, &acl
) == 0)
1562 #ifdef COPYFILE_RECURSIVE
1563 if (add_uberace(&acl
))
1567 acl_permset_t permset
;
1570 if (mbr_uid_to_uuid(getuid(), qual
) != 0)
1574 * First, we create an entry, and give it the special name
1575 * of ACL_FIRST_ENTRY, thus guaranteeing it will be first.
1576 * After that, we clear out all the permissions in it, and
1577 * add three permissions: WRITE_DATA, WRITE_ATTRIBUTES, and
1578 * WRITE_EXTATTRIBUTES. We put these into an ACE that allows
1579 * the functionality, and put this into the ACL.
1581 if (acl_create_entry_np(&acl
, &entry
, ACL_FIRST_ENTRY
) == -1)
1583 if (acl_get_permset(entry
, &permset
) == -1)
1585 if (acl_clear_perms(permset
) == -1)
1587 if (acl_add_perm(permset
, ACL_WRITE_DATA
) == -1)
1589 if (acl_add_perm(permset
, ACL_WRITE_ATTRIBUTES
) == -1)
1591 if (acl_add_perm(permset
, ACL_WRITE_EXTATTRIBUTES
) == -1)
1593 if (acl_set_tag_type(entry
, ACL_EXTENDED_ALLOW
) == -1)
1596 if(acl_set_permset(entry
, permset
) == -1)
1598 if(acl_set_qualifier(entry
, qual
) == -1)
1602 if (filesec_set_property(ret_fsec
, FILESEC_ACL
, &acl
) != 0)
1607 * This is for the normal, mundane, POSIX permission model.
1608 * We make sure that we can write to the file.
1610 if (filesec_get_property(ret_fsec
, FILESEC_MODE
, &mode
) == 0)
1612 if ((mode
& (S_IWUSR
| S_IRUSR
)) != (S_IWUSR
| S_IRUSR
))
1614 mode
|= S_IWUSR
|S_IRUSR
;
1615 if (filesec_set_property(ret_fsec
, FILESEC_MODE
, &mode
) != 0)
1629 filesec_free(ret_fsec
);
1636 * Used to clear out the BSD/POSIX security information from
1640 copyfile_unset_posix_fsec(filesec_t fsec
)
1642 (void)filesec_set_property(fsec
, FILESEC_OWNER
, _FILESEC_UNSET_PROPERTY
);
1643 (void)filesec_set_property(fsec
, FILESEC_GROUP
, _FILESEC_UNSET_PROPERTY
);
1644 (void)filesec_set_property(fsec
, FILESEC_MODE
, _FILESEC_UNSET_PROPERTY
);
1649 * Used to remove acl information from a filesec_t
1650 * Unsetting the acl alone in Tiger was insufficient
1652 static int copyfile_unset_acl(copyfile_state_t s
)
1655 if (filesec_set_property(s
->fsec
, FILESEC_ACL
, NULL
) == -1)
1657 copyfile_debug(5, "unsetting acl attribute on %s", s
->dst
? s
->dst
: "(null dst)");
1660 if (filesec_set_property(s
->fsec
, FILESEC_UUID
, NULL
) == -1)
1662 copyfile_debug(5, "unsetting uuid attribute on %s", s
->dst
? s
->dst
: "(null dst)");
1665 if (filesec_set_property(s
->fsec
, FILESEC_GRPUUID
, NULL
) == -1)
1667 copyfile_debug(5, "unsetting group uuid attribute on %s", s
->dst
? s
->dst
: "(null dst)");
1674 * copyfile_open() does what one expects: it opens up the files
1675 * given in the state structure, if they're not already open.
1676 * It also does some type validation, to ensure that we only
1677 * handle file types we know about.
1679 static int copyfile_open(copyfile_state_t s
)
1681 int oflags
= O_EXCL
| O_CREAT
| O_WRONLY
;
1682 int islnk
= 0, isdir
= 0, isreg
= 0;
1683 int osrc
= 0, dsrc
= 0;
1684 int prot_class
= PROTECTION_CLASS_DEFAULT
;
1685 int set_cprot_explicit
= 0;
1688 if (s
->src
&& s
->src_fd
== -2)
1690 if ((COPYFILE_NOFOLLOW_SRC
& s
->flags
? lstatx_np
: statx_np
)
1691 (s
->src
, &s
->sb
, s
->fsec
))
1693 copyfile_warn("stat on %s", s
->src
);
1697 /* prevent copying on unsupported types */
1698 switch (s
->sb
.st_mode
& S_IFMT
)
1702 if ((size_t)s
->sb
.st_size
> SIZE_T_MAX
) {
1703 s
->err
= ENOMEM
; /* too big for us to copy */
1715 if (!(strcmp(s
->src
, "/dev/null") == 0 && (s
->flags
& COPYFILE_METADATA
))) {
1721 * If we're packing, then we are actually
1722 * creating a file, no matter what the source
1725 if (s
->flags
& COPYFILE_PACK
) {
1727 * O_SYMLINK and O_NOFOLLOW are not compatible options:
1728 * if the file is a symlink, and O_NOFOLLOW is specified,
1729 * open will return ELOOP, whether or not O_SYMLINK is set.
1730 * However, we know whether or not it was a symlink from
1731 * the stat above (although there is a potentiaal for a race
1732 * condition here, but it will err on the side of returning
1736 osrc
= (s
->flags
& COPYFILE_NOFOLLOW_SRC
) ? O_NOFOLLOW
: 0;
1740 if ((s
->src_fd
= open(s
->src
, O_RDONLY
| osrc
, 0)) < 0)
1742 copyfile_warn("open on %s", s
->src
);
1745 copyfile_debug(2, "open successful on source (%s)", s
->src
);
1747 (void)copyfile_quarantine(s
);
1750 if (s
->dst
&& s
->dst_fd
== -2)
1753 * COPYFILE_UNLINK tells us to try removing the destination
1754 * before we create it. We don't care if the file doesn't
1755 * exist, so we ignore ENOENT.
1757 if (COPYFILE_UNLINK
& s
->flags
)
1759 if (remove(s
->dst
) < 0 && errno
!= ENOENT
)
1761 copyfile_warn("%s: remove", s
->dst
);
1766 if (s
->flags
& COPYFILE_NOFOLLOW_DST
) {
1770 if (lstat(s
->dst
, &st
) != -1) {
1771 if ((st
.st_mode
& S_IFMT
) == S_IFLNK
)
1776 if (!(s
->internal_flags
& cfSrcProtSupportValid
))
1778 if ((error
= does_copy_protection(s
->src_fd
)) > 0)
1780 s
->internal_flags
|= cfSrcSupportsCProtect
;
1784 copyfile_warn("does_copy_protection failed on (%s) with error <%d>", s
->src
, errno
);
1787 s
->internal_flags
|= cfSrcProtSupportValid
;
1790 /* copy protection is only valid for regular files and directories. */
1791 if ((isreg
|| isdir
) && (s
->internal_flags
& cfSrcSupportsCProtect
))
1793 prot_class
= GET_PROT_CLASS(s
->src_fd
);
1796 copyfile_warn("GET_PROT_CLASS failed on (%s) with error <%d>", s
->src
, errno
);
1802 size_t sz
= (size_t)s
->sb
.st_size
+ 1;
1807 copyfile_warn("cannot allocate %zd bytes", sz
);
1810 if (readlink(s
->src
, bp
, sz
-1) == -1) {
1811 copyfile_warn("cannot readlink %s", s
->src
);
1815 if (symlink(bp
, s
->dst
) == -1) {
1816 if (errno
!= EEXIST
|| (s
->flags
& COPYFILE_EXCL
)) {
1817 copyfile_warn("Cannot make symlink %s", s
->dst
);
1823 s
->dst_fd
= open(s
->dst
, O_RDONLY
| O_SYMLINK
);
1824 if (s
->dst_fd
== -1) {
1825 copyfile_warn("Cannot open symlink %s for reading", s
->dst
);
1830 mode
= (s
->sb
.st_mode
& ~S_IFMT
) | S_IRWXU
;
1832 if (mkdir(s
->dst
, mode
) == -1) {
1833 if (errno
!= EEXIST
|| (s
->flags
& COPYFILE_EXCL
)) {
1834 copyfile_warn("Cannot make directory %s", s
->dst
);
1838 s
->dst_fd
= open(s
->dst
, O_RDONLY
| dsrc
);
1839 if (s
->dst_fd
== -1) {
1840 copyfile_warn("Cannot open directory %s for reading", s
->dst
);
1843 set_cprot_explicit
= 1;
1844 } else while((s
->dst_fd
= open_dprotected_np(s
->dst
, oflags
| dsrc
, prot_class
, 0, s
->sb
.st_mode
| S_IWUSR
)) < 0)
1847 * We set S_IWUSR because fsetxattr does not -- at the time this comment
1848 * was written -- allow one to set an extended attribute on a file descriptor
1849 * for a read-only file, even if the file descriptor is opened for writing.
1850 * This will only matter if the file does not already exist.
1855 copyfile_debug(3, "open failed, retrying (%s)", s
->dst
);
1856 if (s
->flags
& COPYFILE_EXCL
)
1858 oflags
= oflags
& ~O_CREAT
;
1859 /* if O_CREAT isn't set in open_dprotected_np, it won't set protection class.
1860 * Set the flag here so we know to do it later.
1862 set_cprot_explicit
= 1;
1863 if (s
->flags
& (COPYFILE_PACK
| COPYFILE_DATA
))
1865 copyfile_debug(4, "truncating existing file (%s)", s
->dst
);
1870 if(chmod(s
->dst
, (s
->sb
.st_mode
| S_IWUSR
) & ~S_IFMT
) == 0)
1874 * If we're trying to write to a directory to which we don't
1875 * have access, the create above would have failed, but chmod
1876 * here would have given us ENOENT. But the real error is
1877 * still one of access, so we change the errno we're reporting.
1878 * This could cause confusion with a race condition.
1881 if (errno
== ENOENT
)
1886 copyfile_debug(3, "open failed because it is a directory (%s)", s
->dst
);
1887 if (((s
->flags
& COPYFILE_EXCL
) ||
1888 (!isdir
&& (s
->flags
& COPYFILE_DATA
)))
1889 && !(s
->flags
& COPYFILE_UNPACK
))
1891 oflags
= (oflags
& ~(O_WRONLY
|O_CREAT
|O_TRUNC
)) | O_RDONLY
;
1894 copyfile_warn("open on %s", s
->dst
);
1897 copyfile_debug(2, "open successful on destination (%s)", s
->dst
);
1899 if (s
->internal_flags
& cfSrcSupportsCProtect
)
1901 if (!(s
->internal_flags
& cfDstProtSupportValid
))
1903 if ((error
= does_copy_protection(s
->dst_fd
)) > 0)
1905 s
->internal_flags
|= cfDstSupportsCProtect
;
1909 copyfile_warn("does_copy_protection failed on (%s) with error <%d>", s
->dst
, errno
);
1912 s
->internal_flags
|= cfDstProtSupportValid
;
1915 if ((isreg
|| isdir
)
1916 && set_cprot_explicit
1917 && (s
->internal_flags
& cfDstSupportsCProtect
))
1919 /* Protection class is set in open_dprotected_np for regular files that aren't truncated.
1920 * We set the protection class here for truncated files and directories.
1922 if (SET_PROT_CLASS(s
->dst_fd
, prot_class
) != 0)
1924 copyfile_warn("SET_PROT_CLASS failed on (%s) with error <%d>", s
->dst
, errno
);
1931 if (s
->dst_fd
< 0 || s
->src_fd
< 0)
1933 copyfile_debug(1, "file descriptors not open (src: %d, dst: %d)",
1934 s
->src_fd
, s
->dst_fd
);
1943 * copyfile_check(), as described above, essentially tells you
1944 * what you'd have to copy, if you wanted it to copy the things
1945 * you asked it to copy.
1946 * In other words, if you pass in COPYFILE_ALL, and the file in
1947 * question had no extended attributes but did have an ACL, you'd
1948 * get back COPYFILE_ACL.
1950 static copyfile_flags_t
copyfile_check(copyfile_state_t s
)
1953 copyfile_flags_t ret
= 0;
1954 int nofollow
= (s
->flags
& COPYFILE_NOFOLLOW_SRC
);
1964 if (COPYFILE_XATTR
& s
->flags
)
1965 if (listxattr(s
->src
, 0, 0, nofollow
? XATTR_NOFOLLOW
: 0) > 0)
1967 ret
|= COPYFILE_XATTR
;
1970 if (COPYFILE_ACL
& s
->flags
)
1972 (COPYFILE_NOFOLLOW_SRC
& s
->flags
? lstatx_np
: statx_np
)
1973 (s
->src
, &s
->sb
, s
->fsec
);
1975 if (filesec_get_property(s
->fsec
, FILESEC_ACL
, &acl
) == 0)
1976 ret
|= COPYFILE_ACL
;
1979 copyfile_debug(2, "check result: %d (%s)", ret
, s
->src
);
1985 /* If the state has had quarantine info set already, we use that */
1986 ret
|= ((s
->flags
& COPYFILE_XATTR
) ? COPYFILE_XATTR
: COPYFILE_ACL
);
1988 qinfo
= qtn_file_alloc();
1990 * For quarantine information, we need to see if the source file
1991 * has any. Since it may be a symlink, however, and we may, or
1992 * not be following, *and* there's no qtn* routine which can optionally
1993 * follow or not follow a symlink, we need to instead work around
2002 * If we care about not following symlinks, *and* the file exists
2003 * (which is to say, lstat doesn't return an error), *and* the file
2004 * is a symlink, then we open it up (with O_SYMLINK), and use
2005 * qtn_file_init_with_fd(); if none of that is true, however, then
2006 * we can simply use qtn_file_init_with_path().
2009 && lstat(s
->src
, &sbuf
) == 0
2010 && ((sbuf
.st_mode
& S_IFMT
) == S_IFLNK
)) {
2011 fd
= open(s
->src
, O_RDONLY
| O_SYMLINK
);
2013 if (!qtn_file_init_with_fd(qinfo
, fd
)) {
2014 qret
|= ((s
->flags
& COPYFILE_XATTR
) ? COPYFILE_XATTR
: COPYFILE_ACL
);
2019 if (!qtn_file_init_with_path(qinfo
, s
->src
)) {
2020 qret
|= ((s
->flags
& COPYFILE_XATTR
) ? COPYFILE_XATTR
: COPYFILE_ACL
);
2023 qtn_file_free(qinfo
);
2031 * Attempt to copy the data section of a file sparsely.
2032 * Requires that the source and destination file systems support sparse files.
2033 * Also requires that the source file descriptor's offset is a multiple of the smaller of the
2034 * source and destination file systems' block size.
2035 * In practice, this means that we refuse to perform copies that are only partially sparse.
2036 * Returns 0 if the source sparse file was copied, -1 on an unrecoverable error that
2037 * callers should propagate, and ENOTSUP where this routine refuses to copy the source file.
2038 * In this final case, callers are free to attempt a full copy.
2040 static int copyfile_data_sparse(copyfile_state_t s
, size_t input_blk_size
, size_t output_blk_size
)
2042 int src_fd
= s
->src_fd
, dst_fd
= s
->dst_fd
, rc
= 0;
2043 off_t src_start
, dst_start
, src_size
= s
->sb
.st_size
;
2044 off_t first_hole_offset
, next_hole_offset
, current_src_offset
, next_src_offset
;
2046 size_t iosize
= MIN(input_blk_size
, output_blk_size
);
2047 copyfile_callback_t status
= s
->statuscb
;
2049 bool use_punchhole
= true;
2053 if (!(s
->flags
& COPYFILE_DATA_SPARSE
)) {
2054 // Don't attempt this unless the right flags are passed.
2056 } else if (src_size
<= 0) {
2057 // The file size of our source is invalid; there's nothing to copy.
2062 // Since a major underlying filesystem requires that holes are block-aligned,
2063 // we only punch holes if we can guarantee that all holes from the source can
2064 // be holes in the destination, which requires that the source filesystem's block size
2065 // be an integral multiple of the destination filesystem's block size.
2066 if (input_blk_size
% output_blk_size
!= 0) {
2067 use_punchhole
= false;
2070 // Get the starting src/dest file descriptor offsets.
2071 src_start
= lseek(src_fd
, 0, SEEK_CUR
);
2072 dst_start
= lseek(dst_fd
, 0, SEEK_CUR
);
2073 if (src_start
< 0 || src_start
>= src_size
|| dst_start
< 0) {
2075 * Invalid starting source/destination offset:
2076 * Either < 0 which is plainly invalid (lseek may have failed),
2077 * or > EOF which means that the copy operation is undefined,
2078 * as by definition there is no data past EOF.
2083 copyfile_warn("Invalid file descriptor offset, cannot perform a sparse copy");
2085 } else if (src_start
!= (off_t
) roundup(src_start
, iosize
) ||
2086 dst_start
!= (off_t
) roundup(dst_start
, iosize
)) {
2087 // If the starting offset isn't a multiple of the iosize, we can't do an entire sparse copy.
2088 // Fall back to copyfile_data(), which will perform a full copy from the starting position.
2092 // Make sure that there is at least one hole in this [part of the] file.
2093 first_hole_offset
= lseek(src_fd
, src_start
, SEEK_HOLE
);
2094 if (first_hole_offset
== -1 || first_hole_offset
== src_size
) {
2096 * Either an error occurred, the src starting position is EOF, or there are no
2097 * holes in this [portion of the] source file. Regardless, we rewind the source file
2098 * and return ENOTSUP so copyfile_data() can attempt a full copy.
2100 if (lseek(src_fd
, src_start
, SEEK_SET
) == -1) {
2106 // We are ready to begin copying.
2107 // First, truncate the destination file to zero out any existing contents.
2108 // Then, truncate it again to its eventual size.
2109 if (ftruncate(dst_fd
, dst_start
) == -1) {
2110 copyfile_warn("Could not zero destination file before copy");
2112 } else if (ftruncate(dst_fd
, dst_start
+ src_size
- src_start
) == -1) {
2113 copyfile_warn("Could not set destination file size before copy");
2117 // Set the source's offset to the first data section.
2118 current_src_offset
= lseek(src_fd
, src_start
, SEEK_DATA
);
2119 if (current_src_offset
== -1) {
2120 if (errno
== ENXIO
) {
2121 // There are no more data sections in the file, so there's nothing to copy.
2122 goto set_total_copied
;
2127 // Now, current_src_offset points at the start of src's first data region.
2128 // Update dst_fd to point to the same offset (respecting its start).
2129 if (lseek(dst_fd
, dst_start
+ current_src_offset
- src_start
, SEEK_SET
) == -1) {
2130 copyfile_warn("failed to set dst to first data section");
2134 // Allocate a temporary buffer to copy data sections into.
2135 bp
= malloc(iosize
);
2137 copyfile_warn("No memory for copy buffer");
2142 * Performing a sparse copy:
2143 * While our source fd points to a data section (and is < EOF), read iosize bytes in.
2144 * Then, write those bytes to the dest fd, using the same iosize.
2145 * Finally, update our source and dest fds to point to the next data section.
2147 while ((nread
= read(src_fd
, bp
, iosize
)) > 0) {
2149 size_t left
= nread
;
2154 nwritten
= write(dst_fd
, ptr
, left
);
2158 copyfile_warn("writing to output %d times resulted in 0 bytes written", loop
);
2164 copyfile_warn("writing to output file failed");
2166 int rv
= (*status
)(COPYFILE_COPY_DATA
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
2167 if (rv
== COPYFILE_SKIP
) { // Skip the data copy
2170 } else if (rv
== COPYFILE_CONTINUE
) { // Retry the write
2175 // If we get here, we either have no callback or it didn't tell us to continue.
2180 ptr
= ((char*)ptr
) + nwritten
;
2184 s
->totalCopied
+= nwritten
;
2186 int rv
= (*status
)(COPYFILE_COPY_DATA
, COPYFILE_PROGRESS
, s
, s
->src
, s
->dst
, s
->ctx
);
2187 if (rv
== COPYFILE_QUIT
) {
2193 current_src_offset
+= nread
;
2195 // Find the next area of src_fd to copy.
2196 // Since data sections can be any length, we need see if current_src_offset points
2198 // If we get ENXIO, we're done copying (the last part of the file was a data section).
2199 // If this is not a hole, we do not need to alter current_src_offset yet.
2200 // If this is a hole, then we need to look for the next data section.
2201 next_hole_offset
= lseek(src_fd
, current_src_offset
, SEEK_HOLE
);
2202 if (next_hole_offset
== -1) {
2203 if (errno
== ENXIO
) {
2204 break; // We're done copying data sections.
2206 copyfile_warn("unable to find next hole in file during copy");
2208 } else if (next_hole_offset
!= current_src_offset
) {
2209 // Keep copying this data section (we must rewind src_fd to current_src_offset).
2210 if (lseek(src_fd
, current_src_offset
, SEEK_SET
) == -1) {
2216 // If we get here, we need to find the next data section to copy.
2217 next_src_offset
= lseek(src_fd
, current_src_offset
, SEEK_DATA
);
2218 if (next_src_offset
== -1) {
2219 if (errno
== ENXIO
) {
2220 // There are no more data sections in this file, so we're done with the copy.
2224 copyfile_warn("unable to advance src to next data section");
2228 // Advance the dst_fd to match (taking into account where it started).
2229 if (lseek(dst_fd
, dst_start
+ (next_src_offset
- src_start
), SEEK_SET
) == -1) {
2230 copyfile_warn("unable to advance dst to next data section");
2234 current_src_offset
= next_src_offset
;
2237 copyfile_warn("error %d reading from %s", errno
, s
->src
? s
->src
: "(null src)");
2241 // Punch holes where possible if needed.
2242 if (use_punchhole
) {
2243 struct fpunchhole punchhole_args
;
2244 off_t hole_start
= first_hole_offset
, hole_end
;
2245 bool trailing_hole
= true;
2247 // First, reset the source and destination file descriptors.
2248 if (lseek(src_fd
, src_start
, SEEK_SET
) == -1 || lseek(dst_fd
, dst_start
, SEEK_SET
) == -1) {
2249 copyfile_warn("unable to reset file descriptors to punch holes");
2250 // We have still copied the data, so there's no need to return an error here.
2251 goto set_total_copied
;
2254 // Now, find holes in the source (first_hole_offset already points to a source hole),
2255 // determining their length by the presence of a data section.
2256 while ((hole_end
= lseek(src_fd
, hole_start
+ (off_t
) iosize
, SEEK_DATA
)) != -1) {
2257 memset(&punchhole_args
, 0, sizeof(punchhole_args
));
2259 // Fix up the offset and length for the destination file.
2260 punchhole_args
.fp_offset
= hole_start
- src_start
+ dst_start
;
2261 punchhole_args
.fp_length
= hole_end
- hole_start
;
2262 if (fcntl(dst_fd
, F_PUNCHHOLE
, &punchhole_args
) == -1) {
2263 copyfile_warn("unable to punch hole in destination file, offset %lld length %lld",
2264 hole_start
- src_start
+ dst_start
, hole_end
- hole_start
);
2265 goto set_total_copied
;
2268 // Now, find the start of the next hole.
2269 hole_start
= lseek(src_fd
, hole_end
, SEEK_HOLE
);
2270 if (hole_start
== -1 || hole_start
== src_size
) {
2271 // No more holes (or lseek failed), so break.
2272 trailing_hole
= false;
2277 if ((hole_end
== -1 || hole_start
== -1) && errno
!= ENXIO
) {
2278 // A call to lseek() failed. Hole punching is best effort, so exit.
2279 copyfile_warn("lseek during hole punching failed");
2280 goto set_total_copied
;
2283 // We will still have a trailing hole to punch if the last lseek(SEEK_HOLE) succeeded.
2284 if (trailing_hole
) {
2285 // Since we can only punch iosize-aligned holes, we must make sure the last hole
2286 // is iosize-aligned. Unfortunately, no good truncate macros are in scope here,
2287 // so we must round down the end of the trailing hole to an iosize boundary ourselves.
2288 hole_end
= (src_size
% iosize
== 0) ? src_size
: roundup(src_size
, iosize
) - iosize
;
2290 memset(&punchhole_args
, 0, sizeof(punchhole_args
));
2291 punchhole_args
.fp_offset
= hole_start
- src_start
+ dst_start
;
2292 punchhole_args
.fp_length
= hole_end
- hole_start
;
2293 if (fcntl(dst_fd
, F_PUNCHHOLE
, &punchhole_args
) == -1) {
2294 copyfile_warn("unable to punch trailing hole in destination file, offset %lld",
2295 hole_start
- src_start
+ dst_start
);
2296 goto set_total_copied
;
2302 // Since we don't know in advance how many bytes we're copying, we advance this number
2303 // as we copy, but to match copyfile_data() we set it here to the amount of bytes that would
2304 // have been transferred in a full copy.
2305 s
->totalCopied
= src_size
- src_start
;
2322 * Attempt to copy the data section of a file. Using blockisize
2323 * is not necessarily the fastest -- it might be desirable to
2324 * specify a blocksize, somehow. But it's a size that should be
2325 * guaranteed to work.
2327 static int copyfile_data(copyfile_state_t s
)
2333 size_t iBlocksize
= 0, iMinblocksize
= 0;
2334 size_t oBlocksize
= 0, oMinblocksize
= 0; // If 0, we don't support sparse copying.
2335 const size_t blocksize_limit
= 1 << 30; // 1 GiB
2337 copyfile_callback_t status
= s
->statuscb
;
2339 /* Unless it's a normal file, we don't copy. For now, anyway */
2340 if ((s
->sb
.st_mode
& S_IFMT
) != S_IFREG
)
2343 #ifdef VOL_CAP_FMT_DECMPFS_COMPRESSION
2344 if (s
->internal_flags
& cfSawDecmpEA
) {
2345 if (s
->sb
.st_flags
& UF_COMPRESSED
) {
2346 if ((s
->flags
& COPYFILE_STAT
) == 0) {
2347 if (fchflags(s
->dst_fd
, UF_COMPRESSED
) == 0) {
2355 // Calculate the input and output block sizes.
2356 // Our output block size can be no greater than our input block size.
2357 if (fstatfs(s
->src_fd
, &sfs
) == -1) {
2358 iBlocksize
= s
->sb
.st_blksize
;
2360 iBlocksize
= sfs
.f_iosize
;
2361 iMinblocksize
= sfs
.f_bsize
;
2364 if (fstatfs(s
->dst_fd
, &sfs
) == -1) {
2365 oBlocksize
= iBlocksize
;
2367 oBlocksize
= (sfs
.f_iosize
== 0) ? iBlocksize
: MIN((size_t) sfs
.f_iosize
, iBlocksize
);
2368 oMinblocksize
= sfs
.f_bsize
;
2371 // 6453525 and 34848916 require us to limit our blocksize to resonable values.
2372 if ((size_t) s
->sb
.st_size
< iBlocksize
&& iMinblocksize
> 0) {
2373 copyfile_debug(3, "rounding up block size from fsize: %lld to multiple of %zu\n", s
->sb
.st_size
, iMinblocksize
);
2374 iBlocksize
= roundup((size_t) s
->sb
.st_size
, iMinblocksize
);
2375 oBlocksize
= MIN(oBlocksize
, iBlocksize
);
2378 if (iBlocksize
> blocksize_limit
) {
2379 iBlocksize
= blocksize_limit
;
2380 oBlocksize
= MIN(oBlocksize
, iBlocksize
);
2383 copyfile_debug(3, "input block size: %zu output block size: %zu\n", iBlocksize
, oBlocksize
);
2387 // If requested, attempt a sparse copy.
2388 if (s
->flags
& COPYFILE_DATA_SPARSE
) {
2389 // Check if the source & destination volumes both support sparse files.
2390 long min_hole_size
= MIN(fpathconf(s
->src_fd
, _PC_MIN_HOLE_SIZE
),
2391 fpathconf(s
->dst_fd
, _PC_MIN_HOLE_SIZE
));
2393 // If holes are supported on both the source and dest volumes, make sure our min_hole_size
2394 // is reasonable: if it's smaller than the source/dest block size,
2395 // our copy performance will suffer (and we may not create sparse files).
2396 if (iMinblocksize
> 0 && oMinblocksize
> 0 && (size_t) min_hole_size
>= iMinblocksize
2397 && (size_t) min_hole_size
>= oMinblocksize
) {
2399 ret
= copyfile_data_sparse(s
, iMinblocksize
, oMinblocksize
);
2401 // If we returned an error, exit gracefully.
2402 // If sparse copying is not supported, we try full copying if allowed by our caller.
2405 } else if (ret
!= ENOTSUP
) {
2411 // Make sure we're allowed to perform non-sparse copying.
2412 if (!(s
->flags
& COPYFILE_DATA
)) {
2419 if ((bp
= malloc(iBlocksize
)) == NULL
)
2424 /* If supported, do preallocation for Xsan / HFS / apfs volumes */
2425 #ifdef F_PREALLOCATE
2427 off_t dst_bytes_allocated
= 0;
2430 if (fstat(s
->dst_fd
, &dst_sb
) == 0) {
2431 // The destination may already have
2432 // preallocated space we can use.
2433 dst_bytes_allocated
= dst_sb
.st_blocks
* S_BLKSIZE
;
2436 if (dst_bytes_allocated
< s
->sb
.st_size
) {
2440 fst
.fst_posmode
= F_PEOFPOSMODE
;
2442 fst
.fst_length
= s
->sb
.st_size
- dst_bytes_allocated
;
2444 copyfile_debug(3, "preallocating %lld bytes on destination", fst
.fst_length
);
2445 /* Ignore errors; this is merely advisory. */
2446 (void)fcntl(s
->dst_fd
, F_PREALLOCATE
, &fst
);
2451 while ((nread
= read(s
->src_fd
, bp
, blen
)) > 0)
2454 size_t left
= nread
;
2459 nwritten
= write(s
->dst_fd
, ptr
, MIN(left
, oBlocksize
));
2463 copyfile_warn("writing to output %d times resulted in 0 bytes written", loop
);
2470 copyfile_warn("writing to output file got error");
2472 int rv
= (*status
)(COPYFILE_COPY_DATA
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
2473 if (rv
== COPYFILE_SKIP
) { // Skip the data copy
2477 if (rv
== COPYFILE_CONTINUE
) { // Retry the write
2486 ptr
= ((char*)ptr
) + nwritten
;
2490 s
->totalCopied
+= nwritten
;
2492 int rv
= (*status
)(COPYFILE_COPY_DATA
, COPYFILE_PROGRESS
, s
, s
->src
, s
->dst
, s
->ctx
);
2493 if (rv
== COPYFILE_QUIT
) {
2494 ret
= -1; s
->err
= errno
= ECANCELED
;
2502 copyfile_warn("reading from %s", s
->src
? s
->src
: "(null src)");
2507 if (ftruncate(s
->dst_fd
, s
->totalCopied
) < 0)
2523 * copyfile_security() will copy the ACL set, and the
2524 * POSIX set. Complexities come when dealing with
2525 * inheritied permissions, and when dealing with both
2526 * POSIX and ACL permissions.
2528 static int copyfile_security(copyfile_state_t s
)
2532 acl_t acl_src
= NULL
, acl_tmp
= NULL
, acl_dst
= NULL
;
2534 filesec_t tmp_fsec
= NULL
;
2535 filesec_t fsec_dst
= filesec_init();
2537 if (fsec_dst
== NULL
)
2541 if (COPYFILE_ACL
& s
->flags
)
2543 if (filesec_get_property(s
->fsec
, FILESEC_ACL
, &acl_src
))
2545 if (errno
== ENOENT
)
2551 /* grab the destination acl
2552 cannot assume it's empty due to inheritance
2554 if(fstatx_np(s
->dst_fd
, &sb
, fsec_dst
))
2557 if (filesec_get_property(fsec_dst
, FILESEC_ACL
, &acl_dst
))
2559 if (errno
== ENOENT
)
2565 if (acl_src
== NULL
&& acl_dst
== NULL
)
2568 acl_tmp
= acl_init(4);
2569 if (acl_tmp
== NULL
)
2573 acl_entry_t ace
= NULL
;
2574 acl_entry_t tmp
= NULL
;
2576 acl_get_entry(acl_src
,
2577 ace
== NULL
? ACL_FIRST_ENTRY
: ACL_NEXT_ENTRY
,
2580 acl_flagset_t flags
= { 0 };
2581 acl_get_flagset_np(ace
, &flags
);
2582 if (!acl_get_flag_np(flags
, ACL_ENTRY_INHERITED
))
2584 if ((ret
= acl_create_entry(&acl_tmp
, &tmp
)) == -1)
2587 if ((ret
= acl_copy_entry(tmp
, ace
)) == -1)
2590 copyfile_debug(2, "copied acl entry from %s to %s",
2591 s
->src
? s
->src
: "(null src)",
2592 s
->dst
? s
->dst
: "(null tmp)");
2598 acl_entry_t ace
= NULL
;
2599 acl_entry_t tmp
= NULL
;
2600 acl_flagset_t flags
= { 0 };
2601 for (copied
= 0;acl_get_entry(acl_dst
,
2602 ace
== NULL
? ACL_FIRST_ENTRY
: ACL_NEXT_ENTRY
,
2605 acl_get_flagset_np(ace
, &flags
);
2606 if (acl_get_flag_np(flags
, ACL_ENTRY_INHERITED
))
2608 if ((ret
= acl_create_entry(&acl_tmp
, &tmp
)) == -1)
2611 if ((ret
= acl_copy_entry(tmp
, ace
)) == -1)
2614 copyfile_debug(2, "copied acl entry from %s to %s",
2615 s
->src
? s
->src
: "(null dst)",
2616 s
->dst
? s
->dst
: "(null tmp)");
2621 if (!filesec_set_property(s
->fsec
, FILESEC_ACL
, &acl_tmp
))
2623 copyfile_debug(3, "altered acl");
2628 * The following code is attempting to ensure that only the requested
2629 * security information gets copied over to the destination file.
2630 * We essentially have four cases: COPYFILE_ACL, COPYFILE_STAT,
2631 * COPYFILE_(STAT|ACL), and none (in which case, we wouldn't be in
2634 * If we have both flags, we copy everything; if we have ACL but not STAT,
2635 * we remove the POSIX information from the filesec object, and apply the
2636 * ACL; if we have STAT but not ACL, then we just use fchmod(), and ignore
2637 * the extended version.
2639 tmp_fsec
= filesec_dup(s
->fsec
);
2640 if (tmp_fsec
== NULL
) {
2644 switch (COPYFILE_SECURITY
& s
->flags
) {
2646 copyfile_unset_posix_fsec(tmp_fsec
);
2648 case COPYFILE_ACL
| COPYFILE_STAT
:
2649 if (fchmodx_np(s
->dst_fd
, tmp_fsec
) < 0) {
2652 * The call could have failed for a number of reasons, since
2653 * it does a number of things: it changes the mode of the file,
2654 * sets the owner and group, and applies an ACL (if one exists).
2655 * The typical failure is going to be trying to set the group of
2656 * the destination file to match the source file, when the process
2657 * doesn't have permission to put files in that group. We try to
2658 * work around this by breaking the steps out and doing them
2659 * discretely. We don't care if the fchown fails, but we do care
2660 * if the mode or ACL can't be set. For historical reasons, we
2661 * simply log those failures, however.
2663 * Big warning here: we may NOT have COPYFILE_STAT set, since
2664 * we fell-through from COPYFILE_ACL. So check for the fchmod.
2667 #define NS(x) ((x) ? (x) : "(null string)")
2668 if ((s
->flags
& COPYFILE_STAT
) &&
2669 fchmod(s
->dst_fd
, s
->sb
.st_mode
) == -1) {
2670 copyfile_warn("could not change mode of destination file %s to match source file %s", NS(s
->dst
), NS(s
->src
));
2672 (void)fchown(s
->dst_fd
, s
->sb
.st_uid
, s
->sb
.st_gid
);
2673 if (filesec_get_property(tmp_fsec
, FILESEC_ACL
, &acl
) == 0) {
2674 if (acl_set_fd(s
->dst_fd
, acl
) == -1) {
2675 copyfile_warn("could not apply acl to destination file %s from source file %s", NS(s
->dst
), NS(s
->src
));
2683 (void)fchmod(s
->dst_fd
, s
->sb
.st_mode
);
2686 filesec_free(tmp_fsec
);
2688 filesec_free(fsec_dst
);
2689 if (acl_src
) acl_free(acl_src
);
2690 if (acl_dst
) acl_free(acl_dst
);
2691 if (acl_tmp
) acl_free(acl_tmp
);
2702 * Attempt to set the destination file's stat information -- including
2703 * flags and time-related fields -- to the source's.
2705 static int copyfile_stat(copyfile_state_t s
)
2707 unsigned int added_flags
= 0, dst_flags
= 0;
2708 struct attrlist attrlist
;
2711 /* Order of these structs matters for setattrlist. */
2712 struct timespec mod_time
;
2713 struct timespec acc_time
;
2717 * NFS doesn't support chflags; ignore errors as a result, since
2718 * we don't return failure for this.
2720 if (s
->internal_flags
& cfMakeFileInvisible
)
2721 added_flags
|= UF_HIDDEN
;
2724 * We need to check if SF_RESTRICTED was set on the destination
2725 * by the kernel. If it was, don't drop it.
2727 if (fstat(s
->dst_fd
, &dst_sb
))
2729 if (dst_sb
.st_flags
& SF_RESTRICTED
)
2730 added_flags
|= SF_RESTRICTED
;
2732 /* Copy file flags, masking out any we don't want to preserve */
2733 dst_flags
= (s
->sb
.st_flags
& ~COPYFILE_OMIT_FLAGS
) | added_flags
;
2734 (void)fchflags(s
->dst_fd
, dst_flags
);
2736 /* If this fails, we don't care */
2737 (void)fchown(s
->dst_fd
, s
->sb
.st_uid
, s
->sb
.st_gid
);
2739 /* This may have already been done in copyfile_security() */
2740 (void)fchmod(s
->dst_fd
, s
->sb
.st_mode
& ~S_IFMT
);
2742 /* Try to set m/atimes using setattrlist(), for nanosecond precision. */
2743 memset(&attrlist
, 0, sizeof(attrlist
));
2744 attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
2745 attrlist
.commonattr
= ATTR_CMN_MODTIME
| ATTR_CMN_ACCTIME
;
2746 ma_times
.mod_time
= s
->sb
.st_mtimespec
;
2747 ma_times
.acc_time
= s
->sb
.st_atimespec
;
2748 (void)fsetattrlist(s
->dst_fd
, &attrlist
, &ma_times
, sizeof(ma_times
), 0);
2754 * Similar to copyfile_security() in some ways; this
2755 * routine copies the extended attributes from the source,
2756 * and sets them on the destination.
2757 * The procedure is pretty simple, even if it is verbose:
2758 * for each named attribute on the destination, get its name, and
2759 * remove it. We should have none after that.
2760 * For each named attribute on the source, get its name, get its
2761 * data, and set it on the destination.
2763 static int copyfile_xattr(copyfile_state_t s
)
2766 char *namebuf
, *end
;
2769 ssize_t bufsize
= 4096;
2773 int look_for_decmpea
= 0;
2775 /* delete EAs on destination */
2776 if ((nsize
= flistxattr(s
->dst_fd
, 0, 0, 0)) > 0)
2778 if ((namebuf
= (char *) malloc(nsize
)) == NULL
)
2781 nsize
= flistxattr(s
->dst_fd
, namebuf
, nsize
, 0);
2785 * With this, end points to the last byte of the allocated buffer
2786 * This *should* be NUL, from flistxattr, but if it's not, we can
2787 * set it anyway -- it'll result in a truncated name, which then
2788 * shouldn't match when we get them later.
2790 end
= namebuf
+ nsize
- 1;
2793 for (name
= namebuf
; name
<= end
; name
+= strlen(name
) + 1) {
2794 /* If the quarantine information shows up as an EA, we skip over it */
2795 if (strncmp(name
, XATTR_QUARANTINE_NAME
, end
- name
) == 0) {
2798 fremovexattr(s
->dst_fd
, name
,0);
2805 if (errno
== ENOTSUP
|| errno
== EPERM
)
2811 #ifdef DECMPFS_XATTR_NAME
2812 if ((s
->flags
& COPYFILE_DATA
) &&
2813 (s
->sb
.st_flags
& UF_COMPRESSED
) &&
2814 doesdecmpfs(s
->src_fd
) &&
2815 doesdecmpfs(s
->dst_fd
)) {
2816 look_for_decmpea
= XATTR_SHOWCOMPRESSION
;
2820 /* get name list of EAs on source */
2821 if ((nsize
= flistxattr(s
->src_fd
, 0, 0, look_for_decmpea
)) < 0)
2823 if (errno
== ENOTSUP
|| errno
== EPERM
)
2831 if ((namebuf
= (char *) malloc(nsize
)) == NULL
)
2834 nsize
= flistxattr(s
->src_fd
, namebuf
, nsize
, look_for_decmpea
);
2842 * With this, end points to the last byte of the allocated buffer
2843 * This *should* be NUL, from flistxattr, but if it's not, we can
2844 * set it anyway -- it'll result in a truncated name, which then
2845 * shouldn't match when we get them later.
2847 end
= namebuf
+ nsize
- 1;
2851 if ((xa_dataptr
= (void *) malloc(bufsize
)) == NULL
) {
2856 for (name
= namebuf
; name
<= end
; name
+= strlen(name
) + 1)
2858 if (s
->xattr_name
) {
2859 free(s
->xattr_name
);
2860 s
->xattr_name
= NULL
;
2863 /* If the quarantine information shows up as an EA, we skip over it */
2864 if (strncmp(name
, XATTR_QUARANTINE_NAME
, end
- name
) == 0)
2867 if ((xa_size
= fgetxattr(s
->src_fd
, name
, 0, 0, 0, look_for_decmpea
)) < 0)
2872 if (xa_size
> bufsize
)
2874 void *tdptr
= xa_dataptr
;
2877 (void *) realloc((void *) xa_dataptr
, bufsize
)) == NULL
)
2885 if ((asize
= fgetxattr(s
->src_fd
, name
, xa_dataptr
, xa_size
, 0, look_for_decmpea
)) < 0)
2890 if (xa_size
!= asize
)
2893 #ifdef DECMPFS_XATTR_NAME
2894 if (strncmp(name
, DECMPFS_XATTR_NAME
, end
-name
) == 0)
2896 decmpfs_disk_header
*hdr
= xa_dataptr
;
2899 * If the EA has the decmpfs name, but is too
2900 * small, or doesn't have the right magic number,
2901 * or isn't the right type, we'll just skip it.
2902 * This means it won't end up in the destination
2903 * file, and data copy will happen normally.
2905 if ((size_t)xa_size
< sizeof(decmpfs_disk_header
)) {
2908 if (OSSwapLittleToHostInt32(hdr
->compression_magic
) != DECMPFS_MAGIC
) {
2912 * From AppleFSCompression documentation:
2913 * "It is incumbent on the aware copy engine to identify
2914 * the type of compression being used, and to perform an
2915 * unaware copy of any file it does not recognize."
2917 * Compression Types are defined in:
2918 * "AppleFSCompression/Common/compressorCommon.h"
2920 * Unfortunately, they don't provide a way to dynamically
2921 * determine what possible compression_type values exist,
2922 * so we have to update this every time a new compression_type
2923 * is added. Types 7->10 were added in 10.10, Types 11 & 12
2924 * were added in 10.11.
2926 * Ubiquity faulting file compression type 0x80000001 are
2927 * deprecated as of Yosemite, per rdar://17714998 don't copy the
2928 * decmpfs xattr on these files, zero byte files are safer
2929 * than a fault nobody knows how to handle.
2931 switch (OSSwapLittleToHostInt32(hdr
->compression_type
)) {
2932 case 3: /* zlib-compressed data in xattr */
2933 case 4: /* 64k chunked zlib-compressed data in resource fork */
2935 case 7: /* LZVN-compressed data in xattr */
2936 case 8: /* 64k chunked LZVN-compressed data in resource fork */
2938 case 9: /* uncompressed data in xattr (similar to but not identical to CMP_Type1) */
2939 case 10: /* 64k chunked uncompressed data in resource fork */
2941 case 11: /* LZFSE-compressed data in xattr */
2942 case 12: /* 64k chunked LZFSE-compressed data in resource fork */
2944 /* valid compression type, we want to copy. */
2947 case 5: /* specifies de-dup within the generation store. Don't copy decmpfs xattr. */
2948 copyfile_debug(3, "compression_type <5> on attribute com.apple.decmpfs for src file %s is not copied.",
2949 s
->src
? s
->src
: "(null string)");
2952 case 6: /* unused */
2953 case 0x80000001: /* faulting files are deprecated, don't copy decmpfs xattr */
2955 copyfile_warn("Invalid compression_type <%d> on attribute %s for src file %s",
2956 OSSwapLittleToHostInt32(hdr
->compression_type
), name
, s
->src
? s
->src
: "(null string)");
2959 s
->internal_flags
|= cfSawDecmpEA
;
2963 // If we have a copy intention stated, and the EA is to be ignored, we ignore it
2965 && xattr_preserve_for_intent(name
, s
->copyIntent
) == 0)
2968 s
->xattr_name
= strdup(name
);
2972 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_START
, s
, s
->src
, s
->dst
, s
->ctx
);
2973 if (rv
== COPYFILE_QUIT
) {
2976 } else if (rv
== COPYFILE_SKIP
) {
2980 if (fsetxattr(s
->dst_fd
, name
, xa_dataptr
, xa_size
, 0, look_for_decmpea
) < 0)
2983 if (error
== EPERM
&& strcmp(name
, XATTR_ROOT_INSTALLED_NAME
) == 0) {
2984 //Silently ignore if we fail to set XATTR_ROOT_INSTALLED_NAME
2988 else if (s
->statuscb
)
2992 if (s
->xattr_name
== NULL
)
2993 s
->xattr_name
= strdup(name
);
2994 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
2995 if (rv
== COPYFILE_QUIT
)
3006 copyfile_warn("could not set attributes %s on destination file descriptor: %s", name
, strerror(error
));
3012 if (s
->xattr_name
== NULL
)
3013 s
->xattr_name
= strdup(name
);
3015 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_FINISH
, s
, s
->src
, s
->dst
, s
->ctx
);
3016 if (rv
== COPYFILE_QUIT
) {
3025 free((void *) xa_dataptr
);
3026 if (s
->xattr_name
) {
3027 free(s
->xattr_name
);
3028 s
->xattr_name
= NULL
;
3034 * API interface into getting data from the opaque data type.
3036 int copyfile_state_get(copyfile_state_t s
, uint32_t flag
, void *ret
)
3046 case COPYFILE_STATE_SRC_FD
:
3047 *(int*)ret
= s
->src_fd
;
3049 case COPYFILE_STATE_DST_FD
:
3050 *(int*)ret
= s
->dst_fd
;
3052 case COPYFILE_STATE_SRC_FILENAME
:
3053 *(char**)ret
= s
->src
;
3055 case COPYFILE_STATE_DST_FILENAME
:
3056 *(char**)ret
= s
->dst
;
3058 case COPYFILE_STATE_QUARANTINE
:
3059 *(qtn_file_t
*)ret
= s
->qinfo
;
3062 case COPYFILE_STATE_STATS
:
3063 ret
= s
->stats
.global
;
3065 case COPYFILE_STATE_PROGRESS_CB
:
3066 ret
= s
->callbacks
.progress
;
3069 #ifdef COPYFILE_STATE_STATUS_CB
3070 case COPYFILE_STATE_STATUS_CB
:
3071 *(copyfile_callback_t
*)ret
= s
->statuscb
;
3073 case COPYFILE_STATE_STATUS_CTX
:
3074 *(void**)ret
= s
->ctx
;
3076 case COPYFILE_STATE_COPIED
:
3077 *(off_t
*)ret
= s
->totalCopied
;
3080 #ifdef COPYFILE_STATE_XATTRNAME
3081 case COPYFILE_STATE_XATTRNAME
:
3082 *(char**)ret
= s
->xattr_name
;
3085 #ifdef COPYFILE_STATE_INTENT
3086 case COPYFILE_STATE_INTENT
:
3087 *(xattr_operation_intent_t
*)ret
= s
->copyIntent
;
3090 case COPYFILE_STATE_WAS_CLONED
:
3091 *(bool *)ret
= s
->was_cloned
;
3102 * Public API for setting state data (remember that the state is
3103 * an opaque data type).
3105 int copyfile_state_set(copyfile_state_t s
, uint32_t flag
, const void * thing
)
3107 #define copyfile_set_string(DST, SRC) \
3109 if (SRC != NULL) { \
3110 DST = strdup((char *)SRC); \
3112 if (DST != NULL) { \
3127 case COPYFILE_STATE_SRC_FD
:
3128 s
->src_fd
= *(int*)thing
;
3130 case COPYFILE_STATE_DST_FD
:
3131 s
->dst_fd
= *(int*)thing
;
3133 case COPYFILE_STATE_SRC_FILENAME
:
3134 copyfile_set_string(s
->src
, thing
);
3136 case COPYFILE_STATE_DST_FILENAME
:
3137 copyfile_set_string(s
->dst
, thing
);
3139 case COPYFILE_STATE_QUARANTINE
:
3142 qtn_file_free(s
->qinfo
);
3145 if (*(qtn_file_t
*)thing
)
3146 s
->qinfo
= qtn_file_clone(*(qtn_file_t
*)thing
);
3149 case COPYFILE_STATE_STATS
:
3150 s
->stats
.global
= thing
;
3152 case COPYFILE_STATE_PROGRESS_CB
:
3153 s
->callbacks
.progress
= thing
;
3156 #ifdef COPYFILE_STATE_STATUS_CB
3157 case COPYFILE_STATE_STATUS_CB
:
3158 s
->statuscb
= (copyfile_callback_t
)thing
;
3160 case COPYFILE_STATE_STATUS_CTX
:
3161 s
->ctx
= (void*)thing
;
3164 #ifdef COPYFILE_STATE_INTENT
3165 case COPYFILE_STATE_INTENT
:
3166 s
->copyIntent
= *(xattr_operation_intent_t
*)thing
;
3174 #undef copyfile_set_string
3179 * Make this a standalone program for testing purposes by
3180 * defining _COPYFILE_TEST.
3182 #ifdef _COPYFILE_TEST
3183 #define COPYFILE_OPTION(x) { #x, COPYFILE_ ## x },
3185 struct {char *s
; int v
;} opts
[] = {
3186 COPYFILE_OPTION(ACL
)
3187 COPYFILE_OPTION(STAT
)
3188 COPYFILE_OPTION(XATTR
)
3189 COPYFILE_OPTION(DATA
)
3190 COPYFILE_OPTION(SECURITY
)
3191 COPYFILE_OPTION(METADATA
)
3192 COPYFILE_OPTION(ALL
)
3193 COPYFILE_OPTION(NOFOLLOW_SRC
)
3194 COPYFILE_OPTION(NOFOLLOW_DST
)
3195 COPYFILE_OPTION(NOFOLLOW
)
3196 COPYFILE_OPTION(EXCL
)
3197 COPYFILE_OPTION(MOVE
)
3198 COPYFILE_OPTION(UNLINK
)
3199 COPYFILE_OPTION(PACK
)
3200 COPYFILE_OPTION(UNPACK
)
3201 COPYFILE_OPTION(CHECK
)
3202 COPYFILE_OPTION(CLONE
)
3203 COPYFILE_OPTION(CLONE_FORCE
)
3204 COPYFILE_OPTION(VERBOSE
)
3205 COPYFILE_OPTION(RECURSIVE
)
3206 COPYFILE_OPTION(DEBUG
)
3207 COPYFILE_OPTION(CLONE
)
3208 COPYFILE_OPTION(CLONE_FORCE
)
3209 COPYFILE_OPTION(DATA_SPARSE
)
3213 int main(int c
, char *v
[])
3217 copyfile_state_t state
= NULL
;
3220 errx(1, "insufficient arguments");
3224 for (i
= 0; opts
[i
].s
!= NULL
; ++i
)
3226 if (strcasecmp(opts
[i
].s
, v
[c
]) == 0)
3228 printf("option %d: %s <- %d\n", c
, opts
[i
].s
, opts
[i
].v
);
3235 if (flags
& COPYFILE_DEBUG
) {
3236 state
= copyfile_state_alloc();
3237 state
->debug
= 10; // Turn on all debug statements
3239 ret
= copyfile(v
[1], v
[2], state
, flags
);
3241 (void)copyfile_state_free(state
);
3248 * Apple Double Create
3250 * Create an Apple Double "._" file from a file's extented attributes
3252 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3256 #define offsetof(type, member) __builtin_offsetof(type, member)
3258 #define XATTR_MAXATTRLEN (16*1024*1024)
3262 Typical "._" AppleDouble Header File layout:
3263 ------------------------------------------------------------
3268 .-- AD ENTRY[0] Finder Info Entry (must be first)
3269 .--+-- AD ENTRY[1] Resource Fork Entry (must be last)
3271 | ///////////// Fixed Size Data (32 bytes)
3275 | ATTR ENTRY[1] --+--.
3276 | ATTR ENTRY[2] --+--+--.
3278 | ATTR ENTRY[N] --+--+--+--.
3279 | ATTR DATA 0 <-' | | |
3280 | //////////// | | |
3281 | ATTR DATA 1 <----' | |
3283 | ATTR DATA 2 <-------' |
3286 | ATTR DATA N <----------'
3288 | Attribute Free Space
3290 '----> RESOURCE FORK
3291 ///////////// Variable Sized Data
3300 ------------------------------------------------------------
3302 NOTE: The EXT ATTR HDR, ATTR ENTRY's and ATTR DATA's are
3303 stored as part of the Finder Info. The length in the Finder
3304 Info AppleDouble entry includes the length of the extended
3305 attribute header, attribute entries, and attribute data.
3310 * On Disk Data Structures
3312 * Note: Motorola 68K alignment and big-endian.
3314 * See RFC 1740 for additional information about the AppleDouble file format.
3318 #define ADH_MAGIC 0x00051607
3319 #define ADH_VERSION 0x00020000
3320 #define ADH_MACOSX "Mac OS X "
3323 * AppleDouble Entry ID's
3325 #define AD_DATA 1 /* Data fork */
3326 #define AD_RESOURCE 2 /* Resource fork */
3327 #define AD_REALNAME 3 /* File's name on home file system */
3328 #define AD_COMMENT 4 /* Standard Mac comment */
3329 #define AD_ICONBW 5 /* Mac black & white icon */
3330 #define AD_ICONCOLOR 6 /* Mac color icon */
3331 #define AD_UNUSED 7 /* Not used */
3332 #define AD_FILEDATES 8 /* File dates; create, modify, etc */
3333 #define AD_FINDERINFO 9 /* Mac Finder info & extended info */
3334 #define AD_MACINFO 10 /* Mac file info, attributes, etc */
3335 #define AD_PRODOSINFO 11 /* Pro-DOS file info, attrib., etc */
3336 #define AD_MSDOSINFO 12 /* MS-DOS file info, attributes, etc */
3337 #define AD_AFPNAME 13 /* Short name on AFP server */
3338 #define AD_AFPINFO 14 /* AFP file info, attrib., etc */
3339 #define AD_AFPDIRID 15 /* AFP directory ID */
3340 #define AD_ATTRIBUTES AD_FINDERINFO
3343 #define ATTR_FILE_PREFIX "._"
3344 #define ATTR_HDR_MAGIC 0x41545452 /* 'ATTR' */
3346 #define ATTR_BUF_SIZE 4096 /* default size of the attr file and how much we'll grow by */
3348 /* Implementation Limits */
3349 #define ATTR_MAX_SIZE (16*1024*1024) /* 16 megabyte maximum attribute data size */
3350 #define ATTR_MAX_NAME_LEN 128
3351 #define ATTR_MAX_HDR_SIZE (65536+18)
3354 * Note: ATTR_MAX_HDR_SIZE is the largest attribute header
3355 * size supported (including the attribute entries). All of
3356 * the attribute entries must reside within this limit.
3360 #define FINDERINFOSIZE 32
3362 typedef struct apple_double_entry
3364 u_int32_t type
; /* entry type: see list, 0 invalid */
3365 u_int32_t offset
; /* entry data offset from the beginning of the file. */
3366 u_int32_t length
; /* entry data length in bytes. */
3367 } __attribute__((aligned(2), packed
)) apple_double_entry_t
;
3370 typedef struct apple_double_header
3372 u_int32_t magic
; /* == ADH_MAGIC */
3373 u_int32_t version
; /* format version: 2 = 0x00020000 */
3374 u_int32_t filler
[4];
3375 u_int16_t numEntries
; /* number of entries which follow */
3376 apple_double_entry_t entries
[2]; /* 'finfo' & 'rsrc' always exist */
3377 u_int8_t finfo
[FINDERINFOSIZE
]; /* Must start with Finder Info (32 bytes) */
3378 u_int8_t pad
[2]; /* get better alignment inside attr_header */
3379 } __attribute__((aligned(2), packed
)) apple_double_header_t
;
3382 /* Entries are aligned on 4 byte boundaries */
3383 typedef struct attr_entry
3385 u_int32_t offset
; /* file offset to data */
3386 u_int32_t length
; /* size of attribute data */
3388 u_int8_t namelen
; /* length of name including NULL termination char */
3389 u_int8_t name
[1]; /* NULL-terminated UTF-8 name (up to 128 bytes max) */
3390 } __attribute__((aligned(2), packed
)) attr_entry_t
;
3394 /* Header + entries must fit into 64K */
3395 typedef struct attr_header
3397 apple_double_header_t appledouble
;
3398 u_int32_t magic
; /* == ATTR_HDR_MAGIC */
3399 u_int32_t debug_tag
; /* for debugging == file id of owning file */
3400 u_int32_t total_size
; /* total size of attribute header + entries + data */
3401 u_int32_t data_start
; /* file offset to attribute data area */
3402 u_int32_t data_length
; /* length of attribute data area */
3403 u_int32_t reserved
[3];
3405 u_int16_t num_attrs
;
3406 } __attribute__((aligned(2), packed
)) attr_header_t
;
3408 /* Empty Resource Fork Header */
3409 /* This comes by way of xnu's vfs_xattr.c */
3410 typedef struct rsrcfork_header
{
3411 u_int32_t fh_DataOffset
;
3412 u_int32_t fh_MapOffset
;
3413 u_int32_t fh_DataLength
;
3414 u_int32_t fh_MapLength
;
3415 u_int8_t systemData
[112];
3416 u_int8_t appData
[128];
3417 u_int32_t mh_DataOffset
;
3418 u_int32_t mh_MapOffset
;
3419 u_int32_t mh_DataLength
;
3420 u_int32_t mh_MapLength
;
3422 u_int16_t mh_RefNum
;
3424 u_int8_t mh_InMemoryAttr
;
3427 u_int16_t typeCount
;
3428 } __attribute__((aligned(2), packed
)) rsrcfork_header_t
;
3429 #define RF_FIRST_RESOURCE 256
3430 #define RF_NULL_MAP_LENGTH 30
3431 #define RF_EMPTY_TAG "This resource fork intentionally left blank "
3433 static const rsrcfork_header_t empty_rsrcfork_header
= {
3434 OSSwapHostToBigInt32(RF_FIRST_RESOURCE
), // fh_DataOffset
3435 OSSwapHostToBigInt32(RF_FIRST_RESOURCE
), // fh_MapOffset
3437 OSSwapHostToBigInt32(RF_NULL_MAP_LENGTH
), // fh_MapLength
3438 { RF_EMPTY_TAG
, }, // systemData
3440 OSSwapHostToBigInt32(RF_FIRST_RESOURCE
), // mh_DataOffset
3441 OSSwapHostToBigInt32(RF_FIRST_RESOURCE
), // mh_MapOffset
3443 OSSwapHostToBigInt32(RF_NULL_MAP_LENGTH
), // mh_MapLength
3447 0, // mh_InMemoryAttr
3448 OSSwapHostToBigInt16(RF_NULL_MAP_LENGTH
- 2), // mh_Types
3449 OSSwapHostToBigInt16(RF_NULL_MAP_LENGTH
), // mh_Names
3450 OSSwapHostToBigInt16(-1), // typeCount
3453 #define SWAP16(x) OSSwapBigToHostInt16(x)
3454 #define SWAP32(x) OSSwapBigToHostInt32(x)
3455 #define SWAP64(x) OSSwapBigToHostInt64(x)
3457 #define ATTR_ALIGN 3L /* Use four-byte alignment */
3459 #define ATTR_ENTRY_LENGTH(namelen) \
3460 ((sizeof(attr_entry_t) - 1 + (namelen) + ATTR_ALIGN) & (~ATTR_ALIGN))
3462 #define ATTR_NEXT(ae) \
3463 (attr_entry_t *)((u_int8_t *)(ae) + ATTR_ENTRY_LENGTH((ae)->namelen))
3465 #define XATTR_SECURITY_NAME "com.apple.acl.text"
3468 * Endian swap Apple Double header
3471 swap_adhdr(apple_double_header_t
*adh
)
3473 #if BYTE_ORDER == LITTLE_ENDIAN
3477 count
= (adh
->magic
== ADH_MAGIC
) ? adh
->numEntries
: SWAP16(adh
->numEntries
);
3479 adh
->magic
= SWAP32 (adh
->magic
);
3480 adh
->version
= SWAP32 (adh
->version
);
3481 adh
->numEntries
= SWAP16 (adh
->numEntries
);
3483 for (i
= 0; i
< count
; i
++)
3485 adh
->entries
[i
].type
= SWAP32 (adh
->entries
[i
].type
);
3486 adh
->entries
[i
].offset
= SWAP32 (adh
->entries
[i
].offset
);
3487 adh
->entries
[i
].length
= SWAP32 (adh
->entries
[i
].length
);
3495 * Endian swap a single attr_entry_t
3498 swap_attrhdr_entry(attr_entry_t
*ae
)
3500 #if BYTE_ORDER == LITTLE_ENDIAN
3501 ae
->offset
= SWAP32 (ae
->offset
);
3502 ae
->length
= SWAP32 (ae
->length
);
3503 ae
->flags
= SWAP16 (ae
->flags
);
3510 * For a validated/endian swapped attr_header_t*
3511 * ah, endian swap all of the entries.
3514 swap_attrhdr_entries(attr_header_t
*ah
)
3516 #if BYTE_ORDER == LITTLE_ENDIAN
3519 attr_entry_t
*entry
;
3522 /* If we're in copyfile_pack, num_args is native endian,
3523 * if we're in _unpack, num_args is big endian. Use
3524 * the magic number to test for endianess.
3526 count
= (ah
->magic
== ATTR_HDR_MAGIC
) ? ah
->num_attrs
: SWAP16(ah
->num_attrs
);
3528 entry
= (attr_entry_t
*)(&ah
[1]);
3529 for (i
= 0; i
< count
; i
++) {
3530 next
= ATTR_NEXT(entry
);
3531 swap_attrhdr_entry(entry
);
3540 * Endian swap extended attributes header
3543 swap_attrhdr(attr_header_t
*ah
)
3545 #if BYTE_ORDER == LITTLE_ENDIAN
3546 ah
->magic
= SWAP32 (ah
->magic
);
3547 ah
->debug_tag
= SWAP32 (ah
->debug_tag
);
3548 ah
->total_size
= SWAP32 (ah
->total_size
);
3549 ah
->data_start
= SWAP32 (ah
->data_start
);
3550 ah
->data_length
= SWAP32 (ah
->data_length
);
3551 ah
->flags
= SWAP16 (ah
->flags
);
3552 ah
->num_attrs
= SWAP16 (ah
->num_attrs
);
3558 static const u_int32_t emptyfinfo
[8] = {0};
3561 * Given an Apple Double file in src, turn it into a
3562 * normal file (possibly with multiple forks, EAs, and
3565 static int copyfile_unpack(copyfile_state_t s
)
3568 void * buffer
, * endptr
, * dataptr
= NULL
;
3569 apple_double_header_t
*adhdr
;
3573 if (s
->sb
.st_size
< ATTR_MAX_HDR_SIZE
)
3574 hdrsize
= (ssize_t
)s
->sb
.st_size
;
3576 hdrsize
= ATTR_MAX_HDR_SIZE
;
3578 buffer
= calloc(1, hdrsize
);
3579 if (buffer
== NULL
) {
3580 copyfile_debug(1, "copyfile_unpack: calloc(1, %zu) returned NULL", hdrsize
);
3584 endptr
= (char*)buffer
+ hdrsize
;
3586 bytes
= pread(s
->src_fd
, buffer
, hdrsize
, 0);
3590 copyfile_debug(1, "pread returned: %zd", bytes
);
3594 if (bytes
< hdrsize
)
3597 "pread couldn't read entire header: %d of %d",
3598 (int)bytes
, (int)s
->sb
.st_size
);
3602 adhdr
= (apple_double_header_t
*)buffer
;
3605 * Check for Apple Double file.
3607 if ((size_t)bytes
< sizeof(apple_double_header_t
) - 2 ||
3608 SWAP32(adhdr
->magic
) != ADH_MAGIC
||
3609 SWAP32(adhdr
->version
) != ADH_VERSION
||
3610 SWAP16(adhdr
->numEntries
) != 2 ||
3611 SWAP32(adhdr
->entries
[0].type
) != AD_FINDERINFO
)
3613 if (COPYFILE_VERBOSE
& s
->flags
)
3614 copyfile_warn("Not a valid Apple Double header");
3621 * Remove any extended attributes on the target.
3624 if ((bytes
= flistxattr(s
->dst_fd
, 0, 0, 0)) > 0)
3626 char *namebuf
, *name
;
3628 if ((namebuf
= (char*) malloc(bytes
)) == NULL
)
3633 bytes
= flistxattr(s
->dst_fd
, namebuf
, bytes
, 0);
3636 for (name
= namebuf
; name
< namebuf
+ bytes
; name
+= strlen(name
) + 1)
3637 (void)fremovexattr(s
->dst_fd
, name
, 0);
3643 if (errno
!= ENOTSUP
&& errno
!= EPERM
)
3648 * Extract the extended attributes.
3651 * This assumes that the data is already in memory (not
3652 * the case when there are lots of attributes or one of
3653 * the attributes is very large.
3655 if (adhdr
->entries
[0].length
> FINDERINFOSIZE
)
3657 attr_header_t
*attrhdr
;
3658 attr_entry_t
*entry
;
3662 if ((size_t)hdrsize
< sizeof(attr_header_t
)) {
3663 copyfile_warn("bad attribute header: %zu < %zu", hdrsize
, sizeof(attr_header_t
));
3668 attrhdr
= (attr_header_t
*)buffer
;
3669 swap_attrhdr(attrhdr
);
3670 if (attrhdr
->magic
!= ATTR_HDR_MAGIC
)
3672 if (COPYFILE_VERBOSE
& s
->flags
)
3673 copyfile_warn("bad attribute header");
3677 count
= attrhdr
->num_attrs
;
3678 entry
= (attr_entry_t
*)&attrhdr
[1];
3680 for (i
= 0; i
< count
; i
++)
3683 * First we do some simple sanity checking.
3684 * +) See if entry is within the buffer's range;
3686 * +) Check the attribute name length; if it's longer than the
3687 * maximum, we truncate it down. (We could error out as well;
3688 * I'm not sure which is the better way to go here.)
3690 * +) If, given the name length, it goes beyond the end of
3691 * the buffer, error out.
3693 * +) If the last byte isn't a NUL, make it a NUL. (Since we
3694 * truncated the name length above, we truncate the name here.)
3696 * +) If entry->offset is so large that it causes dataptr to
3697 * go beyond the end of the buffer -- or, worse, so large that
3698 * it wraps around! -- we error out.
3700 * +) If entry->length would cause the entry to go beyond the
3701 * end of the buffer (or, worse, wrap around to before it),
3702 * *or* if the length is larger than the hdrsize, we error out.
3703 * (An explanation of that: what we're checking for there is
3704 * the small range of values such that offset+length would cause
3705 * it to go beyond endptr, and then wrap around past buffer. We
3706 * care about this because we are passing entry->length down to
3707 * fgetxattr() below, and an erroneously large value could cause
3708 * problems there. By making sure that it's less than hdrsize,
3709 * which has already been sanity-checked above, we're safe.
3710 * That may mean that the check against < buffer is unnecessary.)
3712 if ((void*)entry
>= endptr
|| (void*)entry
< buffer
) {
3713 if (COPYFILE_VERBOSE
& s
->flags
)
3714 copyfile_warn("Incomplete or corrupt attribute entry");
3720 if (((char*)entry
+ sizeof(*entry
)) > (char*)endptr
) {
3721 if (COPYFILE_VERBOSE
& s
->flags
)
3722 copyfile_warn("Incomplete or corrupt attribute entry");
3729 * Endian swap the entry we're looking at. Previously
3730 * we did this swap as part of swap_attrhdr, but that
3731 * allowed a maliciously constructed file to overrun
3732 * our allocation. Instead do the swap after we've verified
3733 * the entry struct is within the buffer's range.
3735 swap_attrhdr_entry(entry
);
3737 if (entry
->namelen
< 2) {
3738 if (COPYFILE_VERBOSE
& s
->flags
)
3739 copyfile_warn("Corrupt attribute entry (only %d bytes)", entry
->namelen
);
3745 if (entry
->namelen
> XATTR_MAXNAMELEN
+ 1) {
3746 if (COPYFILE_VERBOSE
& s
->flags
)
3747 copyfile_warn("Corrupt attribute entry (name length is %d bytes)", entry
->namelen
);
3753 if ((void*)(entry
->name
+ entry
->namelen
) > endptr
) {
3754 if (COPYFILE_VERBOSE
& s
->flags
)
3755 copyfile_warn("Incomplete or corrupt attribute entry");
3761 /* Because namelen includes the NUL, we check one byte back */
3762 if (entry
->name
[entry
->namelen
-1] != 0) {
3763 if (COPYFILE_VERBOSE
& s
->flags
)
3764 copyfile_warn("Corrupt attribute entry (name is not NUL-terminated)");
3770 copyfile_debug(3, "extracting \"%s\" (%d bytes) at offset %u",
3771 entry
->name
, entry
->length
, entry
->offset
);
3774 dataptr
= (char *)attrhdr
+ entry
->offset
;
3776 if (dataptr
> endptr
|| dataptr
< buffer
) {
3777 copyfile_debug(1, "Entry %d overflows: offset = %u", i
, entry
->offset
);
3779 s
->err
= EINVAL
; /* Invalid buffer */
3783 if (((char*)dataptr
+ entry
->length
) > (char*)endptr
||
3784 (((char*)dataptr
+ entry
->length
) < (char*)buffer
) ||
3785 (entry
->length
> (size_t)hdrsize
)) {
3786 if (COPYFILE_VERBOSE
& s
->flags
)
3787 copyfile_warn("Incomplete or corrupt attribute entry");
3788 copyfile_debug(1, "Entry %d length overflows: offset = %u, length = %u",
3789 i
, entry
->offset
, entry
->length
);
3791 s
->err
= EINVAL
; /* Invalid buffer */
3796 dataptr
= malloc(entry
->length
);
3797 if (dataptr
== NULL
) {
3798 copyfile_debug(1, "no memory for %u bytes\n", entry
->length
);
3803 if (pread(s
->src_fd
, dataptr
, entry
->length
, entry
->offset
) != (ssize_t
)entry
->length
) {
3804 copyfile_debug(1, "failed to read %u bytes at offset %u\n", entry
->length
, entry
->offset
);
3811 if (strcmp((char*)entry
->name
, XATTR_QUARANTINE_NAME
) == 0)
3813 qtn_file_t tqinfo
= NULL
;
3815 if (s
->qinfo
== NULL
)
3817 tqinfo
= qtn_file_alloc();
3821 if ((x
= qtn_file_init_with_data(tqinfo
, dataptr
, entry
->length
)) != 0)
3823 copyfile_warn("qtn_file_init_with_data failed: %s", qtn_error(x
));
3824 qtn_file_free(tqinfo
);
3836 x
= qtn_file_apply_to_fd(tqinfo
, s
->dst_fd
);
3838 copyfile_warn("qtn_file_apply_to_fd failed: %s", qtn_error(x
));
3841 s
->xattr_name
= (char*)XATTR_QUARANTINE_NAME
;
3842 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
3843 s
->xattr_name
= NULL
;
3844 if (rv
== COPYFILE_QUIT
) {
3845 error
= s
->err
= x
< 0 ? ENOTSUP
: errno
;
3849 error
= s
->err
= x
< 0 ? ENOTSUP
: errno
;
3854 if (tqinfo
&& !s
->qinfo
)
3856 qtn_file_free(tqinfo
);
3859 /* Look for ACL data */
3860 else if (strcmp((char*)entry
->name
, XATTR_SECURITY_NAME
) == 0)
3865 char *tcp
= dataptr
;
3867 if (entry
->length
== 0) {
3868 /* Not sure how we got here, but we had one case
3869 * where it was 0. In a normal EA, we can have a 0-byte
3870 * payload. That means nothing in this case, so we'll
3871 * simply skip the EA.
3877 * acl_from_text() requires a NUL-terminated string. The ACL EA,
3878 * however, may not be NUL-terminated. So in that case, we need to
3879 * copy it to a +1 sized buffer, to ensure it's got a terminated string.
3881 if (tcp
[entry
->length
- 1] != 0) {
3882 char *tmpstr
= malloc(entry
->length
+ 1);
3883 if (tmpstr
== NULL
) {
3887 // Can't use strlcpy here: tcp is not NUL-terminated!
3888 memcpy(tmpstr
, tcp
, entry
->length
);
3889 tmpstr
[entry
->length
] = 0;
3890 acl
= acl_from_text(tmpstr
);
3893 acl
= acl_from_text(tcp
);
3900 if ((fsec_tmp
= filesec_init()) == NULL
)
3902 else if((error
= fstatx_np(s
->dst_fd
, &sb
, fsec_tmp
)) < 0)
3904 else if (filesec_set_property(fsec_tmp
, FILESEC_ACL
, &acl
) < 0)
3907 while (fchmodx_np(s
->dst_fd
, fsec_tmp
) < 0)
3909 if (errno
== ENOTSUP
)
3911 if (retry
&& !copyfile_unset_acl(s
))
3917 copyfile_warn("setting security information");
3923 filesec_free(fsec_tmp
);
3930 /* And, finally, everything else */
3933 if (s
->copyIntent
||
3934 xattr_preserve_for_intent((char*)entry
->name
, s
->copyIntent
) == 1) {
3937 s
->xattr_name
= strdup((char*)entry
->name
);
3939 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_START
, s
, s
->src
, s
->dst
, s
->ctx
);
3940 if (s
->xattr_name
) {
3941 free(s
->xattr_name
);
3942 s
->xattr_name
= NULL
;
3944 if (rv
== COPYFILE_QUIT
) {
3950 //Silently ignore failure to set XATTR_ROOT_INSTALLED_NAME
3951 int result
= fsetxattr(s
->dst_fd
, (char *)entry
->name
, dataptr
, entry
->length
, 0, 0);
3952 int errorcode
= errno
;
3953 if (result
== -1 && !(errorcode
== EPERM
&&
3954 strcmp((char*)entry
->name
, XATTR_ROOT_INSTALLED_NAME
) == 0)) {
3956 if (COPYFILE_VERBOSE
& s
->flags
)
3957 copyfile_warn("error %d setting attribute %s", errorcode
, entry
->name
);
3961 s
->xattr_name
= strdup((char*)entry
->name
);
3962 rv
= (s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
3963 if (s
->xattr_name
) {
3964 free(s
->xattr_name
);
3965 s
->xattr_name
= NULL
;
3967 if (rv
== COPYFILE_QUIT
) {
3975 } else if (s
->statuscb
) {
3978 s
->xattr_name
= strdup((char*)entry
->name
);
3979 s
->totalCopied
= entry
->length
;
3980 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_FINISH
, s
, s
->src
, s
->dst
, s
->ctx
);
3981 if (s
->xattr_name
) {
3982 free(s
->xattr_name
);
3983 s
->xattr_name
= NULL
;
3985 if (rv
== COPYFILE_QUIT
) {
3999 entry
= ATTR_NEXT(entry
);
4004 * Extract the Finder Info.
4006 if (adhdr
->entries
[0].offset
> (hdrsize
- sizeof(emptyfinfo
))) {
4011 if (bcmp((u_int8_t
*)buffer
+ adhdr
->entries
[0].offset
, emptyfinfo
, sizeof(emptyfinfo
)) != 0)
4015 enum { kFinderInvisibleMask
= 1 << 14 };
4017 newFinfo
= (u_int8_t
*)buffer
+ adhdr
->entries
[0].offset
;
4018 fFlags
= (uint16_t*)&newFinfo
[8];
4019 copyfile_debug(3, " extracting \"%s\" (32 bytes)", XATTR_FINDERINFO_NAME
);
4022 s
->xattr_name
= (char*)XATTR_FINDERINFO_NAME
;
4023 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_START
, s
, s
->src
, s
->dst
, s
->ctx
);
4024 s
->xattr_name
= NULL
;
4025 if (rv
== COPYFILE_QUIT
) {
4029 } else if (rv
== COPYFILE_SKIP
) {
4033 error
= fsetxattr(s
->dst_fd
, XATTR_FINDERINFO_NAME
, (u_int8_t
*)buffer
+ adhdr
->entries
[0].offset
, sizeof(emptyfinfo
), 0, 0);
4037 s
->xattr_name
= (char *)XATTR_FINDERINFO_NAME
;
4038 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
4039 s
->xattr_name
= NULL
;
4040 if (rv
== COPYFILE_QUIT
) {
4047 } else if (s
->statuscb
) {
4049 s
->xattr_name
= (char *)XATTR_FINDERINFO_NAME
;
4050 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_FINISH
, s
, s
->src
, s
->dst
, s
->ctx
);
4051 s
->xattr_name
= NULL
;
4052 if (rv
== COPYFILE_QUIT
) {
4058 if (SWAP16(*fFlags
) & kFinderInvisibleMask
)
4059 s
->internal_flags
|= cfMakeFileInvisible
;
4064 * Extract the Resource Fork.
4066 if (adhdr
->entries
[1].type
== AD_RESOURCE
&&
4067 adhdr
->entries
[1].length
> 0)
4069 void * rsrcforkdata
= NULL
;
4073 struct attrlist attrlist
;
4075 /* Order of these structs matters for setattrlist. */
4076 struct timespec mod_time
;
4077 struct timespec acc_time
;
4080 length
= adhdr
->entries
[1].length
;
4081 offset
= adhdr
->entries
[1].offset
;
4082 rsrcforkdata
= malloc(length
);
4084 if (rsrcforkdata
== NULL
) {
4085 copyfile_debug(1, "could not allocate %zu bytes for rsrcforkdata",
4091 if (fstat(s
->dst_fd
, &sb
) < 0)
4093 copyfile_debug(1, "couldn't stat destination file");
4098 bytes
= pread(s
->src_fd
, rsrcforkdata
, length
, offset
);
4099 if (bytes
< (ssize_t
)length
)
4103 copyfile_debug(1, "couldn't read resource fork");
4108 "couldn't read resource fork (only read %d bytes of %d)",
4109 (int)bytes
, (int)length
);
4116 s
->xattr_name
= (char *)XATTR_RESOURCEFORK_NAME
;
4117 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_START
, s
, s
->src
, s
->dst
, s
->ctx
);
4118 s
->xattr_name
= NULL
;
4119 if (rv
== COPYFILE_QUIT
) {
4125 } else if (rv
== COPYFILE_SKIP
) {
4129 error
= fsetxattr(s
->dst_fd
, XATTR_RESOURCEFORK_NAME
, rsrcforkdata
, bytes
, 0, 0);
4133 * For filesystems that do not natively support named attributes,
4134 * the kernel creates an AppleDouble file that -- for compatabilty
4135 * reasons -- has a resource fork containing nothing but a rsrcfork_header_t
4136 * structure that says there are no resources. So, if fsetxattr has
4137 * failed, and the resource fork is that empty structure, *and* the
4138 * target file is a directory, then we do nothing with it.
4140 if ((bytes
== sizeof(rsrcfork_header_t
)) &&
4141 ((sb
.st_mode
& S_IFMT
) == S_IFDIR
) &&
4142 (memcmp(rsrcforkdata
, &empty_rsrcfork_header
, bytes
) == 0)) {
4143 copyfile_debug(2, "not setting empty resource fork on directory");
4149 s
->xattr_name
= (char *)XATTR_RESOURCEFORK_NAME
;
4150 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
4151 s
->xattr_name
= NULL
;
4152 if (rv
== COPYFILE_CONTINUE
) {
4157 copyfile_debug(1, "error %d setting resource fork attribute", error
);
4160 } else if (s
->statuscb
) {
4162 s
->xattr_name
= (char *)XATTR_RESOURCEFORK_NAME
;
4163 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_FINISH
, s
, s
->src
, s
->dst
, s
->ctx
);
4164 s
->xattr_name
= NULL
;
4165 if (rv
== COPYFILE_QUIT
) {
4173 copyfile_debug(3, "extracting \"%s\" (%d bytes)",
4174 XATTR_RESOURCEFORK_NAME
, (int)length
);
4176 if (!(s
->flags
& COPYFILE_STAT
))
4178 /* Try to set m/atimes using setattrlist(), for nanosecond precision. */
4179 memset(&attrlist
, 0, sizeof(attrlist
));
4180 attrlist
.bitmapcount
= ATTR_BIT_MAP_COUNT
;
4181 attrlist
.commonattr
= ATTR_CMN_MODTIME
| ATTR_CMN_ACCTIME
;
4182 ma_times
.mod_time
= sb
.st_mtimespec
;
4183 ma_times
.acc_time
= sb
.st_atimespec
;
4184 if (fsetattrlist(s
->dst_fd
, &attrlist
, &ma_times
, sizeof(ma_times
), 0) != 0) {
4185 copyfile_warn("%s: set times", s
->dst
? s
->dst
: "(null dst)");
4193 if (COPYFILE_STAT
& s
->flags
)
4195 error
= copyfile_stat(s
);
4198 if (buffer
) free(buffer
);
4199 if (dataptr
) free(dataptr
);
4203 static int copyfile_pack_quarantine(copyfile_state_t s
, void **buf
, ssize_t
*len
)
4206 char qbuf
[QTN_SERIALIZED_DATA_MAX
];
4207 size_t qlen
= sizeof(qbuf
);
4209 if (s
->qinfo
== NULL
)
4215 if (qtn_file_to_data(s
->qinfo
, qbuf
, &qlen
) != 0)
4221 *buf
= malloc(qlen
);
4224 memcpy(*buf
, qbuf
, qlen
);
4231 static int copyfile_pack_acl(copyfile_state_t s
, void **buf
, ssize_t
*len
)
4237 if (filesec_get_property(s
->fsec
, FILESEC_ACL
, &acl
) < 0)
4239 if (errno
!= ENOENT
)
4242 if (COPYFILE_VERBOSE
& s
->flags
)
4243 copyfile_warn("getting acl");
4249 if ((acl_text
= acl_to_text(acl
, len
)) != NULL
)
4252 * acl_to_text() doesn't include the NUL at the endo
4253 * in it's count (*len). It does, however, promise to
4254 * return a valid C string, so we need to up the count
4258 *buf
= malloc(*len
);
4260 memcpy(*buf
, acl_text
, *len
);
4265 copyfile_debug(2, "copied acl (%ld) %p", *len
, *buf
);
4272 static int copyfile_pack_rsrcfork(copyfile_state_t s
, attr_header_t
*filehdr
)
4275 char *databuf
= NULL
;
4280 * do COPYFILE_COPY_XATTR here; no need to
4281 * the work if we want to skip.
4288 s
->xattr_name
= (char*)XATTR_RESOURCEFORK_NAME
;
4290 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_START
, s
, s
->src
, s
->dst
, s
->ctx
);
4291 s
->xattr_name
= NULL
;
4292 if (rv
== COPYFILE_SKIP
) {
4296 if (rv
== COPYFILE_QUIT
) {
4302 /* Get the resource fork size */
4303 if ((datasize
= fgetxattr(s
->src_fd
, XATTR_RESOURCEFORK_NAME
, NULL
, 0, 0, 0)) < 0)
4305 if (COPYFILE_VERBOSE
& s
->flags
)
4306 copyfile_warn("skipping attr \"%s\" due to error %d", XATTR_RESOURCEFORK_NAME
, errno
);
4310 if (datasize
> INT_MAX
) {
4318 s
->xattr_name
= (char*)XATTR_RESOURCEFORK_NAME
;
4321 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_PROGRESS
, s
, s
->src
, s
->dst
, s
->ctx
);
4322 s
->xattr_name
= NULL
;
4323 if (rv
== COPYFILE_QUIT
) {
4329 if ((databuf
= malloc(datasize
)) == NULL
)
4331 copyfile_warn("malloc");
4336 if (fgetxattr(s
->src_fd
, XATTR_RESOURCEFORK_NAME
, databuf
, datasize
, 0, 0) != datasize
)
4338 if (COPYFILE_VERBOSE
& s
->flags
)
4339 copyfile_warn("couldn't read entire resource fork");
4344 /* Write the resource fork to disk. */
4345 if (pwrite(s
->dst_fd
, databuf
, datasize
, filehdr
->appledouble
.entries
[1].offset
) != datasize
)
4347 if (COPYFILE_VERBOSE
& s
->flags
)
4348 copyfile_warn("couldn't write resource fork");
4353 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_FINISH
, s
, s
->src
, s
->dst
, s
->ctx
);
4354 if (rv
== COPYFILE_QUIT
) {
4359 copyfile_debug(3, "copied %zd bytes of \"%s\" data @ offset 0x%08x",
4360 datasize
, XATTR_RESOURCEFORK_NAME
, filehdr
->appledouble
.entries
[1].offset
);
4361 filehdr
->appledouble
.entries
[1].length
= (u_int32_t
)datasize
;
4364 if (ret
== -1 && s
->statuscb
)
4367 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
4368 if (rv
== COPYFILE_CONTINUE
)
4371 if (s
->xattr_name
) {
4372 s
->xattr_name
= NULL
;
4379 * Do status callback here
4380 * If ret == -1, then error callback
4386 * The opposite of copyfile_unpack(), obviously.
4388 static int copyfile_pack(copyfile_state_t s
)
4390 char *attrnamebuf
= NULL
, *endnamebuf
;
4391 void *databuf
= NULL
;
4392 attr_header_t
*filehdr
, *endfilehdr
;
4393 attr_entry_t
*entry
;
4394 ssize_t listsize
= 0;
4400 int hasrsrcfork
= 0;
4402 int seenq
= 0; // Have we seen any quarantine info already?
4404 filehdr
= (attr_header_t
*) calloc(1, ATTR_MAX_HDR_SIZE
);
4406 if (filehdr
== NULL
) {
4410 endfilehdr
= (attr_header_t
*)(((char*)filehdr
) + ATTR_MAX_HDR_SIZE
);
4413 attrnamebuf
= calloc(1, ATTR_MAX_HDR_SIZE
);
4414 if (attrnamebuf
== NULL
) {
4418 endnamebuf
= ((char*)attrnamebuf
) + ATTR_MAX_HDR_SIZE
;
4422 * Fill in the Apple Double Header defaults.
4424 filehdr
->appledouble
.magic
= ADH_MAGIC
;
4425 filehdr
->appledouble
.version
= ADH_VERSION
;
4426 filehdr
->appledouble
.numEntries
= 2;
4427 filehdr
->appledouble
.entries
[0].type
= AD_FINDERINFO
;
4428 filehdr
->appledouble
.entries
[0].offset
= (u_int32_t
)offsetof(apple_double_header_t
, finfo
);
4429 filehdr
->appledouble
.entries
[0].length
= FINDERINFOSIZE
;
4430 filehdr
->appledouble
.entries
[1].type
= AD_RESOURCE
;
4431 filehdr
->appledouble
.entries
[1].offset
= (u_int32_t
)offsetof(apple_double_header_t
, pad
);
4432 filehdr
->appledouble
.entries
[1].length
= 0;
4433 bcopy(ADH_MACOSX
, filehdr
->appledouble
.filler
, sizeof(filehdr
->appledouble
.filler
));
4436 * Fill in the initial Attribute Header.
4438 filehdr
->magic
= ATTR_HDR_MAGIC
;
4439 filehdr
->debug_tag
= 0;
4440 filehdr
->data_start
= (u_int32_t
)sizeof(attr_header_t
);
4443 * Collect the attribute names.
4445 entry
= (attr_entry_t
*)((char *)filehdr
+ sizeof(attr_header_t
));
4448 * Test if there are acls to copy
4450 if (COPYFILE_ACL
& s
->flags
)
4452 acl_t temp_acl
= NULL
;
4453 if (filesec_get_property(s
->fsec
, FILESEC_ACL
, &temp_acl
) < 0)
4455 copyfile_debug(2, "no acl entries found (errno = %d)", errno
);
4458 offset
= strlen(XATTR_SECURITY_NAME
) + 1;
4459 strcpy(attrnamebuf
, XATTR_SECURITY_NAME
);
4460 endnamebuf
= attrnamebuf
+ offset
;
4466 if (COPYFILE_XATTR
& s
->flags
)
4468 ssize_t left
= ATTR_MAX_HDR_SIZE
- offset
;
4469 if ((listsize
= flistxattr(s
->src_fd
, attrnamebuf
+ offset
, left
, 0)) <= 0)
4471 copyfile_debug(2, "no extended attributes found (%d)", errno
);
4473 if (listsize
> left
)
4475 copyfile_debug(1, "extended attribute list too long");
4479 endnamebuf
= attrnamebuf
+ offset
+ (listsize
> 0 ? listsize
: 0);
4480 if (endnamebuf
> (attrnamebuf
+ ATTR_MAX_HDR_SIZE
)) {
4486 sort_xattrname_list(attrnamebuf
, endnamebuf
- attrnamebuf
);
4488 for (nameptr
= attrnamebuf
; nameptr
< endnamebuf
; nameptr
+= namelen
)
4490 namelen
= strlen(nameptr
) + 1;
4491 /* Skip over FinderInfo or Resource Fork names */
4492 if (strcmp(nameptr
, XATTR_FINDERINFO_NAME
) == 0 ||
4493 strcmp(nameptr
, XATTR_RESOURCEFORK_NAME
) == 0) {
4496 if (strcmp(nameptr
, XATTR_QUARANTINE_NAME
) == 0) {
4500 /* The system should prevent this from happening, but... */
4501 if (namelen
> XATTR_MAXNAMELEN
+ 1) {
4502 namelen
= XATTR_MAXNAMELEN
+ 1;
4504 if (s
->copyIntent
&&
4505 xattr_preserve_for_intent(nameptr
, s
->copyIntent
) == 0) {
4507 size_t amt
= endnamebuf
- (nameptr
+ namelen
);
4508 memmove(nameptr
, nameptr
+ namelen
, amt
);
4509 endnamebuf
-= namelen
;
4510 /* Set namelen to 0 so continue doesn't miss names */
4517 char eaname
[namelen
];
4518 bcopy(nameptr
, eaname
, namelen
);
4519 eaname
[namelen
- 1] = 0; // Just to be sure!
4520 s
->xattr_name
= eaname
;
4521 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_START
, s
, s
->src
, s
->dst
, s
->ctx
);
4522 s
->xattr_name
= NULL
;
4523 if (rv
== COPYFILE_QUIT
) {
4527 } else if (rv
== COPYFILE_SKIP
) {
4528 size_t amt
= endnamebuf
- (nameptr
+ namelen
);
4529 memmove(nameptr
, nameptr
+ namelen
, amt
);
4530 endnamebuf
-= namelen
;
4531 /* Set namelen to 0 so continue doesn't miss names */
4536 entry
->namelen
= namelen
;
4538 if (nameptr
+ namelen
> endnamebuf
) {
4543 bcopy(nameptr
, &entry
->name
[0], namelen
);
4544 copyfile_debug(2, "copied name [%s]", entry
->name
);
4546 entrylen
= ATTR_ENTRY_LENGTH(namelen
);
4547 entry
= (attr_entry_t
*)(((char *)entry
) + entrylen
);
4549 if ((void*)entry
>= (void*)endfilehdr
) {
4554 /* Update the attributes header. */
4555 filehdr
->num_attrs
++;
4556 filehdr
->data_start
+= (u_int32_t
)entrylen
;
4561 * If we have any quarantine data, we always pack it.
4562 * But if we've already got it in the EA list, don't put it in again.
4564 if (s
->qinfo
&& !seenq
)
4566 ssize_t left
= ATTR_MAX_HDR_SIZE
- offset
;
4567 /* strlcpy returns number of bytes copied, but we need offset to point to the next byte */
4568 offset
+= strlcpy(attrnamebuf
+ offset
, XATTR_QUARANTINE_NAME
, left
) + 1;
4573 * Collect the attribute data.
4575 entry
= (attr_entry_t
*)((char *)filehdr
+ sizeof(attr_header_t
));
4577 for (nameptr
= attrnamebuf
; nameptr
< endnamebuf
; nameptr
+= namelen
+ 1)
4579 namelen
= strlen(nameptr
);
4581 if (strcmp(nameptr
, XATTR_SECURITY_NAME
) == 0)
4582 copyfile_pack_acl(s
, &databuf
, &datasize
);
4583 else if (s
->qinfo
&& strcmp(nameptr
, XATTR_QUARANTINE_NAME
) == 0)
4585 copyfile_pack_quarantine(s
, &databuf
, &datasize
);
4587 /* Check for Finder Info. */
4588 else if (strcmp(nameptr
, XATTR_FINDERINFO_NAME
) == 0)
4593 s
->xattr_name
= (char*)XATTR_FINDERINFO_NAME
;
4594 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_START
, s
, s
->src
, s
->dst
, s
->ctx
);
4595 s
->xattr_name
= NULL
;
4596 if (rv
== COPYFILE_QUIT
)
4598 s
->xattr_name
= NULL
;
4603 else if (rv
== COPYFILE_SKIP
)
4605 s
->xattr_name
= NULL
;
4609 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_PROGRESS
, s
, s
->src
, s
->dst
, s
->ctx
);
4610 s
->xattr_name
= NULL
;
4611 if (rv
== COPYFILE_QUIT
)
4618 datasize
= fgetxattr(s
->src_fd
, nameptr
, (u_int8_t
*)filehdr
+ filehdr
->appledouble
.entries
[0].offset
, 32, 0, 0);
4623 s
->xattr_name
= strdup(nameptr
);
4624 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
4625 if (s
->xattr_name
) {
4626 free(s
->xattr_name
);
4627 s
->xattr_name
= NULL
;
4629 if (rv
== COPYFILE_QUIT
) {
4634 if (COPYFILE_VERBOSE
& s
->flags
)
4635 copyfile_warn("skipping attr \"%s\" due to error %d", nameptr
, errno
);
4636 } else if (datasize
!= 32)
4638 if (COPYFILE_VERBOSE
& s
->flags
)
4639 copyfile_warn("unexpected size (%ld) for \"%s\"", datasize
, nameptr
);
4642 if (COPYFILE_VERBOSE
& s
->flags
)
4643 copyfile_warn(" copied 32 bytes of \"%s\" data @ offset 0x%08x",
4644 XATTR_FINDERINFO_NAME
, filehdr
->appledouble
.entries
[0].offset
);
4647 s
->xattr_name
= strdup(nameptr
);
4648 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_FINISH
, s
, s
->src
, s
->dst
, s
->ctx
);
4649 if (s
->xattr_name
) {
4650 free(s
->xattr_name
);
4651 s
->xattr_name
= NULL
;
4653 if (rv
== COPYFILE_QUIT
) {
4659 continue; /* finder info doesn't have an attribute entry */
4661 /* Check for Resource Fork. */
4662 else if (strcmp(nameptr
, XATTR_RESOURCEFORK_NAME
) == 0)
4668 /* Just a normal attribute. */
4672 s
->xattr_name
= strdup(nameptr
);
4674 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_PROGRESS
, s
, s
->src
, s
->dst
, s
->ctx
);
4675 if (s
->xattr_name
) {
4676 free(s
->xattr_name
);
4677 s
->xattr_name
= NULL
;
4680 * Due to the nature of the packed file, we can't skip at this point.
4682 if (rv
== COPYFILE_QUIT
)
4689 datasize
= fgetxattr(s
->src_fd
, nameptr
, NULL
, 0, 0, 0);
4694 if (COPYFILE_VERBOSE
& s
->flags
)
4695 copyfile_warn("skipping attr \"%s\" due to error %d", nameptr
, errno
);
4699 s
->xattr_name
= strdup(nameptr
);
4700 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_ERR
, s
, s
->src
, s
->dst
, s
->ctx
);
4701 if (s
->xattr_name
) {
4702 free(s
->xattr_name
);
4703 s
->xattr_name
= NULL
;
4705 if (rv
== COPYFILE_QUIT
)
4714 if (datasize
> XATTR_MAXATTRLEN
)
4716 if (COPYFILE_VERBOSE
& s
->flags
)
4717 copyfile_warn("skipping attr \"%s\" (too big)", nameptr
);
4720 databuf
= malloc(datasize
);
4721 if (databuf
== NULL
) {
4725 datasize
= fgetxattr(s
->src_fd
, nameptr
, databuf
, datasize
, 0, 0);
4728 s
->xattr_name
= strdup(nameptr
);
4729 rv
= (*s
->statuscb
)(COPYFILE_COPY_XATTR
, COPYFILE_FINISH
, s
, s
->src
, s
->dst
, s
->ctx
);
4730 if (s
->xattr_name
) {
4731 free(s
->xattr_name
);
4732 s
->xattr_name
= NULL
;
4734 if (rv
== COPYFILE_QUIT
) {
4742 entry
->length
= (u_int32_t
)datasize
;
4743 entry
->offset
= filehdr
->data_start
+ filehdr
->data_length
;
4745 filehdr
->data_length
+= (u_int32_t
)datasize
;
4749 * This assumes that the data is fits in memory (not
4750 * the case when there are lots of attributes or one of
4751 * the attributes is very large.
4753 if (entry
->offset
> ATTR_MAX_SIZE
||
4754 (entry
->offset
+ datasize
> ATTR_MAX_SIZE
)) {
4757 bcopy(databuf
, (char*)filehdr
+ entry
->offset
, datasize
);
4760 if (pwrite(s
->dst_fd
, databuf
, datasize
, entry
->offset
) != datasize
) {
4766 copyfile_debug(3, "copied %ld bytes of \"%s\" data @ offset 0x%08x", datasize
, nameptr
, entry
->offset
);
4768 /* bump to next entry */
4769 entrylen
= ATTR_ENTRY_LENGTH(entry
->namelen
);
4770 entry
= (attr_entry_t
*)((char *)entry
+ entrylen
);
4773 /* Now we know where the resource fork data starts. */
4774 filehdr
->appledouble
.entries
[1].offset
= (filehdr
->data_start
+ filehdr
->data_length
);
4776 /* We also know the size of the "Finder Info entry. */
4777 filehdr
->appledouble
.entries
[0].length
=
4778 filehdr
->appledouble
.entries
[1].offset
- filehdr
->appledouble
.entries
[0].offset
;
4780 filehdr
->total_size
= filehdr
->appledouble
.entries
[1].offset
;
4782 /* Copy Resource Fork. */
4783 if (hasrsrcfork
&& (error
= copyfile_pack_rsrcfork(s
, filehdr
)))
4786 /* Write the header to disk. */
4787 datasize
= filehdr
->data_start
;
4789 swap_adhdr(&filehdr
->appledouble
);
4790 swap_attrhdr(filehdr
);
4791 swap_attrhdr_entries(filehdr
);
4793 if (pwrite(s
->dst_fd
, filehdr
, datasize
, 0) != datasize
)
4795 if (COPYFILE_VERBOSE
& s
->flags
)
4796 copyfile_warn("couldn't write file header");
4801 if (filehdr
) free(filehdr
);
4802 if (attrnamebuf
) free(attrnamebuf
);
4807 return copyfile_stat(s
);