Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6372792

File.listFiles() returns null under Windows, but not under Linux

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Cannot Reproduce
    • Icon: P5 P5
    • None
    • 5.0
    • core-libs
    • x86
    • windows_xp

      A DESCRIPTION OF THE REQUEST :
      On Linux, the java.io.File.canRead() method is a reliable indication as to whether file-system permissions allow a traversal to descend into a sub-directory. If canRead() returns true, then File.listFiles() always returns non-null (even if the directory is empty, i.e., listFiles().length == 0). This seems to fit with the javadoc method description, and only the canRead() test is required on Linux.

      However, on Windows, canRead() is not a reliable test (at least within the meaning of the javadoc for the listFiles() method). The canRead() method can return true, ostensibly indicating that descent is permitted, but when traversal does descend using listFiles(), the listFiles() method can still return null. Therefore, on Windows, the canRead() test is not definitive; only the test (listFiles() != null) is definitive. For Windows, this seems to be out of line with the javadoc description of listFiles().

      JUSTIFICATION :
      Traversal needs greater cross-platform consistency across the canRead() and listFiles() method.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      On any platform, if someDirectory.canRead() returns true, then someDirectory.listFiles() should never return null.
      ACTUAL -
      On Linux, if someDirectory.canRead() returns true, then someDirectory.listFiles() always returns non-null, even if the directory is empty.

      But on Windows, if someDirectory.canRead() returns true, then someDirectory.listFiles() can still return null.

      ---------- BEGIN SOURCE ----------
      // see if "mysubdir" is accessible, and, if so, descend.

      File subdir = new File("mysubdir");

      // canRead() can return true on windows for inaccessibe dirs
      if (!subdir.canRead())
       return; // inaccessible dir

      File[] subdirFileList = subdir.listFiles(); // accessible, so descend

      // The following test, or any subdirFileList dereference, throws NPE on windows,
      // but not on Linux.

      if (subdirFileList.length == 0)
       return; // accessible, but empty dir

      // The following test is required on windows before any subdirFileList
      // dereference to avoid NPE on windows.

      if (subdirFileList.length == null)
       return; // accessible, but empty dir

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use (File.listFiles() == null) as the definitive test as to whether permissions allows traversal to descend a subdir.

            iris Iris Clark
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: