From 93b2eadbf8fd0ff6464ad90ea17b03e463ff5606 Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Sat, 4 Jun 2011 15:08:37 +0000 Subject: [PATCH] Add [NSURL -checkResourceIsReachableAndReturnError:]. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@33245 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 9 +++++++-- Headers/Foundation/NSURL.h | 4 ++++ Source/NSURL.m | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index e45dc0022..2ef91227a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,12 +1,17 @@ +2011-06-04 Fred Kiefer + + * Headers/Foundation/NSURL.h, + * Source/NSURL.m: Add -checkResourceIsReachableAndReturnError: + 2011-06-03 Wolfgang Lux * Source/NSString.m: [(-getLineStart:end:contentsEnd:forRange:)] - Fix off-by-one error iun reported contents end for DOS style lines. + Fix off-by-one error in reported contents end for DOS style lines. 2011-06-02 Riccardo Mottola * Source/NSTimeZone.m: Disable use of tzname on OpenBSD too. - + 2011-06-02 Richard Frith-Macdonald * Source/NSTimeZone.m: Allow for quotes around zone name in diff --git a/Headers/Foundation/NSURL.h b/Headers/Foundation/NSURL.h index bc0f5ff61..bad2fee5f 100644 --- a/Headers/Foundation/NSURL.h +++ b/Headers/Foundation/NSURL.h @@ -34,6 +34,7 @@ extern "C" { #if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST) +@class NSError; @class NSNumber; /** @@ -68,6 +69,9 @@ GS_EXPORT NSString* const NSURLFileScheme; - (NSString*) absoluteString; - (NSURL*) absoluteURL; - (NSURL*) baseURL; +#if OS_API_VERSION(100600,GS_API_LATEST) +- (BOOL) checkResourceIsReachableAndReturnError: (NSError **)error; +#endif - (NSString*) fragment; - (NSString*) host; - (BOOL) isFileURL; diff --git a/Source/NSURL.m b/Source/NSURL.m index f50d2b020..7ba0f9418 100644 --- a/Source/NSURL.m +++ b/Source/NSURL.m @@ -42,6 +42,7 @@ function may be incorrect #import "Foundation/NSCoder.h" #import "Foundation/NSData.h" #import "Foundation/NSDictionary.h" +#import "Foundation/NSError.h" #import "Foundation/NSException.h" #import "Foundation/NSFileManager.h" #import "Foundation/NSLock.h" @@ -1344,6 +1345,43 @@ static NSUInteger urlAlign; return _baseURL; } +- (BOOL) checkResourceIsReachableAndReturnError: (NSError **)error +{ + NSString *errorStr = nil; + + if ([self isFileURL]) + { + NSFileManager *mgr = [NSFileManager defaultManager]; + NSString *path = [self path]; + + if ([mgr fileExistsAtPath: path]) + { + if (![mgr isReadableFileAtPath: path]) + { + errorStr = @"File not readable"; + } + } + else + { + errorStr = @"File does not exist"; + } + } + else + { + errorStr = @"No file URL"; + } + + if ((errorStr != nil) && (error != NULL)) + { + *error = [NSError errorWithDomain: @"NSURLError" + code: 0 + userInfo: [NSDictionary + dictionaryWithObjectsAndKeys: errorStr, + NSLocalizedDescriptionKey, nil]]; + } + return (errorStr != nil); +} + /** * Returns the fragment portion of the receiver or nil if there is no * fragment supplied in the URL.