{"record":{"id":"bb7afb05aeb51666","repo":"apache/hadoop","slug":"is-a-directory-bb7afb","errorCode":null,"errorMessage":"{} is a directory","messagePattern":"(.+?) is a directory","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":163,"sourceCode":"    Preconditions.checkArgument(rangeSize > 0, \"Object storage range size must be positive.\");\n\n    FSInputStream fsIn = new ObjectMultiRangeInputStream(taskThreadPool, storage, path,\n        status.getLen(), rangeSize, status.checksum());\n    return new FSDataInputStream(fsIn);\n  }\n\n  public FSDataInputStream open(Path path, byte[] expectedChecksum, Range range) {\n    return new FSDataInputStream(\n        new ObjectRangeInputStream(storage, path, range, expectedChecksum));\n  }\n\n  @Override\n  public FSDataOutputStream create(Path path, FsPermission permission, boolean overwrite,\n      int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {\n    FileStatus fileStatus = getFileStatusOrNull(path);\n    if (fileStatus != null) {\n      if (fileStatus.isDirectory()) {\n        throw new FileAlreadyExistsException(path + \" is a directory\");\n      }\n\n      if (!overwrite) {\n        throw new FileAlreadyExistsException(path + \" already exists\");\n      }\n      LOG.debug(\"Overwriting file {}\", path);\n    }\n\n    if (MagicOutputStream.isMagic(path)) {\n      return new FSDataOutputStream(\n          new MagicOutputStream(this, storage, uploadThreadPool, getConf(), makeQualified(path)),\n          null);\n    } else {\n      ObjectOutputStream out =\n          new ObjectOutputStream(storage, uploadThreadPool, getConf(), makeQualified(path), true);\n\n      if (fileStatus == null && FuseUtils.fuseEnabled()) {\n        // The fuse requires the file to be visible when accessing getFileStatus once we created","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-tos/src/main/java/org/apache/hadoop/fs/tosfs/RawFileSystem.java#L145-L181","documentation":"RawFileSystem.create stats the target first; when an existing directory sits at the path it throws FileAlreadyExistsException('<path> is a directory'). This happens regardless of the overwrite flag, because removing a directory tree to satisfy a file create would destroy data.","triggerScenarios":"create(path, ..., overwrite=true) where path exists as a directory with or without children; a file name equal to an existing partition directory; output path configured to the same location as an input directory.","commonSituations":"Reusing an input directory as the job output; Hive external table locations overlapping plain file writes; leftover directories from previous runs sharing the intended file name.","solutions":["Choose an output path or file name that does not collide with the directory","Delete the directory explicitly with fs.delete(path, true) only when its removal is intended","Guard with a status check before create and fail with a clear message"],"exampleFix":"// before\nfs.create(new Path(\"/outputs/data\"), true); // /outputs/data is a directory\n\n// after\nPath out = new Path(\"/outputs/data\");\nif (fs.exists(out) && fs.getFileStatus(out).isDirectory()) {\n  fs.delete(out, true); // deliberate removal\n}\ntry (FSDataOutputStream os = fs.create(out, true)) {\n  ... // write\n}","handlingStrategy":"validation","validationCode":"FileStatus st = fs.getFileStatusOrNull(path);\nif (st != null && st.isDirectory()) {\n  throw new FileAlreadyExistsException(\"Output path is a directory: \" + path\n      + \"; pick a file name or delete the directory first\");\n}","typeGuard":"static boolean canCreateFile(FileSystem fs, Path p) throws IOException {\n  FileStatus st = fs.getFileStatusOrNull(p);\n  return st == null || st.isFile();\n}","tryCatchPattern":"try {\n  return fs.create(path, overwrite);\n} catch (FileAlreadyExistsException e) {\n  if (e.getMessage() != null && e.getMessage().endsWith(\"is a directory\")) {\n    // overwrite cannot fix a directory: choose a new name or delete(dir, true) deliberately\n  } else {\n    throw e;\n  }\n}","preventionTips":["Never configure output file paths that overlap input or table directories","Check the target with getFileStatusOrNull before create","Remember overwrite=true does not bypass the directory check"],"tags":["hadoop","tos","create","file-already-exists","output-path"],"backgroundTag":"file-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}