mirror of
https://github.com/jagandeepbrar/lunasea.git
synced 2026-08-30 20:24:25 +00:00
fix(database): prevent deadlock in database deletion actions
This commit is contained in:
@@ -3,6 +3,8 @@ 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';
|
||||
|
||||
@@ -10,13 +12,13 @@ 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();
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
@@ -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 {
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user