When running Files.walk(Paths.get(\"/var/\")).count()
as an unprivileged user, the execution might throw an exception as there are folders inside /var/
If what you want is actually skipping the paths where you have no access, you have two approaches:
Streams
In the answer to this question it is explained how to obtain the stream of all files of a subtree you can access.
But this example can be expanded to other use cases too.
FileVisitor
Using a FileVisitor
adds a lot of code, but grants you much more flexibility when walking directory trees. To solve the same problem you can replace Files.walk()
with:
Files.walkFileTree(Path start, FileVisitor<? super Path> visitor);
extending SimpleFileVisitor (to count the files) and overriding some methods.
You can:
visitFileFailed
method, to handle the case you cannot access a file for some reasons; (Lukasz_Plawny's advice)preVisitDirectory
method, checking for permissions before accessing the directory: if you can't access it, you can simply skip its subtree (keep in mind that you may be able to access a directory, but not all its files);e.g. 1
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
// you can log the exception 'exc'
return FileVisitResult.SKIP_SUBTREE;
}
e.g. 2
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
if(!Files.isReadable(dir))
return FileVisitResult.SKIP_SUBTREE;
return FileVisitResult.CONTINUE;
}
FileVisitor docs
FileVisitor tutorial
Hope it helps.
There is no easy way to change permissions. Java is not good at these tasks. There are only some tricks like check permissions on start and try to change permissions via su/sudo then restart application or using Java-gnome. Please read a bit more here: Java: Ask root privileges on Ubuntu
Just a few completely untested ideas:
1) Run your app with root priviledges to begin with:
sudo java -jar myapp.jar
2) Let your app start a launcher-class that requests root permissions and then continues running the rest of your app:
java -jar myapp.jar
This in turn does execute a shell command, but only an xterm that prompts for root password, and then continues to run a java program with root permissions:
xterm -e "sudo sh -c 'java -jar /tmp/myrootapp.jar'"
or perhaps use something nicer-looking using gksudo. Mind the '
and "
.
Maybe the myapp.jar
extracts itself into a temporary directory. myapp.jar
contains myrootapp.jar
and thus it can launch it as described above. /tmp
should of course be retrieved from within java, and preferably be a directory with a random name that only the user running myapp.jar
has access to in order to prevent myrootapp.jar
injection.
Cross-platform
You mentioned /var/
yourself, so I assumed you were on some sort of Linux. If this is supposed to work cross-platform, e.g. on Macintosh or Microsoft Windows too, you need to do some sort of system identification first. Then you can apply StrategyPattern in code to handle the various ways of letting myrootapp.jar
obtain root or administrator permissions.