【问题标题】:android.os.FileUtils.setPermissions() returns 1 in Android Nougatandroid.os.FileUtils.setPermissions() 在 Android Nougat 中返回 1
【发布时间】:2017-01-19 07:54:37
【问题描述】:

我使用反射(查找类android.os.FileUtils和方法setPermissions() 将我的文件(捆绑在应用程序包目录中)标记为可执行文件。它在Android Nougat 之前工作,但在N 上它返回1:

ZipHelper:

Integer result = FileHelper.chmod(outFile, 0755);
if (result == null || result != 0)
     android.util.Log.d("ZipHelper", "chmod failed with error result " + result.intValue());

文件助手:

public static void init_chmod() throws ClassNotFoundException, NoSuchMethodException {
    fileUtils = Class.forName("android.os.FileUtils");
    setPermissions = fileUtils.getMethod("setPermissions", String.class, int.class, int.class, int.class);
}

public static int chmod(File path, int mode) throws Exception {
    return (Integer) setPermissions.invoke(null, path.getAbsolutePath(), mode, -1, -1);
}

在日志中:

01-19 12:46:39.365    2558-2667/name.myname.android.app D/ZipHelper﹕ chmod failed with error result 1
01-19 12:46:39.390    2558-2667/name.myname.android.app W/FileUtils﹕ Failed to chmod(/storage/emulated/0/app/tutorials/cplusplus.com#1.0#1/compounddatatypes/arrays/multidimensional/source3.cpp): android.system.ErrnoException: chmod failed: EPERM (Operation not permitted)

我的应用程序以21 为目标,因此我没有按照 N 所需的方式管理权限。该应用程序自动获得“存储”权限,我可以在 N AVD 的应用程序设置中看到它。我正在模拟器中测试。

返回码1 是什么意思? 我怀疑它与安全性有关,但我不知道应该做什么(我看到了 N 的一般许可方法,但在我的情况下需要什么许可,期望已经授予“存储”)。 我应该怎么做才能让它再次工作?

【问题讨论】:

    标签: android security executable android-7.0-nougat


    【解决方案1】:

    查看 android.os.FileUtils (https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/FileUtils.java) 的源代码,setPermissions 方法在后台执行 chmod,并返回 errno。

    1 = EPERM,或“不允许操作”。

    你能在文件上调用 setExecutable 吗?

    File myFile;
    myFile.setExecutable(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-11
      • 2012-07-23
      • 2017-09-12
      • 1970-01-01
      • 2014-07-20
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多