{"record":{"id":"18e323054dfa29e7","repo":"apache/hadoop","slug":"already-exists-18e323","errorCode":null,"errorMessage":"{} already exists","messagePattern":"(.+?) already exists","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":167,"sourceCode":"    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\n        // the file, so here we close and commit the file to be visible explicitly for fuse, and\n        // then reopen the file output stream for further data bytes writing.\n        out.close();\n        out =","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-tos/src/main/java/org/apache/hadoop/fs/tosfs/RawFileSystem.java#L149-L185","documentation":"RawFileSystem (the tos:// connector for Volcengine TOS object storage) throws FileAlreadyExistsException from create() when the target path already exists as a file/object and the overwrite flag is not set. This mirrors the Hadoop FileSystem contract (O_CREAT|O_EXCL semantics): creating over an existing file must fail loudly unless the caller explicitly asked to overwrite. The sibling branch throws the same exception with 'is a directory' when the path is a directory.","triggerScenarios":"Calling fs.create(path) (overwrite defaults to false), or the CreateFlag overload without CreateFlag.OVERWRITE, on a tos:// path whose object key already exists (including zero-byte files left by a previous run or a pending magic-commit destination).","commonSituations":"Re-running a MapReduce/Spark job into the same output directory without cleanup; two concurrent writers targeting the same object key; leftover output from a crashed earlier attempt; a directory marker existing at the file path.","solutions":["Pass overwrite=true: fs.create(path, true) or include CreateFlag.OVERWRITE in the EnumSet overload","Delete or move the existing object first: fs.delete(path, false) before create when you own the destination","Check the destination before writing: fs.exists(path) && !fs.getFileStatus(path).isFile() to fail with your own clearer error","Use unique per-run output paths (job id / timestamp suffix) so runs never collide"],"exampleFix":"// before\nFSDataOutputStream out = fs.create(path); // throws FileAlreadyExistsException if path exists\n\n// after\nFSDataOutputStream out = fs.create(path, true); // overwrite existing object","handlingStrategy":"validation","validationCode":"Path p = path.makeQualified(fs.getUri(), fs.getWorkingDirectory());\nif (fs.exists(p) && !fs.getFileStatus(p).isDirectory()) {\n  // decide: overwrite, delete, or fail with a clear message BEFORE calling create()\n}","typeGuard":null,"tryCatchPattern":"try {\n  out = fs.create(p, /*overwrite*/ false);\n} catch (FileAlreadyExistsException e) {\n  // deliberately clobber or surface a job-level 'output exists' error\n}","preventionTips":["Pass overwrite=true whenever the destination is disposable","Parameterize output paths with run ids so runs never collide","Fail fast at job setup if the output path exists instead of failing at first write"],"tags":["file-already-exists","create","overwrite","object-storage","tos"],"backgroundTag":"file-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}