From 4f6e76c540aa86a76b84f4828c12e352c511555a Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Fri, 14 Apr 2023 12:15:08 +0200 Subject: [PATCH 1/2] Add database integrity check --- .../cache_object_provider.dart | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_object_provider.dart b/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_object_provider.dart index 8e3b92a6..03cf881c 100644 --- a/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_object_provider.dart +++ b/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_object_provider.dart @@ -4,6 +4,7 @@ import 'package:flutter_cache_manager/src/storage/cache_info_repositories/helper import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; import 'package:sqflite/sqflite.dart'; + import '../cache_object.dart'; import 'cache_info_repository.dart'; @@ -27,6 +28,8 @@ class CacheObjectProvider extends CacheInfoRepository } var path = await _getPath(); await File(path).parent.create(recursive: true); + await _dropCorruptedDb(path); + db = await openDatabase(path, version: 3, onCreate: (Database db, int version) async { await db.execute(''' @@ -214,4 +217,26 @@ class CacheObjectProvider extends CacheInfoRepository } } } + + static Future _dropCorruptedDb(String path) async { + final exists = await File(path).exists(); + if (!exists) { + return; + } + + Database? readDb; + bool isIntegral = true; + + try { + readDb = await openReadOnlyDatabase(path); + final check = await readDb.rawQuery("pragma integrity_check"); + isIntegral = check.length == 1 && check[0].values.first == "ok"; + } finally { + await readDb?.close(); + } + + if (!isIntegral) { + await deleteDatabase(path); + } + } } From 190b8de094250cb53bb2260822080768bdbd69ad Mon Sep 17 00:00:00 2001 From: Jakub Turek Date: Mon, 3 Jul 2023 17:36:22 +0200 Subject: [PATCH 2/2] Catch DatabaseException --- .../cache_info_repositories/cache_object_provider.dart | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_object_provider.dart b/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_object_provider.dart index 03cf881c..4e431603 100644 --- a/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_object_provider.dart +++ b/flutter_cache_manager/lib/src/storage/cache_info_repositories/cache_object_provider.dart @@ -224,15 +224,17 @@ class CacheObjectProvider extends CacheInfoRepository return; } - Database? readDb; + Database? pragmaDb; bool isIntegral = true; try { - readDb = await openReadOnlyDatabase(path); - final check = await readDb.rawQuery("pragma integrity_check"); + pragmaDb = await openDatabase(path); + final check = await pragmaDb.rawQuery("pragma integrity_check"); isIntegral = check.length == 1 && check[0].values.first == "ok"; + } on DatabaseException catch (e) { + isIntegral = false; } finally { - await readDb?.close(); + await pragmaDb?.close(); } if (!isIntegral) {