Refine codes

This commit is contained in:
LoveSy 2023-01-08 17:44:56 +08:00 committed by LoveSy
parent 6b03c6397a
commit ae5f8a70ae
3 changed files with 16 additions and 14 deletions

View File

@ -69,6 +69,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@ -81,6 +82,7 @@ public class ConfigFileManager {
static final Path modulePath = basePath.resolve("modules");
static final Path daemonApkPath = Paths.get(System.getProperty("java.class.path", null));
static final Path managerApkPath = basePath.resolve("manager.apk");
static final Path dummyPath = Paths.get("/", UUID.randomUUID().toString());
static final File magiskDbPath = new File("/data/adb/magisk.db");
private static final Path lockPath = basePath.resolve("lock");
private static final Path configDirPath = basePath.resolve("config");
@ -429,7 +431,7 @@ public class ConfigFileManager {
return preloadDex;
}
static void ensureValidPath(String path) throws RemoteException {
static void ensureModuleFilePath(String path) throws RemoteException {
if (path == null || path.indexOf(File.separatorChar) >= 0 || ".".equals(path) || "..".equals(path)) {
throw new RemoteException("Invalid path: " + path);
}

View File

@ -1,5 +1,6 @@
package org.lsposed.lspd.service;
import static org.lsposed.lspd.service.LSPModuleService.FILES_DIR;
import static org.lsposed.lspd.service.PackageService.PER_USER_RANGE;
import android.os.Binder;
@ -9,7 +10,6 @@ import android.os.RemoteException;
import org.lsposed.lspd.models.Module;
import java.io.File;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@ -45,11 +45,11 @@ public class LSPInjectedModuleService extends ILSPInjectedModuleService.Stub {
@Override
public ParcelFileDescriptor openRemoteFile(String path) throws RemoteException {
ConfigFileManager.ensureValidPath(path);
ConfigFileManager.ensureModuleFilePath(path);
var userId = Binder.getCallingUid() / PER_USER_RANGE;
try {
var dirPath = ConfigFileManager.resolveModuleDir(loadedModule.packageName, "files", userId, -1);
return ParcelFileDescriptor.open(dirPath.resolve(path).toFile(), ParcelFileDescriptor.MODE_READ_ONLY);
var dir = ConfigFileManager.resolveModuleDir(loadedModule.packageName, FILES_DIR, userId, -1);
return ParcelFileDescriptor.open(dir.resolve(path).toFile(), ParcelFileDescriptor.MODE_READ_ONLY);
} catch (Throwable e) {
throw new RemoteException(e.getMessage());
}
@ -57,9 +57,9 @@ public class LSPInjectedModuleService extends ILSPInjectedModuleService.Stub {
@Override
public String[] getRemoteFileList() throws RemoteException {
var userId = Binder.getCallingUid() / PER_USER_RANGE;
try {
var userId = Binder.getCallingUid() / PER_USER_RANGE;
var dir = ConfigFileManager.resolveModuleDir(loadedModule.packageName, "files", userId, -1);
var dir = ConfigFileManager.resolveModuleDir(loadedModule.packageName, FILES_DIR, userId, -1);
var files = dir.toFile().list();
return files == null ? new String[0] : files;

View File

@ -35,8 +35,6 @@ import androidx.annotation.NonNull;
import org.lsposed.daemon.BuildConfig;
import org.lsposed.lspd.models.Module;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -54,6 +52,8 @@ public class LSPModuleService extends IXposedService.Stub {
private final static Set<Integer> uidSet = ConcurrentHashMap.newKeySet();
private final static Map<Module, LSPModuleService> serviceMap = new WeakHashMap<>();
public final static String FILES_DIR = "files";
private final @NonNull
Module loadedModule;
@ -226,9 +226,9 @@ public class LSPModuleService extends IXposedService.Stub {
@Override
public ParcelFileDescriptor openRemoteFile(String path, int mode) throws RemoteException {
var userId = ensureModule();
ConfigFileManager.ensureValidPath(path);
ConfigFileManager.ensureModuleFilePath(path);
try {
var dir = ConfigFileManager.resolveModuleDir(loadedModule.packageName, "files", userId, Binder.getCallingUid());
var dir = ConfigFileManager.resolveModuleDir(loadedModule.packageName, FILES_DIR, userId, Binder.getCallingUid());
return ParcelFileDescriptor.open(dir.resolve(path).toFile(), mode);
} catch (Throwable e) {
throw new RemoteException(e.getMessage());
@ -238,9 +238,9 @@ public class LSPModuleService extends IXposedService.Stub {
@Override
public boolean deleteRemoteFile(String path) throws RemoteException {
var userId = ensureModule();
ConfigFileManager.ensureValidPath(path);
ConfigFileManager.ensureModuleFilePath(path);
try {
var dir = ConfigFileManager.resolveModuleDir(loadedModule.packageName, "files", userId, Binder.getCallingUid());
var dir = ConfigFileManager.resolveModuleDir(loadedModule.packageName, FILES_DIR, userId, Binder.getCallingUid());
return dir.resolve(path).toFile().delete();
} catch (Throwable e) {
throw new RemoteException(e.getMessage());
@ -251,7 +251,7 @@ public class LSPModuleService extends IXposedService.Stub {
public String[] listRemoteFiles() throws RemoteException {
var userId = ensureModule();
try {
var dir = ConfigFileManager.resolveModuleDir(loadedModule.packageName, "files", userId, Binder.getCallingUid());
var dir = ConfigFileManager.resolveModuleDir(loadedModule.packageName, FILES_DIR, userId, Binder.getCallingUid());
var files = dir.toFile().list();
return files == null ? new String[0] : files;
} catch (Throwable e) {