]> git.saurik.com Git - apple/copyfile.git/blob - xattr_properties.c
78eccd0eab53c8729a069fe48b9790175b23b23a
[apple/copyfile.git] / xattr_properties.c
1 /*
2 * Copyright (c) 2013 Apple, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <err.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <sys/xattr.h>
32
33 #include <xattr_properties.h>
34
35 /*
36 * Some default propeteries for EAs we know about internally.
37 */
38 struct defaultList {
39 const char *eaName;
40 const char *propList;
41 int flags; // See below
42 };
43
44 #define propFlagsPrefix 0x0001 // The name is a prefix, so only look at that part
45
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
54 { 0, 0, 0 },
55 };
56
57 /*
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
61 * cleared.
62 */
63 struct propertyListMapping {
64 char enable; // Character to enable
65 char disable; // Character to disable -- usually lower-case of enable
66 CopyOperationProperties_t value;
67 };
68 static const struct propertyListMapping
69 PropertyListMapTable[] = {
70 { 'C', 'c', kCopyOperationPropertyContentDependent },
71 { 'P', 'p', kCopyOperationPropertyNoExport },
72 { 'N', 'n', kCopyOperationPropertyNeverPreserve },
73 { 0, 0, 0 },
74 };
75
76 /*
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.
80 *
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.
85 */
86 static const struct divineIntent {
87 CopyOperationIntent_t intent;
88 int (^checker)(CopyOperationProperties_t);
89 } intentTable[] = {
90 { CopyOperationIntentCopy, ^(CopyOperationProperties_t props) {
91 if (props & kCopyOperationPropertyNeverPreserve)
92 return 0;
93 return 1;
94 } },
95 { CopyOperationIntentSave, ^(CopyOperationProperties_t props) {
96 if (props & (kCopyOperationPropertyContentDependent | kCopyOperationPropertyNeverPreserve))
97 return 0;
98 return 1;
99 } },
100 { CopyOperationIntentShare, ^(CopyOperationProperties_t props) {
101 if ((props & (kCopyOperationPropertyNoExport | kCopyOperationPropertyNeverPreserve)) != 0)
102 return 0;
103 return 1;
104 } },
105 { 0, 0 },
106 };
107
108
109 /*
110 * If an EA name is in the default list, find it, and return the property
111 * list string for it.
112 */
113 static const char *
114 nameInDefaultList(const char *eaname)
115 {
116 const struct defaultList *retval;
117
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;
124 }
125 return NULL;
126 }
127
128 /*
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.
134 */
135 static const char *
136 findPropertyList(const char *eaname)
137 {
138 const char *ptr = strrchr(eaname, '#');
139 if (ptr)
140 return ptr+1;
141 return NULL;
142 }
143
144 /*
145 * Convert a property list string (e.g., "pCd") into a
146 * CopyOperationProperties_t type.
147 */
148 static CopyOperationProperties_t
149 stringToProperties(const char *proplist)
150 {
151 CopyOperationProperties_t retval = 0;
152 const char *ptr;
153
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;
162 }
163 }
164 }
165 return retval;
166 }
167
168 /*
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".
174 *
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.
178 *
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.
183 *
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.)
187 */
188 char *
189 _xattrNameWithProperties(const char *orig, CopyOperationProperties_t propList)
190 {
191 char *retval = NULL;
192 char suffix[66] = { 0 }; // 66: uint64_t for property types, plus '#', plus NUL
193 char *cur = suffix;
194 const struct propertyListMapping *mapPtr;
195
196 *cur++ = '#';
197 for (mapPtr = PropertyListMapTable; mapPtr->enable; mapPtr++) {
198 if ((propList & mapPtr->value) != 0) {
199 *cur++ = mapPtr->enable;
200 }
201 if (cur >= (suffix + sizeof(suffix))) {
202 errno = ENAMETOOLONG;
203 return NULL;
204 }
205
206 }
207
208
209 if (cur == suffix + 1) {
210 // No changes made
211 retval = strdup(orig);
212 if (retval == NULL)
213 errno = ENOMEM;
214 } else {
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);
220 } else {
221 asprintf(&retval, "%s%s", orig, suffix);
222 }
223 if (retval == NULL) {
224 errno = ENOMEM;
225 } else {
226 if (strlen(retval) > XATTR_MAXNAMELEN) {
227 free(retval);
228 retval = NULL;
229 errno = ENAMETOOLONG;
230 }
231 }
232 }
233 return retval;
234 }
235
236 CopyOperationProperties_t
237 _xattrPropertiesFromName(const char *eaname)
238 {
239 CopyOperationProperties_t retval = 0;
240 const char *propList;
241
242 propList = findPropertyList(eaname);
243 if (propList == NULL) {
244 propList = findPropertyList(eaname);
245 }
246 if (propList != NULL) {
247 retval = stringToProperties(propList);
248 }
249
250 return retval;
251 }
252
253 /*
254 * Indicate whether an EA should be preserved, when using the
255 * given intent.
256 *
257 * This returns 0 if it should not be preserved, and 1 if it should.
258 *
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.
263 */
264
265 int
266 _PreserveEA(const char *eaname, CopyOperationIntent_t intent)
267 {
268 const struct divineIntent *ip;
269 CopyOperationProperties_t props;
270 const char *propList;
271
272 if ((propList = findPropertyList(eaname)) == NULL &&
273 (propList = nameInDefaultList(eaname)) == NULL)
274 props = 0;
275 else
276 props = stringToProperties(propList);
277
278 for (ip = intentTable; ip->intent; ip++) {
279 if (ip->intent == intent) {
280 return ip->checker(props);
281 }
282 }
283
284 if ((props & kCopyOperationPropertyNeverPreserve) != 0)
285 return 0; // Special case, don't try to preserve this one
286
287 return 1; // Default to preserving everything
288 }