-
Notifications
You must be signed in to change notification settings - Fork 1
/
TERecord.m
336 lines (324 loc) · 11.6 KB
/
TERecord.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
//
// TERecord.m
//
// Created by Greg Slepak on 5/8/11.
// Copyright 2011 Tao Effect LLC. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. 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.
//
// THIS SOFTWARE IS PROVIDED BY TAO EFFECT LLC ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Tao Effect LLC.
#import <objc/objc-runtime.h>
#import "TERecord.h"
#import "NSFileManager+TEAdditions.h"
#import "Common.h"
@interface TERecordValue : NSObject <NSCoding, NSCopying> {
NSRecursiveLock *lock;
}
@property (nonatomic, retain) id obj;
@property (nonatomic) BOOL atomic;
//@property (nonatomic) BOOL bookmark; // indicates we represent an NSURL that must be restored via bookmark data
- (void)lock;
- (void)unlock;
@end
@implementation TERecordValue
@synthesize obj, atomic;
- (id)init
{
if ( self = [super init] )
lock = [NSRecursiveLock new];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
// log_debug("%s", __func__);
if ( self = [super init] ) {
obj = [aDecoder decodeObjectForKey:@"obj"];
atomic = [aDecoder decodeBoolForKey:@"atomic"];
lock = [NSRecursiveLock new];
// NOTE: see note below in -encodeWithCoder
// bookmark = [aDecoder decodeBoolForKey:@"bookmark"];
// if ( bookmark ) {
// NSError *error;
// obj = [NSURL URLByResolvingBookmarkData:obj
// options:0
// relativeToURL:nil
// bookmarkDataIsStale:NULL
// error:&error];
// if ( error ) {
// log_err("%s: %@: '%@'", __func__, error, obj);
// obj = nil;
// } else if ( ![obj isFileReferenceURL] ) {
// log_warn("%s: not a fileReference: %@", __func__, obj);
// obj = [obj fileReferenceURL];
// }
// log_debug("%s: resolved bookmark: '%@'", __func__, obj);
// }
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
// log_debug("%s", __func__);
if ( [obj isMemberOfClass:[NSURL class]] && [obj isFileReferenceURL] ) {
// save fileReferenceURLs as filePathURLs because reference URLs aren't persistent
[aCoder encodeObject:[obj filePathURL] forKey:@"obj"];
// NOTE: saving bookmark data doesn't help us if the folder is unlocked (mounted)
// and then we relaunch Espionage, as it tries to resolve against the mountpoint
// and fails, returning NULL when calling +URLByResolvingBookmarkData from -initWithCoder
// log_debug("%s: saving bookmark for '%@'", __func__, [obj path]);
// NSData *data = [obj bookmarkDataWithOptions:(NSURLBookmarkCreationSuitableForBookmarkFile|NSURLBookmarkCreationPreferFileIDResolution)
// includingResourceValuesForKeys:$a(NSURLFileResourceIdentifierKey)
// relativeToURL:nil
// error:nil];
// if ( !data ) {
// // save as regular filePathURL instead
// log_warn("%s: couldn't create bookmark for '%@'!", __func__, [obj path]);
// [aCoder encodeObject:[obj filePathURL] forKey:@"obj"];
// } else {
// [aCoder encodeBool:YES forKey:@"bookmark"];
// [aCoder encodeObject:data forKey:@"obj"];
// }
} else {
// just encode the object directly
[aCoder encodeObject:obj forKey:@"obj"];
}
[aCoder encodeBool:atomic forKey:@"atomic"];
}
- (id)copyWithZone:(NSZone *)zone
{
TERecordValue *val = [TERecordValue new];
if ( atomic ) [lock lock];
val.obj = [obj copy];
if ( atomic ) [lock unlock];
val.atomic = atomic;
return val;
}
- (NSUInteger)hash
{
return [obj hash];
}
- (BOOL)isEqual:(id)object
{
return [obj isEqual:object];
}
- (NSString*)description
{
return [obj description];
}
- (void)lock
{
[lock lock];
}
- (void)unlock
{
[lock unlock];
}
@end
@interface TERecord : NSObject <TERecord> {
NSMutableDictionary *dict;
}
- (id)initWithDict:(NSMutableDictionary*)aDict;
- (void)setValue:(id)value forKey:(NSString *)key;
- (id)valueForKey:(NSString *)key;
- (id)valueForKeyPath:(NSString *)keyPath;
@end
@implementation TERecord
- (id)initWithDict:(NSMutableDictionary *)aDict
{
if ( self = [super init] )
dict = aDict;
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder
{
// log_debug("%s", __func__);
[aCoder encodeObject:dict forKey:@"dict"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
// log_debug("%s", __func__);
if ( self = [super init] )
dict = [aDecoder decodeObjectForKey:@"dict"];
return self;
}
- (NSMutableDictionary *)dictCopy
{
NSMutableDictionary *aDict = [NSMutableDictionary dictionaryWithCapacity:[dict count]];
for ( NSString *key in dict ) {
[aDict setObject:[self valueForKey:key] forKey:key];
}
return aDict;
}
- (id)copyWithZone:(NSZone *)zone
{
return [[TERecord alloc] initWithDict:dict];
}
- (void)setValue:(id)value forKey:(NSString *)key
{
TERecordValue *rVal = [dict objectForKey:key];
// NOTE:
// we *don't* do this because we don't know what the value for rVal.atomic is
// instead users of TERecord should make sure that whatever class they store
// as a property of a TERecord supports the NSCoding protocol, *even* if all
// it does in encodeWithCoder is encode a [NSNull null] value. Basically we
// want to make sure that TERecordValues are created by the TERecordCreate function.
// If you need to update the record with new properties, call TERecordUpdateProtocol
// if ( !rVal ) {
// rVal = [TERecordValue new];
// [dict setObject:rVal forKey:key];
// }
[self willChangeValueForKey:key];
if ( rVal.atomic ) [rVal lock];
rVal.obj = value;
if ( rVal.atomic ) [rVal unlock];
[self didChangeValueForKey:key];
// log_debug("%s: %@ %@", __func__, key, rVal ? rVal.obj : @"NULL");
}
- (void)setNilValueForKey:(NSString *)key
{
[self setValue:nil forKey:key];
}
- (id)valueForKey:(NSString *)key
{
TERecordValue *rVal = [dict objectForKey:key];
// log_debug("%s: %@ %@", __func__, key, rVal ? rVal.obj : @"NULL");
id obj;
if ( rVal.atomic ) [rVal lock];
obj = rVal.obj;
if ( rVal.atomic ) [rVal unlock];
return obj;
}
- (id)valueForKeyPath:(NSString *)keyPath
{
NSArray *comps = [keyPath componentsSeparatedByString:@"."];
TERecordValue *rVal = [dict objectForKey:[comps objectAtIndex:0]];
id obj;
NSUInteger i, count = [comps count];
if ( rVal.atomic ) [rVal lock];
obj = rVal.obj;
for ( i=1; i < count; ++i )
obj = [obj valueForKey:[comps objectAtIndex:i]];
if ( rVal.atomic ) [rVal unlock];
return obj;
}
- (void)setValue:(id)value forKeyPath:(NSString *)keyPath
{
NSArray *comps = [keyPath componentsSeparatedByString:@"."];
TERecordValue *rVal = [dict objectForKey:[comps objectAtIndex:0]];
id obj;
NSUInteger i, count = [comps count];
if ( rVal.atomic ) [rVal lock];
if ( count > 1 ) {
obj = rVal.obj;
for ( i=1; i < count-1; ++i )
obj = [obj valueForKey:[comps objectAtIndex:i]];
[obj setValue:value forKey:[comps lastObject]];
} else
rVal.obj = value;
if ( rVal.atomic ) [rVal unlock];
}
- (NSUInteger)hash
{
return [dict hash];
}
- (BOOL)isEqual:(id)object
{
return [dict isEqual:object];
}
- (NSString*)description
{
return [dict description];
}
- (NSMutableDictionary*)dict
{
return dict;
}
// many thanks go to this blog post for some of the code here:
// http://blog.lhunath.com/2010/01/clean-up-your-configuration.html
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
// log_debug("%s: %@", __func__, NSStringFromSelector(sel));
if ( [NSStringFromSelector(sel) hasPrefix:@"set"] )
return [NSMethodSignature signatureWithObjCTypes:"v@:@"];
else
return [NSMethodSignature signatureWithObjCTypes:"@@:"];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
NSString *selector = NSStringFromSelector([anInvocation selector]);
// log_debug("%s: %@", __func__, selector);
if ( [selector hasPrefix:@"set"] )
{
NSRange firstChar = NSMakeRange(3,1);
NSRange rest = NSMakeRange(4, [selector length] - 5);
__unsafe_unretained id value;
selector = [[[selector substringWithRange:firstChar] lowercaseString] stringByAppendingString:[selector substringWithRange:rest]];
[anInvocation getArgument:&value atIndex:2];
[self setValue:value forKey:selector];
}
else
{
__unsafe_unretained id value = [self valueForKey:selector];
[anInvocation setReturnValue:&value];
}
}
@end
// fill a dictionary with TERecordValues for each property in proto
static inline NSMutableDictionary* dictWithProperties(NSMutableDictionary *dict, Protocol *proto)
{
// use property_getAttributes to find out if atomic or not
// see Objective-C Runtime Programming Guide for more info on the possible attributes
unsigned int count, i;
objc_property_t *properties = protocol_copyPropertyList(proto, &count);
if ( !dict ) dict = [NSMutableDictionary dictionaryWithCapacity:count];
for ( i = 0; i < count; ++i ) {
NSString *name = [NSString stringWithUTF8String:property_getName(properties[i])];
NSString *attrs = [NSString stringWithUTF8String:property_getAttributes(properties[i])];
NSArray *attrsAry = [attrs componentsSeparatedByString:@","];
BOOL isAtomic = YES;
for ( NSString *attr in attrsAry ) {
if ( [attr isEqualToString:@"N"] ) {
isAtomic = NO;
break;
}
}
//log_debug("%s: %@: %@ %@", __func__, NSStringFromProtocol(proto), attrs, name);
// now we set defaults so that we can safely handle atomic properties by
// creating the hash-tree map fully so that no more node manipulation happens
if ( !dict || ![dict valueForKey:name] ) {
TERecordValue *value = [TERecordValue new];
value.atomic = isAtomic;
[dict setObject:value forKey:name];
}
}
free(properties);
return dict;
}
id TERecordCreate(Protocol *proto)
{
return [[TERecord alloc] initWithDict:dictWithProperties(NULL, proto)];
}
void TERecordUpdateProtocol(id<TERecord> r, Protocol *proto)
{
dictWithProperties(r.dict, proto);
}