Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -105,11 +105,12 @@ #if defined(OF_HAVE_CHOWN) && defined(OF_HAVE_THREADS) static of_mutex_t mutex; #endif -static int parse_mode(const char *mode) +static int +parse_mode(const char *mode) { if (!strcmp(mode, "r")) return O_RDONLY; if (!strcmp(mode, "rb")) return O_RDONLY | O_BINARY; @@ -192,10 +193,13 @@ return ret; } + (bool)fileExistsAtPath: (OFString*)path { + if (path == nil) + @throw [OFInvalidArgumentException exception]; + #ifndef _WIN32 struct stat s; if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &s) == -1) @@ -213,10 +217,13 @@ return false; } + (bool)directoryExistsAtPath: (OFString*)path { + if (path == nil) + @throw [OFInvalidArgumentException exception]; + #ifndef _WIN32 struct stat s; if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &s) == -1) @@ -234,10 +241,13 @@ return false; } + (void)createDirectoryAtPath: (OFString*)path { + if (path == nil) + @throw [OFInvalidArgumentException exception]; + #ifndef _WIN32 if (mkdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], DIR_MODE)) #else if (_wmkdir([path UTF16String])) @@ -257,10 +267,13 @@ if (!createParents) { [OFFile createDirectoryAtPath: path]; return; } + if (path == nil) + @throw [OFInvalidArgumentException exception]; + pool = objc_autoreleasePoolPush(); pathComponents = [path pathComponents]; enumerator = [pathComponents objectEnumerator]; while ((component = [enumerator nextObject]) != nil) { @@ -286,11 +299,16 @@ objc_autoreleasePoolPop(pool); } + (OFArray*)contentsOfDirectoryAtPath: (OFString*)path { - OFMutableArray *files = [OFMutableArray array]; + OFMutableArray *files; + + if (path == nil) + @throw [OFInvalidArgumentException exception]; + + files = [OFMutableArray array]; #ifndef _WIN32 DIR *dir; struct dirent *dirent; @@ -356,10 +374,13 @@ return files; } + (void)changeCurrentDirectoryPath: (OFString*)path { + if (path == nil) + @throw [OFInvalidArgumentException exception]; + #ifndef _WIN32 if (chdir([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) #else if (_wchdir([path UTF16String])) #endif @@ -367,10 +388,13 @@ exceptionWithDirectoryPath: path]; } + (off_t)sizeOfFileAtPath: (OFString*)path { + if (path == nil) + @throw [OFInvalidArgumentException exception]; + #ifndef _WIN32 struct stat s; if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &s) == -1) @@ -386,10 +410,13 @@ return s.st_size; } + (OFDate*)modificationDateOfFileAtPath: (OFString*)path { + if (path == nil) + @throw [OFInvalidArgumentException exception]; + #ifndef _WIN32 struct stat s; if (stat([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], &s) == -1) @@ -408,10 +435,13 @@ #ifdef OF_HAVE_CHMOD + (void)changePermissionsOfItemAtPath: (OFString*)path permissions: (mode_t)permissions { + if (path == nil) + @throw [OFInvalidArgumentException exception]; + # ifndef _WIN32 if (chmod([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], permissions)) # else if (_wchmod([path UTF16String], permissions)) @@ -428,11 +458,11 @@ group: (OFString*)group { uid_t uid = -1; gid_t gid = -1; - if (owner == nil && group == nil) + if (path == nil || (owner == nil && group == nil)) @throw [OFInvalidArgumentException exception]; # ifdef OF_HAVE_THREADS if (!of_mutex_lock(&mutex)) @throw [OFLockFailedException exception]; @@ -480,16 +510,21 @@ #endif + (void)copyFileAtPath: (OFString*)source toPath: (OFString*)destination { - void *pool = objc_autoreleasePoolPush(); + void *pool; bool override; OFFile *sourceFile = nil; OFFile *destinationFile = nil; char *buffer; size_t pageSize; + + if (source == nil || destination == nil) + @throw [OFInvalidArgumentException exception]; + + pool = objc_autoreleasePoolPush(); if ([self directoryExistsAtPath: destination]) { OFString *filename = [source lastPathComponent]; destination = [OFString stringWithPath: destination, filename, nil]; @@ -538,11 +573,16 @@ } + (void)renameItemAtPath: (OFString*)source toPath: (OFString*)destination { - void *pool = objc_autoreleasePoolPush(); + void *pool; + + if (source == nil || destination == nil) + @throw [OFInvalidArgumentException exception]; + + pool = objc_autoreleasePoolPush(); if ([self directoryExistsAtPath: destination]) { OFString *filename = [source lastPathComponent]; destination = [OFString stringWithPath: destination, filename, nil]; @@ -561,10 +601,13 @@ objc_autoreleasePoolPop(pool); } + (void)removeItemAtPath: (OFString*)path { + if (path == nil) + @throw [OFInvalidArgumentException exception]; + #ifndef _WIN32 if (remove([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE])) #else if (_wremove([path UTF16String])) #endif @@ -574,11 +617,16 @@ #ifdef OF_HAVE_LINK + (void)linkItemAtPath: (OFString*)source toPath: (OFString*)destination { - void *pool = objc_autoreleasePoolPush(); + void *pool; + + if (source == nil || destination == nil) + @throw [OFInvalidArgumentException exception]; + + pool = objc_autoreleasePoolPush(); if ([self directoryExistsAtPath: destination]) { OFString *filename = [source lastPathComponent]; destination = [OFString stringWithPath: destination, filename, nil]; @@ -596,11 +644,16 @@ #ifdef OF_HAVE_SYMLINK + (void)createSymbolicLinkAtPath: (OFString*)source destinationPath: (OFString*)destination { - void *pool = objc_autoreleasePoolPush(); + void *pool; + + if (source == nil || destination == nil) + @throw [OFInvalidArgumentException exception]; + + pool = objc_autoreleasePoolPush(); if ([self directoryExistsAtPath: destination]) { OFString *filename = [source lastPathComponent]; destination = [OFString stringWithPath: destination, filename, nil]; @@ -618,10 +671,13 @@ + (OFString*)destinationOfSymbolicLinkAtPath: (OFString*)path { char destination[PATH_MAX]; ssize_t length; + if (path == nil) + @throw [OFInvalidArgumentException exception]; + length = readlink([path cStringWithEncoding: OF_STRING_ENCODING_NATIVE], destination, PATH_MAX); if (length < 0) @throw [OFOpenFileFailedException exceptionWithPath: path