File: CWLocalCacheManager.m

package info (click to toggle)
pantomime1.2 1.2.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,012 kB
  • ctags: 398
  • sloc: objc: 21,201; ansic: 510; cpp: 22; makefile: 21; sh: 4
file content (689 lines) | stat: -rw-r--r-- 19,931 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
**  CWLocalCacheManager.m
**
**  Copyright (c) 2001-2007
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**          Riccardo Mottola <rm@gnu.org>
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**  
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**  Lesser General Public License for more details.
**  
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#import <Pantomime/CWLocalCacheManager.h>

#include <Pantomime/io.h>
#include <Pantomime/CWConstants.h>
#include <Pantomime/CWFlags.h>
#include <Pantomime/CWLocalFolder.h>
#include <Pantomime/CWLocalMessage.h>
#include <Pantomime/CWParser.h>
#include <Pantomime/NSData+Extensions.h>

#include <Foundation/NSArchiver.h>
#include <Foundation/NSException.h>
#include <Foundation/NSFileManager.h>
#include <Foundation/NSNull.h>
#include <Foundation/NSValue.h>

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>  // For open() and friends.
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>     // For lseek()
#include <netinet/in.h> // For ntohl()

static unsigned short version = 1;

//
// Cache structure:
//
// Start   length Description
// 
// 0       2      Cache version
// 2       4      Number of cache entries
// 6       4      Modification date of the underlying mbox file
//                Modification of the underlying cur/ directory for maildir
// [10]    4      File size of the underlying mbox file. This entry does NOT exist for maildir cache.
// 14+/10+        Beginning of the first cache entry
// 
// 0       4      Record length, including this field. The record consist of cached message headers / attributes.
// 4       4      Flags
// 8       4      Date
// 12      4+     Position for mbox / Filename for maildir
// 16+     4      Size
//
//
@implementation CWLocalCacheManager

//
//
//
- (id) initWithPath: (NSString *) thePath  folder: (id) theFolder
{
  NSDictionary *attributes;
  unsigned int d, s, c;
  unsigned short int v;
  BOOL broken;

  self = [super initWithPath: thePath];

  // We get the attributes of the mailbox
  if ([theFolder type] == PantomimeFormatMbox)
    {
      attributes = [[NSFileManager defaultManager] fileAttributesAtPath: [theFolder path]  traverseLink: NO];
    }
  else
    {
      attributes = [[NSFileManager defaultManager] fileAttributesAtPath: [NSString stringWithFormat: @"%@/cur", [theFolder path]]
						   traverseLink: NO];
    }

  d = [[attributes objectForKey: NSFileModificationDate] timeIntervalSince1970];
  s = [[attributes objectForKey: NSFileSize] intValue];
  broken = NO;

  // We get the attribtes of the cache
  attributes = [[NSFileManager defaultManager] fileAttributesAtPath: thePath  traverseLink: NO];

  _folder = theFolder;
  _count = _modification_date = 0;

  if ((_fd = open([thePath UTF8String], O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) < 0) 
    {
      AUTORELEASE(self);
      return nil;
    }

  if (lseek(_fd, 0L, SEEK_SET) < 0)
    {
      AUTORELEASE(self);
      return nil;
    }
  
  // If the cache exists, lets parse it.
  if ([[attributes objectForKey: NSFileSize] intValue])
    {
      unsigned int i;

      v = read_unsigned_short(_fd);

      // HACK: We IGNORE all the previous cache.
      if (v != version)
	{
	  //NSLog(@"Ignoring the old cache format.");
	  if (ftruncate(_fd, 0) == -1)
            {

              if (errno == EACCES || errno == EROFS)
                NSLog(@"UNABLE TO TRUNCATE CACHE FILE WITH OLD VERSION, NOT WRITABLE");
              else
                NSLog(@"UNABLE TO TRUNCATE CACHE FILE WITH OLD VERSION");
              close(_fd);
              abort();
            }
	  [self synchronize];
	  return self;
	}

      _count = read_unsigned_int(_fd);
      _modification_date = read_unsigned_int(_fd);

      if ([(CWLocalFolder *)_folder type] == PantomimeFormatMbox)
	{
	  _size = read_unsigned_int(_fd);
	  
	  if (s != _size || d != _modification_date) broken = YES;
	}
      else
	{
	  //NSLog(@"Asking enumerator...");
	  c = [[[[NSFileManager defaultManager] enumeratorAtPath: [NSString stringWithFormat: @"%@/cur/", [theFolder path]]] allObjects] count];
	  //NSLog(@"Done! count = %d", c);
	  
	  if (c != _count || d != _modification_date) broken = YES;
	}
 
      if (broken)
	{
	  //NSLog(@"Broken cache, we must invalidate.");
	  _count = _size = 0;
	  	  if (ftruncate(_fd, 0) == -1)
            {

              if (errno == EACCES || errno == EROFS)
                NSLog(@"UNABLE TO TRUNCATE BROKEN CACHE FILE, NOT WRITABLE");
              else
                NSLog(@"UNABLE TO TRUNCATE BROKEN CACHE FILE");
              close(_fd);
              abort();
            }
	  [self synchronize];
	  return self;
	}
      
      //NSLog(@"Version = %i  date  = %d  size = %d count = %d", v, d, _size, _count);

      for (i = 0; i < _count; i++)
	{
	  [((CWFolder *)_folder)->allMessages addObject: AUTORELEASE([[CWLocalMessage alloc] init])];
	}
    }
  else
    {
      [self synchronize];
    }

  return self;
}

//
//
//
- (void) dealloc
{
  //NSLog(@"CWLocalCacheManager: -dealloc");
  
  if (_fd >= 0) close(_fd);

  [super dealloc];
}


//
// If this method is invoked on already initialized messages, we use
// the file position or filename field in order to determine if we already
// initialized or not the message.
//
- (void) initInRange: (NSRange) theRange
{
  CWLocalMessage *aMessage;
  CWFlags *theFlags;
  
  unsigned short int len, tot;
  unsigned char *r, *s;
  int begin, end, i;
  BOOL b;

  begin = theRange.location;
  end = (NSMaxRange(theRange) <= _count ? NSMaxRange(theRange) : _count);

  if (lseek(_fd, ([(CWLocalFolder *)_folder type] == PantomimeFormatMbox) ? 14L : 10L, SEEK_SET) < 0)
    {
      NSLog(@"lseek failed in initInRange:");
      abort();
    }
  
  //NSLog(@"init from %d to %d, count = %d, size of char %d", begin, end, _count, sizeof(char));

  s = (unsigned char *)malloc(65536);

  // We MUST skip the last few bytes...
  for (i = begin; i < end ; i++)
    {
      // aMessage = [[CWLocalMessage alloc] init];
      aMessage = [((CWFolder *)_folder)->allMessages objectAtIndex: i];
      [aMessage setFolder: _folder];
      [aMessage setMessageNumber: i+1];
      b = NO;

      // With read: 1.157
      // With read+malloc+free: 1.201
      // Without read, malloc, free + in-memory parsing: .847
      // Inline: .892 (not worth it)
      // With 'static' buffer: .839 (not worth it)

      // We parse the record length, date, flags, position in file and the size.
      len = read_unsigned_int(_fd);

      r = (unsigned char *)malloc(len-4);
      
      if (read(_fd, r, len-4) < 0) { NSLog(@"read failed"); abort(); }

      theFlags = AUTORELEASE([[CWFlags alloc] initWithFlags: read_unsigned_int_memory(r)]);
      [aMessage setReceivedDate: [NSCalendarDate dateWithTimeIntervalSince1970: read_unsigned_int_memory(r+4)]];

      if ([(CWLocalFolder *)_folder type] == PantomimeFormatMbox)
	{
	  if ([aMessage filePosition] == 0)
	    {
	      [aMessage setFilePosition: read_unsigned_int_memory(r+8)];
	      [aMessage setSize: read_unsigned_int_memory(r+12)];
	      b = YES;
	    }
	  tot = 16;
	}
      else
	{
	  read_string_memory(r+8, s, &len);
	  if (![aMessage filename])
	    {
	      [aMessage setMailFilename: [NSString stringWithUTF8String: (const char *)s]];
	      [aMessage setSize: read_unsigned_int_memory(r+len+10)];
	      b = YES;
	    }
	  tot = len+14;
	}

      if (!b)
	{
	  free(r);
	  continue;
	}

      // We set the flags, only if we need to as they might have
      // changed since the last time this method was called.
      [aMessage setFlags: theFlags];

      // .209ms
      read_string_memory(r+tot, s, &len);
      [CWParser parseFrom: [NSData dataWithBytes: s  length: len]  inMessage: aMessage  quick: YES];
      tot += len+2;
     
      // .462ms (+253ms), .217ms
      read_string_memory(r+tot, s, &len);
      [CWParser parseInReplyTo: [NSData dataWithBytes: s  length: len]  inMessage: aMessage  quick: YES];
      tot += len+2;
      
      // .468ms (+4ms)
      read_string_memory(r+tot, s, &len);
      [CWParser parseMessageID: [NSData dataWithBytes: s  length: len]  inMessage: aMessage  quick: YES];
      tot += len+2;

      // .560ms (+92ms)
      read_string_memory(r+tot, s, &len);
      [CWParser parseReferences: [NSData dataWithBytes: s  length: len]  inMessage: aMessage  quick: YES];
      tot += len+2;

      // .731ms (+171ms), .135ms
      read_string_memory(r+tot, s, &len);
      [CWParser parseSubject:  [NSData dataWithBytes: s  length: len]  inMessage: aMessage  quick: YES];
      tot += len+2;
      
      // .823ms (+92ms), .80ms
      read_string_memory(r+tot, s, &len);
      [CWParser parseDestination: [NSData dataWithBytes: s  length: len]
		forType: PantomimeToRecipient
		inMessage: aMessage
		quick: YES];
      tot += len+2;
      
      // .948ms (+125 to +163ms)
      read_string_memory(r+tot, s, &len);
      [CWParser parseDestination: [NSData dataWithBytes: s  length: len]
		forType: PantomimeCcRecipient
		inMessage: aMessage
		quick: YES];

      free(r);
    }

  free(s);
}

//
// access/mutation methods
//
- (NSDate *) modificationDate
{
  return [NSDate dateWithTimeIntervalSince1970: _modification_date];
}

- (void) setModificationDate: (NSDate *) theDate
{
  _modification_date = [theDate timeIntervalSince1970];
}

//
//
//
- (unsigned int) fileSize
{
  return _size;
}

- (void) setFileSize: (unsigned int) theSize
{
  _size = theSize;
}

//
//
//
- (BOOL) synchronize
{
  NSDictionary *attributes; 
  CWLocalMessage *aMessage;
  unsigned int len, flags;
  int i;

  if ([(CWLocalFolder *)_folder type] == PantomimeFormatMbox)
    {
      attributes = [[NSFileManager defaultManager] fileAttributesAtPath: [(CWLocalFolder *)_folder path]
						   traverseLink: NO];
    }
  else
    {
      attributes = [[NSFileManager defaultManager] fileAttributesAtPath: [NSString stringWithFormat: @"%@/cur", [(CWLocalFolder *)_folder path]]
						   traverseLink: NO];
    }
  
  _modification_date = [[attributes objectForKey: NSFileModificationDate] timeIntervalSince1970];
  _count = [_folder->allMessages count];

  if (lseek(_fd, 0L, SEEK_SET) < 0)
    {
      NSLog(@"fseek failed");
    }
  
  // We write our cache version, count, modification date and size.
  write_unsigned_short(_fd, version);
  write_unsigned_int(_fd, _count);
  write_unsigned_int(_fd, _modification_date);

  if ([(CWLocalFolder *)_folder type] == PantomimeFormatMbox)
    {
      _size = [[attributes objectForKey: NSFileSize] intValue];
      write_unsigned_int(_fd, _size);
    }

  // We now update the message flags
  //NSLog(@"Synching flags for mailbox %@, count = %d", [(CWLocalFolder *)_folder path], _count);
  for (i = 0; i < _count; i++)
    {
      len = read_unsigned_int(_fd);
      //NSLog(@"len = %d", len);

      if ((NSNull *)(aMessage = [_folder->allMessages objectAtIndex: i]) != [NSNull null])
	{
	  flags = ((CWFlags *)[aMessage flags])->flags;
	  write_unsigned_int(_fd, flags);
	  lseek(_fd, (len-8), SEEK_CUR);
	  //NSLog(@"wrote = %d", flags);
	}
      else
	{
	  lseek(_fd, (len-4), SEEK_CUR);
	}
    }
  //NSLog(@"Done!");
 
  return (fsync(_fd) == 0);
}


//
//
//
- (NSUInteger) count
{
  return _count;
}

//
//
//
- (void) writeRecord: (cache_record *) theRecord
{
  unsigned int len;

  if (lseek(_fd, 0L, SEEK_END) < 0)
    {
      NSLog(@"COULD NOT LSEEK TO END OF FILE");
      abort();
    }
  
  // We calculate the length of this record (including the
  // first five fields, which is 20 bytes long and is added
  // at the very end)
  len = 0;
  len += [theRecord->from length]+2;
  len += [theRecord->in_reply_to length]+2;
  len += [theRecord->message_id length]+2;
  len += [theRecord->references length]+2;
  len += [theRecord->subject length]+2;
  len += [theRecord->to length]+2;
  len += [theRecord->cc length]+2;

  if ([(CWLocalFolder *)_folder type] == PantomimeFormatMaildir)
    {
      len += strlen(theRecord->filename)+2;
      len += 16;
    }
  else
    {
      len += 20;
    }

  // We write the length of our entry
  write_unsigned_int(_fd, len);

  // We write the flags, date, position and the size of the message.
  write_unsigned_int(_fd, theRecord->flags);
  write_unsigned_int(_fd, theRecord->date);

  if ([(CWLocalFolder *)_folder type] == PantomimeFormatMbox)
    {
      write_unsigned_int(_fd, theRecord->position);
    }
  else
    {
      write_string(_fd, (unsigned char *)theRecord->filename, strlen(theRecord->filename));
    }
  
  write_unsigned_int(_fd, theRecord->size);
  
  // We write the read of our cached headers (From, In-Reply-To, Message-ID, References, Subject and To)
  write_string(_fd, (unsigned char *)[theRecord->from bytes], [theRecord->from length]);
  write_string(_fd, (unsigned char *)[theRecord->in_reply_to bytes], [theRecord->in_reply_to length]);
  write_string(_fd, (unsigned char *)[theRecord->message_id bytes], [theRecord->message_id length]);
  write_string(_fd, (unsigned char *)[theRecord->references bytes], [theRecord->references length]);
  write_string(_fd, (unsigned char *)[theRecord->subject bytes], [theRecord->subject length]);
  write_string(_fd, (unsigned char *)[theRecord->to bytes], [theRecord->to length]);
  write_string(_fd, (unsigned char *)[theRecord->cc bytes], [theRecord->cc length]);

  _count++;
}


//
// For mbox-based and maildir-base cache:
//
// This method MUST be called after writing the new mbox
// on disk but BEFORE we actually removed the deleted
// messages from the allMessages ivar.
//
//
- (void) expunge
{
  NSDictionary *attributes;
  CWLocalMessage *aMessage;

  unsigned int cache_size, flags, i, len, total_deleted, total_length, type, v;
  short delta;
  char *buf;

  //NSLog(@"rewriting cache");

  // We get the current cache size
  cache_size = lseek(_fd, 0L, SEEK_END);

  if (lseek(_fd, ([(CWLocalFolder *)_folder type] == PantomimeFormatMbox) ? 14L : 10L, SEEK_SET) < 0)
    {
      NSLog(@"fseek failed");
      abort();
    }

  total_deleted = total_length = 0;  
  type = [(CWLocalFolder *)_folder type];
  
  //
  // We alloc a little bit more memory that we really need in
  // case we have to rewrite the filename for a maildir cache
  // and the filename length is greater than the previous one.
  //
  buf = (char *)malloc(cache_size+[_folder count]*10);
  _count = [_folder->allMessages count];

  for (i = 0; i < _count; i++)
    {
      len = read_unsigned_int(_fd);
      aMessage = [_folder->allMessages objectAtIndex: i];
      flags = ((CWFlags *)[aMessage flags])->flags;
      delta = 0;

      if ((flags&PantomimeDeleted) == PantomimeDeleted)
	{
	  // We skip over that record
	  lseek(_fd, len-4, SEEK_CUR);
	  total_deleted++;
	  //NSLog(@"Skip %d bytes, index %d!", len, i);
	}
      else
	{	  
	  //
	  // For mbox-based caches, we must update the file position of
	  // our cache entries and also the size of the message in the cache.
	  //
	  if (type == PantomimeFormatMbox)
	    {
	      // We write the rest of the record into the memory
	      if (read(_fd, (buf+total_length+4), len-4) < 0) { NSLog(@"read failed"); abort(); }

	      // We update the position in the mailbox file by
	      // overwriting the current value in memory
	      v = htonl([aMessage filePosition]);
	      memcpy((buf+total_length+12), (char *)&v, 4);
	      //NSLog(@"Wrote file position %d", ntohl(v));
	      
	      // We update the size of the message by overwriting
	      // the current value in memory
	      v = htonl([aMessage size]);
	      memcpy((buf+total_length+16), (char *)&v, 4);
	      //NSLog(@"Wrote message size %d", ntohl(v));
	    }
	  //
	  // For maildir-based caches, we must update the filename of our
	  // cache entries in case flags were flushed to the disk.
	  //
	  else
	    {
	      unsigned short c0, c1, old_len, r;
	      char *filename;
	      int s_len;

	      // We read our Flags, Date, and the first two bytes
	      // of our filename into memory.
	      if (read(_fd, (buf+total_length+4), 10) < 0) { NSLog(@"read failed"); abort(); }

	      // We read the length of our previous string
	      c0 = *(buf+total_length+12);
	      c1 = *(buf+total_length+13);
	      old_len = ntohs((c1<<8)|c0);

	      //NSLog(@"Previous length = %d  Filename = |%@|", old_len, [aMessage mailFilename]);
	      filename = (char *)[[aMessage mailFilename] UTF8String];
	      s_len = strlen(filename);
	      delta = s_len-old_len;
	      
	      //if (delta != 0) NSLog(@"i = %d  delta = %d |%@| s_len = %d", i, delta, [aMessage mailFilename], s_len);
	      
	      // We write back our filename
	      r = htons(s_len);
	      memcpy((buf+total_length+12), (char *)&r, 2);
	      memcpy((buf+total_length+14), filename, s_len);

	      // We read the rest in our memory. We first skip or old filename string.
	      if (lseek(_fd, old_len, SEEK_CUR) < 0) { NSLog(@"lseek failed"); abort(); }
	      //NSLog(@"must read back into memory %d bytes", len-old_len-14);
	      if (read(_fd, (buf+total_length+s_len+14), len-old_len-14) < 0) { NSLog(@"read failed"); abort(); }
	      //NSLog(@"current file pos after full read %d", lseek(_fd, 0L, SEEK_CUR));
	    }

	  // We write back our record length, adjusting its size if we need
	  // to, in the case we are handling a maildir-based cache.
	  len += delta;
	  v = htonl(len);
	  memcpy((buf+total_length), (char *)&v, 4);

	  total_length += len;
	  //NSLog(@"_size = %d  total_length = %d", _size, total_length);
	}
    }

  if (lseek(_fd, 0L, SEEK_SET) < 0)
    {
      NSLog(@"fseek failed");
    }

  // We write our cache version, count, modification date our new size
  cache_size = total_length+([(CWLocalFolder *)_folder type] == PantomimeFormatMbox ? 14 : 10);
  _count -= total_deleted;

  write_unsigned_short(_fd, version);
  write_unsigned_int(_fd, _count);

  if ([(CWLocalFolder *)_folder type] == PantomimeFormatMbox)
      {
	attributes = [[NSFileManager defaultManager] fileAttributesAtPath: [(CWLocalFolder *)_folder path]
						     traverseLink: NO];
	
	_modification_date = [[attributes objectForKey: NSFileModificationDate] timeIntervalSince1970];
	_size = [[attributes objectForKey: NSFileSize] intValue];
	write_unsigned_int(_fd, _modification_date);
	write_unsigned_int(_fd, _size);
      }
  else
    {
      attributes = [[NSFileManager defaultManager] fileAttributesAtPath: [NSString stringWithFormat: @"%@/cur", [(CWLocalFolder *)_folder path]]
						   traverseLink: NO];
      _modification_date = [[attributes objectForKey: NSFileModificationDate] timeIntervalSince1970];
      _size = 0;
      write_unsigned_int(_fd, _modification_date);
    }
  
  // We write our memory cache
    if (write(_fd, buf, total_length) != total_length)
    {
      if (errno == EAGAIN)
        {
          // Perhaps we could handle this more gracefully?
          NSLog(@"EXPUNGE CACHE: WRITE OUT ERROR, EAGAIN");
        }
      else
        {
          NSLog(@"EXPUNGE CACHE: WRITE OUT INCOMPLETE");
        }
      abort();
    }

  //ftruncate(_fd, _size);
  if (ftruncate(_fd, cache_size) == -1)
    {
      
      if (errno == EACCES || errno == EROFS)
        NSLog(@"UNABLE TO EXPUNGE CACHE, NOT WRITABLE");
      else if (errno == EFBIG)
        NSLog(@"UNABLE TO EXPUNGE CACHE, EFBIG");
      else
        NSLog(@"UNABLE TO EXPUNGE CACHE");
      abort();
    }
  free(buf);

  //NSLog(@"Done!");
}
@end