{"record":{"id":"d08a0a60fe19aa4d","repo":"apache/hadoop","slug":"source-path-and-dest-path-refer-the-same-file-or-d","errorCode":null,"errorMessage":"Source path and dest path refer the same file or directory","messagePattern":"Source path and dest path refer the same file or directory","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java","lineNumber":620,"sourceCode":"\n  @Override\n  public boolean rename(Path src, Path dst) throws IOException {\n    LOG.debug(\"Rename source path: [{}] to dest path: [{}].\", src, dst);\n\n    // Renaming the root directory is not allowed\n    if (src.isRoot()) {\n      LOG.debug(\"Cannot rename the root directory of a filesystem.\");\n      return false;\n    }\n\n    // check the source path whether exists or not\n    FileStatus srcFileStatus = this.getFileStatus(src);\n\n    // Source path and destination path are not allowed to be the same\n    if (src.equals(dst)) {\n      LOG.debug(\"Source path and dest path refer to \"\n          + \"the same file or directory: [{}].\", dst);\n      throw new IOException(\"Source path and dest path refer \"\n          + \"the same file or directory\");\n    }\n\n    // It is not allowed to rename a parent directory to its subdirectory\n    Path dstParentPath;\n    for (dstParentPath = dst.getParent();\n         null != dstParentPath && !src.equals(dstParentPath);\n         dstParentPath = dstParentPath.getParent()) {\n      // Recursively find the common parent path of the source and\n      // destination paths.\n      LOG.debug(\"Recursively find the common parent directory of the source \"\n              + \"and destination paths. The currently found parent path: {}\",\n          dstParentPath);\n    }\n\n    if (null != dstParentPath) {\n      LOG.debug(\"It is not allowed to rename a parent directory:[{}] \"\n          + \"to its subdirectory:[{}].\", src, dst);","sourceCodeStart":602,"sourceCodeEnd":638,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java#L602-L638","documentation":"rename(src, dst) throws IOException('Source path and dest path refer the same file or directory') when src.equals(dst). Unlike some FileSystem implementations that return true or false for a self-rename, CosN treats renaming a path onto itself as a caller error and fails fast. The check runs after the source-exists check and before the ancestor checks.","triggerScenarios":"fs.rename(p, p); a computed destination collapses onto the source after Path normalization — e.g. rename('/a/b', '/a/b/') where the trailing slash normalizes away, or unqualified vs qualified forms of the same path comparing equal.","commonSituations":"Move logic built from user input where src and dst coincide; mixing 'cosn://bucket/x' and relative '/x' forms that resolve to the same key; config-driven rename where source and destination variables point at the same location.","solutions":["Guard before calling: if src.equals(dst), skip and treat as success.","Qualify both paths with fs.makeQualified(src/dst) before comparing or renaming.","Validate user-supplied move parameters and fail early with a clear message."],"exampleFix":"// before\nfs.rename(src, dst); // IOException when src.equals(dst)\n\n// after\nif (!fs.makeQualified(src).equals(fs.makeQualified(dst))) {\n  fs.rename(src, dst);\n}","handlingStrategy":"validation","validationCode":"Path s = fs.makeQualified(src);\nPath d = fs.makeQualified(dst);\nif (s.equals(d)) {\n  return true; // self-move is a no-op success\n}\nreturn fs.rename(s, d);","typeGuard":null,"tryCatchPattern":"try {\n  fs.rename(src, dst);\n} catch (IOException e) {\n  if (src.equals(dst) || e.getMessage().contains('the same file or directory')) {\n    return true; // treat as no-op\n  }\n  throw e;\n}","preventionTips":["Qualify both paths with makeQualified before comparing or renaming","Validate move parameters early and fail with a clear message","Treat self-renames as no-ops in move utilities"],"tags":["cosn","hadoop","rename","same-path","path-equality"],"backgroundTag":"rename-same-source-destination","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}