Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Move temporary files outside of the cache directory #2193

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ class MainActivity : FlutterActivity() {
"An error occurred:\n$stack"
)
}
} finally {
inFile.delete()
tmpDir.deleteRecursively()
Comment on lines +369 to +370
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this done on the flutter side already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly, pressing the back button to return to the dashboard, or installing the app and pressing the "Open" button delete entire patcher temporary directory in flutter side.
However, if the user closes the app directly, all the temp files will remain.
Therefore, I chose to delete everything except out.apk from the patcher temporary directory immediately after patch ended.

(Since the temporary directories are no longer inside the cacheDir, I believe the Manager is now more responsible than ever for ensuring that the temporary directories are cleared.)

}

handler.post { result.success(null) }
Expand Down
10 changes: 9 additions & 1 deletion lib/services/patcher_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PatcherAPI {
Future<void> initialize() async {
await loadPatches();
await _managerAPI.downloadIntegrations();
final Directory appCache = await getTemporaryDirectory();
final Directory appCache = await getApplicationSupportDirectory();
_dataDir = await getExternalStorageDirectory() ?? appCache;
_tmpDir = Directory('${appCache.path}/patcher');
_keyStoreFile = File('${_dataDir.path}/revanced-manager.keystore');
Expand Down Expand Up @@ -151,6 +151,7 @@ class PatcherAPI {
String packageName,
String apkFilePath,
List<Patch> selectedPatches,
bool isFromStorage,
) async {
final File? integrationsFile = await _managerAPI.downloadIntegrations();
final Map<String, Map<String, dynamic>> options = {};
Expand All @@ -176,6 +177,13 @@ class PatcherAPI {
final File inApkFile = File('${workDir.path}/in.apk');
await File(apkFilePath).copy(inApkFile.path);

if (isFromStorage) {
// The selected apk was copied to cacheDir by the file picker, so it's not needed anymore.
// rename() can't be used here, as Android system also counts the size of files moved out from cacheDir
// as part of the app's cache size.
File(apkFilePath).delete();
}

outFile = File('${workDir.path}/out.apk');

final Directory tmpDir =
Expand Down
1 change: 1 addition & 0 deletions lib/ui/views/installer/installer_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ class InstallerViewModel extends BaseViewModel {
_app.packageName,
_app.apkFilePath,
_patches,
_app.isFromStorage,
);
_app.appliedPatches = _patches.map((p) => p.name).toList();
if (_managerAPI.isLastPatchedAppEnabled()) {
Expand Down