2 Copyright (C) 2000-2004 SKYRIX Software AG
4 This file is part of OpenGroupware.org.
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 #include "NGMimeFileData.h"
26 #include <sys/types.h>
30 @implementation NGMimeFileData
32 static NSString *TmpPath = nil;
33 static NSProcessInfo *Pi = nil;
34 static unsigned tmpmask = 0600;
37 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
40 TmpPath = [ud stringForKey:@"NGMimeBuildMimeTempDirectory"];
41 if (TmpPath == nil) TmpPath = @"/tmp/";
42 TmpPath = [[TmpPath stringByAppendingPathComponent:@"OGo"] copy];
44 if (Pi == nil) Pi = [[NSProcessInfo processInfo] retain];
47 - (id)initWithPath:(NSString *)_path removeFile:(BOOL)_remove {
48 if ((self = [super init])) {
49 if (![[NSFileManager defaultManager] fileExistsAtPath:_path]) {
50 NSLog(@"ERROR[%s]: missing file at path %@", __PRETTY_FUNCTION__, _path);
54 self->path = [_path copy];
55 self->removeFile = _remove;
61 - (id)initWithBytes:(const void*)_bytes
62 length:(unsigned int)_length
64 NSString *filename = nil;
67 filename = [Pi temporaryFileName:TmpPath];
69 fd = open([filename fileSystemRepresentation],
70 O_WRONLY | O_CREAT | O_TRUNC, tmpmask);
72 fprintf(stderr, "Could not open file for writing %s: %s\n",
73 [filename fileSystemRepresentation], strerror(errno));
77 if (write(fd, _bytes, _length) != (int)_length) {
78 fprintf(stderr, "Failed to write %i bytes to %s: %s\n",
79 _length, [filename fileSystemRepresentation], strerror(errno));
84 return [self initWithPath:filename removeFile:YES];
88 if (self->removeFile) {
89 [[NSFileManager defaultManager]
90 removeFileAtPath:self->path handler:nil];
97 return [NSData dataWithContentsOfMappedFile:self->path];
100 - (id)copyWithZone:(NSZone *)zone {
101 return [self retain];
104 - (const void*)bytes {
105 return [[self _data] bytes];
108 - (unsigned int)length {
109 if (self->length == -1) {
110 self->length = [[[[NSFileManager defaultManager]
111 fileAttributesAtPath:self->path
113 objectForKey:NSFileSize] intValue];
118 - (NSString *)description {
119 return [NSString stringWithFormat:@"<0x%08X[%@]: path=%@>",
120 self, NSStringFromClass([self class]), self->path];
123 - (BOOL)appendDataToFileDesc:(int)_fd {
130 if (![[NSFileManager defaultManager] isReadableFileAtPath:self->path]) {
131 NSLog(@"ERROR[%s] missing file at path %@", __PRETTY_FUNCTION__,
136 fileLen = [self length];
138 fs = [NGFileStream alloc]; /* to keep gcc 3.4 happy */
139 fs = [fs initWithPath:self->path];
141 if (![fs openInMode:@"r"]) {
142 NSLog(@"%s: could not open file stream ... %@",
143 __PRETTY_FUNCTION__, self->path);
144 [fs release]; fs = nil;
154 read = (bufCnt > (fileLen - alreadyRead)) ? fileLen - alreadyRead : bufCnt;
156 while ((read = [fs readBytes:buffer count:read])) {
158 if (write(_fd, buffer, read) != read) {
159 fprintf(stderr, "%s: Failed to write %i bytes to file\n",
160 __PRETTY_FUNCTION__, read);
164 if (alreadyRead == fileLen)
169 printf("got exceptions %s\n", [[localException description] cString]);
170 if (![localException isKindOfClass:[NGEndOfStreamException class]]) {
171 [fs release]; fs = nil;
176 [fs release]; fs = nil;
180 @end /* NGMimeFileData */