{"record":{"id":"0017d98bb52c61c5","repo":"apache/hadoop","slug":"destination-exists-and-is-not-a-directory-p2f","errorCode":null,"errorMessage":"Destination exists and is not a directory: \" + p2f.getCanonicalPath()","messagePattern":"Destination exists and is not a directory: \" \\+ p2f\\.getCanonicalPath\\(\\)","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java","lineNumber":916,"sourceCode":"  }\n\n  private boolean mkdirsWithOptionalPermission(Path f, FsPermission permission)\n      throws IOException {\n    if(f == null) {\n      throw new IllegalArgumentException(\"mkdirs path arg is null\");\n    }\n    Path parent = f.getParent();\n    File p2f = pathToFile(f);\n    File parent2f = null;\n    if(parent != null) {\n      parent2f = pathToFile(parent);\n      if(parent2f != null && parent2f.exists() && !parent2f.isDirectory()) {\n        throw new ParentNotDirectoryException(\"Parent path is not a directory: \"\n            + parent);\n      }\n    }\n    if (p2f.exists() && !p2f.isDirectory()) {\n      throw new FileAlreadyExistsException(\"Destination exists\" +\n              \" and is not a directory: \" + p2f.getCanonicalPath());\n    }\n    return (parent == null || parent2f.exists() || mkdirs(parent)) &&\n      (mkOneDirWithMode(f, p2f, permission) || p2f.isDirectory());\n  }\n  \n  \n  @Override\n  public Path getHomeDirectory() {\n    return this.makeQualified(new Path(System.getProperty(\"user.home\")));\n  }\n\n  /**\n   * Set the working directory to the given directory.\n   */\n  @Override\n  public void setWorkingDirectory(Path newDir) {\n    workingDir = makeAbsolute(newDir);","sourceCodeStart":898,"sourceCodeEnd":934,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/RawLocalFileSystem.java#L898-L934","documentation":"mkdirsWithOptionalPermission throws FileAlreadyExistsException('Destination exists and is not a directory') when the final target path itself exists as a regular file. mkdirs refuses to overwrite an existing file with a directory. The message reports the canonical path, which resolves symlinks — useful when the visible path differs from the real blocker.","triggerScenarios":"Calling fs.mkdirs(\"/data/daily\") when /data/daily is a regular file (e.g. created by a previous run's create() or a redirect target), or a symlink at that path pointing to a file.","commonSituations":"Job output layouts switching between file-per-day and directory-per-day, leftover files from prior runs under temp dirs, shell redirections (cmd > /data/daily) having created a file where code now wants a directory.","solutions":["Check the canonical path in the message with ls -l; confirm it is a regular file or a symlink to one.","Delete or move the file, then retry mkdirs.","Disambiguate the layout: use distinct names for files vs. directories (e.g. /data/daily/ for dirs, /data/daily.dat for files).","Pre-flight in code: if (fs.exists(p) && !fs.getFileStatus(p).isDirectory()) handle the conflict explicitly."],"exampleFix":"// before\nPath daily = new Path(\"/data/daily\"); // exists as a regular file\nfs.mkdirs(daily); // FileAlreadyExistsException\n\n// after\nPath daily = new Path(\"/data/daily\");\nif (fs.exists(daily) && !fs.getFileStatus(daily).isDirectory()) {\n  fs.rename(daily, new Path(\"/data/daily.old-\" + System.currentTimeMillis()));\n}\nfs.mkdirs(daily);","handlingStrategy":"validation","validationCode":"if (fs.exists(p)) {\n  if (!fs.getFileStatus(p).isDirectory()) {\n    throw new FileAlreadyExistsException(\n        p + \" exists as a file; refusing to replace with directory\");\n  }\n} else {\n  fs.mkdirs(p);\n}","typeGuard":null,"tryCatchPattern":"try {\n  fs.mkdirs(p);\n} catch (FileAlreadyExistsException e) {\n  // message contains the canonical path; inspect and resolve the file conflict\n  throw new IOException(\"File occupies directory target \" + p\n      + \"; delete or relocate it\", e);\n}","preventionTips":["Before mkdirs, verify the exact target is not an existing file.","Keep file and directory names distinct in output schemes (dir/ vs file.ext).","Clean stale outputs between runs when layouts changed."],"tags":["hadoop","local-filesystem","mkdirs","file-exists","path-conflict"],"backgroundTag":"file-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}