Android Marshmallow:新文件(...)授予拒绝权限

时间:2015-12-31 14:26:27

标签: android file zip android-6.0-marshmallow

已编辑以反映进展情况。

如果已经回答了这个问题,我很抱歉。

目前正在编写一个应用程序,其中内容(在一个小的zip文件中)从外部位置下载并存储在/ data / data / package_name_removed / files /中,以便稍后阅读。

我目前在该目录“Test.zip”中有一个zip文件。

包含以下内容的try-catch循环:

//where filename is Test.zip


Log.d("Target file is", sourceContext.getFilesDir() +"/"+ fileName);
File file = new File(sourceContext.getFilesDir() +"/"+ fileName);
ZipFile loadedFile = new ZipFile(file);  

//where sourceContext is passed into this object from the base Activity class

似乎不适用于棉花糖:

D/Target file is: /data/user/0/package_name_removed/files/Test.zip
W/package_name_removed: type=1400 audit(0.0:11699): avc: denied { open } for name="Test.zip" dev="mmcblk0p29" ino=57426 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:app_data_file:s0 tclass=file permissive=0

这是IOException getMessage的Log.d;我认为这是从新的File()语句生成的:

D/Log_title_omitted: /data/user/0/package_name_removed/files/Test.zip: open failed: EACCES (Permission denied)

我在Lollipop工作过;我正在为棒棒糖前调试它,直到我最近升级到Marshmallow。我不认为我可以安全地继续,直到我解决这个问题,因为这意味着我的应用程序将不支持6.0。

我已经设法将一个文本文件写入此目录,同时使用相同的应用程序没有任何问题,所以我不明白为什么尝试从该目录打开zip文件不起作用。

编辑:我还尝试将有问题的zip文件解压缩到自己的目录,但无济于事:

//where file is now a json file "Test.json"
//located in .getFilesDir()/Test/
String loadedName = fileName.substring(0, fileName.length() - 5); //I need to reuse the string later.".json" is 5 char.
Log.d("Target is", sourceContext.getFilesDir() +"/"+ loadedName +"/" + fileName);
File file = new File(sourceContext.getFilesDir() + "/"+ loadedStoryName +"/" + fileName);

得到了:

D/DOH: /data/user/0/package_name_removed/files/Test/Test.json: open failed: EACCES (Permission denied)
W/package_name_removed: type=1400 audit(0.0:22152): avc: denied { search } for name="Test" dev="mmcblk0p29" ino=57184 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:app_data_file:s0 tclass=dir permissive=0

我知道Marshmallow现在真正强制执行SELinux,我必须为这种可能性编写代码 - 但我无法弄清楚如何在代码中获得适当的权限。根据文档的说法,我不应该在任何地方需要READ_EXTERNAL,因此无需在内部存储中请求应用程序自己的文件目录的权限。

有任何帮助吗?

注意:包名称省略,不重要。

1 个答案:

答案 0 :(得分:0)

来自用户CommonsWare:

  

" Rooting不会禁用SELinux" - 不,但这并不意味着您通过root shell创建的文件以某种方式获得相同的权限位/ ACL /等。 as>执行您通过应用程序代码创建的文件。 adb shell run-as可能会以这种方式表现得更好。

是的,确实是因为我自己复制了文件,而不是让应用程序这样做;拥有r权限并不足以满足SELinux,应用程序必须拥有"拥有"该文件是"创建的文件"它

我还没有在外部写目录上测试它,但对于内部应用程序自己的目录也是如此。

相关问题