summaryrefslogtreecommitdiff
path: root/ExtendedAttributes.m
blob: 776e8d22c1b0fc3687ae61d43b21ccd8cadc5ae6 (plain)
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
//
//  ExtendedAttributes.m, based on UKXattrMetadataStore.m
//
//	License: MIT <http://opensource.org/licenses/mit-license.php>
//
//  Modified by Chris Lee <clee@mg8.org>
//  Originally created by Uli Kusterer on 12.03.06.
//  Copyright 2006 Uli Kusterer. All rights reserved.
//

#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
// -----------------------------------------------------------------------------
//	Headers:
// -----------------------------------------------------------------------------

#import "ExtendedAttributes.h"
#import <sys/xattr.h>


@implementation ExtendedAttributes

// -----------------------------------------------------------------------------
//	allKeysAtPath:
//		Return an NSArray of NSStrings containing all xattr names currently set
//		for the file at the specified path.
// -----------------------------------------------------------------------------

+ (NSArray *)allKeysAtPath: (NSString *)path
{
	NSMutableArray*	allKeys = [NSMutableArray array];
	size_t dataSize = listxattr([path fileSystemRepresentation], NULL, ULONG_MAX, 0);
	if (dataSize == ULONG_MAX)
		return allKeys;	// Empty list.

	NSMutableData* listBuffer = [NSMutableData dataWithLength: dataSize];
	dataSize = listxattr([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;
}


// -----------------------------------------------------------------------------
//	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 forKey: (NSString *)key atPath: (NSString *)path
{
	setxattr([path fileSystemRepresentation], [key UTF8String], [data bytes], [data length], 0, 0);
}


// -----------------------------------------------------------------------------
//	setObject:forKey:atPath:
//		Set the xattr with name key to an XML property list representation of
//		the specified object (or object graph).
//		path is the file whose xattr you want to set.
// -----------------------------------------------------------------------------

+ (void)setObject: (id)obj forKey: (NSString *)key atPath: (NSString *)path
{
	// Serialize our objects into a property list XML string:
	NSString* errMsg = nil;
	NSData* plistData = [NSPropertyListSerialization dataFromPropertyList: obj
								format: NSPropertyListXMLFormat_v1_0
								errorDescription: &errMsg];
	if (errMsg) {
		[errMsg autorelease];
		[NSException raise: @"UKXattrMetastoreCantSerialize" format: @"%@", errMsg];
	}
	else
		[[self class] setData: plistData forKey: key atPath: path];
}


// -----------------------------------------------------------------------------
//	setString:forKey:atPath:
//		Set the xattr with name key to an XML property list representation of
//		the specified object (or object graph).
//		path is the file whose xattr you want to set.
// -----------------------------------------------------------------------------

+ (void)setString: (NSString *)str forKey: (NSString *)key atPath: (NSString *)path
{
	NSData *data = [str dataUsingEncoding: NSUTF8StringEncoding];

	if (!data)
		[NSException raise: NSCharacterConversionException format: @"Couldn't convert string to UTF8 for xattr storage."];

	[[self class] setData: data forKey: key atPath: path];
}


// -----------------------------------------------------------------------------
//	dataForKey:atPath:
//		Retrieve the xattr with name key as a raw block of data.
//		path is the file whose xattr you want to set.
// -----------------------------------------------------------------------------

+ (NSMutableData *)dataForKey: (NSString *)key atPath: (NSString *)path
{
	size_t dataSize = getxattr([path fileSystemRepresentation], [key UTF8String], NULL, ULONG_MAX, 0, 0);
	if (dataSize == ULONG_MAX)
		return nil;
	NSMutableData *data = [NSMutableData dataWithLength: dataSize];
	getxattr([path fileSystemRepresentation], [key UTF8String], [data mutableBytes], [data length], 0, 0);
	return data;
}


// -----------------------------------------------------------------------------
//	objectForKey:atPath:
//		Retrieve the xattr with name key, which is an XML property list
//		and unserialize it back into an object or object graph.
//		path is the file whose xattr you want to set.
// -----------------------------------------------------------------------------

+ (id)objectForKey: (NSString *)key atPath: (NSString *)path
{
	NSString *errMsg = nil;
	NSMutableData *data = [[self class] dataForKey: key atPath: path];
	NSPropertyListFormat outFormat = NSPropertyListXMLFormat_v1_0;
	id obj = [NSPropertyListSerialization propertyListFromData: data mutabilityOption: NSPropertyListImmutable format: &outFormat errorDescription: &errMsg];
	if (errMsg) {
		[errMsg autorelease];
		[NSException raise: @"UKXattrMetastoreCantUnserialize" format: @"%@", errMsg];
	}

	return obj;
}


// -----------------------------------------------------------------------------
//	stringForKey:atPath:
//		Retrieve the xattr with name key, which is an XML property list
//		and unserialize it back into an object or object graph.
//		path is the file whose xattr you want to set.
// -----------------------------------------------------------------------------

+ (id)stringForKey: (NSString *)key atPath: (NSString *)path
{
	NSMutableData *data = [[self class] dataForKey: key atPath: path];	
	return [[[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding] autorelease];
}


@end

#endif /*MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4*/