Skip to content

Commit

Permalink
[Backup] Fix UID and GUID on restoring data
Browse files Browse the repository at this point in the history
  • Loading branch information
MuntashirAkon committed Aug 27, 2020
1 parent f829544 commit eea79a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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 349
versionCode 350
versionName "2.5.14"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;

import org.json.JSONException;

Expand All @@ -47,6 +48,7 @@
import io.github.muntashirakon.AppManager.rules.RulesStorageManager;
import io.github.muntashirakon.AppManager.rules.compontents.ComponentsBlocker;
import io.github.muntashirakon.AppManager.runner.RootShellRunner;
import io.github.muntashirakon.AppManager.runner.Runner;
import io.github.muntashirakon.AppManager.runner.RunnerUtils;
import io.github.muntashirakon.AppManager.utils.IOUtils;
import io.github.muntashirakon.AppManager.utils.PackageUtils;
Expand Down Expand Up @@ -419,6 +421,16 @@ 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()) {
Log.e("BSM - Restore", "Failed to get restore owner for index " + i + ".");
return false;
}
}
}
if ((flags & BACKUP_RULES) != 0) {
Expand Down Expand Up @@ -461,6 +473,15 @@ private boolean cleanup(@NonNull File backupPath) {
return false;
}

@Nullable
public Pair<String, String> getUidAndGid(String filepath) {
Runner.Result result = RootShellRunner.runCommand(String.format("stat -c '%%u %%g' \"%s\"", filepath));
if (!result.isSuccessful()) return null;
String[] uidGid = result.getOutput().split(" ");
if (uidGid.length != 2) return null;
return new Pair<>(uidGid[0], uidGid[1]);
}

@Nullable
private File[] getSourceFiles(@NonNull File backupPath) {
return backupPath.listFiles((dir, name) -> name.startsWith(SOURCE_PREFIX));
Expand Down

0 comments on commit eea79a8

Please sign in to comment.