@@ -9,10 +9,13 @@ * the packaging of this file. */ #import #import +#import +#import +#import #import "OFFile.h" #import "OFExceptions.h" @implementation OFFile @@ -20,10 +23,45 @@ andMode: (const char*)mode { return [[OFFile alloc] initWithPath: path andMode: mode]; } + ++ (int)changeModeOfFile: (const char*)path + toMode: (mode_t)mode +{ + // FIXME: On error, throw exception + return chmod(path, mode); +} + ++ (int)changeOwnerOfFile: (const char*)path + toOwner: (uid_t)owner + andGroup: (gid_t)group +{ + // FIXME: On error, throw exception + return chown(path, owner, group); +} + ++ (int)delete: (const char*)path +{ + // FIXME: On error, throw exception + return unlink(path); +} + ++ (int)link: (const char*)src + to: (const char*)dest +{ + // FIXME: On error, throw exception + return link(src, dest); +} + ++ (int)symlink: (const char*)src + to: (const char*)dest +{ + // FIXME: On error, throw exception + return symlink(src, dest); +} - initWithPath: (const char*)path andMode: (const char*)mode { if ((self = [super init])) { @@ -39,11 +77,11 @@ { fclose(fp); return [super free]; } -- (BOOL)isEndOfFile +- (BOOL)atEndOfFile { return feof(fp); } - (size_t)readIntoBuffer: (char*)buf @@ -50,14 +88,14 @@ withSize: (size_t)size andNItems: (size_t)nitems { size_t ret; - if ((ret = fread(buf, size, nitems, fp)) == 0 && ![self isEndOfFile]) - [OFReadFailedException newWithObject: self - andSize: size - andNItems: nitems]; + if ((ret = fread(buf, size, nitems, fp)) == 0 && !feof(fp)) + [[OFReadFailedException newWithObject: self + andSize: size + andNItems: nitems] raise]; return ret; } - (char*)readWithSize: (size_t)size @@ -66,15 +104,15 @@ uint64_t memsize; char *ret; if (size >= 0xFFFFFFFF || nitems >= 0xFFFFFFFF || (memsize = (uint64_t)nitems * size) > 0xFFFFFFFF) { - [OFOverflowException newWithObject: self]; + [[OFOverflowException newWithObject: self] raise]; return NULL; } - ret = [self getMem: (size_t)memsize]; + ret = [self getMemWithSize: (size_t)memsize]; @try { [self readIntoBuffer: ret withSize: size andNItems: nitems]; @@ -93,12 +131,12 @@ { size_t ret; if ((ret = fwrite(buf, size, nitems, fp)) == 0 && size != 0 && nitems != 0) - [OFWriteFailedException newWithObject: self - andSize: size - andNItems: nitems]; + [[OFWriteFailedException newWithObject: self + andSize: size + andNItems: nitems] raise]; return ret; } @end