{"record":{"id":"affabba55789e980","repo":"MyCATApache/Mycat-Server","slug":"rename-old-file-failed-for-upload-file-file","errorCode":null,"errorMessage":"rename old file failed for upload file {file}","messagePattern":"rename old file failed for upload file (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/main/java/io/mycat/manager/handler/ConfFileHandler.java","lineNumber":229,"sourceCode":"\t\t\t\ttry {\n\t\t\t\t\tbuff.close();\n\t\t\t\t\tsuc = true;\n\t\t\t\t} catch (IOException e) {\n\t\t\t\t\tLOGGER.warn(\"save config file err \" + e);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (suc) {\n\t\t\t// if succcess\n\t\t\tFile oldFile = new File(SystemConfig.getHomePath(), \"conf\"\n\t\t\t\t\t+ File.separator + fileName);\n\t\t\tif (oldFile.exists()) {\n\t\t\t\tFile backUP = new File(SystemConfig.getHomePath(), \"conf\"\n\t\t\t\t\t\t+ File.separator + fileName + \"_\"\n\t\t\t\t\t\t+ System.currentTimeMillis() + \"_auto\");\n\t\t\t\tif (!oldFile.renameTo(backUP)) {\n\t\t\t\t\tString msg = \"rename old file failed\";\n\t\t\t\t\tLOGGER.warn(msg + \" for upload file \"\n\t\t\t\t\t\t\t+ oldFile.getAbsolutePath());\n\t\t\t\t\treturn showInfo(c, buffer, packetId, msg);\n\t\t\t\t}\n\t\t\t}\n\t\t\tFile dest = new File(SystemConfig.getHomePath(), \"conf\"\n\t\t\t\t\t+ File.separator + fileName);\n\t\t\tif (!tempFile.renameTo(dest)) {\n\t\t\t\tString msg = \"rename file failed\";\n\t\t\t\tLOGGER.warn(msg + \" for upload file \"\n\t\t\t\t\t\t+ tempFile.getAbsolutePath());\n\t\t\t\treturn showInfo(c, buffer, packetId, msg);\n\t\t\t}\n\t\t\treturn showInfo(c, buffer, packetId, \"SUCCESS SAVED FILE:\"\n\t\t\t\t\t+ fileName);\n\t\t} else {\n\t\t\treturn showInfo(c, buffer, packetId, \"UPLOAD ERROR OCCURD:\"\n\t\t\t\t\t+ fileName);\n\t\t}","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/MyCATApache/Mycat-Server/blob/65f8d8beb752f935752f2a0eec0ab017facab9ef/src/main/java/io/mycat/manager/handler/ConfFileHandler.java#L211-L247","documentation":"In ConfFileHandler.upLoadConfigFile(), before an uploaded config file replaces the live file in MYCAT_HOME/conf, the existing file is renamed to a timestamped '_auto' backup; if File.renameTo fails, the handler logs 'rename old file failed for upload file <path>' and returns that message to the manager client without saving. This is an atomicity guard: the old file could not be moved aside, so the upload is aborted.","triggerScenarios":"File.renameTo(oldFile -> conf/<name>_<ts>_auto) returns false — typically when the destination backup path is not writable, the Mycat process lacks filesystem permissions on the conf directory, the file is locked/open by another process, or home path resolution puts the backup on a different filesystem.","commonSituations":"Uploading schema.xml/server.xml through Mycat manager (9066 port) while the conf directory is root-owned or read-only to the Mycat user; running in a container with a read-only mounted conf dir; files held open by tail/editors on some filesystems preventing rename.","solutions":["Check and fix permissions on MYCAT_HOME/conf (and the files) so the Mycat process user can rename files there (chmod/chown).","Ensure the conf directory is on a writable, same-filesystem mount (not read-only); verify SystemConfig.getHomePath() points at the intended directory.","Close processes holding the old file open (editors, tail -f, backup jobs) and retry the upload."],"exampleFix":"// before: conf dir not writable by mycat user -> renameTo false\n$ ls -l /usr/local/mycat/conf  # owned by root, mycat cannot rename\n// after\n$ chown -R mycat:mycat /usr/local/mycat/conf\n$ chmod u+rwX /usr/local/mycat/conf\n# re-run the manager upload; backup rename succeeds","handlingStrategy":"validation","validationCode":"// before uploading via the manager, verify conf is rename-writable\nFile confDir = new File(SystemConfig.getHomePath(), \"conf\");\nFile target = new File(confDir, \"schema.xml\");\nif (!confDir.canWrite() || (target.exists() && !target.renameTo(target))) {\n    throw new IllegalStateException(\"conf dir not writable or file locked: \" + confDir);\n}\nif (!target.getCanonicalPath().startsWith(confDir.getCanonicalPath() + File.separator)) {\n    throw new IllegalStateException(\"path traversal in upload fileName\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run the Mycat process as a user that owns MYCAT_HOME/conf.","Mount conf as read-write in containers; never read-only if config upload is used.","Verify free disk space and that conf is on a writable filesystem before uploads.","Close editors/backup jobs holding config files open during uploads."],"tags":["filesystem","file-rename","config-upload","permissions"],"backgroundTag":"file-write-failed","analyzedSha":"65f8d8beb752f935752f2a0eec0ab017facab9ef","analyzedAt":"2026-09-11T00:12:21.696Z","contentChangedAt":"2026-09-11T00:12:21.696Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}