Index: src/OFExceptions.h ================================================================== --- src/OFExceptions.h +++ src/OFExceptions.h @@ -353,10 +353,106 @@ * An OFException indicating a write to the file failed. */ @interface OFWriteFailedException: OFReadOrWriteFailedException {} @end +/** + * An OFException indicating that creating a link failed. + */ +@interface OFLinkFailedException: OFException +{ + OFString *src; + OFString *dest; + int err; +} + +/** + * \param class The class of the object which caused the exception + * \param source The source for the link + * \param destination The destination for the link + * \return A new link failed exception + */ ++ newWithClass: (Class)class + source: (OFString*)src + destination: (OFString*)dest; + +/** + * Initializes an already allocated open file failed exception. + * + * \param class The class of the object which caused the exception + * \param source The source for the link + * \param destination The destination for the link + * \return An initialized link failed exception + */ +- initWithClass: (Class)class + source: (OFString*)src + destination: (OFString*)dest; + +/** + * \return The errno from when the exception was created + */ +- (int)errNo; + +/** + * \return A string of the source for the link + */ +- (OFString*)source; + +/** + * \return A string of the destination for the link + */ +- (OFString*)destination; +@end + +/** + * An OFException indicating that creating a symlink failed. + */ +@interface OFSymlinkFailedException: OFException +{ + OFString *src; + OFString *dest; + int err; +} + +/** + * \param class The class of the object which caused the exception + * \param source The source for the symlink + * \param destination The destination for the symlink + * \return A new symlink failed exception + */ ++ newWithClass: (Class)class + source: (OFString*)src + destination: (OFString*)dest; + +/** + * Initializes an already allocated open file failed exception. + * + * \param class The class of the object which caused the exception + * \param source The source for the symlink + * \param destination The destination for the symlink + * \return An initialized symlink failed exception + */ +- initWithClass: (Class)class + source: (OFString*)src + destination: (OFString*)dest; + +/** + * \return The errno from when the exception was created + */ +- (int)errNo; + +/** + * \return A string of the source for the symlink + */ +- (OFString*)source; + +/** + * \return A string of the destination for the symlink + */ +- (OFString*)destination; +@end + /** * An OFException indicating that setting an option failed. */ @interface OFSetOptionFailedException: OFException {} @end Index: src/OFExceptions.m ================================================================== --- src/OFExceptions.m +++ src/OFExceptions.m @@ -511,10 +511,140 @@ [class className], ERRPARAM]; return string; } @end + +@implementation OFLinkFailedException ++ newWithClass: (Class)class_ + source: (OFString*)src_ + destination: (OFString*)dest_ +{ + return [[self alloc] initWithClass: class_ + source: src_ + destination: dest_]; +} + +- initWithClass: (Class)class_ +{ + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; +} + +- initWithClass: (Class)class_ + source: (OFString*)src_ + destination: (OFString*)dest_ +{ + self = [super initWithClass: class_]; + + src = [src_ retain]; + dest = [dest_ retain]; + err = GET_ERR; + + return self; +} + +- (void)dealloc +{ + [src release]; + [dest release]; + + [super dealloc]; +} + +- (OFString*)string +{ + if (string != nil) + return string; + + string = [[OFString alloc] initWithFormat: + @"Failed to link file %s to %s in class %s! " ERRFMT, + [src cString], [dest cString], [self className], ERRPARAM]; + + return string; +} + +- (int)errNo +{ + return err; +} + +- (OFString*)source +{ + return src; +} + +- (OFString*)destination +{ + return dest; +} +@end + +@implementation OFSymlinkFailedException ++ newWithClass: (Class)class_ + source: (OFString*)src_ + destination: (OFString*)dest_ +{ + return [[self alloc] initWithClass: class_ + source: src_ + destination: dest_]; +} + +- initWithClass: (Class)class_ +{ + @throw [OFNotImplementedException newWithClass: isa + selector: _cmd]; +} + +- initWithClass: (Class)class_ + source: (OFString*)src_ + destination: (OFString*)dest_ +{ + self = [super initWithClass: class_]; + + src = [src_ retain]; + dest = [dest_ retain]; + err = GET_ERR; + + return self; +} + +- (void)dealloc +{ + [src release]; + [dest release]; + + [super dealloc]; +} + +- (OFString*)string +{ + if (string != nil) + return string; + + string = [[OFString alloc] initWithFormat: + @"Failed to symlink file %s to %s in class %s! " ERRFMT, + [src cString], [dest cString], [self className], ERRPARAM]; + + return string; +} + +- (int)errNo +{ + return err; +} + +- (OFString*)source +{ + return src; +} + +- (OFString*)destination +{ + return dest; +} +@end @implementation OFSetOptionFailedException - (OFString*)string { if (string != nil) Index: src/OFFile.h ================================================================== --- src/OFFile.h +++ src/OFFile.h @@ -60,11 +60,11 @@ * \param path The path to the file of which the mode should be changed as a * string * \param mode The new mode for the file * \return A boolean whether the operation succeeded */ -+ (void)changeModeOfFile: (OFString*)path ++ (BOOL)changeModeOfFile: (OFString*)path toMode: (mode_t)mode; /** * Changes the owner of a file. * @@ -74,11 +74,11 @@ * string * \param owner The new owner for the file * \param group The new group for the file * \return A boolean whether the operation succeeded */ -+ (void)changeOwnerOfFile: (OFString*)path ++ (BOOL)changeOwnerOfFile: (OFString*)path owner: (uid_t)owner group: (gid_t)group; /** * Deletes a file. Index: src/OFFile.m ================================================================== --- src/OFFile.m +++ src/OFFile.m @@ -60,29 +60,32 @@ initWithFilePointer: stderr]; return of_file_stderr; } -+ (void)changeModeOfFile: (OFString*)path ++ (BOOL)changeModeOfFile: (OFString*)path toMode: (mode_t)mode { +#ifndef _WIN32 + return (chmod([path cString], mode) == 0 ? YES : NO); +#else /* - * FIXME: On error, throw exception * FIXME: On Win32, change write access */ -#ifndef _WIN32 - chmod([path cString], mode); + return NO; #endif } -+ (void)changeOwnerOfFile: (OFString*)path ++ (BOOL)changeOwnerOfFile: (OFString*)path owner: (uid_t)owner group: (gid_t)group { /* FIXME: On error, throw exception */ #ifndef _WIN32 - chown([path cString], owner, group); + return (chown([path cString], owner, group) == 0 ? YES : NO); +#else + return NO; #endif } + (void)delete: (OFString*)path { @@ -91,22 +94,32 @@ } + (void)link: (OFString*)src to: (OFString*)dest { - /* FIXME: On error, throw exception */ #ifndef _WIN32 - link([src cString], [dest cString]); + if (link([src cString], [dest cString]) != 0) + @throw [OFLinkFailedException newWithClass: self + source: src + destination: dest]; +#else + @throw [OFNotImplementedException newWithClass: self + selector: _cmd]; #endif } + (void)symlink: (OFString*)src to: (OFString*)dest { - /* FIXME: On error, throw exception */ #ifndef _WIN32 - symlink([src cString], [dest cString]); + if (symlink([src cString], [dest cString]) != 0) + @throw [OFSymlinkFailedException newWithClass: self + source: src + destination: dest]; +#else + @throw [OFNotImplementedException newWithClass: self + selector: _cmd]; #endif } - init {