This repository has been archived by the owner on Apr 25, 2018. It is now read-only.
/
NSFileManager+FileSize.m
72 lines (63 loc) · 2.2 KB
/
NSFileManager+FileSize.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
//
// NSFileManager+FileSize.m
// BuildCleaner
//
// Created by Dave DeLong on 8/13/09.
// Copyright 2009 Home. All rights reserved.
//
#import "NSFileManager+FileSize.h"
#import "NSString+FSRef.h"
@implementation NSFileManager (FileSize)
- (NSUInteger) _sizeOfItemForFSRef:(FSRef *)theFileRef {
FSIterator thisDirEnum = NULL;
NSUInteger totalSize = 0;
// Iterate the directory contents, recursing as necessary
if (FSOpenIterator(theFileRef, kFSIterateFlat, &thisDirEnum) == noErr) {
const ItemCount kMaxEntriesPerFetch = 40;
ItemCount actualFetched;
FSRef fetchedRefs[kMaxEntriesPerFetch];
FSCatalogInfo fetchedInfos[kMaxEntriesPerFetch];
OSErr fsErr = FSGetCatalogInfoBulk(thisDirEnum,
kMaxEntriesPerFetch,
&actualFetched,
NULL,
kFSCatInfoDataSizes | kFSCatInfoNodeFlags | kFSCatInfoRsrcSizes,
fetchedInfos,
fetchedRefs,
NULL,
NULL);
while ((fsErr == noErr) || (fsErr == errFSNoMoreItems)) {
ItemCount thisIndex;
for (thisIndex = 0; thisIndex < actualFetched; thisIndex++) {
// Recurse if it's a folder
if (fetchedInfos[thisIndex].nodeFlags & kFSNodeIsDirectoryMask) {
totalSize += [self _sizeOfItemForFSRef:&fetchedRefs[thisIndex]];
} else {
// add the size for this item
totalSize += fetchedInfos[thisIndex].dataLogicalSize;
}
}
if (fsErr == errFSNoMoreItems) {
break;
} else {
// get more items
fsErr = FSGetCatalogInfoBulk(thisDirEnum,
kMaxEntriesPerFetch,
&actualFetched,
NULL,
kFSCatInfoDataSizes | kFSCatInfoNodeFlags | kFSCatInfoRsrcSizes,
fetchedInfos,
fetchedRefs,
NULL,
NULL);
}
}
FSCloseIterator(thisDirEnum);
}
return totalSize;
}
- (NSUInteger) sizeOfItemAtPath:(NSString *)filePath {
FSRef theFileRef = [filePath fsref];
return [self _sizeOfItemForFSRef:&theFileRef];
}
@end