{"record":{"id":"a6e040653caddef0","repo":"apache/hadoop","slug":"can-t-make-directory-for-path-s-it-is-a-file-a6e040","errorCode":null,"errorMessage":"Can't make directory for path '%s', it is a file.","messagePattern":"Can't make directory for path '(.+?)', it is a file\\.","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-tos/src/main/java/org/apache/hadoop/fs/tosfs/RawFileSystem.java","lineNumber":489,"sourceCode":"      }\n    } catch (FileNotFoundException e) {\n      Path dir = makeQualified(path);\n      validatePath(dir);\n      fsOps.mkdirs(dir);\n    }\n    return true;\n  }\n\n  private void validatePath(Path path) throws IOException {\n    Path parent = path.getParent();\n    do {\n      try {\n        FileStatus fileStatus = innerFileStatus(parent);\n        if (fileStatus.isDirectory()) {\n          // If path exists and a directory, exit\n          break;\n        } else {\n          throw new FileAlreadyExistsException(String.format(\"Can't make directory for path '%s',\"\n                  + \" it is a file.\", parent));\n        }\n      } catch (FileNotFoundException ignored) {\n      }\n      parent = parent.getParent();\n    } while (parent != null);\n  }\n\n  @Override\n  public FileStatus getFileStatus(Path path) throws IOException {\n    try {\n      return innerFileStatus(path);\n    } catch (ParentNotDirectoryException e) {\n      // Treat ParentNotDirectoryException as FileNotFoundException for the case that check whether\n      // path exist or not.\n      throw new FileNotFoundException(e.getMessage());\n    }\n  }","sourceCodeStart":471,"sourceCodeEnd":507,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-tos/src/main/java/org/apache/hadoop/fs/tosfs/RawFileSystem.java#L471-L507","documentation":"validatePath() walks up the ancestor chain before mkdirs and throws FileAlreadyExistsException when any ancestor of the directory being created exists as a file: a directory cannot be created under a path occupied by an object. The message names the offending ancestor, not the requested directory.","triggerScenarios":"fs.mkdirs('/a/b/c') where '/a' or '/a/b' exists as a file object; the loop climbs parent.getParent() until it finds an existing non-directory or reaches the root.","commonSituations":"Deep partition paths whose prefix was previously written as a flat file ('user.csv' vs 'user.csv/day=1/'); key-layout migrations that introduce hierarchy below existing objects; date-partitioned outputs that collide with file names chosen earlier.","solutions":["Rename or delete the file occupying the ancestor path, then retry mkdirs","Redesign the key layout so files never occupy prefixes of directory paths (object stores require prefix/file disjointness)","Pre-check the ancestor chain and emit a clear error naming the blocking file"],"exampleFix":"// before\nfs.mkdirs(new Path(\"/reports/2024/day-1\")); // '/reports' is a file object -> exception\n\n// after: keep files and directory prefixes disjoint\nfs.delete(new Path(\"/reports\"), false); // remove the blocking file\nfs.mkdirs(new Path(\"/reports/2024/day-1\"));","handlingStrategy":"validation","validationCode":"static Path fileAncestor(FileSystem fs, Path dir) throws IOException {\n  for (Path p = dir.getParent(); p != null; p = p.getParent()) {\n    if (fs.exists(p) && !fs.getFileStatus(p).isDirectory()) return p;\n  }\n  return null;\n}\nPath blocker = fileAncestor(fs, dir);\nif (blocker != null) throw new IllegalStateException(\"ancestor is a file: \" + blocker);","typeGuard":null,"tryCatchPattern":"try { fs.mkdirs(dir); }\ncatch (FileAlreadyExistsException e) {\n  // message names the ancestor file: parse or walk parents to relocate it\n}","preventionTips":["Design key layouts where no file is ever a prefix of a directory","Validate the full ancestor chain before deep mkdirs","Watch date/partition schemes that collide with earlier flat file names"],"tags":["mkdirs","ancestor-is-file","path-collision","tos"],"backgroundTag":"parent-not-a-directory","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}