diff --git a/ChangeLog b/ChangeLog index 815804464..55be00864 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2015-09-16 Riccardo Mottola + + * Headers/Foundation/NSFileManager.h + * Source/NSFileManager.m + Add simple (files only and no keys handling) implementation of 10.6 method contentsOfDirectoryAtURL. + 2015-09-08 Niels Grewe * Source/NSPropertyList.m: Fix writing base64 data. diff --git a/Headers/Foundation/NSFileManager.h b/Headers/Foundation/NSFileManager.h index 4b6b94e16..76b06b0d2 100644 --- a/Headers/Foundation/NSFileManager.h +++ b/Headers/Foundation/NSFileManager.h @@ -198,6 +198,13 @@ typedef uint32_t OSType; #define OSTYPE_DECLARED #endif +typedef enum : NSUInteger + { + NSDirectoryEnumerationSkipsSubdirectoryDescendants = 1L << 0, + NSDirectoryEnumerationSkipsPackageDescendants = 1L << 1, + NSDirectoryEnumerationSkipsHiddenFiles = 1L << 2 + } NSDirectoryEnumerationOptions; + @interface NSFileManager : NSObject { #if GS_EXPOSE(NSFileManager) @@ -299,9 +306,23 @@ typedef uint32_t OSType; - (BOOL) contentsEqualAtPath: (NSString*)path1 andPath: (NSString*)path2; +#if OS_API_VERSION(MAC_OS_X_VERSION_10_6, GS_API_LATEST) +/** + * Returns an array of NSURL of the contents of the specified directory.
+ * The listing is shallow and does not recurse into subdirectories. + * The special files '.' and '..' are excluded but it can return hidden files.
+ * The only mask option supported is NSDirectoryEnumerationSkipsHiddenFiles.
+ * The current implementation handles only files and property keys are ignored. + */ +- (NSArray*) contentsOfDirectoryAtURL:(NSURL*)url + includingPropertiesForKeys:(NSArray*)keys + options:(NSDirectoryEnumerationOptions)mask + error:(NSError **)error; +#endif + #if OS_API_VERSION(MAC_OS_X_VERSION_10_5, GS_API_LATEST) /** - * Returns an array of the contents of the specified directory.
+ * Returns an array of NSStrings of the contents of the specified directory.
* The listing does not recursively list subdirectories.
* The special files '.' and '..' are not listed.
* Indicates an error by returning nil (eg. if path is not a directory or diff --git a/Source/NSFileManager.m b/Source/NSFileManager.m index 59ec6c205..86f7d99a2 100644 --- a/Source/NSFileManager.m +++ b/Source/NSFileManager.m @@ -1,7 +1,7 @@ /** NSFileManager.m - Copyright (C) 1997-2002 Free Software Foundation, Inc. + Copyright (C) 1997-2015 Free Software Foundation, Inc. Author: Mircea Oancea Author: Ovidiu Predescu @@ -693,6 +693,69 @@ static NSStringEncoding defaultEncoding; } } +- (NSArray*) contentsOfDirectoryAtURL:(NSURL*)url + includingPropertiesForKeys:(NSArray*)keys + options:(NSDirectoryEnumerationOptions)mask + error:(NSError **)error +{ + NSArray *result; + NSDirectoryEnumerator *direnum; + NSString *path; + + DESTROY(_lastError); + + if (![[url scheme] isEqualToString:@"file"]) + return nil; + path = [url path]; + + direnum = [[NSDirectoryEnumerator alloc] + initWithDirectoryPath: path + recurseIntoSubdirectories: NO + followSymlinks: NO + justContents: NO + for: self]; + + /* we make an array of NSURLs */ + result = nil; + if (nil != direnum) + { + IMP nxtImp; + NSMutableArray *urlArray; + NSString *tempPath; + + + nxtImp = [direnum methodForSelector: @selector(nextObject)]; + + urlArray = [NSMutableArray arrayWithCapacity:128]; + while ((tempPath = (*nxtImp)(direnum, @selector(nextObject))) != nil) + { + NSURL *tempURL; + NSString *lastComponent; + + tempURL = [NSURL fileURLWithPath:tempPath]; + lastComponent = [tempPath lastPathComponent]; + + /* we purge files beginning with . */ + if (!((mask & NSDirectoryEnumerationSkipsHiddenFiles) && [lastComponent hasPrefix:@"."])) + [urlArray addObject:tempURL]; + } + RELEASE(direnum); + + if ([urlArray count] > 0) + result = [NSArray arrayWithArray:urlArray]; + } + + if (error != NULL) + { + if (nil == result) + { + *error = [self _errorFrom: path to: nil]; + } + } + + return result; +} + - (NSArray*) contentsOfDirectoryAtPath: (NSString*)path error: (NSError**)error { NSArray *result;