[SCM] WebKit Debian packaging branch, debian/unstable, updated. debian/1.1.15-1-40151-g37bb677

kocienda kocienda at 268f45cc-cd09-0410-ab3c-d52691b4dbfc
Sat Sep 26 06:11:45 UTC 2009


The following commit has been merged in the debian/unstable branch:
commit 6a9245ee4605fee59a8d6bbe177f82261d9e39ad
Author: kocienda <kocienda at 268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Date:   Thu May 9 16:35:30 2002 +0000

    2002-05-09  Kenneth Kocienda  <kocienda at apple.com>
    
            Reviewed by: Darin Adler
    
            Fixes for these two bugs:
    
            Radar 2859001 (Maintain usage calculation for on-disk cache)
            Radar 2859009 (On-disk cache must be able to be truncated to a specific size)
    
            Now, the on-disk database keeps track of the amount of data it stores and can enforce
            a usage quota. This new system should work quite well as long as there is a one-to-one
            mapping between processes and on-disk cache paths.
    
            Further guarantees for synchronous access to a cache from multiple processes will be
            offered if (when) we switch over to using the sleepycat Berkeley DB for the disk cache
            implementation.
    
            * CacheLoader.subproj/IFURLDiskCache.m:
            (-[IFURLDiskCache initWithSizeLimit:path:]): Added code that calls into new database methods
            (-[IFURLDiskCache dealloc]): Added missing call to [super dealloc]
            (-[IFURLDiskCache setSizeLimit:]): New
            (-[IFURLDiskCache usage]): New
            * Database.subproj/IFDatabase.h:
            * Database.subproj/IFDatabase.m:
            (-[IFDatabase initWithPath:]): Added code to track size limit and usage
            (-[IFDatabase isOpen]): Added code to track size limit and usage
            (-[IFDatabase count]): New
            (-[IFDatabase sizeLimit]): New
            (-[IFDatabase setSizeLimit:]): New
            (-[IFDatabase usage]): New
            * Database.subproj/IFNDBMDatabase.h: Removed.
            * Database.subproj/IFNDBMDatabase.m: Removed.
            * Database.subproj/IFURLFileDatabase.h:
            * Database.subproj/IFURLFileDatabase.m:
            (+[IFURLFileDatabase uniqueFilePathForKey:]): Moved to private category
            (-[IFURLFileDatabase writeSizeFile:]): New
            (-[IFURLFileDatabase readSizeFile]): New
            (-[IFURLFileDatabase truncateToSizeLimit:]): New
            (-[IFURLFileDatabase initWithPath:]): Added code to deal with size reckoning
            (-[IFURLFileDatabase removeAllObjects]): Added code to deal with size reckoning
            (-[IFURLFileDatabase performSetObject:forKey:]): Added code to deal with size reckoning
            (-[IFURLFileDatabase performRemoveObjectForKey:]): Added code to deal with size reckoning
            (-[IFURLFileDatabase open]): Added code to deal with size reckoning
            (-[IFURLFileDatabase close]): Added code to deal with size reckoning
            (-[IFURLFileDatabase sync]): Added code to deal with size reckoning
            (-[IFURLFileDatabase count]): New
            (-[IFURLFileDatabase setSizeLimit:]): New
            * WebFoundation.pbproj/project.pbxproj: PB has its way again.
    
    
    git-svn-id: http://svn.webkit.org/repository/webkit/trunk@1117 268f45cc-cd09-0410-ab3c-d52691b4dbfc

diff --git a/WebKit/Misc.subproj/WebDatabase.h b/WebKit/Misc.subproj/WebDatabase.h
index 7edcbda..872507a 100644
--- a/WebKit/Misc.subproj/WebDatabase.h
+++ b/WebKit/Misc.subproj/WebDatabase.h
@@ -10,6 +10,8 @@
     NSString *path;
     unsigned count;
     BOOL isOpen;
+    unsigned sizeLimit;
+    unsigned usage;
 }
 
 -(void)setObject:(id)object forKey:(id)key;
@@ -17,7 +19,6 @@
 -(void)removeAllObjects;
 -(id)objectForKey:(id)key;
 -(NSEnumerator *)keys;
--(unsigned)count;
 
 @end
 
@@ -38,4 +39,9 @@
 -(NSString *)path;
 -(BOOL)isOpen;
 
+-(unsigned)count;
+-(unsigned)sizeLimit;
+-(void)setSizeLimit:(unsigned)limit;
+-(unsigned)usage;
+
 @end
diff --git a/WebKit/Misc.subproj/WebDatabase.m b/WebKit/Misc.subproj/WebDatabase.m
index 45cc163..7fce7c7 100644
--- a/WebKit/Misc.subproj/WebDatabase.m
+++ b/WebKit/Misc.subproj/WebDatabase.m
@@ -54,6 +54,8 @@
     
         path = [[thePath stringByStandardizingPath] copy];
         isOpen = NO;
+        sizeLimit = 0;
+        usage = 0;
     
         return self;
     }
@@ -102,4 +104,25 @@
     return isOpen;
 }
 
+-(unsigned)count
+{
+    NSRequestConcreteImplementation(self, _cmd, [IFDatabase class]);
+    return 0;
+}
+
+-(unsigned)sizeLimit
+{
+    return sizeLimit;
+}
+
+-(void)setSizeLimit:(unsigned)limit
+{
+    NSRequestConcreteImplementation(self, _cmd, [IFDatabase class]);
+}
+
+-(unsigned)usage
+{
+    return usage;
+}
+
 @end
diff --git a/WebKit/Misc.subproj/WebFileDatabase.h b/WebKit/Misc.subproj/WebFileDatabase.h
index 1556216..a4540a0 100644
--- a/WebKit/Misc.subproj/WebFileDatabase.h
+++ b/WebKit/Misc.subproj/WebFileDatabase.h
@@ -13,7 +13,8 @@
     NSMutableSet *removeCache;
     NSTimer *timer;
     NSTimeInterval touch;
-    NSLock *mutex;
+    NSRecursiveLock *mutex;
+    char *sizeFilePath;
 }
 
 -(void)performSetObject:(id)object forKey:(id)key;
diff --git a/WebKit/Misc.subproj/WebFileDatabase.m b/WebKit/Misc.subproj/WebFileDatabase.m
index cd19a40..5bafb24 100644
--- a/WebKit/Misc.subproj/WebFileDatabase.m
+++ b/WebKit/Misc.subproj/WebFileDatabase.m
@@ -13,6 +13,8 @@
 #import "IFURLCacheLoaderConstantsPrivate.h"
 #import "WebFoundationDebug.h"
 
+#define SIZE_FILE_NAME @".size"
+
 static NSNumber *IFURLFileDirectoryPosixPermissions;
 static NSNumber *IFURLFilePosixPermissions;
 
@@ -24,6 +26,7 @@ typedef enum
 
 enum
 {
+    MAX_UNSIGNED_LENGTH = 20, // long enough to hold the string representation of a 64-bit unsigned number
     SYNC_INTERVAL = 5,
     SYNC_IDLE_THRESHOLD = 5,
 };
@@ -45,9 +48,7 @@ enum
 // implementation IFURLFileReader -------------------------------------------------------------
 
 /*
- * FIXME: This is a bad hack which really should go away.
- * Here we're using private API to hold us over until
- * this API is made public (which is planned).
+ * FIXME: [kocienda] Radar 2922673 (Remove use of private NSData interface in IFURLFileDatabase.m)
  */
 @interface NSData (IFExtensions)
 - (id)initWithBytes:(void *)bytes length:(unsigned)length copy:(BOOL)copy freeWhenDone:(BOOL)freeBytes bytesAreVM:(BOOL)vm;
@@ -242,6 +243,144 @@ static void URLFileReaderInit(void)
 @end
 
 
+// interface IFURLFileDatabasePrivate -----------------------------------------------------------
+
+ at interface IFURLFileDatabase (IFURLFileDatabasePrivate)
+
++(NSString *)uniqueFilePathForKey:(id)key;
+-(void)writeSizeFile:(unsigned)value;
+-(unsigned)readSizeFile;
+-(void)truncateToSizeLimit:(unsigned)size;
+
+ at end
+
+// implementation IFURLFileDatabasePrivate ------------------------------------------------------
+
+ at implementation IFURLFileDatabase (IFURLFileDatabasePrivate)
+
++(NSString *)uniqueFilePathForKey:(id)key
+{
+    const char *s;
+    UInt32 hash1;
+    UInt32 hash2;
+    CFIndex len;
+    CFIndex cnt;
+
+    s = [[[[key description] lowercaseString] stringByStandardizingPath] lossyCString];
+    len = strlen(s);
+
+    // compute first hash    
+    hash1 = len;
+    for (cnt = 0; cnt < len; cnt++) {
+        hash1 += (hash1 << 8) + s[cnt];
+    }
+    hash1 += (hash1 << (len & 31));
+
+    // compute second hash    
+    hash2 = len;
+    for (cnt = 0; cnt < len; cnt++) {
+        hash2 = (37 * hash2) ^ s[cnt];
+    }
+
+    // create the path and return it
+    return [NSString stringWithFormat:@"%.2u/%.2u/%.10u-%.10u.cache", ((hash1 & 0xff) >> 4), ((hash2 & 0xff) >> 4), hash1, hash2];
+}
+
+
+-(void)writeSizeFile:(unsigned)value
+{
+    void *buf;
+    int fd;
+    
+    [mutex lock];
+    
+    fd = open(sizeFilePath, O_WRONLY | O_CREAT, [IFURLFilePosixPermissions intValue]);
+    if (fd > 0) {
+        buf = calloc(1, MAX_UNSIGNED_LENGTH);
+        sprintf(buf, "%d", value);
+        write(fd, buf, MAX_UNSIGNED_LENGTH);
+        free(buf);
+        close(fd);
+    }
+    
+    [mutex unlock];
+}
+
+-(unsigned)readSizeFile
+{
+    unsigned result;
+    void *buf;
+    int fd;
+    
+    result = 0;
+
+    [mutex lock];
+    
+    fd = open(sizeFilePath, O_RDONLY, 0);
+    if (fd > 0) {
+        buf = calloc(1, MAX_UNSIGNED_LENGTH);
+        read(fd, buf, MAX_UNSIGNED_LENGTH);
+        result = strtol(buf, NULL, 10);
+        free(buf);
+        close(fd);
+    }
+    
+    [mutex unlock];
+
+    return result;
+}
+
+-(void)truncateToSizeLimit:(unsigned)size
+{
+    NSFileManager *defaultManager;
+    NSDirectoryEnumerator *enumerator;
+    NSString *filePath;
+    NSString *fullFilePath;
+    NSDictionary *attributes;
+    NSNumber *fileSize;
+    
+    if (size > usage) {
+        return;
+    }
+
+    if (size == 0) {
+        [self removeAllObjects];
+    }
+    else {
+        defaultManager = [NSFileManager defaultManager];
+        [mutex lock];
+        enumerator = [defaultManager enumeratorAtPath:path];
+        while (usage > size) {
+            filePath = [enumerator nextObject];
+            if (filePath == nil) {
+                break;
+            }
+            if ([filePath isEqualToString:SIZE_FILE_NAME]) {
+                continue;
+            }
+            
+            fullFilePath = [[NSString alloc] initWithFormat:@"%@/%@", path, filePath];
+            attributes = [defaultManager fileAttributesAtPath:fullFilePath traverseLink:YES];
+            if (attributes) {
+                if ([[attributes objectForKey:NSFileType] isEqualToString:NSFileTypeRegular]) {
+                    fileSize = [attributes objectForKey:NSFileSize];
+                    if (fileSize) {
+                        usage -= [fileSize unsignedIntValue];
+                        WEBFOUNDATIONDEBUGLEVEL(WebFoundationLogDiskCacheActivity, "truncateToSizeLimit - %u - %u - %u, %s", size, usage, [fileSize unsignedIntValue], DEBUG_OBJECT(fullFilePath));
+                        [defaultManager removeFileAtPath:fullFilePath handler:nil];
+                    }
+                }
+            }
+            [fullFilePath release];
+        }
+        [self writeSizeFile:usage];
+        [mutex unlock];
+    }
+}
+
+ at end
+
+
 // implementation IFURLFileDatabase -------------------------------------------------------------
 
 @implementation IFURLFileDatabase
@@ -266,8 +405,9 @@ static void URLFileReaderInit(void)
         setCache = [[NSMutableDictionary alloc] init];
         removeCache = [[NSMutableSet alloc] init];
         timer = nil;
-        mutex = [[NSLock alloc] init];
-
+        mutex = [[NSRecursiveLock alloc] init];
+        sizeFilePath = NULL;
+        
         return self;
     }
     
@@ -287,34 +427,6 @@ static void URLFileReaderInit(void)
     [super dealloc];
 }
 
-+(NSString *)uniqueFilePathForKey:(id)key
-{
-    const char *s;
-    UInt32 hash1;
-    UInt32 hash2;
-    CFIndex len;
-    CFIndex cnt;
-
-    s = [[[[key description] lowercaseString] stringByStandardizingPath] lossyCString];
-    len = strlen(s);
-
-    // compute first hash    
-    hash1 = len;
-    for (cnt = 0; cnt < len; cnt++) {
-        hash1 += (hash1 << 8) + s[cnt];
-    }
-    hash1 += (hash1 << (len & 31));
-
-    // compute second hash    
-    hash2 = len;
-    for (cnt = 0; cnt < len; cnt++) {
-        hash2 = (37 * hash2) ^ s[cnt];
-    }
-
-    // create the path and return it
-    return [NSString stringWithFormat:@"%.2u/%.2u/%.10u-%.10u.cache", ((hash1 & 0xff) >> 4), ((hash2 & 0xff) >> 4), hash1, hash2];
-}
-
 -(void)setTimer
 {
     if (timer == nil) {
@@ -367,6 +479,7 @@ static void URLFileReaderInit(void)
     [self close];
     [[NSFileManager defaultManager] _IF_backgroundRemoveFileAtPath:path];
     [self open];
+    [self writeSizeFile:0];
     [mutex unlock];
 
     WEBFOUNDATIONDEBUGLEVEL(WebFoundationLogDiskCacheActivity, "removeAllObjects");
@@ -482,15 +595,29 @@ static void URLFileReaderInit(void)
 
     [archiver release];
     [filePath release];
+    
+    usage += [data length];
+    [self writeSizeFile:usage];
+    [self truncateToSizeLimit:[self sizeLimit]];
 }
 
 -(void)performRemoveObjectForKey:(id)key
 {
     NSString *filePath;
-
+    NSDictionary *attributes;
+    NSNumber *size;
+    
     WEBFOUNDATIONDEBUGLEVEL(WebFoundationLogDiskCacheActivity, "performRemoveObjectForKey - %s", DEBUG_OBJECT(key));
 
     filePath = [[NSString alloc] initWithFormat:@"%@/%@", path, [IFURLFileDatabase uniqueFilePathForKey:key]];
+    attributes = [[NSFileManager defaultManager] fileAttributesAtPath:filePath traverseLink:YES];
+    if (attributes) {
+        size = [attributes objectForKey:NSFileSize];
+        if (size) {
+            usage -= [size unsignedIntValue];
+            [self writeSizeFile:usage];
+        }
+    }
     [[NSFileManager defaultManager] removeFileAtPath:filePath handler:nil];
     [filePath release];
 }
@@ -503,6 +630,8 @@ static void URLFileReaderInit(void)
     NSFileManager *manager;
     NSDictionary *attributes;
     BOOL isDir;
+    const char *tmp;
+    NSString *sizeFilePathString;
     
     if (!isOpen) {
         manager = [NSFileManager defaultManager];
@@ -527,6 +656,12 @@ static void URLFileReaderInit(void)
                 isOpen = [manager _IF_createDirectoryAtPathWithIntermediateDirectories:path attributes:attributes];
             }
         }
+
+        sizeFilePathString = [NSString stringWithFormat:@"%@/%@", path, SIZE_FILE_NAME];
+        tmp = [[NSFileManager defaultManager] fileSystemRepresentationWithPath:sizeFilePathString];
+        sizeFilePath = malloc(strlen(tmp) + 1);
+        strcpy(sizeFilePath, tmp);
+        usage = [self readSizeFile];
     }
     
     return isOpen;
@@ -536,6 +671,11 @@ static void URLFileReaderInit(void)
 {
     if (isOpen) {
         isOpen = NO;
+
+        if (sizeFilePath) {
+            free(sizeFilePath);
+            sizeFilePath = NULL;
+        }
     }
     
     return YES;
@@ -606,4 +746,18 @@ static void URLFileReaderInit(void)
     }
 }
 
+-(unsigned)count
+{
+    // FIXME: [kocienda] Radar 2922874 (On-disk cache does not know how many elements it is storing)
+    return 0;
+}
+
+-(void)setSizeLimit:(unsigned)limit
+{
+    sizeLimit = limit;
+    if (limit < usage) {
+        [self truncateToSizeLimit:limit];
+    }
+}
+
 @end

-- 
WebKit Debian packaging



More information about the Pkg-webkit-commits mailing list