2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
6 SOPE 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 SOPE 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 SOPE; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 #include "NGMimeFileData.h"
25 #include <sys/types.h>
29 @implementation NGMimeFileData
31 static NSString *TmpPath = nil;
32 static NSProcessInfo *Pi = nil;
33 static unsigned tmpmask = 0600;
36 NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
39 TmpPath = [ud stringForKey:@"NGMimeBuildMimeTempDirectory"];
40 if (TmpPath == nil) TmpPath = @"/tmp/";
41 TmpPath = [[TmpPath stringByAppendingPathComponent:@"OGo"] copy];
43 if (Pi == nil) Pi = [[NSProcessInfo processInfo] retain];
46 - (id)initWithPath:(NSString *)_path removeFile:(BOOL)_remove {
47 if ((self = [super init])) {
48 if (![[NSFileManager defaultManager] fileExistsAtPath:_path]) {
49 NSLog(@"ERROR[%s]: missing file at path %@", __PRETTY_FUNCTION__, _path);
53 self->path = [_path copy];
54 self->removeFile = _remove;
60 - (id)initWithBytes:(const void*)_bytes
61 length:(unsigned int)_length
63 NSString *filename = nil;
66 filename = [Pi temporaryFileName:TmpPath];
68 fd = open([filename fileSystemRepresentation],
69 O_WRONLY | O_CREAT | O_TRUNC, tmpmask);
71 fprintf(stderr, "Could not open file for writing %s: %s\n",
72 [filename fileSystemRepresentation], strerror(errno));
76 if (write(fd, _bytes, _length) != (int)_length) {
77 fprintf(stderr, "Failed to write %i bytes to %s: %s\n",
78 _length, [filename fileSystemRepresentation], strerror(errno));
83 return [self initWithPath:filename removeFile:YES];
87 if (self->removeFile) {
88 [[NSFileManager defaultManager]
89 removeFileAtPath:self->path handler:nil];
96 return [NSData dataWithContentsOfMappedFile:self->path];
99 - (id)copyWithZone:(NSZone *)zone {
100 return [self retain];
103 - (const void*)bytes {
104 return [[self _data] bytes];
107 - (unsigned int)length {
108 if (self->length == -1) {
109 self->length = [[[[NSFileManager defaultManager]
110 fileAttributesAtPath:self->path
112 objectForKey:NSFileSize] intValue];
117 - (BOOL)appendDataToFileDesc:(int)_fd {
124 if (![[NSFileManager defaultManager] isReadableFileAtPath:self->path]) {
125 NSLog(@"ERROR[%s] missing file at path %@", __PRETTY_FUNCTION__,
130 fileLen = [self length];
132 fs = [NGFileStream alloc]; /* to keep gcc 3.4 happy */
133 fs = [fs initWithPath:self->path];
135 if (![fs openInMode:@"r"]) {
136 NSLog(@"%s: could not open file stream ... %@",
137 __PRETTY_FUNCTION__, self->path);
138 [fs release]; fs = nil;
148 read = (bufCnt > (fileLen - alreadyRead)) ? fileLen - alreadyRead : bufCnt;
150 while ((read = [fs readBytes:buffer count:read])) {
152 if (write(_fd, buffer, read) != read) {
153 fprintf(stderr, "%s: Failed to write %i bytes to file\n",
154 __PRETTY_FUNCTION__, read);
158 if (alreadyRead == fileLen)
163 printf("got exceptions %s\n", [[localException description] cString]);
164 if (![localException isKindOfClass:[NGEndOfStreamException class]]) {
165 [fs release]; fs = nil;
170 [fs release]; fs = nil;
176 - (NSString *)description {
177 return [NSString stringWithFormat:@"<0x%08X[%@]: path=%@>",
178 self, NSStringFromClass([self class]), self->path];
181 @end /* NGMimeFileData */