Skip to content

Commit

Permalink
[Backup] Fetch uid and gid before restoring data
Browse files Browse the repository at this point in the history
  • Loading branch information
MuntashirAkon committed Aug 27, 2020
1 parent eea79a8 commit 38b3747
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ android {
applicationId 'io.github.muntashirakon.AppManager'
minSdkVersion 21
targetSdkVersion 29
versionCode 350
versionCode 351
versionName "2.5.14"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,16 @@ public boolean restore() {
for (int i = 0; i<metadataV1.dataDirs.length; ++i) {
dataSource = metadataV1.dataDirs[i];
dataFiles = getDataFiles(backupPath, i);
Pair<String, String> uidAndGid = getUidAndGid(dataSource);
if (dataFiles == null) {
Log.e("BSM - Restore", "Data restore is requested but there are no data files for index " + i + ".");
return false;
}
// Fix UID and GID
if (RunnerUtils.fileExists(dataSource) && uidAndGid == null) {
Log.e("BSM - Restore", "Failed to get owner info for index " + i + ".");
return false;
}
StringBuilder cmdData = new StringBuilder();
// FIXME: Fix API 23 external storage issue
for (File file: dataFiles) cmdData.append(" \"").append(file.getAbsolutePath()).append("\"");
Expand All @@ -421,13 +427,7 @@ public boolean restore() {
// Clear cache if exists: return value is not important for us
RootShellRunner.runCommand(String.format("rm -rf %s/cache %s/code_cache", dataSource, dataSource));
}
// Fix UID and GID
Pair<String, String> uidAndGid = getUidAndGid(dataSource);
if (uidAndGid == null) {
Log.e("BSM - Restore", "Failed to get owner info for index " + i + ".");
return false;
}
if (!RootShellRunner.runCommand(String.format("chown -R %s:%s \"%s\"", uidAndGid.first, uidAndGid.second, dataSource)).isSuccessful()) {
if (uidAndGid != null && !RootShellRunner.runCommand(String.format("chown -R %s:%s \"%s\"", uidAndGid.first, uidAndGid.second, dataSource)).isSuccessful()) {
Log.e("BSM - Restore", "Failed to get restore owner for index " + i + ".");
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.File;
import java.util.List;

import androidx.annotation.NonNull;
import io.github.muntashirakon.AppManager.AppManager;
import io.github.muntashirakon.AppManager.BuildConfig;

Expand All @@ -36,9 +37,9 @@ public final class RunnerUtils {
public static final String CMD_CLEAR_CACHE_PREFIX = "rm -rf";
public static final String CMD_CLEAR_CACHE_DIR_SUFFIX = " %s/cache %s/code_cache";
public static final String CMD_CLEAR_PACKAGE_DATA = CMD_PM + " clear %s";
public static final String CMD_ENABLE_PACKAGE = CMD_PM + " enable %s";
public static final String CMD_ENABLE_PACKAGE = CMD_PM + " enable %s";
public static final String CMD_DISABLE_PACKAGE = CMD_PM + " disable-user %s";
public static final String CMD_FORCE_STOP_PACKAGE = CMD_AM + " force-stop %s";
public static final String CMD_FORCE_STOP_PACKAGE = CMD_AM + " force-stop %s";
public static final String CMD_UNINSTALL_PACKAGE = CMD_PM + " uninstall -k --user 0 %s";
public static final String CMD_UNINSTALL_PACKAGE_WITH_DATA = CMD_PM + " uninstall --user 0 %s";
public static final String CMD_INSTALL_PACKAGE = CMD_PM + " install --user 0 -r -i " + BuildConfig.APPLICATION_ID + " %s";
Expand All @@ -60,6 +61,7 @@ public final class RunnerUtils {
public static final String CMD_PID_PACKAGE = "pidof %s";
public static final String CMD_KILL_SIG9 = "kill -9 %s";

@NonNull
public static Runner.Result clearPackageCache(String packageName) {
try {
ApplicationInfo applicationInfo = AppManager.getContext().getPackageManager().getApplicationInfo(packageName, 0);
Expand Down Expand Up @@ -107,47 +109,62 @@ public String getOutput() {
}
}

@NonNull
public static Runner.Result clearPackageData(String packageName) {
return Runner.runCommand(String.format(CMD_CLEAR_PACKAGE_DATA, packageName));
}

@NonNull
public static Runner.Result enablePackage(String packageName) {
return Runner.runCommand(String.format(CMD_ENABLE_PACKAGE, packageName));
}

@NonNull
public static Runner.Result disablePackage(String packageName) {
return Runner.runCommand(String.format(CMD_DISABLE_PACKAGE, packageName));
}

@NonNull
public static Runner.Result forceStopPackage(String packageName) {
return Runner.runCommand(String.format(CMD_FORCE_STOP_PACKAGE, packageName));
}

@NonNull
public static Runner.Result uninstallPackageWithoutData(String packageName) {
return Runner.runCommand(String.format(CMD_UNINSTALL_PACKAGE, packageName));
}

@NonNull
public static Runner.Result uninstallPackageWithData(String packageName) {
return Runner.runCommand(String.format(CMD_UNINSTALL_PACKAGE_WITH_DATA, packageName));
}

@NonNull
public static Runner.Result installPackage(String packageLocation) {
return Runner.runCommand(String.format(CMD_INSTALL_PACKAGE, packageLocation));
}

@NonNull
public static Runner.Result disableComponent(String packageName, String componentName) {
return Runner.runCommand(String.format(CMD_COMPONENT_DISABLE, packageName, componentName));
}

@NonNull
public static Runner.Result enableComponent(String packageName, String componentName) {
return Runner.runCommand(String.format(CMD_COMPONENT_ENABLE, packageName, componentName));
}

@NonNull
public static Runner.Result grantPermission(String packageName, String permissionName) {
return Runner.runCommand(String.format(CMD_PERMISSION_GRANT, packageName, permissionName));
}

@NonNull
public static Runner.Result revokePermission(String packageName, String permissionName) {
return Runner.runCommand(String.format(CMD_PERMISSION_REVOKE, packageName, permissionName));
}

public static boolean fileExists(String fileName) {
return Runner.runCommand(String.format("test -e \"%s\"", fileName)).isSuccessful();
}
}

0 comments on commit 38b3747

Please sign in to comment.