Expand '~' abbreviations in localFromOpenStepPath: and change lines modified

by last windows patch to conform to coding standards.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@20646 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
CaS 2005-02-02 09:43:29 +00:00
parent e1b944688b
commit e22f1c0d4f
2 changed files with 927 additions and 829 deletions

View file

@ -1,3 +1,9 @@
2005-02-02 09:40 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSFileManager.m: Restructure last patch to conform to
gnustep coding standards.
(localFromOpenStepPath:) for unix paths, expand '~' abbreviations.
2005-01-31 Adam Fedor <fedor@gnu.org>
* Documentation/coding-standards.texi: Add section of documentation.

View file

@ -310,9 +310,9 @@ static NSFileManager* defaultManager = nil;
- (BOOL) changeFileAttributes: (NSDictionary*)attributes atPath: (NSString*)path
{
#ifdef __MINGW__
const unichar *wpath = NULL;
const unichar *wpath = 0;
#else
const char *cpath = NULL;
const char *cpath = 0;
#endif
unsigned long num;
NSString *str;
@ -596,7 +596,7 @@ static NSFileManager* defaultManager = nil;
NSDictionary *needChown = nil;
#endif
/* This is consitent with MacOSX - just return NO for an invalid path. */
/* This is consistent with MacOSX - just return NO for an invalid path. */
if ([path length] == 0)
return NO;
@ -619,8 +619,9 @@ static NSFileManager* defaultManager = nil;
else
{
const unichar *wpath;
wpath = [[self localFromOpenStepPath: completePath] unicharString];
if (CreateDirectory(wpath, NULL) == FALSE)
if (CreateDirectory(wpath, 0) == FALSE)
{
return NO;
}
@ -766,7 +767,7 @@ static NSFileManager* defaultManager = nil;
{
if (len > 0)
{
WriteFile(fh, [contents bytes], len, &written, NULL);
WriteFile(fh, [contents bytes], len, &written, 0);
}
CloseHandle(fh);
if (attributes != nil
@ -832,24 +833,28 @@ static NSFileManager* defaultManager = nil;
NSString *currentDir = nil;
#if defined(__MINGW__)
int len = GetCurrentDirectory(0, NULL);
int len = GetCurrentDirectory(0, 0);
if (len > 0)
{
unichar *wpath = (unichar*)calloc(len+10,sizeof(unichar));
if (wpath != NULL)
if (wpath != 0)
{
if (GetCurrentDirectory(len, wpath)>0)
currentDir = [self openStepPathFromLocal:[NSString stringWithCharacters: wpath length: len]];
{
currentDilr = [self openStepPathFromLocal:
[NSString stringWithCharacters: wpath length: len]];
}
free(wpath);
}
}
#else
char path[PATH_MAX];
#ifdef HAVE_GETCWD
if (getcwd(path, PATH_MAX-1) == NULL)
if (getcwd(path, PATH_MAX-1) == 0)
return nil;
#else
if (getwd(path) == NULL)
if (getwd(path) == 0)
return nil;
#endif /* HAVE_GETCWD */
currentDir = [self stringWithFileSystemRepresentation: path length: strlen(path)];
@ -955,16 +960,24 @@ static NSFileManager* defaultManager = nil;
toPath: (NSString*)destination
handler: (id)handler
{
BOOL sourceIsDir, fileExists;
#if defined(__MINGW__)
const unichar* sourcePath = [[self localFromOpenStepPath: source] unicharString];
const unichar* destPath = [[self localFromOpenStepPath: destination] unicharString];
#else
const char* sourcePath = [self fileSystemRepresentationWithPath: source];
const char* destPath = [self fileSystemRepresentationWithPath: destination];
#endif
BOOL sourceIsDir;
BOOL fileExists;
NSString *destinationParent;
unsigned int sourceDevice, destinationDevice;
unsigned int sourceDevice;
unsigned int destinationDevice;
#if defined(__MINGW__)
const unichar *sourcePath;
const unichar *destPath;
sourcePath = [[self localFromOpenStepPath: source] unicharString];
destPath = [[self localFromOpenStepPath: destination] unicharString];
#else
const char *sourcePath;
const char *destPath;
sourcePath = [self fileSystemRepresentationWithPath: source];
destPath = [self fileSystemRepresentationWithPath: destination];
#endif
if ([self fileExistsAtPath: destination] == YES)
{
@ -991,7 +1004,9 @@ static NSFileManager* defaultManager = nil;
isn't possible. */
if (sourceIsDir && [[destination stringByAppendingString: @"/"]
hasPrefix: [source stringByAppendingString: @"/"]])
{
return NO;
}
if ([self copyPath: source toPath: destination handler: handler])
{
@ -1003,8 +1018,10 @@ static NSFileManager* defaultManager = nil;
return [self removeFileAtPath: source handler: handler];
}
else
{
return NO;
}
}
else
{
/* source and destination are on the same device so we can simply
@ -1260,7 +1277,7 @@ static NSFileManager* defaultManager = nil;
*/
- (BOOL) fileExistsAtPath: (NSString*)path
{
return [self fileExistsAtPath: path isDirectory: NULL];
return [self fileExistsAtPath: path isDirectory: 0];
}
/**
@ -1280,21 +1297,29 @@ static NSFileManager* defaultManager = nil;
}
if (wpath == 0 || *wpath == L'\0')
{
return NO;
}
else
{
DWORD res;
res = GetFileAttributes(wpath);
if (res == WIN32ERR)
{
return NO;
}
if (isDirectory != 0)
{
if (res & FILE_ATTRIBUTE_DIRECTORY)
{
*isDirectory = YES;
}
else
{
*isDirectory = NO;
}
}
return YES;
}
#else
@ -1314,7 +1339,9 @@ static NSFileManager* defaultManager = nil;
struct stat statbuf;
if (stat(cpath, &statbuf) != 0)
{
return NO;
}
if (isDirectory)
{
@ -1336,14 +1363,17 @@ static NSFileManager* defaultManager = nil;
const unichar* wpath = [[self localFromOpenStepPath: path] unicharString];
if (wpath == 0 || *wpath == L'\0')
{
return NO;
}
else
{
DWORD res= GetFileAttributes(wpath);
if (res == WIN32ERR)
{
return NO;
}
return YES;
}
#else
@ -1370,14 +1400,17 @@ static NSFileManager* defaultManager = nil;
const unichar* wpath = [[self localFromOpenStepPath: path] unicharString];
if (wpath == 0 || *wpath == L'\0')
{
return NO;
}
else
{
DWORD res= GetFileAttributes(wpath);
if (res == WIN32ERR)
{
return NO;
}
return (res & FILE_ATTRIBUTE_READONLY) ? NO : YES;
}
#else
@ -1405,27 +1438,36 @@ static NSFileManager* defaultManager = nil;
const unichar* wpath = [[self localFromOpenStepPath: path] unicharString];
if (wpath == 0 || *wpath == L'\0')
{
return NO;
}
else
{
DWORD res= GetFileAttributes(wpath);
if (res == WIN32ERR)
{
return NO;
}
if ([[[path pathExtension] lowercaseString] isEqualToString: @"exe"])
{
return YES;
}
/* FIXME: On unix, directory accessable == executable, so we simulate that
here for Windows. Is there a better check for directory access? */
if (res & FILE_ATTRIBUTE_DIRECTORY)
{
return YES;
}
return NO;
}
#else
const char* cpath = [self fileSystemRepresentationWithPath: path];
if (cpath == 0 || *cpath == '\0')
{
return NO;
}
else
{
return (access(cpath, X_OK) == 0);
@ -1443,15 +1485,18 @@ static NSFileManager* defaultManager = nil;
const unichar* wpath = [[self localFromOpenStepPath: path] unicharString];
if (wpath == 0 || *wpath == L'\0')
{
return NO;
}
else
{
// TODO - handle directories
DWORD res= GetFileAttributes(wpath);
if (res == WIN32ERR)
{
return NO;
}
return (res & FILE_ATTRIBUTE_READONLY) ? NO : YES;
}
#else
@ -1599,7 +1644,9 @@ static NSFileManager* defaultManager = nil;
if (!GetDiskFreeSpace(wpath, &SectorsPerCluster,
&BytesPerSector, &NumberFreeClusters, &TotalNumberClusters))
{
return nil;
}
totalsize = (unsigned long long)TotalNumberClusters
* (unsigned long long)SectorsPerCluster
@ -1638,14 +1685,19 @@ static NSFileManager* defaultManager = nil;
};
if (stat(cpath, &statbuf) != 0)
{
return nil;
}
#ifdef HAVE_STATVFS
if (statvfs(cpath, &statfsbuf) != 0)
{
return nil;
}
#else
if (statfs(cpath, &statfsbuf) != 0)
{
return nil;
}
#endif
totalsize = (unsigned long long) statfsbuf.f_bsize
@ -1830,12 +1882,15 @@ static NSFileManager* defaultManager = nil;
*/
- (const char*) fileSystemRepresentationWithPath: (NSString*)path
{
NSString *localPath = [self localFromOpenStepPath:path];
const char *local_c_path = NULL;
NSString *localPath;
const char *local_c_path = 0;
if (localPath && [localPath canBeConvertedToEncoding: [NSString defaultCStringEncoding]])
localPath = [self localFromOpenStepPath: path];
if (localPath
&& [localPath canBeConvertedToEncoding: [NSString defaultCStringEncoding]])
{
local_c_path = [localPath cString];
}
return (local_c_path);
}
@ -1858,7 +1913,7 @@ static NSFileManager* defaultManager = nil;
* can handle it
*/
int wcount; // count unichars
unichar *wc_path = NULL;
unichar *wc_path = 0;
int l;
path = [path stringByStandardizingPath];
@ -1883,14 +1938,16 @@ static NSFileManager* defaultManager = nil;
wc_path[1] = L':';
newpath = [NSString stringWithCharacters: wc_path length: wcount];
}
else if (l >= 3 && wc_path[0] == L'/' && wc_path[1] == L'/' && iswalpha(wc_path[2]))
else if (l >= 3 && wc_path[0] == L'/' && wc_path[1] == L'/'
&& iswalpha(wc_path[2]))
{
if (l == 3 || wc_path[3] == L'/')
{
/* Cygwin "//c/" type absolute path */
wc_path[1] = wc_path[2];
wc_path[2] = L':';
newpath = [NSString stringWithCharacters:&wc_path[1] length:wcount-1];
newpath = [NSString stringWithCharacters: &wc_path[1]
length: wcount-1];
}
else
{
@ -1906,11 +1963,13 @@ static NSFileManager* defaultManager = nil;
else if (wc_path[0] == L'/')
{
#ifdef __CYGWIN__
if (l > 11 && wcsncmp(wc_path, L"/cygdrive/", 10) == 0 && wc_path[11] == L'/')
if (l > 11 && wcsncmp(wc_path, L"/cygdrive/", 10) == 0
&& wc_path[11] == L'/')
{
wc_path[9] = wc_path[10];
wc_path[10] = L':';
newpath = [NSString stringWithCharacters:&wc_path[9] length:wcount-9];
newpath = [NSString stringWithCharacters: &wc_path[9]
length: wcount-9];
}
else
{
@ -1936,7 +1995,8 @@ static NSFileManager* defaultManager = nil;
{
/* Mingw /drive/... format */
wc_path[2] = L':';
newpath = [NSString stringWithCharacters:&wc_path[1] length:wcount-1];
newpath = [NSString stringWithCharacters: &wc_path[1]
length: wcount-1];
}
else
{
@ -1950,17 +2010,23 @@ static NSFileManager* defaultManager = nil;
}
newpath = [newpath stringByReplacingString: @"/" withString: @"\\"];
if (wc_path)
{
free (wc_path);
}
}
else
{
newpath = path;
}
#else
/*
* NB ... Don't standardize path, since that would automatically
* follow symbolic links ... and mess up any code wishing to
* examine the link itsself.
* We just need the path in a form where it can be interpreted by
* operating system calls (no '~' abbreviations for user directories).
*/
newpath = path;
newpath = [path stringByExpandingTildeInPath];
#endif
return (newpath);
@ -1979,8 +2045,10 @@ return (newpath);
{
NSString *localPath = nil;
if (string != NULL)
if (string != 0)
{
localPath = [NSString stringWithCString: string length: len];
}
return([self openStepPathFromLocal: localPath]);
}
@ -1998,7 +2066,7 @@ return (newpath);
#ifdef __MINGW__
int len; // count unichars
unichar *wc_path = NULL;
unichar *wc_path = 0;
len = [localPath length];
if (len != 0)
@ -2022,7 +2090,8 @@ return (newpath);
free(wc_path);
return @"";
}
if (len >= 2 && ((ptr[1] == L'/' && ptr[0] == L'/') || (ptr[1] == L'\\' && ptr[0] == L'\\')))
if (len >= 2 && ((ptr[1] == L'/' && ptr[0] == L'/')
|| (ptr[1] == L'\\' && ptr[0] == L'\\')))
{
/*
* Convert '//<servername>/' to '~@<servername>/' sequences.
@ -2099,7 +2168,9 @@ return (newpath);
return [NSString stringWithCharacters: buf length: j];
}
else
{
return(@"");
}
#endif
return localPath;
@ -2130,7 +2201,7 @@ typedef struct _GSEnumeratedDirectory {
inline void gsedRelease(GSEnumeratedDirectory X)
{
[X.path release];
DESTROY(X.path);
#ifdef __MINGW__
_wclosedir(X.pointer);
#else
@ -2208,18 +2279,21 @@ static SEL ospfl = 0;
_flags.isRecursive = recurse;
_flags.isFollowing = follow;
_flags.justContents = justContents;
_topPath = [[NSString stringWithString:path] retain];
_topPath = [[NSString alloc] initWithString: path];
#ifdef __MINGW__
dir_pointer = _wopendir([[defaultManager localFromOpenStepPath: path] unicharString]);
dir_pointer
= _wopendir([[defaultManager localFromOpenStepPath: path] unicharString]);
#else
dir_pointer = opendir([defaultManager fileSystemRepresentationWithPath:path]);
dir_pointer
= opendir([defaultManager fileSystemRepresentationWithPath: path]);
#endif
if (dir_pointer)
{
GSIArrayItem item;
item.ext.path = [[NSString stringWithString:@""] retain];
item.ext.path = @"";
item.ext.pointer = dir_pointer;
GSIArrayAddItem(_stack, item);
@ -2236,8 +2310,8 @@ static SEL ospfl = 0;
{
GSIArrayEmpty(_stack);
NSZoneFree([self zone], _stack);
[_topPath release];
[_currentFilePath release];
DESTROY(_topPath);
DESTROY(_currentFilePath);
[super dealloc];
}
@ -2275,35 +2349,31 @@ static SEL ospfl = 0;
if (GSIArrayCount(_stack) > 0)
{
GSIArrayRemoveLastItem(_stack);
if (_currentFilePath != NULL)
if (_currentFilePath != 0)
{
[_currentFilePath release];
_currentFilePath = NULL;
DESTROY(_currentFilePath);
}
}
}
// Enumerate next
/*
* finds the next file according to the top enumerator
* - if there is a next file it is put in currentFile
* - if the current file is a directory and if isRecursive calls
* recurseIntoDirectory: currentFile
* - if the current file is a symlink to a directory and if isRecursive
* and isFollowing calls recurseIntoDirectory: currentFile
* - if at end of current directory pops stack and attempts to
* find the next entry in the parent
* - sets currentFile to nil if there are no more files to enumerate
*/
- (id) nextObject
{
/*
finds the next file according to the top enumerator
- if there is a next file it is put in currentFile
- if the current file is a directory and if isRecursive calls
recurseIntoDirectory: currentFile
- if the current file is a symlink to a directory and if isRecursive
and isFollowing calls recurseIntoDirectory: currentFile
- if at end of current directory pops stack and attempts to
find the next entry in the parent
- sets currentFile to nil if there are no more files to enumerate
*/
NSString *returnFileName = NULL;
NSString *returnFileName = 0;
if (_currentFilePath != NULL)
if (_currentFilePath != 0)
{
[_currentFilePath release];
_currentFilePath = NULL;
DESTROY(_currentFilePath);
}
while (GSIArrayCount(_stack) > 0)
@ -2328,7 +2398,9 @@ static SEL ospfl = 0;
/* Skip "." and ".." directory entries */
if (wcscmp(dirbuf->d_name, L".") == 0
|| wcscmp(dirbuf->d_name, L"..") == 0)
{
continue;
}
/* Name of file to return */
returnFileName = _openStepPathFromLocalImp(defaultManager, ospfl,
[NSString stringWithCharacters: dirbuf->d_name
@ -2337,16 +2409,21 @@ static SEL ospfl = 0;
/* Skip "." and ".." directory entries */
if (strcmp(dirbuf->d_name, ".") == 0
|| strcmp(dirbuf->d_name, "..") == 0)
{
continue;
}
/* Name of file to return */
returnFileName = _openStepPathFromLocalImp(defaultManager, ospfl,
[NSString stringWithCString: dirbuf->d_name]);
#endif
returnFileName = [[dir.path stringByAppendingPathComponent:returnFileName] retain];
returnFileName = [dir.path stringByAppendingPathComponent:
returnFileName];
RETAIN(returnFileName);
/* TODO - can this one can be removed ? */
if (!_flags.justContents)
_currentFilePath = [[_topPath stringByAppendingPathComponent:returnFileName] retain];
_currentFilePath = RETAIN([_topPath stringByAppendingPathComponent:
returnFileName]);
if (_flags.isRecursive == YES)
{
@ -2357,23 +2434,32 @@ static SEL ospfl = 0;
#else
if (!_flags.isFollowing)
{
if (lstat([_currentFilePath fileSystemRepresentation], &statbuf) != 0)
if (lstat([_currentFilePath fileSystemRepresentation],
&statbuf) != 0)
{
break;
}
// If link then return it as link
if (S_IFLNK == (S_IFMT & statbuf.st_mode))
{
break;
}
}
else
#endif
#endif
{
#ifdef __MINGW__
if (_wstat([[_currentFilePath localFromOpenStepPath] unicharString], &statbuf) != 0)
if (_wstat([[_currentFilePath localFromOpenStepPath]
unicharString], &statbuf) != 0)
#else
if (stat([_currentFilePath fileSystemRepresentation], &statbuf) != 0)
if (stat([_currentFilePath fileSystemRepresentation],
&statbuf) != 0)
#endif
{
break;
}
}
if (S_IFDIR == (S_IFMT & statbuf.st_mode))
{
#ifdef __MINGW__
@ -2390,15 +2476,15 @@ static SEL ospfl = 0;
{
GSIArrayItem item;
item.ext.path = [returnFileName retain];
item.ext.path = RETAIN(returnFileName);
item.ext.pointer = dir_pointer;
GSIArrayAddItem(_stack, item);
}
else
{
NSLog(@"Failed to recurse into directory '%@' - %s",_currentFilePath,
GSLastErrorStr(errno));
NSLog(@"Failed to recurse into directory '%@' - %s",
_currentFilePath, GSLastErrorStr(errno));
}
}
}
@ -2407,10 +2493,9 @@ static SEL ospfl = 0;
else
{
GSIArrayRemoveLastItem(_stack);
if (_currentFilePath != NULL)
if (_currentFilePath != 0)
{
[_currentFilePath release];
_currentFilePath = NULL;
DESTROY(_currentFilePath);
}
}
}
@ -2965,18 +3050,21 @@ static NSSet *fileKeys = nil;
}
#ifdef __MINGW__
+ (NSDictionary*) wattributesAt: (const unichar*)wpath traverseLink: (BOOL)traverse
+ (NSDictionary*) wattributesAt: (const unichar*)wpath
traverseLink: (BOOL)traverse
{
GSAttrDictionary *d;
if (wpath == 0 || *wpath == L'\0')
{
return nil;
}
d = (GSAttrDictionary*)NSAllocateObject(self, 0, NSDefaultMallocZone());
d->name = NULL; //seems to be unused
d->name = 0; //seems to be unused
if (_wstat(wpath, (struct _stat*)&d->statbuf) != 0)
{
DESTROY(d);
}
return AUTORELEASE(d);
}
#endif
@ -3116,13 +3204,14 @@ static NSSet *fileKeys = nil;
"myfile.txt",
GENERIC_READ,
FILE_SHARE_READ,
NULL,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
0);
// Check GetLastError for CreateFile error code.
if (hFile == INVALID_HANDLE_VALUE) {
if (hFile == INVALID_HANDLE_VALUE)
{
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
@ -3146,13 +3235,14 @@ static NSSet *fileKeys = nil;
SE_FILE_OBJECT,
OWNER_SECURITY_INFORMATION,
&pSidOwner,
NULL,
NULL,
NULL,
0,
0,
0,
&pSD);
// Check GetLastError for GetSecurityInfo error condition.
if (dwRtnCode != ERROR_SUCCESS) {
if (dwRtnCode != ERROR_SUCCESS)
{
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
@ -3162,7 +3252,7 @@ static NSSet *fileKeys = nil;
// First call to LookupAccountSid to get the buffer sizes.
bRtnBool = LookupAccountSid(
NULL, // local computer
0, // local computer
pSidOwner,
AcctName,
(LPDWORD)&dwAcctName,
@ -3176,7 +3266,8 @@ static NSSet *fileKeys = nil;
dwAcctName);
// Check GetLastError for GlobalAlloc error condition.
if (AcctName == NULL) {
if (AcctName == 0)
{
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
@ -3189,18 +3280,18 @@ static NSSet *fileKeys = nil;
dwDomainName);
// Check GetLastError for GlobalAlloc error condition.
if (DomainName == NULL) {
if (DomainName == 0)
{
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
_tprintf(TEXT("GlobalAlloc error = %d\n"), dwErrorCode);
return -1;
}
// Second call to LookupAccountSid to get the account name.
bRtnBool = LookupAccountSid(
NULL, // name of local or remote computer
0, // name of local or remote computer
pSidOwner, // security identifier
AcctName, // account name buffer
(LPDWORD)&dwAcctName, // size of account name buffer
@ -3209,7 +3300,8 @@ static NSSet *fileKeys = nil;
&eUse); // SID type
// Check GetLastError for LookupAccountSid error condition.
if (bRtnBool == FALSE) {
if (bRtnBool == FALSE)
{
DWORD dwErrorCode = 0;
dwErrorCode = GetLastError();
@ -3219,12 +3311,12 @@ static NSSet *fileKeys = nil;
else
_tprintf(TEXT("Error in LookupAccountSid.\n"));
return -1;
} else if (bRtnBool == TRUE)
}
else if (bRtnBool == TRUE)
{
// Print the account name.
_tprintf(TEXT("Account owner = %s\n"), AcctName);
}
return 0;
}