From 0dfbc5ac98a1ea4981b4a46b4d48b16cae23c16c Mon Sep 17 00:00:00 2001 From: DGoiana Date: Wed, 6 Mar 2024 22:09:15 +0000 Subject: [PATCH 1/4] Solving race conditions --- uni/lib/controller/cleanup.dart | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/uni/lib/controller/cleanup.dart b/uni/lib/controller/cleanup.dart index 8b8c519db..0a557c007 100644 --- a/uni/lib/controller/cleanup.dart +++ b/uni/lib/controller/cleanup.dart @@ -22,18 +22,16 @@ Future cleanupStoredData(BuildContext context) async { final faculties = PreferencesController.getUserFaculties(); await prefs.clear(); - unawaited( - Future.wait([ - AppLecturesDatabase().deleteLectures(), - AppExamsDatabase().deleteExams(), - AppCoursesDatabase().deleteCourses(), - AppUserDataDatabase().deleteUserData(), - AppLastUserInfoUpdateDatabase().deleteLastUpdate(), - AppBusStopDatabase().deleteBusStops(), - AppCourseUnitsDatabase().deleteCourseUnits(), - NetworkRouter.killSigarraAuthentication(faculties), - ]), - ); + await Future.wait([ + AppLecturesDatabase().deleteLectures(), + AppExamsDatabase().deleteExams(), + AppCoursesDatabase().deleteCourses(), + AppUserDataDatabase().deleteUserData(), + AppLastUserInfoUpdateDatabase().deleteLastUpdate(), + AppBusStopDatabase().deleteBusStops(), + AppCourseUnitsDatabase().deleteCourseUnits(), + NetworkRouter.killSigarraAuthentication(faculties), + ]); final path = (await getApplicationDocumentsDirectory()).path; final directory = Directory(path); From 0774db1d9082d377db41fd1fae53273a664e7bbe Mon Sep 17 00:00:00 2001 From: DGoiana Date: Thu, 7 Mar 2024 00:09:41 +0000 Subject: [PATCH 2/4] Handling deletion of directory --- uni/lib/controller/cleanup.dart | 39 ++++++++++++++------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/uni/lib/controller/cleanup.dart b/uni/lib/controller/cleanup.dart index 0a557c007..a438676ca 100644 --- a/uni/lib/controller/cleanup.dart +++ b/uni/lib/controller/cleanup.dart @@ -33,11 +33,8 @@ Future cleanupStoredData(BuildContext context) async { NetworkRouter.killSigarraAuthentication(faculties), ]); - final path = (await getApplicationDocumentsDirectory()).path; - final directory = Directory(path); - if (directory.existsSync()) { - directory.deleteSync(recursive: true); - } + final toCleanDirectory = await getApplicationDocumentsDirectory(); + await cleanDirectory(toCleanDirectory, DateTime.now()); } Future cleanupCachedFiles() async { @@ -51,26 +48,24 @@ Future cleanupCachedFiles() async { final toCleanDirectory = await getApplicationDocumentsDirectory(); final threshold = DateTime.now().subtract(const Duration(days: 30)); - final directories = toCleanDirectory.listSync(followLinks: false); - for (final directory in directories) { - if (directory is Directory) { - final files = directory.listSync(recursive: true, followLinks: false); + await cleanDirectory(toCleanDirectory, threshold); - final oldFiles = files.where((file) { - try { - final fileDate = File(file.path).lastModifiedSync(); - return fileDate.isBefore(threshold); - } catch (e) { - return false; - } - }); + await PreferencesController.setLastCleanUpDate(DateTime.now()); +} - for (final file in oldFiles) { - await File(file.path).delete(); - } +Future cleanDirectory(Directory directory, DateTime threshold) async { + final entities = directory.listSync(recursive: true, followLinks: false); + final toDeleteEntities = entities.where((e) { + try { + final fileDate = File(e.path).lastModifiedSync(); + return fileDate.isBefore(threshold); + } catch (e) { + return false; } - } + }); - await PreferencesController.setLastCleanUpDate(DateTime.now()); + for (final entity in toDeleteEntities) { + await File(entity.path).delete(); + } } From d44a9b96fafc0ffddcb4f2a12ceb2709bfc9719e Mon Sep 17 00:00:00 2001 From: DGoiana Date: Thu, 7 Mar 2024 17:06:44 +0000 Subject: [PATCH 3/4] Avoiding deletion of .db files --- uni/lib/controller/cleanup.dart | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/uni/lib/controller/cleanup.dart b/uni/lib/controller/cleanup.dart index a438676ca..bcdce1335 100644 --- a/uni/lib/controller/cleanup.dart +++ b/uni/lib/controller/cleanup.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:io'; import 'package:flutter/material.dart'; +import 'package:path/path.dart' as path; import 'package:path_provider/path_provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:uni/controller/local_storage/database/app_bus_stop_database.dart'; @@ -56,16 +57,16 @@ Future cleanupCachedFiles() async { Future cleanDirectory(Directory directory, DateTime threshold) async { final entities = directory.listSync(recursive: true, followLinks: false); - final toDeleteEntities = entities.where((e) { + final toDeleteEntities = entities.whereType().where((file) { try { - final fileDate = File(e.path).lastModifiedSync(); - return fileDate.isBefore(threshold); + final fileDate = File(file.path).lastModifiedSync(); + return fileDate.isBefore(threshold) && path.extension(file.path) != '.db'; } catch (e) { return false; } }); for (final entity in toDeleteEntities) { - await File(entity.path).delete(); + entity.deleteSync(); } } From ee0fec03857bd654bc496b2476a3e3fc02c9a40b Mon Sep 17 00:00:00 2001 From: DGoiana Date: Fri, 8 Mar 2024 20:08:11 +0000 Subject: [PATCH 4/4] Removing unecessary cast --- uni/lib/controller/cleanup.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uni/lib/controller/cleanup.dart b/uni/lib/controller/cleanup.dart index bcdce1335..c30d4a2b5 100644 --- a/uni/lib/controller/cleanup.dart +++ b/uni/lib/controller/cleanup.dart @@ -59,7 +59,7 @@ Future cleanDirectory(Directory directory, DateTime threshold) async { final entities = directory.listSync(recursive: true, followLinks: false); final toDeleteEntities = entities.whereType().where((file) { try { - final fileDate = File(file.path).lastModifiedSync(); + final fileDate = file.lastModifiedSync(); return fileDate.isBefore(threshold) && path.extension(file.path) != '.db'; } catch (e) { return false;