简体   繁体   English

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

[英]Android Marshmallow: new File (…) gives permission denied

EDITED to reflect progress. 编辑以反映进度。

If this question has been answered already, my apologies. 如果已经回答了这个问题,我深表歉意。

Currently writing an app where content (in a small zip file) is downloaded from an external location and stored inside /data/data/package_name_removed/files/, to be read later. 当前正在编写一个应用程序,其中的内容(在一个小的zip文件中)是从外部位置下载的,并存储在/ data / data / package_name_removed / files /中,以便稍后阅读。

I currently have a zip file in that directory "Test.zip". 我目前在该目录“ Test.zip”中有一个zip文件。

A try-catch loop containing: 一个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

Doesn't seem to work in Marshmallow: 在棉花糖中似乎不起作用:

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

This is a Log.d of the IOException getMessage; 这是IOException getMessage的Log.d; I think this one's generated from the new File() statement: 我认为这是从新的File()语句生成的:

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

I had this working in Lollipop; 我在棒棒糖上工作过; was debugging it for pre-lollipop too until I recently upgraded to Marshmallow. 也在调试它以用于棒棒糖之前,直到我最近升级到棉花糖为止。 I don't think I can safely continue until I sort this out, because it means my app won't support 6.0. 我认为在我解决这个问题之前我不能安全地继续,因为这意味着我的应用程序将不支持6.0。

I have managed to write a text file to this directory with no problems whatsoever with the same app, so I do not understand why attempting to open a zip file from that directory doesn't work. 我已经设法在同一个应用程序中毫无问题地将文本文件写入该目录,所以我不明白为什么尝试从该目录打开zip文件不起作用。

Edit: I have also attempted to unzip the offending zip file to its own directory but to no avail: 编辑:我也试图将有问题的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);

And got: 并得到:

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

I understand Marshmallow is really enforcing SELinux now, and I have to code for this eventuality - but I haven't been able to figure out how to get the proper permissions in code. 我了解Marshmallow现在确实在执行SELinux,因此我必须为此编写代码-但我还无法弄清楚如何在代码中获得适当的权限。 Rightfully I should not need a READ_EXTERNAL anywhere as according to the documentation, there is no need to ask for permissions to the app's own files directory in internal storage. 正确地,根据文档,我在任何地方都不需要READ_EXTERNAL,也无需请求对内部存储中应用程序自己文件目录的许可。

Any help? 有什么帮助吗?

Note: package name omitted, not important. 注意:包名省略,不重要。

From user CommonsWare: 从用户CommonsWare:

"Rooting does not disable SELinux" -- no, but it does not mean that files that >you create through a root shell somehow get the same permission bits/ACLs/etc. “ Rooting不会禁用SELinux”-否,但这并不意味着您通过根shell创建的文件会以某种方式获得相同的权限位/ ACL / etc。 as >do files you create through your application code. 作为>通过应用程序代码创建的文件。 adb shell run-as might behave >better in this fashion. adb shell运行方式可能会以这种方式表现更好。

Yep, it was indeed because I copied the file myself rather than have the app do it; 是的,确实是因为我自己复制了文件,而不是让应用程序来完成。 having r permissions across the board is not enough to satisfy SELinux, the app must "own" the file by being the one that "creates" it. 全面拥有r权限不足以满足SELinux,该应用必须通过“创建”该文件来“拥有”该文件。

I have yet to test this on the external write directory, but it is true for the internal app's own directory. 我尚未在外部写入目录上对此进行测试,但是对于内部应用程序自己的目录而言,这是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM