Good morning,

I am trying to use Files.walk() in a Java 8 way but it is broken.

I am trying to create an app to manage files and size on all my Linux
system (so I would walk from the root directory and show the 5 biggest
files of each directory for example).

*JDK-8039910* has been raised for this issue but it is closed (2014-11-22).
Alan Bateman wrote that the method is working as intended but I don't see
how this could be intended because at the moment, it is unusable in a Java
8 way and for me.

When we use a terminal operations on the Stream returned by Files.walk(),
we get a crash because of AccessDeniedException

For example, here is the directory (rules.d) that makes everything crash :

[9:51:20 - ghabran@arch:/tmp/chromium-pepper-flash]$ ll /usr/share/polkit-1

total 8.0K
drwxr-xr-x 2 root root    4.0K Apr 14 09:18 actions
*drwxr-x--- 2 root polkitd 4.0K Apr 14 09:18 rules.d*

As you can see, as a simple user, I don't have access.

It is not possible to apply a filter to check the attributes to manage the
file/directory to see if we have the rights because an exception is thrown
before we get to check the attributes.

Here are several scenarios :

1) This doesn't crash (there seems to have no exception thrown). I get
error code 0, no exception is printed on my terminal.

try {
    Stream<Path> directoryTree = Files.walk(*Paths.get("/")*);

    directoryTree
        .filter(p -> {
            try {
            return Files.isRegularFile(p) && Files.isReadable(p);
            } catch (Exception e) {
            return false;
            }
        });
} catch (IOException ioe) {
    System.out.println(ioe);
}

2) this raises the exception AccessDeniedException (this goes in the catch)

try {
    Stream<Path> directoryTree = Files.walk(
*Paths.get("/usr/share/polkit-1/rules.d")*);

    directoryTree
        .filter(p -> {
            try {
            return Files.isRegularFile(p) && Files.isReadable(p);
            } catch (Exception e) {
            return false;
            }
        });
*} catch (IOException ioe) {*
*    System.out.println(ioe);*
*}*

3) this crashes with a stack trace : AccessDeniedException *(this doesn't
go in the catch)*

try {
    Stream<Path> directoryTree = Files.walk(*Paths.get("/")*);

    directoryTree
        .filter(p -> {
            try {
            return Files.isRegularFile(p) && Files.isReadable(p);
            } catch (Exception e) {
            return false;
            }
        })*.forEach(System.out::println);*
} catch (IOException ioe) {
    System.out.println(ioe);
}

4) this crashes but goes into the catch

try {
    Stream<Path> directoryTree = Files.walk(
*Paths.get("/usr/share/polkit-1/rules.d")*);

    directoryTree
        .filter(p -> {
            try {
            return Files.isRegularFile(p) && Files.isReadable(p);
            } catch (Exception e) {
            return false;
            }
        })*.forEach(System.out::println)*;
*} catch (IOException ioe) {*
*    System.out.println(ioe);*
*}*

In the meantime, I will use the old way with walkFileTree and a FileVisitor.

How am I supposed to walk() a directory tree in Java 8 without walk() ? I
was looking forward to using Stream<Path> to do it but I can't. What should
I do ?

Thank you.

Regards,

Gilles

Reply via email to