Skip to content

Commit

Permalink
fix(database): prevent deadlock in database deletion actions
Browse files Browse the repository at this point in the history
  • Loading branch information
JagandeepBrar committed Aug 29, 2022
1 parent aed7e85 commit 9960124
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
31 changes: 24 additions & 7 deletions lib/database/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ import 'package:lunasea/database/models/profile.dart';
import 'package:lunasea/database/table.dart';
import 'package:lunasea/database/tables/bios.dart';
import 'package:lunasea/database/tables/lunasea.dart';
import 'package:lunasea/system/filesystem/filesystem.dart';
import 'package:lunasea/system/logger.dart';
import 'package:lunasea/system/platform.dart';
import 'package:lunasea/vendor.dart';

class LunaDatabase {
static const String _DATABASE_LEGACY_PATH = 'database';
static const String _DATABASE_PATH = 'LunaSea/database';

String get _path {
String get path {
if (LunaPlatform.isWindows || LunaPlatform.isLinux) return _DATABASE_PATH;
return _DATABASE_LEGACY_PATH;
}

Future<void> initialize() async {
await Hive.initFlutter(_path);
await Hive.initFlutter(path);
LunaTable.register();
await open();
}
Expand All @@ -25,13 +27,28 @@ class LunaDatabase {
try {
await LunaBox.open();
if (LunaBox.profiles.isEmpty) await bootstrap();
} catch (error) {
for (final box in LunaBox.values) {
await Hive.deleteBoxFromDisk(box.key);
}

} catch (error, stack) {
await nuke();
await LunaBox.open();
await bootstrap(databaseCorruption: true);

LunaLogger().error(
'Database corruption detected',
error,
stack,
);
}
}

Future<void> nuke() async {
await Hive.close();

for (final box in LunaBox.values) {
await Hive.deleteBoxFromDisk(box.key, path: path);
}

if (LunaFileSystem.isSupported) {
await LunaFileSystem().nuke();
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/system/filesystem/filesystem.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ abstract class LunaFileSystem {

Future<bool> save(BuildContext context, String name, List<int> data);
Future<LunaFile?> read(BuildContext context, List<String> extensions);
Future<void> nuke();
}
18 changes: 16 additions & 2 deletions lib/system/filesystem/platform/filesystem_io.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:lunasea/database/database.dart';
import 'package:lunasea/vendor.dart';
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';
Expand All @@ -21,7 +22,20 @@ LunaFileSystem getFileSystem() {
throw UnsupportedError('LunaFileSystem unsupported');
}

class _Desktop implements LunaFileSystem {
abstract class _Shared implements LunaFileSystem {
@override
Future<void> nuke() async {
final subpath = LunaDatabase().path;
final appDocDir = await getApplicationDocumentsDirectory();
final database = Directory('${appDocDir.path}/$subpath');

if (database.existsSync()) {
database.deleteSync(recursive: true);
}
}
}

class _Desktop extends _Shared {
@override
Future<bool> save(BuildContext context, String name, List<int> data) async {
try {
Expand Down Expand Up @@ -69,7 +83,7 @@ class _Desktop implements LunaFileSystem {
}
}

class _Mobile implements LunaFileSystem {
class _Mobile extends _Shared {
@override
Future<bool> save(BuildContext context, String name, List<int> data) async {
try {
Expand Down

0 comments on commit 9960124

Please sign in to comment.