[SOUP] Use a webkit subdirectory for the disk cache
authorcarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 Apr 2015 07:17:07 +0000 (07:17 +0000)
committercarlosgc@webkit.org <carlosgc@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 24 Apr 2015 07:17:07 +0000 (07:17 +0000)
https://bugs.webkit.org/show_bug.cgi?id=144048

Reviewed by Martin Robinson.

Source/WebCore:

Add a static method to SoupNetworkSession to clear a soup cache
given its directory.

* platform/network/soup/SoupNetworkSession.cpp:
(WebCore::strIsNumeric):
(WebCore::SoupNetworkSession::clearCache):
* platform/network/soup/SoupNetworkSession.h:

Source/WebKit2:

Recent versions of libsoup remove any file in cache dir not
referenced by the index when the cache is loaded to workaround
leaked resources when load/dump is unbalanced for whatever reason,
like a crash. We currently use $XDG_CACHE_HOME/app-name as default
disk cache directory, but that directory could be used by apps to
cache other things, and the soup cache might end up deleting other
stuff. The soup cache assumes the given directory is only for the
disk cache, so we should ensure that.

* NetworkProcess/soup/NetworkProcessSoup.cpp:
(WebKit::NetworkProcess::platformInitializeNetworkProcess): Append
webkit to the given disk cache and clear the previous soup cache if it exists.
* WebProcess/soup/WebProcessSoup.cpp:
(WebKit::WebProcess::platformInitializeWebProcess): Ditto.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@183255 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/network/soup/SoupNetworkSession.cpp
Source/WebCore/platform/network/soup/SoupNetworkSession.h
Source/WebKit2/ChangeLog
Source/WebKit2/NetworkProcess/soup/NetworkProcessSoup.cpp
Source/WebKit2/WebProcess/soup/WebProcessSoup.cpp

index 007c2df1523c7ea510c46ea4085675e566a438c9..f07817ad5ded7b0a0c8a077041e272e0f64ea744 100644 (file)
@@ -1,3 +1,18 @@
+2015-04-24  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [SOUP] Use a webkit subdirectory for the disk cache
+        https://bugs.webkit.org/show_bug.cgi?id=144048
+
+        Reviewed by Martin Robinson.
+
+        Add a static method to SoupNetworkSession to clear a soup cache
+        given its directory.
+
+        * platform/network/soup/SoupNetworkSession.cpp:
+        (WebCore::strIsNumeric):
+        (WebCore::SoupNetworkSession::clearCache):
+        * platform/network/soup/SoupNetworkSession.h:
+
 2015-04-23  Andy Estes  <aestes@apple.com>
 
         Fix the iOS build after r183234.
index 8e5dd9c292e587f6e17fcb23455b8da3d6c6560b..48e521d5552d931145e1478cb23acc09d8695d66 100644 (file)
 
 #include "AuthenticationChallenge.h"
 #include "CookieJarSoup.h"
+#include "FileSystem.h"
 #include "GUniquePtrSoup.h"
 #include "Logging.h"
 #include "ResourceHandle.h"
+#include <glib/gstdio.h>
 #include <libsoup/soup.h>
 #include <wtf/text/CString.h>
 #include <wtf/text/StringBuilder.h>
@@ -165,6 +167,37 @@ SoupCache* SoupNetworkSession::cache() const
     return soupCache ? SOUP_CACHE(soupCache) : nullptr;
 }
 
+static inline bool stringIsNumeric(const char* str)
+{
+    while (*str) {
+        if (!g_ascii_isdigit(*str))
+            return false;
+        str++;
+    }
+    return true;
+}
+
+void SoupNetworkSession::clearCache(const String& cacheDirectory)
+{
+    CString cachePath = fileSystemRepresentation(cacheDirectory);
+    GUniquePtr<char> cacheFile(g_build_filename(cachePath.data(), "soup.cache2", nullptr));
+    if (!g_file_test(cacheFile.get(), G_FILE_TEST_IS_REGULAR))
+        return;
+
+    GUniquePtr<GDir> dir(g_dir_open(cachePath.data(), 0, nullptr));
+    if (!dir)
+        return;
+
+    while (const char* name = g_dir_read_name(dir.get())) {
+        if (!g_str_has_prefix(name, "soup.cache") && !stringIsNumeric(name))
+            continue;
+
+        GUniquePtr<gchar> filename(g_build_filename(cachePath.data(), name, nullptr));
+        if (g_file_test(filename.get(), G_FILE_TEST_IS_REGULAR))
+            g_unlink(filename.get());
+    }
+}
+
 void SoupNetworkSession::setSSLPolicy(SSLPolicy flags)
 {
     g_object_set(m_soupSession.get(),
index c913b4618b3976bccdc9ca0a23e8db727f1d2696..796927da74504c97053efca585b4c06f51a14720 100644 (file)
@@ -60,6 +60,7 @@ public:
 
     void setCache(SoupCache*);
     SoupCache* cache() const;
+    static void clearCache(const String& cacheDirectory);
 
     void setSSLPolicy(SSLPolicy);
     SSLPolicy sslPolicy() const;
index dc8a98a7e26cc1e4454d0a7da351f9c6bb7abf4b..921e973d75427d84026b4cac159e1cc0519333a9 100644 (file)
@@ -1,3 +1,25 @@
+2015-04-24  Carlos Garcia Campos  <cgarcia@igalia.com>
+
+        [SOUP] Use a webkit subdirectory for the disk cache
+        https://bugs.webkit.org/show_bug.cgi?id=144048
+
+        Reviewed by Martin Robinson.
+
+        Recent versions of libsoup remove any file in cache dir not
+        referenced by the index when the cache is loaded to workaround
+        leaked resources when load/dump is unbalanced for whatever reason,
+        like a crash. We currently use $XDG_CACHE_HOME/app-name as default
+        disk cache directory, but that directory could be used by apps to
+        cache other things, and the soup cache might end up deleting other
+        stuff. The soup cache assumes the given directory is only for the
+        disk cache, so we should ensure that.
+
+        * NetworkProcess/soup/NetworkProcessSoup.cpp:
+        (WebKit::NetworkProcess::platformInitializeNetworkProcess): Append
+        webkit to the given disk cache and clear the previous soup cache if it exists.
+        * WebProcess/soup/WebProcessSoup.cpp:
+        (WebKit::WebProcess::platformInitializeWebProcess): Ditto.
+
 2015-04-24  Carlos Garcia Campos  <cgarcia@igalia.com>
 
         Unreviewed. Remove incorrect ASSERT added in r183176.
index 5179311a776ba153ca75967837635f57519aaf03..dff76166e694f8e1a9517ed69364a1bc09bcedb6 100644 (file)
@@ -83,7 +83,13 @@ void NetworkProcess::userPreferredLanguagesChanged(const Vector<String>& languag
 void NetworkProcess::platformInitializeNetworkProcess(const NetworkProcessCreationParameters& parameters)
 {
     ASSERT(!parameters.diskCacheDirectory.isEmpty());
-    GRefPtr<SoupCache> soupCache = adoptGRef(soup_cache_new(parameters.diskCacheDirectory.utf8().data(), SOUP_CACHE_SINGLE_USER));
+
+    // We used to use the given cache directory for the soup cache, but now we use a subdirectory to avoid
+    // conflicts with other cache files in the same directory. Remove the old cache files if they still exist.
+    SoupNetworkSession::defaultSession().clearCache(parameters.diskCacheDirectory);
+
+    String diskCachePath = WebCore::pathByAppendingComponent(parameters.diskCacheDirectory, "webkit");
+    GRefPtr<SoupCache> soupCache = adoptGRef(soup_cache_new(diskCachePath.utf8().data(), SOUP_CACHE_SINGLE_USER));
     SoupNetworkSession::defaultSession().setCache(soupCache.get());
     // Set an initial huge max_size for the SoupCache so the call to soup_cache_load() won't evict any cached
     // resource. The final size of the cache will be set by NetworkProcess::platformSetCacheModel().
index 2f09d6c33f6a580ecde7bf27cf1fccb2884f088a..a00592e6d31dde1e7d487b3184a348667dc3e9fc 100644 (file)
@@ -154,7 +154,13 @@ void WebProcess::platformInitializeWebProcess(WebProcessCreationParameters&& par
         return;
 
     ASSERT(!parameters.diskCacheDirectory.isEmpty());
-    GRefPtr<SoupCache> soupCache = adoptGRef(soup_cache_new(parameters.diskCacheDirectory.utf8().data(), SOUP_CACHE_SINGLE_USER));
+
+    // We used to use the given cache directory for the soup cache, but now we use a subdirectory to avoid
+    // conflicts with other cache files in the same directory. Remove the old cache files if they still exist.
+    WebCore::SoupNetworkSession::defaultSession().clearCache(parameters.diskCacheDirectory);
+
+    String diskCachePath = WebCore::pathByAppendingComponent(parameters.diskCacheDirectory, "webkit");
+    GRefPtr<SoupCache> soupCache = adoptGRef(soup_cache_new(diskCachePath.utf8().data(), SOUP_CACHE_SINGLE_USER));
     WebCore::SoupNetworkSession::defaultSession().setCache(soupCache.get());
     // Set an initial huge max_size for the SoupCache so the call to soup_cache_load() won't evict any cached
     // resource. The final size of the cache will be set by NetworkProcess::platformSetCacheModel().