ReVanced / revanced-manager

💊 Application to use ReVanced on Android
https://revanced.app
GNU General Public License v3.0
18.21k stars 748 forks source link

bug: tmp directory should not be inside the cache directory #2120

Closed kitadai31 closed 2 months ago

kitadai31 commented 3 months ago

Bug description

When the storage space is not enough, the Android system deletes apps cache automatically even if the app is running. Currently, ReVanced Manager (for both Compose and Flutter) places the tmp directory inside the cache folder. Therefore, if the cache deletion occurs during the patching process, the patching will fail.

The following files are better to be moved out of the cache folder to ensure they are kept until the end of patching:

This automatic deletion is only occurs for the cache directory. The cacheDir is unreliable for working directory due to deletion by Android system. So, changing the tempDir place from app.cacheDir.resolve("ephemeral") to like app.getDir("ephemeral", Context.MODE_PRIVATE) solves this issue.

This puts the responsibility on the ReVanced Manager to ensure that the ephemeral folder is deleted (as users cannot manage the data directory), but this is not a problem since it is already deleted every time the app is launched and the patching is finished.

cf. https://discord.com/channels/952946952348270622/1135563848586379264/1269377385669857282 (Discussion of this issue)

I can make a PR for this.

Version of ReVanced Manager and version & name of app you are patching

Flutter Manager v1.21.1 Compose Manager 32f2710 (compose-dev)

Patch logs

Examples of cache deletion issues:

[Case 1]
The input apk was deleted immediately after start patching.

- Logs
Copying APK
Reading APK
Decoding app manifest
Loading patches
Merging integrations
An error occurred:
java.io.FileNotFoundException: /data/user/0/app.revanced.manager.flutter/cache/patcher/tmp-MOERDE/base.apk

[Case 2]
The resources folder was deleted while executing patches.

- Logs
Copying APK
Reading APK
Decoding app manifest
Loading patches
Merging integrations
Deleting existing resource cache directory
Decoding resources
Executing patches
Applied 55 patches
SponsorBlock failed: app.revanced.patcher.patch.PatchException: 'SponsorBlock' depends on 'SponsorBlockResourcePatch' that raised an exception:
app.revanced.patcher.patch.PatchException: /data/user/0/app.revanced.manager.flutter/cache/patcher/tmp-ILRKZL/cache/res/layout/revanced_sb_inline_sponsor_overlay.xml
        at app.revanced.patcher.Patcher$apply$1.invokeSuspend$executePatch(Patcher.kt:185)
        at ... (omitted)
Caused by: java.nio.file.NoSuchFileException: /data/user/0/app.revanced.manager.flutter/cache/patcher/tmp-ILRKZL/cache/res/layout/revanced_sb_inline_sponsor_overlay.xml

[Case 3]
Failed to copy resources from Patches jar because the Patches jar was removed after start patches executing.
This will happens only on Flutter Manager.
Flow of reproduce: https://discord.com/channels/952946952348270622/1135563848586379264/1269404907522490502

Alternative thumbnails failed: app.revanced.patcher.patch.PatchException: 'Alternative thumbnails' depends on 'SettingsPatch' that raised an exception:
app.revanced.patcher.patch.PatchException: 'SettingsPatch' depends on 'SettingsResourcePatch' that raised an exception:
app.revanced.patcher.patch.PatchException
    at ... (omitted)

Caused by: java.lang.NullPointerException
    at app.revanced.util.ResourceUtilsKt.copyResources(ResourceUtils.kt:58)
    at app.revanced.patches.shared.misc.settings.BaseSettingsResourcePatch.execute(BaseSettingsResourcePatch.kt:33)
    at app.revanced.patches.youtube.misc.settings.SettingsResourcePatch.execute(SettingsResourcePatch.kt:29)
    at app.revanced.patches.youtube.misc.settings.SettingsResourcePatch.execute(SettingsResourcePatch.kt:13)
    at app.revanced.patcher.Patcher$apply$1.invokeSuspend$executePatch(Unknown Source:190)
    ... 20 more

Acknowledgements

oSumAtrIX commented 3 months ago

I can make a PR for this.

That would be helpful. I'll assign the issue to you