| 1 |
/* |
| 2 |
** CWPOP3CacheManager.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 |
** Copyright (C) 2014-2020 Riccardo Mottola |
| 6 |
** |
| 7 |
** Author: Ludovic Marcotte <[email protected]> |
| 8 |
** Riccardo Mottola <[email protected]> |
| 9 |
** |
| 10 |
** This library is free software; you can redistribute it and/or |
| 11 |
** modify it under the terms of the GNU Lesser General Public |
| 12 |
** License as published by the Free Software Foundation; either |
| 13 |
** version 2.1 of the License, or (at your option) any later version. |
| 14 |
** |
| 15 |
** This library is distributed in the hope that it will be useful, |
| 16 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
** Lesser General Public License for more details. |
| 19 |
** |
| 20 |
** You should have received a copy of the GNU General Public License |
| 21 |
** along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#import <Pantomime/CWPOP3CacheManager.h> |
| 25 |
|
| 26 |
#import <Pantomime/io.h> |
| 27 |
#import <Pantomime/CWConstants.h> |
| 28 |
#import <Pantomime/CWPOP3CacheObject.h> |
| 29 |
|
| 30 |
#import <Foundation/NSArchiver.h> |
| 31 |
#import <Foundation/NSAutoreleasePool.h> |
| 32 |
#import <Foundation/NSData.h> |
| 33 |
#import <Foundation/NSException.h> |
| 34 |
#import <Foundation/NSFileManager.h> |
| 35 |
|
| 36 |
#include <stdlib.h> |
| 37 |
#include <sys/types.h> |
| 38 |
#include <sys/stat.h> |
| 39 |
#include <fcntl.h> |
| 40 |
#include <errno.h> |
| 41 |
#include <unistd.h> |
| 42 |
#include <netinet/in.h> |
| 43 |
|
| 44 |
static unsigned short version = 1; |
| 45 |
|
| 46 |
// |
| 47 |
// |
| 48 |
// |
| 49 |
@implementation CWPOP3CacheManager |
| 50 |
|
| 51 |
- (id) initWithPath: (NSString *) thePath |
| 52 |
{ |
| 53 |
NSDictionary *attributes; |
| 54 |
unsigned short int v; |
| 55 |
|
| 56 |
self = [super initWithPath: thePath]; |
| 57 |
if (self) |
| 58 |
{ |
| 59 |
|
| 60 |
_table = NSCreateMapTable(NSObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 128); |
| 61 |
_count = 0; |
| 62 |
|
| 63 |
if ((_fd = open([thePath UTF8String], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) |
| 64 |
{ |
| 65 |
NSLog(@"CANNOT CREATE OR OPEN THE CACHE!)"); |
| 66 |
abort(); |
| 67 |
} |
| 68 |
|
| 69 |
if (lseek(_fd, 0L, SEEK_SET) < 0) |
| 70 |
{ |
| 71 |
NSLog(@"UNABLE TO LSEEK INITIAL"); |
| 72 |
abort(); |
| 73 |
} |
| 74 |
|
| 75 |
attributes = [[NSFileManager defaultManager] fileAttributesAtPath: thePath traverseLink: NO]; |
| 76 |
|
| 77 |
// If the cache exists, lets parse it. |
| 78 |
if ([[attributes objectForKey: NSFileSize] intValue]) |
| 79 |
{ |
| 80 |
NSString *aUID; |
| 81 |
NSDate *aDate; |
| 82 |
|
| 83 |
unsigned short len; |
| 84 |
char *s; |
| 85 |
NSUInteger i; |
| 86 |
|
| 87 |
v = read_uint16(_fd); |
| 88 |
|
| 89 |
// Version mismatch. We ignore the cache for now. |
| 90 |
if (v != version) |
| 91 |
{ |
| 92 |
if (ftruncate(_fd, 0) == -1) |
| 93 |
{ |
| 94 |
|
| 95 |
if (errno == EACCES || errno == EROFS) |
| 96 |
NSLog(@"UNABLE TO TRUNCATE CACHE FILE WITH OLD VERSION, NOT WRITABLE"); |
| 97 |
else |
| 98 |
NSLog(@"UNABLE TO TRUNCATE CACHE FILE WITH OLD VERSION"); |
| 99 |
close(_fd); |
| 100 |
abort(); |
| 101 |
} |
| 102 |
[self synchronize]; |
| 103 |
return self; |
| 104 |
} |
| 105 |
|
| 106 |
if(read_uint32(_fd, &_count) <= 0) |
| 107 |
{ |
| 108 |
NSLog(@"CWPOP3CacheManager initWithPath: error reading count"); |
| 109 |
} |
| 110 |
|
| 111 |
//NSLog(@"Init with count = %d version = %d", _count, v); |
| 112 |
|
| 113 |
s = (char *)malloc(4096); |
| 114 |
|
| 115 |
for (i = 0; i < _count; i++) |
| 116 |
{ |
| 117 |
unsigned int dateInt; |
| 118 |
|
| 119 |
read_uint32(_fd, &dateInt); |
| 120 |
aDate = [NSCalendarDate dateWithTimeIntervalSince1970: dateInt]; |
| 121 |
if (read_string(_fd, s, &len) < 0) |
| 122 |
{ |
| 123 |
NSLog(@"CWPOP3CacheManager initWithPath: error reading data (%lu/%lu)", (unsigned long)i, (unsigned long)_count); |
| 124 |
break; |
| 125 |
} |
| 126 |
// FIXME, this could use read_data |
| 127 |
aUID = AUTORELEASE([[NSString alloc] initWithData: [NSData dataWithBytes: s length: len] |
| 128 |
encoding: NSASCIIStringEncoding]); |
| 129 |
NSMapInsert(_table, aUID, aDate); |
| 130 |
} |
| 131 |
|
| 132 |
free(s); |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
[self synchronize]; |
| 137 |
} |
| 138 |
} |
| 139 |
return self; |
| 140 |
} |
| 141 |
|
| 142 |
// |
| 143 |
// |
| 144 |
// |
| 145 |
- (void) dealloc |
| 146 |
{ |
| 147 |
//NSLog(@"CWPOP3CacheManager: -dealloc, _fd was = %d", _fd); |
| 148 |
|
| 149 |
NSFreeMapTable(_table); |
| 150 |
if (_fd >= 0) close(_fd); |
| 151 |
[super dealloc]; |
| 152 |
} |
| 153 |
|
| 154 |
// |
| 155 |
// |
| 156 |
// |
| 157 |
- (NSCalendarDate *) dateForUID: (NSString *) theUID |
| 158 |
{ |
| 159 |
return NSMapGet(_table, theUID); |
| 160 |
} |
| 161 |
|
| 162 |
// |
| 163 |
// |
| 164 |
// |
| 165 |
- (BOOL) synchronize |
| 166 |
{ |
| 167 |
if (lseek(_fd, 0L, SEEK_SET) < 0) |
| 168 |
{ |
| 169 |
NSLog(@"fseek failed"); |
| 170 |
abort(); |
| 171 |
return NO; |
| 172 |
} |
| 173 |
|
| 174 |
// We write our cache version, count and UID validity. |
| 175 |
write_uint16(_fd, version); |
| 176 |
write_uint32(_fd, _count); |
| 177 |
|
| 178 |
return (fsync(_fd) == 0); |
| 179 |
} |
| 180 |
|
| 181 |
// |
| 182 |
// |
| 183 |
// |
| 184 |
- (void) expunge |
| 185 |
{ |
| 186 |
NSLog(@"Warning! POP3 Cache has no expunge implementation"); |
| 187 |
} |
| 188 |
|
| 189 |
// |
| 190 |
// |
| 191 |
// |
| 192 |
- (void) writeRecord: (cache_record *) theRecord |
| 193 |
{ |
| 194 |
NSData *aData; |
| 195 |
|
| 196 |
// We do NOT write a record we already have in our cache. |
| 197 |
// Some POP3 servers, like popa3d, might return the same UID |
| 198 |
// for messages at different index but with the same content. |
| 199 |
// If that happens, we just don't write that value in our cache. |
| 200 |
if (NSMapGet(_table, theRecord->pop3_uid)) |
| 201 |
{ |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
if (lseek(_fd, 0L, SEEK_END) < 0) |
| 206 |
{ |
| 207 |
NSLog(@"COULD NOT LSEEK TO END OF FILE"); |
| 208 |
abort(); |
| 209 |
} |
| 210 |
|
| 211 |
write_uint32(_fd, theRecord->date); |
| 212 |
|
| 213 |
aData = [theRecord->pop3_uid dataUsingEncoding: NSASCIIStringEncoding]; |
| 214 |
write_data(_fd, aData); |
| 215 |
|
| 216 |
|
| 217 |
NSMapInsert(_table, theRecord->pop3_uid, [NSCalendarDate dateWithTimeIntervalSince1970: theRecord->date]); |
| 218 |
_count++; |
| 219 |
} |
| 220 |
|
| 221 |
@end |