Fix directory creation to match actual OSX sematics as determined by adding

a few testcases ...
The basic method should fail if the directory already exists.
The intermediate directories method should fail if the directory exists unless
the option to create intermediate directories is selected, in which case a
pre-existing directory is counted as a success.
This commit is contained in:
Richard Frith-Macdonald 2017-06-18 09:13:57 +01:00
parent c494785630
commit 24d29934cf
4 changed files with 42 additions and 20 deletions

View file

@ -1,3 +1,9 @@
2017-06-18 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSFileManager.m: Make directory creation semantics match OSX
* Tests/base/NSFileManager/general.m: Add directory creation testcases
* Tests/base/NSDistributedLock/basic.m: Cleanup lock after tests
2017-06-17 Richard Frith-Macdonald <rfm@gnu.org>
* Headers/GNUstepBase/GSIMap.h: Fix GSI_MAP_NODE_IS_EMPTY macros as

View file

@ -782,11 +782,12 @@ static NSStringEncoding defaultEncoding;
}
/**
* Creates a new directory and all intermediate directories. if flag is YES.
* Creates a new directory (and all intermediate directories if flag is YES).
* Creates only the last directory in the path if flag is NO.<br />
* The directory is created with the attributes
* specified in attributes and any error is returned in error.<br />
* returns YES on success, NO on failure.
* The directory is created with the attributes specified, and any problem
* is returned in error.<br />
* Returns YES if the directory is created (or flag is YES and the directory
* already exists), NO on failure.
*/
- (BOOL) createDirectoryAtPath: (NSString *)path
withIntermediateDirectories: (BOOL)flag
@ -802,7 +803,8 @@ static NSStringEncoding defaultEncoding;
NSString *path = nil;
NSString *dir = [NSString string];
while ((path = (NSString *)[paths nextObject]) != nil)
result = YES;
while (YES == result && (path = (NSString *)[paths nextObject]) != nil)
{
dir = [dir stringByAppendingPathComponent: path];
// create directory only if it doesn't exist
@ -811,11 +813,6 @@ static NSStringEncoding defaultEncoding;
result = [self createDirectoryAtPath: dir
attributes: attributes];
}
// an existing not created dir is equivalent to a created one
else
{
result = YES;
}
}
}
else
@ -848,11 +845,12 @@ static NSStringEncoding defaultEncoding;
/**
* Creates a new directory and all intermediate directories in the file URL
* if flag is YES.
* if flag is YES.<br />
* Creates only the last directory in the URL if flag is NO.<br />
* The directory is created with the attributes
* specified in attributes and any error is returned in error.<br />
* returns YES on success, NO on failure.
* The directory is created with the attributes specified and any problem
* is returned in error.<br />
* Returns YES if the directory is created (or flag is YES and the directory
* already exists), NO on failure.
*/
- (BOOL) createDirectoryAtURL: (NSURL *)url
withIntermediateDirectories: (BOOL)flag
@ -868,7 +866,7 @@ static NSStringEncoding defaultEncoding;
/**
* Creates a new directory, and sets its attributes as specified.<br />
* Fails if directories in the path are missing.<br />
* Returns YES if the directory was created (or already exists), NO otherwise.
* Returns YES if the directory was actually created, NO otherwise.
*/
- (BOOL) createDirectoryAtPath: (NSString*)path
attributes: (NSDictionary*)attributes
@ -884,15 +882,20 @@ static NSStringEncoding defaultEncoding;
if (YES == [self fileExistsAtPath: path isDirectory: &isDir])
{
NSString *e;
if (NO == isDir)
{
NSString *e;
e = [NSString stringWithFormat:
@"path %@ exists, but is not a directory", path];
ASSIGN(_lastError, e);
return NO;
}
else
{
e = [NSString stringWithFormat:
@"path %@ exists ... cannot create", path];
}
ASSIGN(_lastError, e);
return NO;
}
else
{

View file

@ -26,6 +26,7 @@ int main()
[lock1 unlock];
PASS(YES == [lock2 tryLock], "unlocking first lock allows second lock");
[lock2 unlock];
END_SET("basic")
return 0;

View file

@ -40,6 +40,18 @@ int main()
PASS([mgr fileExistsAtPath: dir isDirectory: &isDir] &&
isDir == YES,
"exists and is a directory");
PASS(NO == [mgr createDirectoryAtPath: dir attributes: nil],
"-createDirectoryAtPath:attributes: fails for existing directory");
PASS(NO == [mgr createDirectoryAtPath: dir withIntermediateDirectories: NO
attributes: nil error: 0],
"-createDirectoryAtPath:withIntermediateDirectories:attributes:error:"
" fails for existing directory if flag is NO");
PASS(YES == [mgr createDirectoryAtPath: dir withIntermediateDirectories: YES
attributes: nil error: 0],
"-createDirectoryAtPath:withIntermediateDirectories:attributes:error:"
" succeeds for existing directory if flag is YES");
PASS([mgr fileAttributesAtPath: dir traverseLink: NO] != nil,
"NSFileManager returns non-nil for attributes of existing file");
attr = [mgr fileAttributesAtPath: dir traverseLink: NO];
@ -47,7 +59,7 @@ int main()
"NSFileManager returns non-nil for attributes of existing file");
PASS([NSUserName() isEqual: [attr fileOwnerAccountName]],
"newly created file is owned by current user");
NSLog(@"'%@', '%@'", NSUserName(), [attr fileOwnerAccountName]);
//NSLog(@"'%@', '%@'", NSUserName(), [attr fileOwnerAccountName]);
err = (id)(void*)42;
attr = [mgr attributesOfItemAtPath: dir error: &err];
PASS(attr != nil && err == (id)(void*)42,