Skip to content

Commit 0c88453

Browse files
author
Chris Brody
committed
Support optional corrupt database handler (ref: #169).
Add additional safeguards to public SQLiteDatabase.markTableSyncable() functions in case the database is not opened. NOTE: The DefaultDatabaseErrorHandler follows the existing implementation from SQLiteDatabase.onCorruption() which is to close the database object and then delete it. This is different from the AOSP version of DefaultDatabaseErrorHandler.java which also attempts to delete all attached database files. FUTURE/TBD I am not sure if we want to do this or not.
1 parent 488189b commit 0c88453

File tree

4 files changed

+280
-43
lines changed

4 files changed

+280
-43
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2010 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.sqlcipher;
18+
19+
import net.sqlcipher.database.SQLiteDatabase;
20+
21+
/**
22+
* An interface to let the apps define the actions to take when the following errors are detected
23+
* database corruption
24+
*/
25+
public interface DatabaseErrorHandler {
26+
27+
/**
28+
* defines the method to be invoked when database corruption is detected.
29+
* @param dbObj the {@link SQLiteDatabase} object representing the database on which corruption
30+
* is detected.
31+
*/
32+
void onCorruption(SQLiteDatabase dbObj);
33+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (C) 2010 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.sqlcipher;
18+
19+
import java.io.File;
20+
import java.util.List;
21+
22+
import net.sqlcipher.database.SQLiteDatabase;
23+
import net.sqlcipher.database.SQLiteException;
24+
25+
import android.util.Log;
26+
import android.util.Pair;
27+
28+
/**
29+
* Default class used to define the actions to take when the database corruption is reported
30+
* by sqlite.
31+
* <p>
32+
* If null is specified for DatabaeErrorHandler param in the above calls, then this class is used
33+
* as the default {@link DatabaseErrorHandler}.
34+
*/
35+
public final class DefaultDatabaseErrorHandler implements DatabaseErrorHandler {
36+
37+
private static final String TAG = "DefaultDatabaseErrorHandler";
38+
39+
/**
40+
* defines the default method to be invoked when database corruption is detected.
41+
* @param dbObj the {@link SQLiteDatabase} object representing the database on which corruption
42+
* is detected.
43+
*/
44+
public void onCorruption(SQLiteDatabase dbObj) {
45+
Log.e(TAG, "Corruption reported by sqlite on database, deleting and re-creating: " + dbObj.getPath());
46+
47+
if (dbObj.isOpen()) {
48+
Log.e(TAG, "Database object for corrupted database is open, not expected for this implementation");
49+
try {
50+
dbObj.close();
51+
} catch (Exception e) {
52+
/* ignore */
53+
Log.e(TAG, "Exception closing Database object for corrupted database, ignored", e);
54+
}
55+
}
56+
57+
deleteDatabaseFile(dbObj.getPath());
58+
}
59+
60+
private void deleteDatabaseFile(String fileName) {
61+
if (fileName.equalsIgnoreCase(":memory:") || fileName.trim().length() == 0) {
62+
return;
63+
}
64+
Log.e(TAG, "deleting the database file: " + fileName);
65+
try {
66+
new File(fileName).delete();
67+
} catch (Exception e) {
68+
/* print warning and ignore exception */
69+
Log.w(TAG, "delete failed: " + e.getMessage());
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)