| 1 |
/* |
| 2 |
** CWIMAPCacheManager.m |
| 3 |
** |
| 4 |
** Copyright (c) 2001-2007 Ludovic Marcotte |
| 5 |
** Copyright (C) 2013-2018 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/CWIMAPCacheManager.h> |
| 25 |
|
| 26 |
#import <Pantomime/io.h> |
| 27 |
#import <Pantomime/CWConstants.h> |
| 28 |
#import <Pantomime/CWFlags.h> |
| 29 |
#import <Pantomime/CWFolder.h> |
| 30 |
#import <Pantomime/CWIMAPMessage.h> |
| 31 |
|
| 32 |
#import <Foundation/NSArchiver.h> |
| 33 |
#import <Foundation/NSAutoreleasePool.h> |
| 34 |
#import <Foundation/NSException.h> |
| 35 |
#import <Foundation/NSFileManager.h> |
| 36 |
#import <Foundation/NSValue.h> |
| 37 |
|
| 38 |
#include <stdlib.h> |
| 39 |
#include <string.h> |
| 40 |
#include <sys/types.h> |
| 41 |
#include <sys/stat.h> |
| 42 |
#include <fcntl.h> |
| 43 |
#include <unistd.h> |
| 44 |
#include <netinet/in.h> |
| 45 |
#include <errno.h> |
| 46 |
#include <unistd.h> |
| 47 |
|
| 48 |
static unsigned short version = 1; |
| 49 |
|
| 50 |
// |
| 51 |
// |
| 52 |
// |
| 53 |
@implementation CWIMAPCacheManager |
| 54 |
|
| 55 |
- (id) initWithPath: (NSString *) thePath folder: (CWFolder *) theFolder |
| 56 |
{ |
| 57 |
NSDictionary *attributes; |
| 58 |
unsigned short int v; |
| 59 |
|
| 60 |
self = [super initWithPath: thePath]; |
| 61 |
if (self) |
| 62 |
{ |
| 63 |
uint32_t count; |
| 64 |
|
| 65 |
_table = NSCreateMapTable(NSIntMapKeyCallBacks, NSObjectMapValueCallBacks, 128); |
| 66 |
_count = _UIDValidity = 0; |
| 67 |
_folder = theFolder; |
| 68 |
|
| 69 |
|
| 70 |
if ((_fd = open([thePath UTF8String], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) |
| 71 |
{ |
| 72 |
NSLog(@"CANNOT CREATE OR OPEN THE CACHE!)"); |
| 73 |
abort(); |
| 74 |
} |
| 75 |
|
| 76 |
if (lseek(_fd, 0L, SEEK_SET) < 0) |
| 77 |
{ |
| 78 |
close(_fd); |
| 79 |
NSLog(@"UNABLE TO LSEEK INITIAL"); |
| 80 |
abort(); |
| 81 |
} |
| 82 |
|
| 83 |
attributes = [[NSFileManager defaultManager] fileAttributesAtPath: thePath traverseLink: NO]; |
| 84 |
|
| 85 |
// If the cache exists, lets parse it. |
| 86 |
if ([[attributes objectForKey: NSFileSize] intValue]) |
| 87 |
{ |
| 88 |
v = read_uint16(_fd); |
| 89 |
|
| 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(@"CWIMAPCacheManger initWithPath: Failed to read count"); |
| 109 |
abort(); |
| 110 |
} |
| 111 |
_count = (NSUInteger)count; |
| 112 |
if (read_uint32(_fd, &_UIDValidity) <= 0) |
| 113 |
{ |
| 114 |
NSLog(@"CWIMAPCacheManger initWithPath: Failed to read UIDValidity"); |
| 115 |
abort(); |
| 116 |
} |
| 117 |
} |
| 118 |
else |
| 119 |
{ |
| 120 |
[self synchronize]; |
| 121 |
} |
| 122 |
} |
| 123 |
return self; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
// |
| 128 |
// |
| 129 |
// |
| 130 |
- (void) dealloc |
| 131 |
{ |
| 132 |
//NSLog(@"CWIMAPCacheManager: -dealloc"); |
| 133 |
|
| 134 |
NSFreeMapTable(_table); |
| 135 |
if (_fd >= 0) close(_fd); |
| 136 |
|
| 137 |
[super dealloc]; |
| 138 |
} |
| 139 |
|
| 140 |
// |
| 141 |
// |
| 142 |
// |
| 143 |
- (void) readAllMessages |
| 144 |
{ |
| 145 |
[self readMessagesInRange:NSMakeRange(0, NSUIntegerMax)]; |
| 146 |
} |
| 147 |
|
| 148 |
// |
| 149 |
// |
| 150 |
// |
| 151 |
- (void) readMessagesInRange: (NSRange) theRange |
| 152 |
{ |
| 153 |
NSAutoreleasePool *pool; |
| 154 |
CWIMAPMessage *aMessage; |
| 155 |
unsigned int tot; |
| 156 |
NSUInteger begin, end, i; |
| 157 |
unsigned char *r; |
| 158 |
|
| 159 |
if (lseek(_fd, 10L, SEEK_SET) < 0) |
| 160 |
{ |
| 161 |
NSLog(@"lseek failed in initInRange:"); |
| 162 |
abort(); |
| 163 |
} |
| 164 |
|
| 165 |
begin = theRange.location; |
| 166 |
end = (NSMaxRange(theRange) <= _count ? NSMaxRange(theRange) : _count); |
| 167 |
|
| 168 |
//NSLog(@"init from %d to %d, count = %d, size of char %d UID validity = %d", begin, end, _count, sizeof(char), _UIDValidity); |
| 169 |
|
| 170 |
pool = [[NSAutoreleasePool alloc] init]; |
| 171 |
|
| 172 |
// We MUST skip the last few bytes... |
| 173 |
for (i = begin; i < end ; i++) |
| 174 |
{ |
| 175 |
uint16_t len; |
| 176 |
cache_record cr; |
| 177 |
unsigned int record_length; |
| 178 |
|
| 179 |
// We parse the record length, date, flags, position in file and the size. |
| 180 |
if (read_uint32(_fd, &record_length) <= 0) |
| 181 |
{ |
| 182 |
NSLog(@"CWIMAPCacheManager readMessagesInRange (%lu/%lu)", (unsigned long)i, (unsigned long)end); |
| 183 |
break; |
| 184 |
} |
| 185 |
//NSLog(@"i = %d, len = %d", i, record_length); |
| 186 |
|
| 187 |
r = (unsigned char *)malloc(record_length-4); |
| 188 |
|
| 189 |
if (r == NULL) // may be in case len was 4 |
| 190 |
{ |
| 191 |
continue; |
| 192 |
} |
| 193 |
|
| 194 |
if (read(_fd, r, record_length-4) < 0) { NSLog(@"read failed"); abort(); } |
| 195 |
|
| 196 |
cr.flags = read_uint32_memory(r); // FASTER and _RIGHT_ since we can't call -setFlags: on CWIMAPMessage |
| 197 |
cr.date = read_uint32_memory(r+4); |
| 198 |
cr.imap_uid = read_uint32_memory(r+8); |
| 199 |
cr.size = read_uint32_memory(r+12); |
| 200 |
tot = 16; |
| 201 |
|
| 202 |
|
| 203 |
cr.from = read_data_memory(r+tot, &len); |
| 204 |
tot += len+2; |
| 205 |
|
| 206 |
cr.in_reply_to = read_data_memory(r+tot, &len); |
| 207 |
tot += len+2; |
| 208 |
|
| 209 |
cr.message_id = read_data_memory(r+tot, &len); |
| 210 |
tot += len+2; |
| 211 |
|
| 212 |
cr.references = read_data_memory(r+tot, &len); |
| 213 |
tot += len+2; |
| 214 |
|
| 215 |
cr.subject = read_data_memory(r+tot, &len); |
| 216 |
tot += len+2; |
| 217 |
|
| 218 |
cr.to = read_data_memory(r+tot, &len); |
| 219 |
tot += len+2; |
| 220 |
|
| 221 |
cr.cc = read_data_memory(r+tot, &len); |
| 222 |
|
| 223 |
// zero unused fields |
| 224 |
cr.position = 0; |
| 225 |
cr.filename = NULL; |
| 226 |
cr.pop3_uid = NULL; |
| 227 |
|
| 228 |
aMessage = [[CWIMAPMessage alloc] initWithCacheRecord:cr]; |
| 229 |
[aMessage setMessageNumber: (unsigned int)i+1]; |
| 230 |
|
| 231 |
[((CWFolder *)_folder)appendMessage: aMessage]; |
| 232 |
NSMapInsert(_table, (void *)[aMessage UID], aMessage); |
| 233 |
//[self addObject: aMessage]; // MOVE TO CWFIMAPOLDER |
| 234 |
//[((CWFolder *)_folder)->allMessages replaceObjectAtIndex: i withObject: aMessage]; |
| 235 |
RELEASE(aMessage); |
| 236 |
|
| 237 |
free(r); |
| 238 |
} |
| 239 |
|
| 240 |
RELEASE(pool); |
| 241 |
} |
| 242 |
|
| 243 |
// |
| 244 |
// |
| 245 |
// |
| 246 |
- (void) removeMessageWithUID: (NSUInteger) theUID |
| 247 |
{ |
| 248 |
NSMapRemove(_table, (void *)theUID); |
| 249 |
} |
| 250 |
|
| 251 |
// |
| 252 |
// |
| 253 |
// |
| 254 |
- (CWIMAPMessage *) messageWithUID: (NSUInteger) theUID |
| 255 |
{ |
| 256 |
return NSMapGet(_table, (void *)theUID); |
| 257 |
} |
| 258 |
|
| 259 |
// |
| 260 |
// |
| 261 |
// |
| 262 |
- (unsigned int) UIDValidity |
| 263 |
{ |
| 264 |
return _UIDValidity; |
| 265 |
} |
| 266 |
|
| 267 |
// |
| 268 |
// |
| 269 |
// |
| 270 |
- (void) setUIDValidity: (unsigned int) theUIDValidity |
| 271 |
{ |
| 272 |
_UIDValidity = theUIDValidity; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
// |
| 277 |
// |
| 278 |
// |
| 279 |
- (void) invalidate |
| 280 |
{ |
| 281 |
NSDebugLog(@"IMAPCacheManager - INVALIDATING the cache..."); |
| 282 |
[super invalidate]; |
| 283 |
_UIDValidity = 0; |
| 284 |
[self synchronize]; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
// |
| 289 |
// |
| 290 |
// |
| 291 |
- (BOOL) synchronize |
| 292 |
{ |
| 293 |
unsigned int len, flags; |
| 294 |
NSUInteger i; |
| 295 |
|
| 296 |
_count = [[_folder messages] count]; |
| 297 |
|
| 298 |
//NSLog(@"CWIMAPCacheManager: -synchronize with folder count = %d", _count); |
| 299 |
|
| 300 |
if (lseek(_fd, 0L, SEEK_SET) < 0) |
| 301 |
{ |
| 302 |
NSLog(@"fseek failed"); |
| 303 |
abort(); |
| 304 |
} |
| 305 |
|
| 306 |
// We write our cache version, count and UID validity. |
| 307 |
write_uint16(_fd, version); |
| 308 |
write_uint32(_fd, (uint32_t)_count); |
| 309 |
write_uint32(_fd, _UIDValidity); |
| 310 |
|
| 311 |
//NSLog(@"Synching flags"); |
| 312 |
for (i = 0; i < _count; i++) |
| 313 |
{ |
| 314 |
read_uint32(_fd, &len); |
| 315 |
flags = ((CWFlags *)[[[_folder messages] objectAtIndex: i] flags])->flags; |
| 316 |
write_uint32(_fd, flags); |
| 317 |
lseek(_fd, (len-8), SEEK_CUR); |
| 318 |
} |
| 319 |
//NSLog(@"Done!"); |
| 320 |
|
| 321 |
return (fsync(_fd) == 0); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
// |
| 326 |
// |
| 327 |
// |
| 328 |
- (void) writeRecord: (cache_record *) theRecord message: (id) theMessage |
| 329 |
{ |
| 330 |
unsigned int len; |
| 331 |
|
| 332 |
if (lseek(_fd, 0L, SEEK_END) < 0) |
| 333 |
{ |
| 334 |
NSLog(@"COULD NOT LSEEK TO END OF FILE"); |
| 335 |
abort(); |
| 336 |
} |
| 337 |
|
| 338 |
// We calculate the length of this record (including the |
| 339 |
// first five fields, which is 20 bytes long and is added |
| 340 |
// at the very end) |
| 341 |
len = 0; |
| 342 |
len += (unsigned int)[theRecord->from length]+2; |
| 343 |
len += (unsigned int)[theRecord->in_reply_to length]+2; |
| 344 |
len += (unsigned int)[theRecord->message_id length]+2; |
| 345 |
len += (unsigned int)[theRecord->references length]+2; |
| 346 |
len += (unsigned int)[theRecord->subject length]+2; |
| 347 |
len += (unsigned int)[theRecord->to length]+2; |
| 348 |
len += (unsigned int)[theRecord->cc length]+22; |
| 349 |
write_uint32(_fd, len); |
| 350 |
|
| 351 |
// We write the flags, date, position and the size of the message. |
| 352 |
write_uint32(_fd, theRecord->flags); |
| 353 |
write_uint32(_fd, theRecord->date); |
| 354 |
write_uint32(_fd, theRecord->imap_uid); |
| 355 |
write_uint32(_fd, theRecord->size); |
| 356 |
|
| 357 |
// We write the read of our cached headers (From, In-Reply-To, Message-ID, References, |
| 358 |
// Subject, To and Cc) |
| 359 |
write_data(_fd, theRecord->from); |
| 360 |
write_data(_fd, theRecord->in_reply_to); |
| 361 |
write_data(_fd, theRecord->message_id); |
| 362 |
write_data(_fd, theRecord->references); |
| 363 |
write_data(_fd, theRecord->subject); |
| 364 |
write_data(_fd, theRecord->to); |
| 365 |
write_data(_fd, theRecord->cc); |
| 366 |
|
| •367 |
NSMapInsert(_table, (void *)(intptr_t)theRecord->imap_uid, theMessage); |
| 368 |
_count++; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
// |
| 373 |
// |
| 374 |
// |
| 375 |
- (void) expunge |
| 376 |
{ |
| 377 |
NSDictionary *attributes; |
| 378 |
|
| 379 |
off_t size; |
| 380 |
NSUInteger i; |
| 381 |
unsigned total_length; |
| 382 |
unsigned char *buf; |
| 383 |
|
| 384 |
//NSLog(@"expunge: rewriting cache"); |
| 385 |
|
| 386 |
if (lseek(_fd, 10L, SEEK_SET) < 0) |
| 387 |
{ |
| 388 |
NSLog(@"fseek failed"); |
| 389 |
if (errno == EBADF) |
| 390 |
NSLog(@"Bad file descriptor"); |
| 391 |
else if (errno == EINVAL) |
| 392 |
NSLog(@"Seek invalid"); |
| 393 |
else if (errno == EOVERFLOW) |
| 394 |
NSLog(@"Seek overflow"); |
| 395 |
abort(); |
| 396 |
} |
| 397 |
|
| 398 |
attributes = [[NSFileManager defaultManager] |
| 399 |
fileAttributesAtPath: [self path] traverseLink: NO]; |
| 400 |
|
| 401 |
buf = (unsigned char *)malloc([[attributes objectForKey: NSFileSize] |
| 402 |
unsignedLongValue]); |
| 403 |
if (buf == NULL) // nothing to do for us here |
| 404 |
return; |
| 405 |
total_length = 0; |
| 406 |
|
| 407 |
for (i = 0; i < _count; i++) |
| 408 |
{ |
| 409 |
uint32_t len; |
| 410 |
uint32_t v; |
| 411 |
|
| 412 |
//NSLog(@"==========="); |
| 413 |
if (read_uint32(_fd, &len) <= 0) |
| 414 |
{ |
| 415 |
NSLog(@"CWIMAPCacheManager expunge: error reading record length (%lu/%lu)", (unsigned long)i, (unsigned long)_count); |
| 416 |
continue; |
| 417 |
} |
| 418 |
if (len <= 4) // sanity check, we read len-4 bytes later on |
| 419 |
continue; |
| 420 |
|
| 421 |
//NSLog(@"i = %d len = %d", i, len); |
| 422 |
v = htonl(len); |
| 423 |
memcpy((buf+total_length), (char *)&v, 4); |
| 424 |
|
| 425 |
// We write the rest of the record into the memory |
| 426 |
if (read(_fd, (buf+total_length+4), len-4) < 0) |
| 427 |
{ |
| 428 |
NSLog(@"read failed"); |
| 429 |
abort(); |
| 430 |
} |
| 431 |
|
| 432 |
unsigned int uid = read_uint32_memory(buf+total_length+12); |
| 433 |
|
| 434 |
if ([self messageWithUID: uid]) |
| 435 |
{ |
| 436 |
total_length += len; |
| 437 |
} |
| 438 |
else |
| 439 |
{ |
| 440 |
//NSLog(@"Message not found! uid = %d table count = %d", |
| 441 |
// uid, NSCountMapTable(_table)); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
if (lseek(_fd, 0L, SEEK_SET) < 0) |
| 446 |
{ |
| 447 |
NSLog(@"fseek failed"); |
| 448 |
abort(); |
| 449 |
} |
| 450 |
|
| 451 |
// We write our cache version, count, modification date our new size |
| 452 |
_count = [[_folder messages] count]; |
| 453 |
size = total_length+10; |
| 454 |
|
| 455 |
write_uint16(_fd, version); |
| 456 |
write_uint32(_fd, (uint32_t)_count); |
| 457 |
write_uint32(_fd, _UIDValidity); |
| 458 |
|
| 459 |
// We write our memory cache |
| 460 |
if (write(_fd, buf, total_length) != (ssize_t) total_length) |
| 461 |
{ |
| 462 |
if (errno == EAGAIN) |
| 463 |
{ |
| 464 |
// Perhaps we could handle this more gracefully? |
| 465 |
NSLog(@"EXPUNGE CACHE: WRITE OUT ERROR, EAGAIN"); |
| 466 |
} |
| 467 |
else |
| 468 |
{ |
| 469 |
NSLog(@"EXPUNGE CACHE: WRITE OUT INCOMPLETE"); |
| 470 |
} |
| 471 |
abort(); |
| 472 |
} |
| 473 |
|
| 474 |
if (ftruncate(_fd, size) == -1) |
| 475 |
{ |
| 476 |
|
| 477 |
if (errno == EACCES || errno == EROFS) |
| 478 |
NSLog(@"UNABLE TO EXPUNGE CACHE, NOT WRITABLE"); |
| 479 |
else if (errno == EFBIG) |
| 480 |
NSLog(@"UNABLE TO EXPUNGE CACHE, EFBIG"); |
| 481 |
else |
| 482 |
NSLog(@"UNABLE TO EXPUNGE CACHE"); |
| 483 |
abort(); |
| 484 |
} |
| 485 |
free(buf); |
| 486 |
|
| 487 |
//NSLog(@"Done! New size = %d", size); |
| 488 |
} |
| 489 |
@end |