diff --git a/changelog.html b/changelog.html
index e33098e255..339ce25cf8 100644
--- a/changelog.html
+++ b/changelog.html
@@ -55,6 +55,9 @@ Upcoming changes
+ -
+ Fixed a bug in the auto-overwrite of bundled plugins on Windows.
+ (issue 12514)
-
Fixed a temporary memory spike when dealing with rapidly growing large console output and interactive monitoring.
-
diff --git a/core/src/main/java/hudson/PluginManager.java b/core/src/main/java/hudson/PluginManager.java
index 35e3c8ba2e..75be36bb73 100644
--- a/core/src/main/java/hudson/PluginManager.java
+++ b/core/src/main/java/hudson/PluginManager.java
@@ -414,12 +414,9 @@ public abstract class PluginManager extends AbstractModelObject {
File file = new File(rootDir, fileName);
File pinFile = new File(rootDir, fileName+".pinned");
- {// normalization first
- File legacyFile = new File(rootDir,legacyName);
- if (legacyFile.exists()) legacyFile.renameTo(file);
- File legacyPinFile = new File(rootDir,legacyName+".pinned");
- if (legacyPinFile.exists()) legacyPinFile.renameTo(pinFile);
- }
+ // normalization first, if the old file exists.
+ rename(new File(rootDir,legacyName),file);
+ rename(new File(rootDir,legacyName+".pinned"),pinFile);
// update file if:
// - no file exists today
@@ -434,6 +431,20 @@ public abstract class PluginManager extends AbstractModelObject {
}
}
+ /**
+ * Rename a legacy file to a new name, with care to Windows where {@link File#renameTo(File)}
+ * doesn't work if the destination already exists.
+ */
+ private void rename(File legacyFile, File newFile) throws IOException {
+ if (!legacyFile.exists()) return;
+ if (newFile.exists()) {
+ Util.deleteFile(newFile);
+ }
+ if (!legacyFile.renameTo(newFile)) {
+ LOGGER.warning("Failed to rename " + legacyFile + " to " + newFile);
+ }
+ }
+
/**
* Creates a hudson.PluginStrategy, looking at the corresponding system property.
*/