{"record":{"id":"c75db875e1895365","repo":"apache/hadoop","slug":"cannot-rename-s-to-s-s-is-a-file","errorCode":null,"errorMessage":"Cannot rename %s to %s, %s is a file","messagePattern":"Cannot rename (.+?) to (.+?), (.+?) is a file","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":679,"sourceCode":"        } catch (FileNotFoundException e) {\n          statuses = null;\n        }\n        if (null != statuses && statuses.length > 0) {\n          LOG.debug(\"Cannot rename source file: [{}] to dest file: [{}], \"\n              + \"because the file already exists.\", src, dst);\n          throw new FileAlreadyExistsException(\n              String.format(\n                  \"File: %s already exists\", dst\n              )\n          );\n        }\n      }\n    } catch (FileNotFoundException e) {\n      // destination path not exists\n      Path tempDstParentPath = dst.getParent();\n      FileStatus dstParentStatus = this.getFileStatus(tempDstParentPath);\n      if (!dstParentStatus.isDirectory()) {\n        throw new IOException(String.format(\n            \"Cannot rename %s to %s, %s is a file\", src, dst, dst.getParent()\n        ));\n      }\n      // The default root directory is definitely there.\n    }\n\n    boolean result;\n    if (srcFileStatus.isDirectory()) {\n      result = this.copyDirectory(src, dst);\n    } else {\n      result = this.copyFile(src, dst);\n    }\n\n    if (!result) {\n      //Since rename is a non-atomic operation, after copy fails,\n      // it is not allowed to delete the data of the original path.\n      return false;\n    } else {","sourceCodeStart":661,"sourceCodeEnd":697,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-cloud-storage-project/hadoop-cos/src/main/java/org/apache/hadoop/fs/cosn/CosNFileSystem.java#L661-L697","documentation":"When the destination of a rename does not exist, CosN stats dst's parent; if that parent exists as a FILE, rename throws IOException('Cannot rename <src> to <dst>, <dstParent> is a file') — the destination cannot sit beneath a file (ENOTDIR). If the parent is missing entirely, getFileStatus(parent) instead propagates FileNotFoundException out of rename, which is a separate failure mode.","triggerScenarios":"fs.rename('/a/f', '/b/g') where '/b' is an existing file object; renaming into a 'directory' path that was previously created as a file.","commonSituations":"Destination tree half-created with files at directory levels; a partition path used both as file and directory across runs of a pipeline.","solutions":["Delete or relocate the file occupying the destination's parent path.","After removing the conflict, fs.mkdirs(dst.getParent()) and retry the rename.","Choose a destination whose full prefix chain contains only real directories."],"exampleFix":"// before\nfs.rename(new Path('/a/f'), new Path('/b/g')); // IOException: ... /b is a file\n\n// after\nPath parent = new Path('/b');\nif (fs.exists(parent) && fs.getFileStatus(parent).isFile()) {\n  fs.delete(parent, false);\n}\nfs.mkdirs(parent);\nfs.rename(new Path('/a/f'), new Path('/b/g'));","handlingStrategy":"validation","validationCode":"Path parent = dst.getParent();\nif (fs.exists(parent) && !fs.getFileStatus(parent).isDirectory()) {\n  throw new IllegalStateException('Destination parent is a file: ' + parent);\n}\nif (!fs.exists(parent)) { fs.mkdirs(parent); }","typeGuard":null,"tryCatchPattern":"try {\n  fs.rename(src, dst);\n} catch (IOException e) {\n  if (e.getMessage() != null && e.getMessage().contains('is a file')) {\n    fs.delete(dst.getParent(), false);\n    fs.mkdirs(dst.getParent());\n    fs.rename(src, dst);\n  } else { throw e; }\n}","preventionTips":["Validate the full destination prefix chain before renames","Never let a destination parent exist as a file in your layouts","mkdirs the destination parent as part of move utilities"],"tags":["cosn","hadoop","rename","enotdir","parent-is-file"],"backgroundTag":"parent-path-is-file","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}