-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLAlias.m
140 lines (112 loc) · 3.61 KB
/
TLAlias.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
//
// TLAlias.m
// Mercatalog
//
// Created by Nathan Vander Wilt on 7/9/08.
// Copyright 2008 Calf Trail Software, LLC. All rights reserved.
//
#import "TLAlias.h"
// see http://developer.apple.com/documentation/Carbon/Reference/Alias_Manager/Reference/reference.html
#include <CoreServices/CoreServices.h>
static Handle TLCreateHandleFromData(CFDataRef data) {
// see also http://lists.apple.com/archives/carbon-dev/2005/Jan/msg00749.html
// see also http://developer.apple.com/qa/qa2004/qa1350.html
CFIndex dataLength = CFDataGetLength(data);
Handle handle = NewHandle(dataLength);
if (handle) {
HLock(handle); // see http://developer.apple.com/DOCUMENTATION/mac/Memory/Memory-88.html
memcpy(*handle, CFDataGetBytePtr(data), dataLength);
HUnlock(handle);
}
return handle;
}
static CFDataRef TLCreateDataFromHandle(Handle handle) {
Size handleSize = GetHandleSize(handle);
HLock(handle);
CFDataRef data = CFDataCreate(kCFAllocatorDefault, (UInt8*)*handle, handleSize);
HUnlock(handle);
return data;
}
@interface TLAlias (TLAliasPrivateMethods)
- (AliasHandle)newAliasHandleFromPath:(NSString*)path relativeToPath:(NSString*)relativePath;
@end
@implementation TLAlias
#pragma mark Internal accessors
- (AliasHandle)aliasHandle {
return (AliasHandle)internal;
}
- (void)setAliasHandle:(AliasHandle)newAlias {
// do nothing if setting same handle
if (internal == newAlias) return;
// free the old handle
Handle oldAlias = (Handle)[self aliasHandle];
if (oldAlias) {
DisposeHandle(oldAlias);
}
// set the new handle
internal = newAlias;
}
#pragma mark Lifecycle
- (id)initWithAlias:(AliasHandle)alias {
self = [super init];
if (self) {
[self setAliasHandle:alias];
}
return self;
}
- (void)dealloc {
[self setAliasHandle:NULL];
[super dealloc];
}
- (id)initWithPath:(NSString*)path relativeToPath:(NSString*)relativePath {
AliasHandle alias = [self newAliasHandleFromPath:path relativeToPath:relativePath];
return [self initWithAlias:alias];
}
- (id)initWithPath:(NSString*)fullPath {
return [self initWithPath:fullPath relativeToPath:nil];
}
+ (id)aliasWithPath:(NSString*)fullPath {
TLAlias* alias = [[TLAlias alloc] initWithPath:fullPath];
return [alias autorelease];
}
#pragma mark Data conversion
- (AliasHandle)newAliasHandleFromPath:(NSString*)path relativeToPath:(NSString*)relativePath {
AliasHandle alias = NULL;
OSErr result = FSNewAliasFromPath([relativePath UTF8String], [path UTF8String], 0, &alias, NULL);
(void)result;
return alias;
}
- (id)initWithData:(NSData*)data {
AliasHandle alias = (AliasHandle)TLCreateHandleFromData((CFDataRef)data);
return [self initWithAlias:alias];
}
- (NSData*)aliasData {
NSData* data = (NSData*)TLCreateDataFromHandle((Handle)[self aliasHandle]);
return [data autorelease];
}
# pragma mark Public path accessors
- (NSString*)pathRelativeToPath:(NSString*)relativePath {
// Convert relativePath to FSRef unless nil.
FSRef relativeRef;
const FSRef* relativeRefPtr = &relativeRef;
if (relativePath) {
OSStatus relPathResult = FSPathMakeRef((const UInt8*)[relativePath UTF8String], &relativeRef, NULL);
if (relPathResult) return nil;
}
else relativeRefPtr = NULL;
// Resolve alias if possible
Boolean wasChanged = false;
FSRef aliasTarget;
OSErr result = FSResolveAlias(relativeRefPtr, [self aliasHandle], &aliasTarget, &wasChanged);
(void)wasChanged;
if (result) return nil;
// Convert resolved FSRef back to string
CFURLRef pathAsURL = CFURLCreateFromFSRef(kCFAllocatorDefault, &aliasTarget);
NSString* path = [(NSURL*)pathAsURL path];
CFRelease(pathAsURL);
return path;
}
- (NSString*)path {
return [self pathRelativeToPath:nil];
}
@end