From 5b1c89a0c59b5671116d7ddf765617599d91834b Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sat, 23 Dec 2023 19:02:09 +0100 Subject: [PATCH] perf: Reduce amount of shell commands --- lib/services/patcher_api.dart | 4 +- lib/services/root_api.dart | 66 +++++++++---------- .../appInfoView/app_info_viewmodel.dart | 2 +- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/lib/services/patcher_api.dart b/lib/services/patcher_api.dart index 8efd18b036..48a431e1e3 100644 --- a/lib/services/patcher_api.dart +++ b/lib/services/patcher_api.dart @@ -229,7 +229,7 @@ class PatcherAPI { } else if (packageVersion == null) { installErrorDialog(1.2); } else if (packageVersion == patchedApp.version) { - return await _rootAPI.installApp( + return await _rootAPI.install( patchedApp.packageName, patchedApp.apkFilePath, outFile!.path, @@ -241,7 +241,7 @@ class PatcherAPI { } } else { if (await _rootAPI.hasRootPermissions()) { - await _rootAPI.unmount(patchedApp.packageName); + await _rootAPI.uninstall(patchedApp.packageName); } if (context.mounted) { return await installApk( diff --git a/lib/services/root_api.dart b/lib/services/root_api.dart index 83d02e4b6e..3d45968885 100644 --- a/lib/services/root_api.dart +++ b/lib/services/root_api.dart @@ -43,21 +43,22 @@ class RootAPI { String filePath, ) async { try { + final StringBuffer commands = StringBuffer(); if (permissions.isNotEmpty) { - await Root.exec( - cmd: 'chmod $permissions "$filePath"', - ); + commands.writeln('chmod $permissions "$filePath"'); } + if (ownerGroup.isNotEmpty) { - await Root.exec( - cmd: 'chown $ownerGroup "$filePath"', - ); + commands.writeln('chown $ownerGroup "$filePath"'); } + if (seLinux.isNotEmpty) { - await Root.exec( - cmd: 'chcon $seLinux "$filePath"', - ); + commands.writeln('chcon $seLinux "$filePath"'); } + + await Root.exec( + cmd: commands.toString(), + ); } on Exception catch (e) { if (kDebugMode) { print(e); @@ -91,16 +92,12 @@ class RootAPI { return apps; } - Future unmount(String packageName) async { - await Root.exec( - cmd: - 'grep $packageName /proc/mounts | while read -r line; do echo \$line | cut -d " " -f 2 | sed "s/apk.*/apk/" | xargs -r umount -l; done', - ); - await Root.exec( - cmd: 'rm -rf "$_revancedDirPath/$packageName"', - ); + Future uninstall(String packageName) async { await Root.exec( - cmd: 'rm -rf "$_serviceDDirPath/$packageName.sh"', + cmd: ''' + grep $packageName /proc/mounts | while read -r line; do echo \$line | cut -d " " -f 2 | sed "s/apk.*/apk/" | xargs -r umount -l; done + rm -rf $_revancedDirPath/$packageName $_serviceDDirPath/$packageName.sh + ''', ); } @@ -115,29 +112,25 @@ class RootAPI { rm "$_postFsDataDirPath/\$filename" fi done - ''' - .trim(), + ''', ); } - Future installApp( + Future install( String packageName, String originalFilePath, String patchedFilePath, ) async { try { - await Root.exec( - cmd: 'mkdir -p "$_revancedDirPath/$packageName"', - ); await setPermissions( '0755', 'shell:shell', '', '$_revancedDirPath/$packageName', ); + await installPatchedApk(packageName, patchedFilePath); await installServiceDScript(packageName); - await installApk(packageName, patchedFilePath); - await mountApk(packageName); + await runMountScript(packageName); return true; } on Exception catch (e) { if (kDebugMode) { @@ -159,6 +152,9 @@ class RootAPI { until [ "\$(getprop sys.boot_completed)" = 1 ]; do sleep 3; done until [ -d "/sdcard/Android" ]; do sleep 1; done + # Unmount any existing installation to prevent multiple unnecessary mounts. + grep $packageName /proc/mounts | while read -r line; do echo \$line | cut -d " " -f 2 | sed "s/apk.*/apk/" | xargs -r umount -l; done + base_path=$_revancedDirPath/$packageName/base.apk stock_path=\$(pm path $packageName | grep base | sed "s/package://g" ) @@ -176,10 +172,14 @@ class RootAPI { await setPermissions('0744', '', '', scriptFilePath); } - Future installApk(String packageName, String patchedFilePath) async { + Future installPatchedApk( + String packageName, String patchedFilePath) async { final String newPatchedFilePath = '$_revancedDirPath/$packageName/base.apk'; await Root.exec( - cmd: 'cp "$patchedFilePath" "$newPatchedFilePath"', + cmd: ''' + mkdir -p "$_revancedDirPath/$packageName" + cp "$patchedFilePath" "$newPatchedFilePath" + ''', ); await setPermissions( '0644', @@ -189,16 +189,10 @@ class RootAPI { ); } - Future mountApk( + Future runMountScript( String packageName, ) async { - await Root.exec( - cmd: ''' - grep $packageName /proc/mounts | while read -r line; do echo \$line | cut -d " " -f 2 | sed "s/apk.*/apk/" | xargs -r umount -l; done - .$_serviceDDirPath/$packageName.sh - ''' - .trim(), - ); + await Root.exec(cmd: '.$_serviceDDirPath/$packageName.sh'); } Future fileExists(String path) async { diff --git a/lib/ui/widgets/appInfoView/app_info_viewmodel.dart b/lib/ui/widgets/appInfoView/app_info_viewmodel.dart index d4652f445e..d4274972a1 100644 --- a/lib/ui/widgets/appInfoView/app_info_viewmodel.dart +++ b/lib/ui/widgets/appInfoView/app_info_viewmodel.dart @@ -29,7 +29,7 @@ class AppInfoViewModel extends BaseViewModel { if (app.isRooted) { final bool hasRootPermissions = await _rootAPI.hasRootPermissions(); if (hasRootPermissions) { - await _rootAPI.unmount( + await _rootAPI.uninstall( app.packageName, ); if (!onlyUnpatch) {