[core] Prefs for modules in non-primary user space (#582)

This commit is contained in:
LoveSy 2021-05-15 12:34:27 +08:00 committed by GitHub
parent 64efa50685
commit 5032bc0b15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 6 additions and 50 deletions

View File

@ -11,7 +11,5 @@ interface ILSPApplicationService {
String getPrefsPath(String packageName) = 7;
String getCachePath(String fileName) = 8;
ParcelFileDescriptor getModuleLogger() = 9;
}

View File

@ -108,17 +108,6 @@ public class LSPApplicationServiceClient implements ILSPApplicationService {
return null;
}
@Override
public String getCachePath(String fileName) {
try {
if (baseCachePath == null)
baseCachePath = service.getCachePath("");
return baseCachePath + File.separator + fileName;
} catch (RemoteException | NullPointerException ignored) {
}
return null;
}
@Override
public ParcelFileDescriptor getModuleLogger() {
try {

View File

@ -36,8 +36,6 @@ import org.lsposed.lspd.util.Utils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import de.robv.android.xposed.XC_MethodHook;
@ -84,7 +82,6 @@ public class HandleBindAppHooker extends XC_MethodHook {
boolean isModule = moduleBinder != null;
int xposedminversion = -1;
boolean xposedsharedprefs = false;
boolean xposedmigrateprefs = false;
try {
if (isModule) {
Map<String, Object> metaData = MetaDataReader.getMetaData(new File(appInfo.sourceDir));
@ -95,7 +92,6 @@ public class HandleBindAppHooker extends XC_MethodHook {
xposedminversion = MetaDataReader.extractIntPart((String) minVersionRaw);
}
xposedsharedprefs = metaData.containsKey("xposedsharedprefs");
xposedmigrateprefs = metaData.containsKey("xposedmigrateprefs");
}
} catch (NumberFormatException | IOException e) {
Hookers.logE("ApkParser fails", e);
@ -113,32 +109,10 @@ public class HandleBindAppHooker extends XC_MethodHook {
}
}
});
final boolean migratePrefs = xposedmigrateprefs;
XposedHelpers.findAndHookMethod(ContextImpl.class, "getPreferencesDir", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) {
File newDir = new File(serviceClient.getPrefsPath(appInfo.packageName));
if (migratePrefs) {
File oldDir = (File) param.getResult();
for (File oldFile : oldDir.listFiles()) {
Path oldPath = oldFile.toPath();
if (!Files.isSymbolicLink(oldPath)) {
Utils.logD("Migrating prefs file: " + oldFile.getAbsolutePath());
Path newPath = new File(newDir, oldFile.getName()).toPath();
try {
Files.move(oldPath, newPath);
try {
Files.createSymbolicLink(oldPath, newPath);
} catch (IOException e) {
Utils.logD("Symlink creation failed", e);
}
} catch (IOException e) {
Utils.logD("File move operation failed", e);
}
}
}
}
param.setResult(newDir);
param.setResult(new File(serviceClient.getPrefsPath(appInfo.packageName)));
}
});
}

View File

@ -620,8 +620,9 @@ public class ConfigManager {
return miscPath + File.separator + "cache" + File.separator + fileName;
}
public String getPrefsPath(String fileName) {
return miscPath + File.separator + "prefs" + File.separator + fileName;
public String getPrefsPath(String fileName, int uid) {
int userId = uid % PER_USER_RANGE;
return miscPath + File.separator + "prefs" + (userId == 0 ? "" : String.valueOf(userId)) + File.separator + fileName + File.separator;
}
public static void grantManagerPermission() {
@ -651,7 +652,7 @@ public class ConfigManager {
public boolean ensureModulePrefsPermission(int uid) {
String packageName = cachedModule.get(uid);
if (packageName == null) return false;
File path = new File(getPrefsPath(packageName));
File path = new File(getPrefsPath(packageName, uid));
try {
if (path.exists() && !path.isDirectory()) path.delete();
if (!path.exists()) Files.createDirectories(path.toPath());

View File

@ -79,13 +79,7 @@ public class LSPApplicationService extends ILSPApplicationService.Stub {
@Override
public String getPrefsPath(String packageName) throws RemoteException {
ensureRegistered();
return ConfigManager.getInstance().getPrefsPath(packageName);
}
@Override
public String getCachePath(String fileName) throws RemoteException {
ensureRegistered();
return ConfigManager.getInstance().getCachePath(fileName);
return ConfigManager.getInstance().getPrefsPath(packageName, Binder.getCallingUid());
}
@Override