perf: Reduce amount of shell commands

This commit is contained in:
oSumAtrIX 2023-12-23 19:02:09 +01:00
parent 8a1ab478a3
commit 5b1c89a0c5
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
3 changed files with 33 additions and 39 deletions

View File

@ -229,7 +229,7 @@ class PatcherAPI {
} else if (packageVersion == null) { } else if (packageVersion == null) {
installErrorDialog(1.2); installErrorDialog(1.2);
} else if (packageVersion == patchedApp.version) { } else if (packageVersion == patchedApp.version) {
return await _rootAPI.installApp( return await _rootAPI.install(
patchedApp.packageName, patchedApp.packageName,
patchedApp.apkFilePath, patchedApp.apkFilePath,
outFile!.path, outFile!.path,
@ -241,7 +241,7 @@ class PatcherAPI {
} }
} else { } else {
if (await _rootAPI.hasRootPermissions()) { if (await _rootAPI.hasRootPermissions()) {
await _rootAPI.unmount(patchedApp.packageName); await _rootAPI.uninstall(patchedApp.packageName);
} }
if (context.mounted) { if (context.mounted) {
return await installApk( return await installApk(

View File

@ -43,21 +43,22 @@ class RootAPI {
String filePath, String filePath,
) async { ) async {
try { try {
final StringBuffer commands = StringBuffer();
if (permissions.isNotEmpty) { if (permissions.isNotEmpty) {
await Root.exec( commands.writeln('chmod $permissions "$filePath"');
cmd: 'chmod $permissions "$filePath"',
);
} }
if (ownerGroup.isNotEmpty) { if (ownerGroup.isNotEmpty) {
await Root.exec( commands.writeln('chown $ownerGroup "$filePath"');
cmd: 'chown $ownerGroup "$filePath"',
);
} }
if (seLinux.isNotEmpty) { if (seLinux.isNotEmpty) {
await Root.exec( commands.writeln('chcon $seLinux "$filePath"');
cmd: 'chcon $seLinux "$filePath"',
);
} }
await Root.exec(
cmd: commands.toString(),
);
} on Exception catch (e) { } on Exception catch (e) {
if (kDebugMode) { if (kDebugMode) {
print(e); print(e);
@ -91,16 +92,12 @@ class RootAPI {
return apps; return apps;
} }
Future<void> unmount(String packageName) async { Future<void> uninstall(String packageName) async {
await Root.exec( await Root.exec(
cmd: 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', 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
await Root.exec( ''',
cmd: 'rm -rf "$_revancedDirPath/$packageName"',
);
await Root.exec(
cmd: 'rm -rf "$_serviceDDirPath/$packageName.sh"',
); );
} }
@ -115,29 +112,25 @@ class RootAPI {
rm "$_postFsDataDirPath/\$filename" rm "$_postFsDataDirPath/\$filename"
fi fi
done done
''' ''',
.trim(),
); );
} }
Future<bool> installApp( Future<bool> install(
String packageName, String packageName,
String originalFilePath, String originalFilePath,
String patchedFilePath, String patchedFilePath,
) async { ) async {
try { try {
await Root.exec(
cmd: 'mkdir -p "$_revancedDirPath/$packageName"',
);
await setPermissions( await setPermissions(
'0755', '0755',
'shell:shell', 'shell:shell',
'', '',
'$_revancedDirPath/$packageName', '$_revancedDirPath/$packageName',
); );
await installPatchedApk(packageName, patchedFilePath);
await installServiceDScript(packageName); await installServiceDScript(packageName);
await installApk(packageName, patchedFilePath); await runMountScript(packageName);
await mountApk(packageName);
return true; return true;
} on Exception catch (e) { } on Exception catch (e) {
if (kDebugMode) { if (kDebugMode) {
@ -159,6 +152,9 @@ class RootAPI {
until [ "\$(getprop sys.boot_completed)" = 1 ]; do sleep 3; done until [ "\$(getprop sys.boot_completed)" = 1 ]; do sleep 3; done
until [ -d "/sdcard/Android" ]; do sleep 1; 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 base_path=$_revancedDirPath/$packageName/base.apk
stock_path=\$(pm path $packageName | grep base | sed "s/package://g" ) stock_path=\$(pm path $packageName | grep base | sed "s/package://g" )
@ -176,10 +172,14 @@ class RootAPI {
await setPermissions('0744', '', '', scriptFilePath); await setPermissions('0744', '', '', scriptFilePath);
} }
Future<void> installApk(String packageName, String patchedFilePath) async { Future<void> installPatchedApk(
String packageName, String patchedFilePath) async {
final String newPatchedFilePath = '$_revancedDirPath/$packageName/base.apk'; final String newPatchedFilePath = '$_revancedDirPath/$packageName/base.apk';
await Root.exec( await Root.exec(
cmd: 'cp "$patchedFilePath" "$newPatchedFilePath"', cmd: '''
mkdir -p "$_revancedDirPath/$packageName"
cp "$patchedFilePath" "$newPatchedFilePath"
''',
); );
await setPermissions( await setPermissions(
'0644', '0644',
@ -189,16 +189,10 @@ class RootAPI {
); );
} }
Future<void> mountApk( Future<void> runMountScript(
String packageName, String packageName,
) async { ) async {
await Root.exec( await Root.exec(cmd: '.$_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
.$_serviceDDirPath/$packageName.sh
'''
.trim(),
);
} }
Future<bool> fileExists(String path) async { Future<bool> fileExists(String path) async {

View File

@ -29,7 +29,7 @@ class AppInfoViewModel extends BaseViewModel {
if (app.isRooted) { if (app.isRooted) {
final bool hasRootPermissions = await _rootAPI.hasRootPermissions(); final bool hasRootPermissions = await _rootAPI.hasRootPermissions();
if (hasRootPermissions) { if (hasRootPermissions) {
await _rootAPI.unmount( await _rootAPI.uninstall(
app.packageName, app.packageName,
); );
if (!onlyUnpatch) { if (!onlyUnpatch) {