Skip to content

Commit dc376f5

Browse files
Merge pull request #192 from brodybits/cb-custom-corruption-handler
Changes to support custom database corruption error handler
2 parents ed51d6d + d22b5a4 commit dc376f5

File tree

4 files changed

+336
-42
lines changed

4 files changed

+336
-42
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: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
// NOTE: Unlike the AOSP, this version does NOT attempt to delete any attached databases.
46+
// TBD: Are we really certain that the attached databases would really be corrupt?
47+
Log.e(TAG, "Corruption reported by sqlite on database, deleting: " + dbObj.getPath());
48+
49+
if (dbObj.isOpen()) {
50+
Log.e(TAG, "Database object for corrupted database is already open, closing");
51+
52+
try {
53+
dbObj.close();
54+
} catch (Exception e) {
55+
/* ignored */
56+
Log.e(TAG, "Exception closing Database object for corrupted database, ignored", e);
57+
}
58+
}
59+
60+
deleteDatabaseFile(dbObj.getPath());
61+
}
62+
63+
private void deleteDatabaseFile(String fileName) {
64+
if (fileName.equalsIgnoreCase(":memory:") || fileName.trim().length() == 0) {
65+
return;
66+
}
67+
Log.e(TAG, "deleting the database file: " + fileName);
68+
try {
69+
new File(fileName).delete();
70+
} catch (Exception e) {
71+
/* print warning and ignore exception */
72+
Log.w(TAG, "delete failed: " + e.getMessage());
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)