{"record":{"id":"d61fe075c8703274","repo":"apache/hadoop","slug":"can-t-overwrite-non-existent-src","errorCode":null,"errorMessage":"Can't overwrite non-existent <src>","messagePattern":"Can't overwrite non-existent <src>","errorType":"exception","errorClass":"FileNotFoundException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirWriteFileOp.java","lineNumber":347,"sourceCode":"    }\n    INode inode = iip.getLastINode();\n    if (inode != null) {\n      // Verify that the destination does not exist as a directory already.\n      if (inode.isDirectory()) {\n        throw new FileAlreadyExistsException(iip.getPath() +\n            \" already exists as a directory\");\n      }\n      // Verifies it's indeed a file and perms allow overwrite\n      INodeFile.valueOf(inode, src);\n      if (dir.isPermissionEnabled() && flag.contains(CreateFlag.OVERWRITE)) {\n        dir.checkPathAccess(pc, iip, FsAction.WRITE);\n      }\n    } else {\n      if (!createParent) {\n        dir.verifyParentDir(iip);\n      }\n      if (!flag.contains(CreateFlag.CREATE)) {\n        throw new FileNotFoundException(\"Can't overwrite non-existent \" + src);\n      }\n    }\n    return iip;\n  }\n\n\n  /**\n   * Create a new file or overwrite an existing file<br>\n   *\n   * Once the file is create the client then allocates a new block with the next\n   * call using {@link ClientProtocol#addBlock}.\n   * <p>\n   * For description of parameters and exceptions thrown see\n   * {@link ClientProtocol#create}\n   */\n  static HdfsFileStatus startFile(\n      FSNamesystem fsn, INodesInPath iip,\n      PermissionStatus permissions, String holder, String clientMachine,","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirWriteFileOp.java#L329-L365","documentation":"resolvePathForStartFile throws FileNotFoundException when CreateFlag.OVERWRITE is passed without CreateFlag.CREATE and the path does not exist: overwrite-only cannot bring a file into being, so the create fails. The public convenience API FileSystem.create(path, true) always sets both flags, so this is usually seen by code assembling the EnumSet itself.","triggerScenarios":"Direct ClientProtocol.create callers or wrappers that build EnumSet<CreateFlag> manually and omit CREATE (for example EnumSet.of(OVERWRITE)); also a race where the file is deleted between an exists() check and a create flagged only OVERWRITE.","commonSituations":"Custom RPC clients and migration tools copying CreateFlag sets around; older library code assembling flags by hand.","solutions":["Always pair the flags: EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE), or simply use fs.create(path, true)","If overwrite-only is intended, pre-check existence and fail with your own clear error","Handle deletion races by retrying the create with both flags"],"exampleFix":"// before\nfs.create(path, FsPermission.getFileDefault(),\n    EnumSet.of(CreateFlag.OVERWRITE), bufSize, replication, blockSize, null);\n\n// after\nfs.create(path, FsPermission.getFileDefault(),\n    EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE), bufSize, replication, blockSize, null);","handlingStrategy":"validation","validationCode":"EnumSet<CreateFlag> flags = fs.exists(path)\n    ? EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE)\n    : EnumSet.of(CreateFlag.CREATE);","typeGuard":null,"tryCatchPattern":"try {\n  out = fs.create(path, perm, EnumSet.of(CreateFlag.OVERWRITE), bufSize, repl, bs, null);\n} catch (FileNotFoundException e) {\n  // OVERWRITE alone cannot create: retry with CREATE included\n  out = fs.create(path, perm, EnumSet.of(CreateFlag.CREATE, CreateFlag.OVERWRITE), bufSize, repl, bs, null);\n}","preventionTips":["Always include CREATE; OVERWRITE alone cannot create a file","Prefer the public FileSystem.create over assembling ClientProtocol flags by hand","Handle exists()-then-delete races by retrying create with both flags"],"tags":["hdfs","create","create-flags","file-not-found"],"backgroundTag":"file-not-found","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}