Bug 99236 - Delete localstore.rdf when it is corrupt
authorgijskruitbosch@gmail.com
Thu, 26 Apr 2007 10:49:05 -0700
changeset 842 f42d33b9ec468340a1a08f9e96bc39e948f309ac
parent 841 1dea42a17e18cf2a76b64d265178cb06228fd83b
child 843 5ccae52bf036f1ae7d6364e0c985be893ceccb10
push id1
push userbsmedberg@mozilla.com
push dateThu, 20 Mar 2008 16:49:24 +0000
treeherdermozilla-central@61007906a1f8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs99236
milestone1.9a4pre
Bug 99236 - Delete localstore.rdf when it is corrupt p=mossop.bugzilla@blueprintit.co.uk (Dave Townsend AKA Mossop) r=axel@pike.org (Axel Hecht) sr=dveditz@cruzio.com (Daniel Veditz)
rdf/datasource/src/nsLocalStore.cpp
--- a/rdf/datasource/src/nsLocalStore.cpp
+++ b/rdf/datasource/src/nsLocalStore.cpp
@@ -75,16 +75,17 @@ class LocalStoreImpl : public nsILocalSt
                        public nsSupportsWeakReference
 {
 protected:
     nsCOMPtr<nsIRDFDataSource> mInner;
 
     LocalStoreImpl();
     virtual ~LocalStoreImpl();
     nsresult Init();
+    nsresult CreateLocalStore(nsIFile* aFile);
     nsresult LoadData();
 
     friend NS_IMETHODIMP
     NS_NewLocalStore(nsISupports* aOuter, REFNSIID aIID, void** aResult);
 
     nsCOMPtr<nsIRDFService>    mRDFService;
 
 public:
@@ -378,60 +379,70 @@ LocalStoreImpl::Init()
         obs->AddObserver(this, "profile-before-change", PR_TRUE);
         obs->AddObserver(this, "profile-do-change", PR_TRUE);
     }
 
     return NS_OK;
 }
 
 nsresult
+LocalStoreImpl::CreateLocalStore(nsIFile* aFile)
+{
+    nsresult rv;
+
+    rv = aFile->Create(nsIFile::NORMAL_FILE_TYPE, 0666);
+    if (NS_FAILED(rv)) return rv;
+
+    nsCOMPtr<nsIOutputStream> outStream;
+    rv = NS_NewLocalFileOutputStream(getter_AddRefs(outStream), aFile);
+    if (NS_FAILED(rv)) return rv;
+
+    const char defaultRDF[] = 
+        "<?xml version=\"1.0\"?>\n" \
+        "<RDF:RDF xmlns:RDF=\"" RDF_NAMESPACE_URI "\"\n" \
+        "         xmlns:NC=\""  NC_NAMESPACE_URI "\">\n" \
+        "  <!-- Empty -->\n" \
+        "</RDF:RDF>\n";
+
+    PRUint32 count;
+    rv = outStream->Write(defaultRDF, sizeof(defaultRDF)-1, &count);
+    if (NS_FAILED(rv)) return rv;
+
+    if (count != sizeof(defaultRDF)-1)
+        return NS_ERROR_UNEXPECTED;
+
+    // Okay, now see if the file exists _for real_. If it's still
+    // not there, it could be that the profile service gave us
+    // back a read-only directory. Whatever.
+    PRBool fileExistsFlag = PR_FALSE;
+    aFile->Exists(&fileExistsFlag);
+    if (!fileExistsFlag)
+        return NS_ERROR_UNEXPECTED;
+
+    return NS_OK;
+}
+
+nsresult
 LocalStoreImpl::LoadData()
 {
     nsresult rv;
 
     // Look for localstore.rdf in the current profile
     // directory. Bomb if we can't find it.
 
     nsCOMPtr<nsIFile> aFile;
     rv = NS_GetSpecialDirectory(NS_APP_LOCALSTORE_50_FILE, getter_AddRefs(aFile));
     if (NS_FAILED(rv)) return rv;
 
     PRBool fileExistsFlag = PR_FALSE;
     (void)aFile->Exists(&fileExistsFlag);
     if (!fileExistsFlag) {
         // if file doesn't exist, create it
-        (void)aFile->Create(nsIFile::NORMAL_FILE_TYPE, 0666);
-
-        nsCOMPtr<nsIOutputStream> outStream;
-        rv = NS_NewLocalFileOutputStream(getter_AddRefs(outStream), aFile);
-        if (NS_FAILED(rv))
-            return rv;
-
-        const char defaultRDF[] = 
-            "<?xml version=\"1.0\"?>\n" \
-            "<RDF:RDF xmlns:RDF=\"" RDF_NAMESPACE_URI "\"\n" \
-            "         xmlns:NC=\""  NC_NAMESPACE_URI "\">\n" \
-            "  <!-- Empty -->\n" \
-            "</RDF:RDF>\n";
-
-        PRUint32 count;
-        rv = outStream->Write(defaultRDF, sizeof(defaultRDF)-1, &count);
-        if (NS_FAILED(rv))
-            return rv;
-
-        if (count != sizeof(defaultRDF)-1)
-            return NS_ERROR_UNEXPECTED;
-
-        // Okay, now see if the file exists _for real_. If it's still
-        // not there, it could be that the profile service gave us
-        // back a read-only directory. Whatever.
-        fileExistsFlag = PR_FALSE;
-        (void)aFile->Exists(&fileExistsFlag);
-        if (!fileExistsFlag)
-            return NS_ERROR_UNEXPECTED;
+        rv = CreateLocalStore(aFile);
+        if (NS_FAILED(rv)) return rv;
     }
 
     mInner = do_CreateInstance(NS_RDF_DATASOURCE_CONTRACTID_PREFIX "xml-datasource", &rv);
     if (NS_FAILED(rv)) return rv;
 
     nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner, &rv);
     if (NS_FAILED(rv)) return rv;
 
@@ -442,17 +453,28 @@ LocalStoreImpl::LoadData()
     nsCAutoString spec;
     rv = aURI->GetSpec(spec);
     if (NS_FAILED(rv)) return rv;
 
     rv = remote->Init(spec.get());
     if (NS_FAILED(rv)) return rv;
 
     // Read the datasource synchronously.
-    return remote->Refresh(PR_TRUE);
+    rv = remote->Refresh(PR_TRUE);
+    
+    if (NS_FAILED(rv)) {
+        // Load failed, delete and recreate a fresh localstore
+        aFile->Remove(PR_TRUE);
+        rv = CreateLocalStore(aFile);
+        if (NS_FAILED(rv)) return rv;
+        
+        rv = remote->Refresh(PR_TRUE);
+    }
+
+    return rv;
 }
 
 
 NS_IMETHODIMP
 LocalStoreImpl::GetURI(char* *aURI)
 {
     NS_PRECONDITION(aURI != nsnull, "null ptr");
     if (! aURI)