1#import <Foundation/Foundation.h> 2#import "NSFileManager+Custom.h" 3 4@implementation NSFileManager (Custom) 5 6/** 7 * Scans the <code>PATH</code> environment variable for aFilename and 8 * returns the full path, or nil if aFilename cannot be found. 9 */ 10- (NSString *)locateExecutable:(NSString *)aFilename; 11{ 12 NSString *fullPath = nil; 13 NSDictionary *environment = nil; 14 NSString *path = nil; 15 NSScanner *pathScanner; 16 BOOL found = NO; 17 18 environment = [[NSProcessInfo processInfo] environment]; 19 path = [environment objectForKey:@"PATH"]; 20 21 pathScanner = [NSScanner scannerWithString:path]; 22 23 while (([pathScanner isAtEnd] == NO) && (found == NO)) 24 { 25 NSString *directory = nil; 26 27 [pathScanner scanUpToString:@":" intoString:&directory]; 28 [pathScanner scanString:@":" intoString:NULL]; 29 fullPath = [directory stringByAppendingPathComponent:aFilename]; 30 found = [self fileExistsAtPath:fullPath]; 31 } 32 33 if (found == NO) 34 { 35 fullPath = nil; 36 } 37 return fullPath; 38} 39 40/** 41 * creates a temporary directory unique for Zipper. 42 */ 43- (NSString *)createTemporaryDirectory; 44{ 45 int attempt = 0; 46 47 NSParameterAssert(NSTemporaryDirectory() != nil); 48 // don't get caught in an endless loop. If we need more than 500 attempts 49 // to find a temp dir, something's wrong anyway 50 while (attempt < 500) 51 { 52 NSString *tempDir; 53 NSString *tempPath; 54 55 tempDir = [NSString stringWithFormat:@"Zipper-%d", attempt++]; 56 tempPath = [NSString pathWithComponents:[NSArray arrayWithObjects: 57 NSTemporaryDirectory(), tempDir, nil]]; 58 if ([self fileExistsAtPath:tempPath] == NO) 59 { 60 if ([self createDirectoryAtPath:tempPath attributes:nil]) 61 { 62 return tempPath; 63 } 64 } 65 } 66 67 [NSException raise:NSInvalidArgumentException format:@"Could not create temporary directory"]; 68 return nil; 69} 70 71- (void)createDirectoryPathWithParents:(NSString *)aPath 72{ 73 NSString *parent; 74 BOOL isDir; 75 76 parent = [aPath stringByDeletingLastPathComponent]; 77 if (([self fileExistsAtPath:parent isDirectory:&isDir] && isDir) == NO) 78 { 79 // parent path does not exist, create it first 80 [self createDirectoryPathWithParents:parent]; 81 } 82 83 // parent exists, create directory 84 [self createDirectoryAtPath:aPath attributes:nil]; 85} 86 87@end 88