{"record":{"id":"0cdbdf7f921127ac","repo":"MyCATApache/Mycat-Server","slug":"failed-to-list-files-for-dir","errorCode":null,"errorMessage":"Failed to list files for dir: ","messagePattern":"Failed to list files for dir: ","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/memory/unsafe/utils/JavaUtils.java","lineNumber":96,"sourceCode":"        }\n      }\n      if (savedIOException != null) {\n        throw savedIOException;\n      }\n    }\n\n    boolean deleted = file.delete();\n    // Delete can also fail if the file simply did not exist.\n    if (!deleted && file.exists()) {\n      throw new IOException(\"Failed to delete: \" + file.getAbsolutePath());\n    }\n  }\n\n  private static File[] listFilesSafely(File file) throws IOException {\n    if (file.exists()) {\n      File[] files = file.listFiles();\n      if (files == null) {\n        throw new IOException(\"Failed to list files for dir: \" + file);\n      }\n      return files;\n    } else {\n      return new File[0];\n    }\n  }\n\n  private static boolean isSymlink(File file) throws IOException {\n    Preconditions.checkNotNull(file);\n    File fileInCanonicalDir = null;\n    if (file.getParent() == null) {\n      fileInCanonicalDir = file;\n    } else {\n      fileInCanonicalDir = new File(file.getParentFile().getCanonicalFile(), file.getName());\n    }\n    return !fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile());\n  }\n","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/memory/unsafe/utils/JavaUtils.java#L78-L114","documentation":"listFilesSafely is JavaUtils' internal helper used by deleteRecursively to enumerate a directory's children. It throws IOException when File.listFiles() returns null, which means the path is not a readable directory (e.g. permission denied or an I/O error) even though it exists.","triggerScenarios":"deleteRecursively invoked on a path that exists but is not a readable directory, or on a directory whose permissions prevent listing (no read/execute bit), or a filesystem error.","commonSituations":"Deleting trees spanning directories with mixed ownership/permissions; the target path replaced by a symlink or special file; NFS/permissions issues where exists() is true but listFiles() fails.","solutions":["Verify the path is a directory with read+execute permission before recursing (file.isDirectory() && file.canRead()).","Fix permissions (chmod / chown) on the offending directory reported in the message.","Check whether the path is a symlink or special file and resolve/handle it before deletion."],"exampleFix":"// before\nJavaUtils.deleteRecursively(dir);\n// after\nif (dir.isDirectory() && dir.canRead()) {\n  JavaUtils.deleteRecursively(dir);\n} else {\n  throw new IOException(\"Not a readable directory: \" + dir);\n}","handlingStrategy":"validation","validationCode":"if (!dir.isDirectory() || !dir.canRead()) {\n  throw new IOException(\"Cannot list directory: \" + dir);\n}","typeGuard":null,"tryCatchPattern":"try {\n  JavaUtils.deleteRecursively(dir);\n} catch (IOException e) {\n  logger.warn(\"Delete failed (list problem?): {}\", e.getMessage());\n}","preventionTips":["Verify the path is a real, readable directory (isDirectory + canRead) before recursive deletes.","Resolve symlinks explicitly so you know what you are deleting.","Keep directory permissions consistent across the tree you create."],"tags":["java","filesystem","io"],"backgroundTag":"directory-not-found","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}