|
| 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