Files.walk() is unusable because of AccessDeniedException
Paul Sandoz
paul.sandoz at oracle.com
Thu May 26 10:04:58 UTC 2016
Hi,
Jumping in a bit late to this thread…
To achieve this with parity to the non-Stream walk method we would need to implement a FileTreeSpliterator that accepts a FileVistor for the purposes of specialised filtering. You can do this with an Iterator-based implementation from which a Spliterator can be created from, but you could just as easily inherit from AbstractSpliterator. Then we could surface a new method accepting a FileVistor.
You could implement your own Spliterator along such lines then create a Stream from it.
If you go that route and wanna contribute back to OpenJDK I would be happy to help in the acceptance and processing of such a contribution.
Paul.
> On 25 May 2016, at 09:39, Gilles Habran <gilleshabran at gmail.com> wrote:
>
> Hi Henry,
>
> I knew there was a work around as I created something similar than yours
> when I needed it. :)
>
> Apparently, we have two different opinions regarding Files.walk() and how
> this method is supposed to handle permissions. I just sent this email
> because I felt that Files.walk() didn't behave like it should, maybe I am
> wrong.
>
> I read everything from my root directory so it makes sense that that I
> don't have the permissions to read everything (I'll get the same thing when
> I read /proc for sure) but what happens the day I walk the home directory
> of the user as the user. I am supposed to have access to everything in
> there, right ? My application will just crash if there is a dirty file in
> there with bad permissions and there is nothing I can do about it... not
> with Files.walk().
>
> To be sure that I don't have a problem, I will need to create my own method
> like yours because I would take a great risk using Files.walk() if I get an
> exception that stops my process in case there is an unexpected file with
> bad permissions.
>
> Have a nice day.
>
> Best regards,
>
> Gilles
>
> On 25 May 2016 at 08:23, Henry Jen <henry.jen at oracle.com> wrote:
>
>> I think there is a work-around, use list() and flatMap() should get you
>> what you needed.
>>
>> The API is designed to walk a tree where you suppose to have access with.
>> If OS level cause an IOException, that need to be dealt with. Acknowledged
>> that exception handling is not a strong suite in Stream API, developer will
>> need to do some work.
>>
>> Files.find() also allows you to get entries and filter out by permission.
>> What you can do is make sure you have permission on the top level, then
>> call find with maxDepth 1 to only get entries on that directory.
>>
>> Combined with flatMap(), you should be able to get what you want. Try the
>> following code to see if it works for you.
>>
>> import java.nio.file.Files;
>> import java.nio.file.Path;
>> import java.nio.file.Paths;
>> import java.util.stream.Stream;
>> import java.io.IOException;
>>
>> public class ListCanRead {
>> static Stream<Path> walkReadable(Path p) {
>> if (Files.isReadable(p)) {
>> if (Files.isDirectory(p)) {
>> try {
>> return Stream.concat(Stream.of(p), Files.list(p));
>> } catch (IOException ioe) {
>> return Stream.of(p);
>> }
>> } else {
>> return Stream.of(p);
>> }
>> }
>> return Stream.of(p);
>> }
>>
>> public static void main(String[] args) throws IOException {
>> System.out.println("List directory: " + args[0]);
>> walkReadable(Paths.get(args[0])).flatMap(ListCanRead::walkReadable)
>> .forEach(System.out::println);
>>
>> Files.walk(Paths.get(args[0]))
>> .forEach(System.out::println); // Could throw
>> AccessDeniedException
>> }
>> }
>>
>> Cheers,
>> Henry
>>
>> On May 24, 2016 at 4:48:30 PM, Gilles Habran (gilleshabran at gmail.com)
>> wrote:
>>> Good morning,
>>>
>>> well I would like to be able to manage the outcome of the processing
>> myself
>>> and not get an exception in a part where I have no control.
>>>
>>> For example, I would expect to get an exception when I tried to read a
>> file
>>> where I don't have the permission. I would not expect to get an exception
>>> when Java creates the Stream.
>>>
>>> Maybe I am the only one to have a problem with this ? I don't know but it
>>> feels strange to be forced to execute a software with root permissions
>>> where I don't even plan to read file I cannot read because of lack of
>>> permissions.
>>>
>>> For me, we should be able to test the attributes of a file and depending
>> on
>>> the result, read the file or not. If we read the file without permission,
>>> we get an exception, if not, we can go to the next file without error.
>>>
>>> If that's unclear, please let me know, I'll try to give more informations
>>> or examples.
>>>
>>> Thank you.
>>>
>>> On 24 May 2016 at 10:19, Andrew Haley wrote:
>>>
>>>> On 05/20/2016 10:38 AM, Gilles Habran wrote:
>>>>> why is my message still waiting for approval after a month ?
>>>>
>>>> What is it you want Java to do? You can't walk the directory
>>>> because you don't have permission. sudo should work.
>>>>
>>>> Andrew.
>>>>
>>>>
>>>
>>
>>
More information about the core-libs-dev
mailing list