forked from ttscoff/nv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNSFileManager_NV.m
340 lines (287 loc) · 11.2 KB
/
NSFileManager_NV.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//
// NSFileManager_NV.m
// Notation
//
// Created by Zachary Schneirov on 12/31/10.
/*Copyright (c) 2010, Zachary Schneirov. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided with
the distribution.
- Neither the name of Notational Velocity nor the names of its contributors may be used to endorse
or promote products derived from this software without specific prior written permission. */
#import "NSFileManager_NV.h"
@implementation NSFileManager (NV)
#define kMaxDataSize 4096
- (NSArray *)mergedTagsForFileAtPath:(const char*)path
{
NSArray *existingFinderTags=(NSArray *)[self getFinderTagsAtFSPath:path];
NSArray *openMetaTags = (NSArray *)[self getOpenMetaTagsAtFSPath:path];
NSUInteger ct=0;
if (openMetaTags!=nil) {
ct=openMetaTags.count;
}
if (existingFinderTags!=nil) {
ct+=existingFinderTags.count;
}
if (ct>0) {
NSMutableSet *finalTagSet=[[NSMutableSet alloc]initWithCapacity:ct];
if (openMetaTags&&openMetaTags.count>0) {
[finalTagSet addObjectsFromArray:openMetaTags];
}
if ((existingFinderTags!=nil)&&(existingFinderTags.count>0)) {
[finalTagSet addObjectsFromArray:existingFinderTags];
}
NSArray *finalTags;
if (finalTagSet.count>0) {
finalTags=[NSArray arrayWithArray:[finalTagSet allObjects]];
}else{
finalTags=@[];
}
[finalTagSet release];
return finalTags;
}else{
return @[];
}
}
- (id)getTagsAtFSPath:(const char*)path
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"UseFinderTags"])
{
return [self getFinderTagsAtFSPath:path];
}
else
{
return [self getOpenMetaTagsAtFSPath:path];
}
}
- (id)getFinderTagsAtFSPath:(const char*)path
{
if (!path){
NSLog(@"nopath");
return nil;
}
NSString *newPath=[NSString stringWithCString:path encoding:NSUTF8StringEncoding];
NSURL *url = [NSURL fileURLWithPath:newPath];
NSArray *existingTags=nil;
NSError *error=nil;
if (![url getResourceValue:&existingTags forKey:NSURLTagNamesKey error:&error]) {
NSLog(@"trouble getting finder tags:%@",error);
return nil;
}
else
{
return existingTags;
}
}
- (id)getOpenMetaTagsAtFSPath:(const char*)path {
//return convention: empty tags should be an empty array;
//for files that have never been tagged, or that have had their tags removed, return
//files might lose their metadata if edited externally or synced without being first encoded
if (!path) return nil;
const char* inKeyNameC = "com.apple.metadata:kMDItemOMUserTags";
// retrieve data from store.
char* data[kMaxDataSize];
ssize_t dataSize = kMaxDataSize; // ssize_t means SIGNED size_t as getXattr returns - 1 for no attribute found
NSData* nsData = nil;
if ((dataSize = getxattr(path, inKeyNameC, data, dataSize, 0, 0)) > 0) {
nsData = [NSData dataWithBytes:data length:dataSize];
} else {
// I get EINVAL sometimes when setting/getting xattrs on afp servers running 10.5. When I get this error, I find that everything is working correctly... so it seems to make sense to ignore them
// EINVAL means invalid argument. I know that the args are fine.
//if ((errno != ENOATTR) && (errno != EINVAL) && error) // it is not an error to have no attribute set
// *error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:[NSDictionary dictionaryWithObject:[self errnoString:errno] forKey:@"info"]];
return nil;
}
// ok, we have some data
NSPropertyListFormat formatFound;
NSError *err=nil;
id outObject = [NSPropertyListSerialization propertyListWithData:nsData options:NSPropertyListImmutable format:&formatFound error:&err];
if (err) {
NSLog(@"%@: error deserializing labels: %@", NSStringFromSelector(_cmd), err);
// [err autorelease];
return nil;
}
return outObject;
}
- (BOOL)setTags:(id)plistObject atFSPath:(const char*)path {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"UseFinderTags"])
{
return [self setFinderTags:plistObject atFSPath:path];
}
else
{
return [self setOpenMetaTags:plistObject atFSPath:path];
}
}
- (BOOL)setFinderTags:(id)plistObject atFSPath:(const char*)path
{
if (!path){
NSLog(@"setting but no path");
return NO;
}
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithCString:path encoding:NSUTF8StringEncoding]];
NSArray *tagArray = [NSArray arrayWithArray:plistObject];
NSError *error=nil;
if (![url setResourceValue:tagArray forKey:NSURLTagNamesKey error:&error])
{
NSLog(@"%@", error);
NSLog(@"Error setting Finder tags for %@", [url path]);
return NO;
}
else
{
return YES;
}
}
- (BOOL)setOpenMetaTags:(id)plistObject atFSPath:(const char*)path {
if (!path) return NO;
// If the object passed in has no data - is a string of length 0 or an array or dict with 0 objects, then we remove the data at the key.
const char* inKeyNameC = "com.apple.metadata:kMDItemOMUserTags";
long returnVal = 0;
// always set data as binary plist.
NSData* dataToSendNS = nil;
if (plistObject) {
NSString *errorString = nil;
dataToSendNS = [NSPropertyListSerialization dataFromPropertyList:plistObject format:kCFPropertyListBinaryFormat_v1_0 errorDescription:&errorString];
if (errorString) {
NSLog(@"%@: error serializing labels: %@", NSStringFromSelector(_cmd), errorString);
[errorString autorelease];
return NO;
}
}
if (dataToSendNS) {
// also reject for tags over the maximum size:
if ([dataToSendNS length] > kMaxDataSize)
return NO;
returnVal = setxattr(path, inKeyNameC, [dataToSendNS bytes], [dataToSendNS length], 0, 0);
} else {
returnVal = removexattr(path, inKeyNameC, 0);
}
if (returnVal < 0) {
if (errno != ENOATTR) NSLog(@"%@: couldn't set/remove attribute: %d (value '%@')", NSStringFromSelector(_cmd), errno, dataToSendNS);
return NO;
}
return YES;
}
//TODO: use volumeCapabilities in FSExchangeObjectsCompat.c to skip some work on volumes for which we know we would receive ENOTSUP
//for +setTextEncodingAttribute:atFSPath: and +textEncodingAttributeOfFSPath: (test against VOL_CAP_INT_EXTENDED_ATTR)
- (BOOL)setTextEncodingAttribute:(NSStringEncoding)encoding atFSPath:(const char*)path {
if (!path) return NO;
CFStringEncoding cfStringEncoding = CFStringConvertNSStringEncodingToEncoding(encoding);
if (cfStringEncoding == kCFStringEncodingInvalidId) {
NSLog(@"%@: encoding %lu is invalid!", NSStringFromSelector(_cmd), encoding);
return NO;
}
NSString *textEncStr = [(NSString *)CFStringConvertEncodingToIANACharSetName(cfStringEncoding) stringByAppendingFormat:@";%@",
[[NSNumber numberWithInt:cfStringEncoding] stringValue]];
const char *textEncUTF8Str = [textEncStr UTF8String];
if (setxattr(path, "com.apple.TextEncoding", textEncUTF8Str, strlen(textEncUTF8Str), 0, 0) < 0) {
NSLog(@"couldn't set text encoding attribute of %s to '%s': %d", path, textEncUTF8Str, errno);
return NO;
}
return YES;
}
- (NSStringEncoding)textEncodingAttributeOfFSPath:(const char*)path {
if (!path) goto errorReturn;
//We could query the size of the attribute, but that would require a second system call
//and the value for this key shouldn't need to be anywhere near this large, anyway.
//It could be, but it probably won't. If it is, then we won't get the encoding. Too bad.
char xattrValueBytes[128] = { 0 };
if (getxattr(path, "com.apple.TextEncoding", xattrValueBytes, sizeof(xattrValueBytes), 0, 0) < 0) {
if (ENOATTR != errno) NSLog(@"couldn't get text encoding attribute of %s: %d", path, errno);
goto errorReturn;
}
NSString *encodingStr = [NSString stringWithUTF8String:xattrValueBytes];
if (!encodingStr) {
NSLog(@"couldn't make attribute data from %s into a string", path);
goto errorReturn;
}
NSArray *segs = [encodingStr componentsSeparatedByString:@";"];
if ([segs count] >= 2 && [(NSString*)[segs objectAtIndex:1] length] > 1) {
return CFStringConvertEncodingToNSStringEncoding([[segs objectAtIndex:1] intValue]);
} else if ([(NSString*)[segs objectAtIndex:0] length] > 1) {
CFStringEncoding theCFEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)[segs objectAtIndex:0]);
if (theCFEncoding == kCFStringEncodingInvalidId) {
NSLog(@"couldn't convert IANA charset");
goto errorReturn;
}
return CFStringConvertEncodingToNSStringEncoding(theCFEncoding);
}
errorReturn:
return 0;
}
- (NSString*)pathCopiedFromAliasData:(NSData*)aliasData {
AliasHandle inAlias;
CFStringRef path = NULL;
FSAliasInfoBitmap whichInfo = kFSAliasInfoNone;
FSAliasInfo info;
if (aliasData && PtrToHand([aliasData bytes], (Handle*)&inAlias, [aliasData length]) == noErr &&
FSCopyAliasInfo(inAlias, NULL, NULL, &path, &whichInfo, &info) == noErr) {
//this method doesn't always seem to work
return [(NSString*)path autorelease];
}
return nil;
}
- (NSString*)pathFromFSPath:(char*)path {
DebugPath(path);
return [self stringWithFileSystemRepresentation:path length:strlen(path)];
}
- (NSString*)pathWithFSRef:(FSRef*)fsRef {
NSString *path = nil;
const UInt32 maxPathSize = 4 * 1024;
UInt8 *convertedPath = (UInt8*)malloc(maxPathSize * sizeof(UInt8));
if (FSRefMakePath(fsRef, convertedPath, maxPathSize) == noErr) {
path = [self stringWithFileSystemRepresentation:(char*)convertedPath length:strlen((char*)convertedPath)];
}
free(convertedPath);
return path;
}
- (BOOL)createFolderAtPath:(NSString *)path{
return [self createFolderAtPath:path withAttributes:nil];
}
- (BOOL)createFolderAtPath:(NSString *)path withAttributes:(NSDictionary *)attributes{
NSError *err=nil;
if (![self createDirectoryAtPath:path withIntermediateDirectories:NO attributes:attributes error:&err]||(err!=nil)) {
NSLog(@"trouble creating directory at path:>%@<",[err description]);
return NO;
}else{
return YES;
}
}
- (NSDictionary *)attributesAtPath:(NSString *)path followLink:(BOOL)follow{
if (follow) {
path=[path stringByResolvingSymlinksInPath];
}
NSError *err=nil;
NSDictionary *dict=[self attributesOfItemAtPath:path error:&err];
if ((dict!=nil)&&(err==nil)) {
return dict;
}else if (err) {
NSLog(@"trouble getting attributes at path:>%@<\ndict:%@",[err description],dict);
}
return [NSDictionary dictionary];
}
- (NSArray *)folderContentsAtPath:(NSString *)path{
NSError *err=nil;
NSArray *arr=[self contentsOfDirectoryAtPath:path error:&err];
if ((arr!=nil)&&(err==nil)) {
return arr;
}else if (err) {
NSLog(@"trouble getting contents of path:>%@<",[err description]);
}
return @[];
}
- (BOOL)deleteFileAtPath:(NSString *)path{
NSError *err=nil;
if([self removeItemAtPath:path error:&err]&&(err==nil)){
return YES;
}else if(err){
NSLog(@"trouble removing file at path:>%@<",err.localizedDescription);
}
return NO;
}
@end