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
|
//
// GTMNSFileManager+Carbon.m
//
// Copyright 2008 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
//
#import "GTMNSFileManager+Carbon.h"
#import <CoreServices/CoreServices.h>
#import <sys/param.h>
#import "GTMDefines.h"
@implementation NSFileManager (GTMFileManagerCarbonAdditions)
- (NSData *)gtm_aliasDataForPath:(NSString *)path {
NSData *data = nil;
FSRef ref;
AliasHandle alias = NULL;
require_quiet([path length], CantUseParams);
require_noerr(FSPathMakeRef((UInt8 *)[path fileSystemRepresentation],
&ref, NULL), CantMakeRef);
require_noerr(FSNewAlias(NULL, &ref, &alias), CantMakeAlias);
Size length = GetAliasSize(alias);
data = [NSData dataWithBytes:*alias length:length];
DisposeHandle((Handle)alias);
CantMakeAlias:
CantMakeRef:
CantUseParams:
return data;
}
- (NSString *)gtm_pathFromAliasData:(NSData *)data {
NSString *path = nil;
require_quiet(data, CantUseParams);
AliasHandle alias;
const void *bytes = [data bytes];
NSUInteger length = [data length];
require_noerr(PtrToHand(bytes, (Handle *)&alias, length), CantMakeHandle);
FSRef ref;
Boolean wasChanged;
// we don't use a require here because it is quite legitimate for an alias
// resolve to fail.
if (FSResolveAlias(NULL, alias, &ref, &wasChanged) == noErr) {
path = [self gtm_pathFromFSRef:&ref];
}
DisposeHandle((Handle)alias);
CantMakeHandle:
CantUseParams:
return path;
}
- (FSRef *)gtm_FSRefForPath:(NSString *)path {
FSRef* fsRef = NULL;
require_quiet([path length], CantUseParams);
NSMutableData *fsRefData = [NSMutableData dataWithLength:sizeof(FSRef)];
require(fsRefData, CantAllocateFSRef);
fsRef = (FSRef*)[fsRefData mutableBytes];
Boolean isDir = FALSE;
const UInt8 *filePath = (const UInt8 *)[path fileSystemRepresentation];
require_noerr_action(FSPathMakeRef(filePath, fsRef, &isDir),
CantMakeRef, fsRef = NULL);
CantMakeRef:
CantAllocateFSRef:
CantUseParams:
return fsRef;
}
- (NSString *)gtm_pathFromFSRef:(FSRef *)fsRef {
NSString *nsPath = nil;
require_quiet(fsRef, CantUseParams);
char path[MAXPATHLEN];
require_noerr(FSRefMakePath(fsRef, (UInt8 *)path, MAXPATHLEN), CantMakePath);
nsPath = [self stringWithFileSystemRepresentation:path length:strlen(path)];
nsPath = [nsPath stringByStandardizingPath];
CantMakePath:
CantUseParams:
return nsPath;
}
@end
|