机器人如何通过编程设置RW-R-- R--权限机器人、权限、RW

2023-09-06 16:31:27 作者:留我静待彷徨

我正在开发能恢复的应用程序数据恢复/数据/数据​​/ {}的packageName的应用程序。恢复文件后,我设置了权限RW-R-- R--。我以这种方式进行设置:

 公众诠释搭配chmod(文件路径,诠释模式)抛出异常{
    类文件实用=的Class.forName(android.os.FileUtils);
    方法setPermissions = fileUtils.getMethod(setPermissions
            为String.class,int.class,int.class,int.class);
    返程(整数)setPermissions.invoke(NULL,path.getAbsolutePath()
            模式,-1,-1);
}
 

和调用 CHMOD(文件,644);

但是,当我在文件浏览器检查这些文件的权限,它为我--- RWX RX。

所以,我怎么能设置权限为RW-R--的R - ?

解决方案

 工艺过程= NULL;
DataOutputStream类DataOutputStream类= NULL;

尝试 {
    流程=调用Runtime.getRuntime()EXEC(素)。
    DataOutputStream类=新DataOutputStream类(process.getOutputStream());
    dataOutputStream.writeBytes(搭配chmod 644文件路径的\ n);
    dataOutputStream.writeBytes(退出\ n);
    dataOutputStream.flush();
    process.waitFor();
}赶上(例外五){
    返回false;
} 最后 {
    尝试 {
        如果(DataOutputStream类!= NULL){
            dataOutputStream.close();
        }
        process.destroy();
    }赶上(例外五){
    }
}
 

I am developing an application that can restore apps' data back to /data/data/{packageName}. After restoring the files, I am setting the permissions to rw- r-- r--. I set it in this way:

public int chmod(File path, int mode) throws Exception {
    Class fileUtils = Class.forName("android.os.FileUtils");
    Method setPermissions = fileUtils.getMethod("setPermissions",
            String.class, int.class, int.class, int.class);
    return (Integer) setPermissions.invoke(null, path.getAbsolutePath(),
            mode, -1, -1);
}

and calling chmod(file, 644);

But when I check these files' permissions in file explorer it shows me "--- rwx r-x".

So how can I set the permissions to rw- r-- r--?

解决方案

Process process = null;
DataOutputStream dataOutputStream = null;

try {
    process = Runtime.getRuntime().exec("su");
    dataOutputStream = new DataOutputStream(process.getOutputStream());
    dataOutputStream.writeBytes("chmod 644 FilePath\n");
    dataOutputStream.writeBytes("exit\n");
    dataOutputStream.flush();
    process.waitFor();
} catch (Exception e) {
    return false;
} finally {
    try {
        if (dataOutputStream != null) {
            dataOutputStream.close();
        }
        process.destroy();
    } catch (Exception e) {
    }
}