Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-856

FileUtils.listFiles(final File, String[], boolean) can throw NoSuchFileException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.16.1
    • 2.18.0
    • Utilities
    • None

    Description

      ListFiles crashes when vanishing files are involved while listing, ListFiles should simply list, the application should care of if files are not existent any more:
      <pre>

      java.io.UncheckedIOException: java.nio.file.NoSuchFileException: /tmp/20b50a15-b84e-4a9a-953e-223452dac994/a914fa55-50f7-4de0-8ca6-1fd84f10b29a.png
          at java.base/java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:87)
          at java.base/java.nio.file.FileTreeIterator.hasNext(FileTreeIterator.java:103)
          at java.base/java.util.Iterator.forEachRemaining(Iterator.java:132)
          at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1845)
          at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
          at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
          at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
          at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
          at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
          at org.apache.commons.io@2.16.1/org.apache.commons.io.FileUtils.toList(FileUtils.java:3025)
          at org.apache.commons.io@2.16.1/org.apache.commons.io.FileUtils.listFiles(FileUtils.java:2314)
          at com.itth.test/test.ApacheBug.lambda$main$1(ApacheBug.java:39)
          at java.base/java.lang.Thread.run(Thread.java:842)
      Caused by: java.nio.file.NoSuchFileException: /tmp/20b50a15-b84e-4a9a-953e-223452dac994/a914fa55-50f7-4de0-8ca6-1fd84f10b29a.png
          at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
          at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
          at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
          at java.base/sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(UnixFileAttributeViews.java:55)
          at java.base/sun.nio.fs.UnixFileSystemProvider.readAttributes(UnixFileSystemProvider.java:148)
          at java.base/sun.nio.fs.LinuxFileSystemProvider.readAttributes(LinuxFileSystemProvider.java:99)
          at java.base/java.nio.file.Files.readAttributes(Files.java:1851)
          at java.base/java.nio.file.FileTreeWalker.getAttributes(FileTreeWalker.java:226)
          at java.base/java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:277)
          at java.base/java.nio.file.FileTreeWalker.next(FileTreeWalker.java:374)
          at java.base/java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:83)
          ... 12 more

      </pre>

      Use this to reproduce:

      <pre>
      package test;

      import org.apache.commons.io.FileUtils;

      import java.io.BufferedOutputStream;
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.nio.charset.StandardCharsets;
      import java.nio.file.Path;
      import java.util.Collection;
      import java.util.UUID;

      public class ApacheBug {
      public static void main(String[] args) {
      // create random directory in tmp, create the directory if it does not exist
      final File dir = FileUtils.getTempDirectory();
      if (!dir.exists()) {
      if (!dir.mkdirs())

      { throw new RuntimeException("could not create image file path: " + dir.getAbsolutePath()); }

      }

      // create random file in the directory
      new Thread(() -> {
      try {
      while (true)

      { final File file = Path.of(dir.getAbsolutePath(), UUID.randomUUID().toString() + ".png").toFile(); new BufferedOutputStream(new FileOutputStream(file)).write("TEST".getBytes(StandardCharsets.UTF_8)); file.delete(); }

      } catch (IOException e)

      { e.printStackTrace(); }
      }).start();
      new Thread(() -> {
      try {
      while (true) { final Collection<File> files = FileUtils.listFiles(dir, new String[]\{"png"}, true);
      System.out.println(files.size());
      }
      } catch (Exception e) { e.printStackTrace(); }

      }).start();

      try

      { Thread.sleep(10000); }

      catch (InterruptedException e)

      { Thread.currentThread().interrupt(); }

      }
      }
      </pe>
       

      Attachments

        Issue Links

          Activity

            People

              ggregory Gary D. Gregory
              thomas.hartwig@gmail.com Thomas Hartwig
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: