MyCATApache/Mycat-Server · error

rename old file failed for upload file

Error message

rename old file failed for upload file {file}

What it means

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.

Solutions

  1. Check and fix permissions on MYCAT_HOME/conf (and the files) so the Mycat process user can rename files there (chmod/chown).
  2. Ensure the conf directory is on a writable, same-filesystem mount (not read-only); verify SystemConfig.getHomePath() points at the intended directory.
  3. Close processes holding the old file open (editors, tail -f, backup jobs) and retry the upload.

Example fix

// before: conf dir not writable by mycat user -> renameTo false
$ ls -l /usr/local/mycat/conf  # owned by root, mycat cannot rename
// after
$ chown -R mycat:mycat /usr/local/mycat/conf
$ chmod u+rwX /usr/local/mycat/conf
# re-run the manager upload; backup rename succeeds
Defensive patterns

Strategy: validation

Validate before calling

// before uploading via the manager, verify conf is rename-writable
File confDir = new File(SystemConfig.getHomePath(), "conf");
File target = new File(confDir, "schema.xml");
if (!confDir.canWrite() || (target.exists() && !target.renameTo(target))) {
    throw new IllegalStateException("conf dir not writable or file locked: " + confDir);
}
if (!target.getCanonicalPath().startsWith(confDir.getCanonicalPath() + File.separator)) {
    throw new IllegalStateException("path traversal in upload fileName");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of MyCATApache/Mycat-Server@65f8d8beb7 (2026-09-11). Data as JSON: /api/errors/affabba55789e980. Report an issue: GitHub.

Appendix: source

Thrown at src/main/java/io/mycat/manager/handler/ConfFileHandler.java:229

				try {
					buff.close();
					suc = true;
				} catch (IOException e) {
					LOGGER.warn("save config file err " + e);
				}
			}
		}
		if (suc) {
			// if succcess
			File oldFile = new File(SystemConfig.getHomePath(), "conf"
					+ File.separator + fileName);
			if (oldFile.exists()) {
				File backUP = new File(SystemConfig.getHomePath(), "conf"
						+ File.separator + fileName + "_"
						+ System.currentTimeMillis() + "_auto");
				if (!oldFile.renameTo(backUP)) {
					String msg = "rename old file failed";
					LOGGER.warn(msg + " for upload file "
							+ oldFile.getAbsolutePath());
					return showInfo(c, buffer, packetId, msg);
				}
			}
			File dest = new File(SystemConfig.getHomePath(), "conf"
					+ File.separator + fileName);
			if (!tempFile.renameTo(dest)) {
				String msg = "rename file failed";
				LOGGER.warn(msg + " for upload file "
						+ tempFile.getAbsolutePath());
				return showInfo(c, buffer, packetId, msg);
			}
			return showInfo(c, buffer, packetId, "SUCCESS SAVED FILE:"
					+ fileName);
		} else {
			return showInfo(c, buffer, packetId, "UPLOAD ERROR OCCURD:"
					+ fileName);
		}

View on GitHub (pinned to 65f8d8beb7)