forked from jjgod/textus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendedAttributes.m
More file actions
95 lines (78 loc) · 3.02 KB
/
ExtendedAttributes.m
File metadata and controls
95 lines (78 loc) · 3.02 KB
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
//
// ExtendedAttributes.m
// Textus
//
// Created by Jiang Jiang on 9/18/09.
//
#import "ExtendedAttributes.h"
#import <sys/xattr.h>
@implementation NSURL (ExtendedAttributes)
- (NSArray *) allXattrKeys
{
NSMutableArray *allKeys = [NSMutableArray array];
size_t dataSize = listxattr([[self path] fileSystemRepresentation], NULL, ULONG_MAX, 0);
if (dataSize == ULONG_MAX)
return allKeys; // Empty list.
NSMutableData *listBuffer = [NSMutableData dataWithLength: dataSize];
dataSize = listxattr([[self path] fileSystemRepresentation], [listBuffer mutableBytes], [listBuffer length], 0);
char *nameStart = [listBuffer mutableBytes];
int x;
for (x = 0; x < dataSize; x++) {
if (((char*)[listBuffer mutableBytes])[x] == 0) {
NSString* str = [NSString stringWithUTF8String: nameStart];
nameStart = [listBuffer mutableBytes] + x + 1;
[allKeys addObject: str];
}
}
return allKeys;
}
- (NSUInteger) unsignedIntegerFromXattrKey: (NSString *) key
{
const char *path = [[self path] fileSystemRepresentation];
char dataBuf[256];
size_t dataSize = getxattr(path, [key UTF8String], dataBuf, 256, 0, 0);
if (dataSize)
{
dataBuf[dataSize] = '\0';
return strtol(dataBuf, NULL, 10);
}
return 0;
}
- (void) setUnsignedInteger: (NSUInteger) num forXattrKey: (NSString *) key
{
const char *path = [[self path] fileSystemRepresentation];
const char *buf = [[NSString stringWithFormat: @"%lu", num] UTF8String];
setxattr(path, [key UTF8String], buf, strlen(buf), 0, 0);
}
// -----------------------------------------------------------------------------
// setData:forKey:atPath:
// Set the xattr with name key to a block of raw binary data.
// path is the file whose xattr you want to set.
// -----------------------------------------------------------------------------
- (void)setData: (NSData *)data forXattrKey: (NSString *)key
{
setxattr([[self path] fileSystemRepresentation], [key UTF8String], [data bytes], [data length], 0, 0);
}
- (NSMutableData *)dataForXattrKey: (NSString *)key
{
size_t dataSize = getxattr([[self path] fileSystemRepresentation], [key UTF8String], NULL, ULONG_MAX, 0, 0);
if (dataSize == ULONG_MAX)
return nil;
NSMutableData *data = [NSMutableData dataWithLength: dataSize];
getxattr([[self path] fileSystemRepresentation], [key UTF8String], [data mutableBytes], [data length], 0, 0);
return data;
}
- (NSString *) stringFromXattrKey: (NSString *) key
{
NSMutableData *data = [self dataForXattrKey: key];
return [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
}
- (void) setString: (NSString *) str forXattrKey: (NSString *) key
{
NSData *data = [str dataUsingEncoding: NSUTF8StringEncoding];
if (!data)
[NSException raise: NSCharacterConversionException
format: @"Couldn't convert string to UTF8 for xattr storage."];
[self setData: data forXattrKey: key];
}
@end