{"record":{"id":"c1faad4b2ebd0707","repo":"apache/hadoop","slug":"not-a-directory","errorCode":null,"errorMessage":"Not a directory: {}","messagePattern":"Not a directory: (.+?)","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java","lineNumber":285,"sourceCode":"      boolean recursive) throws PathIOException {\n    if (isEmptyDir) {\n      return true;\n    }\n    if (recursive) {\n      return false;\n    } else {\n      throw new PathIOException(this.bucket, \"Can not delete root path\");\n    }\n  }\n\n  @Override\n  public FSDataOutputStream createNonRecursive(Path f, FsPermission permission,\n      EnumSet<CreateFlag> flags, int bufferSize, short replication,\n      long blockSize, Progressable progress) throws IOException {\n    Path parent = f.getParent();\n    if (null != parent) {\n      if (!getFileStatus(parent).isDirectory()) {\n        throw new FileAlreadyExistsException(\"Not a directory: \" + parent);\n      }\n    }\n\n    return create(f, permission, flags.contains(CreateFlag.OVERWRITE),\n        bufferSize, replication, blockSize, progress);\n  }\n\n  @Override\n  public boolean delete(Path f, boolean recursive) throws IOException {\n    LOG.debug(\"Ready to delete path: [{}]. recursive: [{}].\", f, recursive);\n    FileStatus status;\n    try {\n      status = getFileStatus(f);\n    } catch (FileNotFoundException e) {\n      LOG.debug(\"Ready to delete the file: [{}], but it does not exist.\", f);\n      return false;\n    }\n    Path absolutePath = makeAbsolute(f);","sourceCodeStart":267,"sourceCodeEnd":303,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java#L267-L303","documentation":"createNonRecursive checks the immediate parent of the file being created; if getFileStatus(parent) resolves to an existing FILE rather than a directory, it throws FileAlreadyExistsException('Not a directory: <parent>'). If the parent is missing entirely you get FileNotFoundException from getFileStatus instead, so this specific message means the parent exists but has the wrong type.","triggerScenarios":"fs.createNonRecursive(new Path('/a/b/file'), ...) where /a/b exists as a file object; writers that skip mkdirs and assume parents are directories.","commonSituations":"An earlier job wrote a file where a directory level is now needed (easy in object stores with no real directories); a partition path collides with an existing file; concurrent producer created the parent as a file between checks.","solutions":["Delete or rename the file occupying the parent path: fs.delete(parent, false).","If the parent may be missing, call fs.mkdirs(parent) first.","Switch to fs.create(), which creates parent directories implicitly."],"exampleFix":"// before\nout = fs.createNonRecursive(new Path('/logs/2026/part-0'), perms, flags, buf, rep, block, null);\n// throws FileAlreadyExistsException: Not a directory: /logs/2026\n\n// after\nPath parent = new Path('/logs/2026');\nif (fs.exists(parent) && fs.getFileStatus(parent).isFile()) {\n  fs.delete(parent, false);\n}\nfs.mkdirs(parent);\nout = fs.createNonRecursive(new Path('/logs/2026/part-0'), perms, flags, buf, rep, block, null);","handlingStrategy":"validation","validationCode":"Path parent = f.getParent();\nif (parent != null) {\n  try {\n    if (!fs.getFileStatus(parent).isDirectory()) {\n      throw new IllegalStateException('Parent exists but is a file: ' + parent);\n    }\n  } catch (FileNotFoundException e) {\n    fs.mkdirs(parent);\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  fs.createNonRecursive(f, perms, flags, buf, rep, block, null);\n} catch (FileAlreadyExistsException e) {\n  // 'Not a directory: <parent>' -> clear the conflicting file and retry once\n  fs.delete(f.getParent(), false);\n  fs.mkdirs(f.getParent());\n}","preventionTips":["Create the parent directory chain before non-recursive creates","Never let the same key be both a file and a directory level in object stores","Treat 'Not a directory' as layout damage: audit how the file got there"],"tags":["cosn","hadoop","createnonrecursive","parent-not-directory","file-already-exists"],"backgroundTag":"parent-not-a-directory","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}