Skip to content

Instantly share code, notes, and snippets.

@psobko
Created October 30, 2014 17:02
Show Gist options
  • Save psobko/1609e6568768fa01b25d to your computer and use it in GitHub Desktop.
Save psobko/1609e6568768fa01b25d to your computer and use it in GitHub Desktop.
Get device storage using NSFileManager API. This code will print the results of each possible NSSearchPathDirectory.
#import <Foundation/Foundation.h>
void logStorageForLocation(NSSearchPathDirectory location) {
NSError *error;
NSArray * const paths = NSSearchPathForDirectoriesInDomains(location, NSUserDomainMask, YES);
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfFileSystemForPath:[paths lastObject]
error:&error];
uint64_t totalSpace = 0;
uint64_t totalFreeSpace = 0;
NSNumber *fileSystemSizeInBytes = [attributes objectForKey: NSFileSystemSize];
NSNumber *freeFileSystemSizeInBytes = [attributes objectForKey:NSFileSystemFreeSize];
totalSpace = [fileSystemSizeInBytes unsignedLongLongValue];
totalFreeSpace = [freeFileSystemSizeInBytes unsignedLongLongValue];
NSLog(@"Location: %d - Total space %llu MB / Free Space %llu MB", location, ((totalSpace/1024ll)/1024ll), ((totalFreeSpace/1024ll)/1024ll));
}
int main(int argc, char *argv[]) {
@autoreleasepool {
logStorageForLocation(NSApplicationDirectory);
logStorageForLocation(NSDemoApplicationDirectory);
logStorageForLocation(NSDeveloperApplicationDirectory);
logStorageForLocation(NSAdminApplicationDirectory);
logStorageForLocation(NSLibraryDirectory);
logStorageForLocation(NSDeveloperDirectory);
logStorageForLocation(NSUserDirectory);
logStorageForLocation(NSDocumentationDirectory);
logStorageForLocation(NSDocumentDirectory);
logStorageForLocation(NSCoreServiceDirectory);
logStorageForLocation(NSAutosavedInformationDirectory);
logStorageForLocation(NSDesktopDirectory);
logStorageForLocation(NSCachesDirectory);
logStorageForLocation(NSApplicationSupportDirectory);
logStorageForLocation(NSDownloadsDirectory);
logStorageForLocation(NSInputMethodsDirectory);
logStorageForLocation(NSMoviesDirectory);
logStorageForLocation(NSMusicDirectory);
logStorageForLocation(NSPicturesDirectory);
logStorageForLocation(NSPrinterDescriptionDirectory);
logStorageForLocation(NSSharedPublicDirectory);
logStorageForLocation(NSPreferencePanesDirectory);
// logStorageForLocation(NSApplicationScriptsDirectory);
logStorageForLocation(NSItemReplacementDirectory);
logStorageForLocation(NSAllApplicationsDirectory);
logStorageForLocation(NSAllLibrariesDirectory);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment