[FIXED JENKINS-12514]

Fixed the file overwrite problem on Windows.
This commit is contained in:
Kohsuke Kawaguchi committed 2012-03-09 16:51:05 -08:00
1 parent 63529f0f3e
commit 132f8b769d
2 files changed
+20 -6

No files matched your search

+17 -6
View File
@@ -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.
*/