2 * Copyright (c) 2013 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@
30 #include <sys/types.h>
31 #include <sys/xattr.h>
33 #include <xattr_properties.h>
36 * Some default propeteries for EAs we know about internally.
41 int flags
; // See below
44 #define propFlagsPrefix 0x0001 // The name is a prefix, so only look at that part
46 static const struct defaultList
47 defaultPropertyTable
[] = {
48 { "com.apple.quarantine", "PC", 0 }, // not public
49 { "com.apple.TextEncoding", "PC", 0 }, // Content-dependent, public
50 { "com.apple.metadata:", "P", propFlagsPrefix
}, // Don't export, keep for copy & safe save
51 { "com.apple.security.", "N", propFlagsPrefix
},
52 { XATTR_RESOURCEFORK_NAME
, "PC", 0 }, // Don't keep for safe save
53 { XATTR_FINDERINFO_NAME
, "PC", 0 }, // Same as ResourceFork
58 * The property lists on an EA are set by having a suffix character,
59 * and then a list of characters. In general, we're choosing upper-case
60 * to indicate the property is set, and lower-case to indicate it's to be
63 struct propertyListMapping
{
64 char enable
; // Character to enable
65 char disable
; // Character to disable -- usually lower-case of enable
66 CopyOperationProperties_t value
;
68 static const struct propertyListMapping
69 PropertyListMapTable
[] = {
70 { 'C', 'c', kCopyOperationPropertyContentDependent
},
71 { 'P', 'p', kCopyOperationPropertyNoExport
},
72 { 'N', 'n', kCopyOperationPropertyNeverPreserve
},
77 * Given a converted property list (that is, converted to the
78 * CopyOperationProperties_t type), and an intent, determine if
79 * it should be preserved or not.
81 * I've chosen to use a block instead of a simple mask on the belief
82 * that the question may be moderately complex. If it ends up not being
83 * so, then this can simply be turned into a mask of which bits to check
84 * as being exclusionary.
86 static const struct divineIntent
{
87 CopyOperationIntent_t intent
;
88 int (^checker
)(CopyOperationProperties_t
);
90 { CopyOperationIntentCopy
, ^(CopyOperationProperties_t props
) {
91 if (props
& kCopyOperationPropertyNeverPreserve
)
95 { CopyOperationIntentSave
, ^(CopyOperationProperties_t props
) {
96 if (props
& (kCopyOperationPropertyContentDependent
| kCopyOperationPropertyNeverPreserve
))
100 { CopyOperationIntentShare
, ^(CopyOperationProperties_t props
) {
101 if ((props
& (kCopyOperationPropertyNoExport
| kCopyOperationPropertyNeverPreserve
)) != 0)
110 * If an EA name is in the default list, find it, and return the property
111 * list string for it.
114 nameInDefaultList(const char *eaname
)
116 const struct defaultList
*retval
;
118 for (retval
= defaultPropertyTable
; retval
->eaName
; retval
++) {
119 if ((retval
->flags
& propFlagsPrefix
) != 0 &&
120 strncmp(retval
->eaName
, eaname
, strlen(retval
->eaName
)) == 0)
121 return retval
->propList
;
122 if (strcmp(retval
->eaName
, eaname
) == 0)
123 return retval
->propList
;
129 * Given an EA name, see if it has a property list in it, and
130 * return a pointer to it. All this is doing is looking for
131 * the delimiter, and returning the string after that. Returns
132 * NULL if the delimiter isn't found. Note that an empty string
133 * is a valid property list, as far as we're concerned.
136 findPropertyList(const char *eaname
)
138 const char *ptr
= strrchr(eaname
, '#');
145 * Convert a property list string (e.g., "pCd") into a
146 * CopyOperationProperties_t type.
148 static CopyOperationProperties_t
149 stringToProperties(const char *proplist
)
151 CopyOperationProperties_t retval
= 0;
154 // A switch would be more efficient, but less generic.
155 for (ptr
= proplist
; *ptr
; ptr
++) {
156 const struct propertyListMapping
*mapPtr
;
157 for (mapPtr
= PropertyListMapTable
; mapPtr
->enable
; mapPtr
++) {
158 if (*ptr
== mapPtr
->enable
) {
159 retval
|= mapPtr
->value
;
160 } else if (*ptr
== mapPtr
->disable
) {
161 retval
&= ~mapPtr
->value
;
169 * Given an EA name (e.g., "com.apple.lfs.hfs.test"), and a
170 * CopyOperationProperties_t value (it's currently an integral value, so
171 * just a bitmask), cycle through the list of known properties, and return
172 * a string with the EA name, and the property list appended. E.g., we
173 * might return "com.apple.lfs.hfs.test#pD".
175 * The tricky part of this funciton is that it will not append any letters
176 * if the value is only the default properites. In that case, it will copy
177 * the EA name, and return that.
179 * It returns NULL if there was an error. The two errors right now are
180 * no memory (strdup failed), in which case it will set errno to ENOMEM; and
181 * the resulting EA name is longer than XATTR_MAXNAMELEN, in which case it
182 * sets errno to ENAMETOOLONG.
184 * (Note that it also uses ENAMETOOLONG if the buffer it's trying to set
185 * gets too large. I honestly can't see how that would happen, but it's there
186 * for sanity checking. That would require having more than 64 bits to use.)
189 _xattrNameWithProperties(const char *orig
, CopyOperationProperties_t propList
)
192 char suffix
[66] = { 0 }; // 66: uint64_t for property types, plus '#', plus NUL
194 const struct propertyListMapping
*mapPtr
;
197 for (mapPtr
= PropertyListMapTable
; mapPtr
->enable
; mapPtr
++) {
198 if ((propList
& mapPtr
->value
) != 0) {
199 *cur
++ = mapPtr
->enable
;
201 if (cur
>= (suffix
+ sizeof(suffix
))) {
202 errno
= ENAMETOOLONG
;
209 if (cur
== suffix
+ 1) {
211 retval
= strdup(orig
);
215 const char *defaultEntry
= NULL
;
216 if ((defaultEntry
= nameInDefaultList(orig
)) != NULL
&&
217 strcmp(defaultEntry
, suffix
+ 1) == 0) {
218 // Just use the name passed in
219 retval
= strdup(orig
);
221 asprintf(&retval
, "%s%s", orig
, suffix
);
223 if (retval
== NULL
) {
226 if (strlen(retval
) > XATTR_MAXNAMELEN
) {
229 errno
= ENAMETOOLONG
;
236 CopyOperationProperties_t
237 _xattrPropertiesFromName(const char *eaname
)
239 CopyOperationProperties_t retval
= 0;
240 const char *propList
;
242 propList
= findPropertyList(eaname
);
243 if (propList
== NULL
) {
244 propList
= findPropertyList(eaname
);
246 if (propList
!= NULL
) {
247 retval
= stringToProperties(propList
);
254 * Indicate whether an EA should be preserved, when using the
257 * This returns 0 if it should not be preserved, and 1 if it should.
259 * It simply looks through the tables we have above, and compares the
260 * CopyOperationProperties_t for the EA with the intent. If the
261 * EA doesn't have any properties, and it's not on the default list, the
262 * default is to preserve it.
266 _PreserveEA(const char *eaname
, CopyOperationIntent_t intent
)
268 const struct divineIntent
*ip
;
269 CopyOperationProperties_t props
;
270 const char *propList
;
272 if ((propList
= findPropertyList(eaname
)) == NULL
&&
273 (propList
= nameInDefaultList(eaname
)) == NULL
)
276 props
= stringToProperties(propList
);
278 for (ip
= intentTable
; ip
->intent
; ip
++) {
279 if (ip
->intent
== intent
) {
280 return ip
->checker(props
);
284 if ((props
& kCopyOperationPropertyNeverPreserve
) != 0)
285 return 0; // Special case, don't try to preserve this one
287 return 1; // Default to preserving everything