Monday, 12 August 2013

Working around access denied in a FileWalking Tree in Java7

Working around access denied in a FileWalking Tree in Java7

The following is a simple code just to test the Files.walkFileTree()
method, however the folder /etc/ssl/private which has these
permissions(rwx--x---) throws an exception even if I guarded it with an if
statement.(if (permissions.equals("rwx--x---")). What am I doing of wrong?
Thanks in advance.
)
public class WalkingTheThing2 extends SimpleFileVisitor {
public static void main (String []args) throws IOException,
InterruptedException
{
Files.walkFileTree(Paths.get("/"), new WalkingTheThing2());
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes
attrs) throws IOException
{
PosixFileAttributeView posixView = Files.getFileAttributeView(dir,
PosixFileAttributeView.class);
PosixFileAttributes posixAttr = posixView.readAttributes();
String permissions
=PosixFilePermissions.toString(posixAttr.permissions());
if (permissions.equals("rwx--x---"))
{
return FileVisitResult.CONTINUE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
try{
System.out.println(file.getFileName()+" " +Files.size(file));
return FileVisitResult.CONTINUE;
}
catch(IOException io){return FileVisitResult.CONTINUE;}
}
}
The exception I get is: java.nio.file.AccessDeniedException: /etc/ssl/private

No comments:

Post a Comment