forked from CocoaBob/xee
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XeeFSRef.m
281 lines (224 loc) · 6.64 KB
/
XeeFSRef.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
#import "XeeFSRef.h"
@implementation XeeFSRef
+(XeeFSRef *)refForPath:(NSString *)path
{
return [[[XeeFSRef alloc] initWithPath:path] autorelease];
}
-(id)initWithPath:(NSString *)path
{
if(self=[super init])
{
iterator=NULL;
if(FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation],&ref,NULL)!=noErr)
{
[self release];
return nil;
}
FSCatalogInfo catinfo;
FSGetCatalogInfo(&ref,kFSCatInfoNodeID,&catinfo,NULL,NULL,NULL)/*!=noErr)*/;
hash=catinfo.nodeID;
}
return self;
}
-(id)initWithFSRef:(FSRef *)fsref
{
if(self=[super init])
{
ref=*fsref;
iterator=NULL;
FSCatalogInfo catinfo;
FSGetCatalogInfo(&ref,kFSCatInfoNodeID,&catinfo,NULL,NULL,NULL)/*!=noErr*/;
hash=catinfo.nodeID;
}
return self;
}
-(void)dealloc
{
if(iterator) FSCloseIterator(iterator);
[super dealloc];
}
-(FSRef *)FSRef { return &ref; }
-(BOOL)isValid { return FSIsFSRefValid(&ref); }
-(BOOL)isDirectory
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoNodeFlags,&catinfo,NULL,NULL,NULL)!=noErr) return NO;
return catinfo.nodeFlags&kFSNodeIsDirectoryMask?YES:NO;
}
-(BOOL)isRemote
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoVolume,&catinfo,NULL,NULL,NULL)!=noErr) return NO;
HParamBlockRec pb;
GetVolParmsInfoBuffer volparms;
pb.ioParam.ioCompletion=NULL;
pb.ioParam.ioNamePtr=NULL;
pb.ioParam.ioVRefNum=catinfo.volume;
pb.ioParam.ioBuffer=(Ptr)&volparms;
pb.ioParam.ioReqCount=sizeof(volparms);
if(PBHGetVolParmsSync(&pb)!=noErr) return NO;
if((volparms.vMExtendedAttributes&((1<<bIsOnInternalBus)|(1<<bIsOnExternalBus)))==0) return YES;
return NO;
}
-(NSString *)name;
{
HFSUniStr255 name;
if(FSGetCatalogInfo(&ref,kFSCatInfoNone,NULL,&name,NULL,NULL)!=noErr) return nil;
return [NSString stringWithCharacters:name.unicode length:name.length];
}
-(NSString *)path
{
NSString *path=nil;
CFURLRef url=CFURLCreateFromFSRef(kCFAllocatorDefault,&ref);
if(url)
{
path=[(NSString *)CFURLCopyFileSystemPath(url,kCFURLPOSIXPathStyle) autorelease];
CFRelease(url);
}
return path;
}
-(NSURL *)URL { return [(id)CFURLCreateFromFSRef(kCFAllocatorDefault,&ref) autorelease]; }
-(XeeFSRef *)parent
{
FSRef parent;
if(FSGetCatalogInfo(&ref,kFSCatInfoNone,NULL,NULL,NULL,&parent)!=noErr) return nil;
return [[[XeeFSRef alloc] initWithFSRef:&parent] autorelease];
}
-(off_t)dataSize
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoDataSizes,&catinfo,NULL,NULL,NULL)!=noErr) return 0;
return catinfo.dataLogicalSize;
}
-(off_t)dataPhysicalSize
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoDataSizes,&catinfo,NULL,NULL,NULL)!=noErr) return 0;
return catinfo.dataPhysicalSize;
}
-(off_t)resourceSize
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoDataSizes,&catinfo,NULL,NULL,NULL)!=noErr) return 0;
return catinfo.rsrcLogicalSize;
}
-(off_t)resourcePhysicalSize
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoDataSizes,&catinfo,NULL,NULL,NULL)!=noErr) return 0;
return catinfo.rsrcPhysicalSize;
}
-(CFAbsoluteTime)creationTime
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoCreateDate,&catinfo,NULL,NULL,NULL)!=noErr) return 0;
CFAbsoluteTime res;
UCConvertUTCDateTimeToCFAbsoluteTime(&catinfo.createDate,&res);
return res;
}
-(CFAbsoluteTime)modificationTime
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoAllDates,&catinfo,NULL,NULL,NULL)!=noErr) return 0;
CFAbsoluteTime res;
UCConvertUTCDateTimeToCFAbsoluteTime(&catinfo.contentModDate,&res);
return res;
}
-(CFAbsoluteTime)attributeModificationTime
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoAllDates,&catinfo,NULL,NULL,NULL)!=noErr) return 0;
CFAbsoluteTime res;
UCConvertUTCDateTimeToCFAbsoluteTime(&catinfo.attributeModDate,&res);
return res;
}
-(CFAbsoluteTime)accessTime
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoAccessDate,&catinfo,NULL,NULL,NULL)!=noErr) return 0;
CFAbsoluteTime res;
UCConvertUTCDateTimeToCFAbsoluteTime(&catinfo.accessDate,&res);
return res;
}
-(CFAbsoluteTime)backupTime
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoBackupDate,&catinfo,NULL,NULL,NULL)!=noErr) return 0;
CFAbsoluteTime res;
UCConvertUTCDateTimeToCFAbsoluteTime(&catinfo.backupDate,&res);
return res;
}
-(NSString *)HFSTypeCode
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoFinderInfo,&catinfo,NULL,NULL,NULL)!=noErr) return nil;
struct FileInfo *info=(struct FileInfo *)&catinfo.finderInfo;
OSType type=info->fileType;
return [NSString stringWithFormat:@"%c%c%c%c",(type>>24)&0xff,(type>>16)&0xff,(type>>8)&0xff,type&0xff];
}
-(NSString *)HFSCreatorCode
{
FSCatalogInfo catinfo;
if(FSGetCatalogInfo(&ref,kFSCatInfoFinderInfo,&catinfo,NULL,NULL,NULL)!=noErr) return nil;
struct FileInfo *info=(struct FileInfo *)&catinfo.finderInfo;
OSType type=info->fileType;
return [NSString stringWithFormat:@"%c%c%c%c",(type>>24)&0xff,(type>>16)&0xff,(type>>8)&0xff,type&0xff];
}
-(BOOL)startReadingDirectoryWithRecursion:(BOOL)recursive
{
if(iterator) FSCloseIterator(iterator);
// "Iteration over subtrees which do not originate at the root directory of a volume are not currently supported"
if(FSOpenIterator(&ref,recursive?kFSIterateSubtree:kFSIterateFlat,&iterator)!=noErr) return NO;
return YES;
}
-(void)stopReadingDirectory
{
if(iterator) FSCloseIterator(iterator);
iterator=NULL;
}
-(XeeFSRef *)nextDirectoryEntry
{
if(!iterator) return nil;
FSRef newref;
ItemCount num;
OSErr err=FSGetCatalogInfoBulk(iterator,1,&num,NULL,kFSCatInfoNone,NULL,&newref,NULL,NULL);
// ignoring num
if(err==errFSNoMoreItems)
{
FSCloseIterator(iterator);
iterator=NULL;
return nil;
}
else if(err!=noErr) return nil;
return [[[XeeFSRef alloc] initWithFSRef:&newref] autorelease];
}
-(NSArray *)directoryContents
{
if(![self startReadingDirectoryWithRecursion:NO]) return nil;
NSMutableArray *array=[NSMutableArray array];
XeeFSRef *entry;
while(entry=[self nextDirectoryEntry]) [array addObject:entry];
return array;
}
-(BOOL)isEqual:(XeeFSRef *)other
{
// if(![other isKindOfClass:[self class]]) return NO;
//if(![self isValid]||![other isValid]) return NO; // This is REALLY SLOW for some reason.
if(hash!=other->hash) return NO;
return FSCompareFSRefs(&ref,&other->ref)==noErr;
}
-(NSComparisonResult)compare:(XeeFSRef *)other
{
return [[self path] compare:[other path] options:NSCaseInsensitiveSearch|NSNumericSearch];
}
-(NSComparisonResult)compare:(XeeFSRef *)other options:(int)options
{
return [[self path] compare:[other path] options:options];
}
-(unsigned)hash { return hash; }
-(NSString *)description { return [self path]; }
-(id)copyWithZone:(NSZone *)zone
{
return [[XeeFSRef allocWithZone:zone] initWithFSRef:&ref];
}
@end