-
Notifications
You must be signed in to change notification settings - Fork 0
/
SWPathReference.m
81 lines (62 loc) · 2.18 KB
/
SWPathReference.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
//
// SWPathReference.m
// This file is part of the "SWApplicationSupport" project, and is distributed under the MIT License.
//
// Created by Samuel Williams on 19/02/06.
// Copyright 2006 Samuel Williams. All rights reserved.
//
#import "SWPathReference.h"
@implementation SWPathReference
- initWithPath: (NSString*)newPath {
if (self = [super init]) {
if ([SWPathReference getFSRefAtPath:newPath ref:&self->ref] != noErr) {
[self release];
return nil;
}
self->path = [newPath copy];
}
return self;
}
- (void) dealloc {
[path release];
[super dealloc];
}
- (NSString*) initialPath {
return self->path;
}
- (NSString*) path {
NSMutableData *buf = [NSMutableData dataWithLength:512];
FSRefMakePath(&self->ref, [buf mutableBytes], [buf length]);
return [[[NSString alloc] initWithCString:[buf bytes] encoding:NSUTF8StringEncoding] autorelease];
}
+ (OSErr)getFSRefAtPath:(NSString*)sourceItem ref:(FSRef*)sourceRef {
OSErr err;
BOOL isSymLink;
id manager = [NSFileManager defaultManager];
NSDictionary *sourceAttribute = [manager fileAttributesAtPath:sourceItem traverseLink:NO];
isSymLink = ([sourceAttribute objectForKey:@"NSFileType"] == NSFileTypeSymbolicLink);
if(isSymLink && NO){
const char *sourceParentPath;
FSRef sourceParentRef;
HFSUniStr255 sourceFileName;
sourceParentPath = (UInt8*)[[sourceItem stringByDeletingLastPathComponent] fileSystemRepresentation];
err = FSPathMakeRef(sourceParentPath, &sourceParentRef, NULL);
if(err == noErr){
[[sourceItem lastPathComponent] getCharacters:sourceFileName.unicode];
sourceFileName.length = [[sourceItem lastPathComponent] length];
if (sourceFileName.length == 0){
err = fnfErr;
}
else
err = FSMakeFSRefUnicode(&sourceParentRef, sourceFileName.length, sourceFileName.unicode, kTextEncodingFullName, sourceRef);
}
}
else{
err = FSPathMakeRef([sourceItem fileSystemRepresentation], sourceRef, NULL);
}
return err;
}
- (NSString*) description {
return [NSString stringWithFormat: @"<SWPathReference: %@>", [self path]];
}
@end