How do I prevent java FileTreeWalker failing prematurely

874 views Asked by At

I am trying to walk file tree to count files. But even though I handle exceptions within my CountFiles class the call to walkFileTree() has failed prematurely i the FileTreeWalker class preventing it counting the remaining files.

How can I avoid this ?

countFiles = new CountFiles(BaseFolderGuesser.FILE_SUFFIX_SEARCH_PATTERN);
Files.walkFileTree(path, countFiles);
totalCount+=countFiles.getFileCount();

public static class CountFiles
            extends SimpleFileVisitor<Path>
    {
        private int fileCount = 0;
        private Pattern pattern;

        public CountFiles(String regex)
        {
            pattern = Pattern.compile(regex);
        }

        /**
         * SONGKONG-294  Ignore the /proc virtual fs on linux
         *
         * @param dir
         * @param attrs
         * @return
         * @throws IOException
         */
         /*
         * Ignore some dirs
         * @param dir
         * @param attrs
         * @return
         * @throws IOException
         */
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                throws IOException
        {

            if (dir.toString().equals("/proc")) {
                MainWindow.logger.log(Level.SEVERE,"Ignoring /proc");
                return FileVisitResult.SKIP_SUBTREE;
            }
            else if (RecycleBinFolderNames.isMatch(dir.toFile().getName()))
            {
                MainWindow.logger.log(Level.SEVERE,"Ignoring "+dir.toString());
                return FileVisitResult.SKIP_SUBTREE;
            }
            return super.preVisitDirectory(dir, attrs);
        }

        /**
         * Find Music file
         *
         * @param file
         * @param attr
         * @return
         */
        @Override
        public FileVisitResult visitFile(Path file,
                                         BasicFileAttributes attr)
        {
            Path name = file.getFileName();
            if (name != null && pattern.matcher(name.toString().toLowerCase(Locale.UK)).matches())
            {
                fileCount++;
            }
            return FileVisitResult.CONTINUE;
        }

        /**
         * http://stackoverflow.com/questions/14436032/why-is-java-7-files-walkfiletree-throwing-exception-on-encountering-a-tar-file-o/14446993#14446993
         * SONGKONG-294:Ignore exceptions if file is not readable
         *
         * @param file
         * @param exc
         * @return
         * @throws IOException
         */
        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {

            if (file.toString().endsWith(".tar")) {
                //We dont log to reports as this is a bug in Java that we are handling not a problem in SongKong
                MainWindow.logger.log(Level.SEVERE, exc.getMessage());
                return FileVisitResult.CONTINUE;
            }

            try
            {
                FileVisitResult result = super.visitFileFailed(file, exc);
                return result;
            }
            catch(AccessDeniedException ade)
            {
                MainWindow.logger.warning("Unable to count files in:"+file);
                return FileVisitResult.CONTINUE;
            }
        }

        /**
         * SONGKONG-294:Ignore exception if folder is not readable
         *
         * @param dir
         * @param exc
         * @return
         * @throws IOException
         */
        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc)
                throws IOException
        {
            try
            {
                FileVisitResult result = super.postVisitDirectory(dir, exc);
                return result;
            }
            catch(AccessDeniedException ade)
            {
                MainWindow.logger.warning("Unable to count files in dir:"+dir);
                return FileVisitResult.CONTINUE;
            }
        }


        public int getFileCount()
        {
            return fileCount;
        }
    }

Giving Error

  13/09/2017 11.25.11:EDT:com.jthink.songkong.fileloader.CountFilesinFolder:handleException:SEVERE: Unable to count files:/Volumes/PlexMedia/Music/White Stripes, The - De Stijl/1BN0PB~Y
    java.nio.file.NoSuchFileException: /Volumes/PlexMedia/Music/White Stripes, The - De Stijl/1BN0PB~Y
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55)
    at sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:144)
    at java.nio.file.Files.readAttributes(Files.java:1737)
    at java.nio.file.FileTreeWalker.getAttributes(FileTreeWalker.java:219)
    at java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:276)
    at java.nio.file.FileTreeWalker.next(FileTreeWalker.java:372)
    at java.nio.file.Files.walkFileTree(Files.java:2706)
    at java.nio.file.Files.walkFileTree(Files.java:2742)
    at com.jthink.songkong.fileloader.CountFilesinFolder.call(CountFilesinFolder.java:174)
    at com.jthink.songkong.fileloader.CountFilesinFolder.call(CountFilesinFolder.java:26)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
1

There are 1 answers

3
Zsolt V On BEST ANSWER

Your class extends SimpleFileVisitor and in the overridden visitFileFailed method you wrote:

        try
        {
            FileVisitResult result = super.visitFileFailed(file, exc);
            return result;
        }
        catch(AccessDeniedException ade)
        {
            MainWindow.logger.warning("Unable to count files in:"+file);
            return FileVisitResult.CONTINUE;
        }

You call your super class' visitFileFailed which is:

/**
 * Invoked for a file that could not be visited.
 *
 * <p> Unless overridden, this method re-throws the I/O exception that prevented
 * the file from being visited.
 */
@Override
public FileVisitResult visitFileFailed(T file, IOException exc)
    throws IOException
{
    Objects.requireNonNull(file);
    throw exc;
}

Which rethrow the same exception which was handled before. Thatswhy you got the exception. After that you only catch the AccessDeniedException but as I can see you got a NoSuchFileException.

Maybe you can consider catching a broader range of exception or not invoking super.visitFileFailed at all since it does nothing.