From da8ed34d96d556ea177a6db85a2a3b3276f86424 Mon Sep 17 00:00:00 2001 From: Konstantin Bulenkov Date: Tue, 21 Mar 2017 10:00:22 -0700 Subject: [PATCH] add ability to avoid backing up the tool before applying to patch. This makes Toolbox App apply patches ~30% faster --- updater/src/META-INF/MANIFEST.MF | 2 +- updater/src/com/intellij/updater/Patch.java | 8 ++++++-- .../src/com/intellij/updater/PatchFileCreator.java | 12 +++++++++++- updater/src/com/intellij/updater/UpdateAction.java | 3 ++- .../src/com/intellij/updater/UpdateZipAction.java | 4 +++- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/updater/src/META-INF/MANIFEST.MF b/updater/src/META-INF/MANIFEST.MF index 5ea08f94af6c..e70f246130a9 100644 --- a/updater/src/META-INF/MANIFEST.MF +++ b/updater/src/META-INF/MANIFEST.MF @@ -1,3 +1,3 @@ Manifest-Version: 1.0 Main-Class: com.intellij.updater.Bootstrap -Patcher-Version: 2.1 +Patcher-Version: 3.0 diff --git a/updater/src/com/intellij/updater/Patch.java b/updater/src/com/intellij/updater/Patch.java index a374f7178413..0e89e34d588e 100644 --- a/updater/src/com/intellij/updater/Patch.java +++ b/updater/src/com/intellij/updater/Patch.java @@ -311,7 +311,9 @@ public class Patch { if (each.shouldApply(toDir, options)) actionsToProcess.add(each); } - forEach(actionsToProcess, "Backing up files...", ui, true, action -> action.backup(toDir, backupDir)); + if (backupDir != null) { + forEach(actionsToProcess, "Backing up files...", ui, true, action -> action.backup(toDir, backupDir)); + } List appliedActions = new ArrayList<>(); boolean shouldRevert = false; @@ -342,7 +344,9 @@ public class Patch { } if (shouldRevert) { - revert(appliedActions, backupDir, rootDir, ui); + if (backupDir != null) { + revert(appliedActions, backupDir, rootDir, ui); + } appliedActions.clear(); if (cancelled) throw new OperationCancelledException(); diff --git a/updater/src/com/intellij/updater/PatchFileCreator.java b/updater/src/com/intellij/updater/PatchFileCreator.java index 83b8c307c3ce..942830343e37 100644 --- a/updater/src/com/intellij/updater/PatchFileCreator.java +++ b/updater/src/com/intellij/updater/PatchFileCreator.java @@ -27,6 +27,11 @@ import java.util.zip.ZipOutputStream; public class PatchFileCreator { private static final String PATCH_INFO_FILE_NAME = ".patch-info"; + /** + * This property allows applying patches without creating backups. In this case a callee is responsible for that. + * Example: JetBrains Toolbox App copies a tool it wants to update and then applies the patch. + */ + private static final String NO_BACKUP_PROPERTY = "no.backup"; public static Patch create(PatchSpec spec, File patchFile, UpdaterUI ui) throws IOException, OperationCancelledException { Patch patchInfo = new Patch(spec, ui); @@ -72,7 +77,12 @@ public class PatchFileCreator { public static boolean apply(PreparationResult preparationResult, Map options, UpdaterUI ui) throws IOException, OperationCancelledException { - return apply(preparationResult, options, Utils.createTempDir(), ui).applied; + File backupDir = shouldSkipBackups() ? null : Utils.createTempDir(); + return apply(preparationResult, options, backupDir, ui).applied; + } + + private static boolean shouldSkipBackups() { + return Boolean.parseBoolean(System.getProperty(NO_BACKUP_PROPERTY, "false")); } public static Patch.ApplicationResult apply(PreparationResult preparationResult, diff --git a/updater/src/com/intellij/updater/UpdateAction.java b/updater/src/com/intellij/updater/UpdateAction.java index 561466c2d874..0822ad7b4ee3 100644 --- a/updater/src/com/intellij/updater/UpdateAction.java +++ b/updater/src/com/intellij/updater/UpdateAction.java @@ -51,7 +51,8 @@ public class UpdateAction extends BaseUpdateAction { protected void doApply(ZipFile patchFile, File backupDir, File toFile) throws IOException { Runner.logger().info("Update action. File: " + toFile.getAbsolutePath()); - File source = getSource(backupDir); + //in case no backup is required + File source = backupDir == null ? toFile : getSource(backupDir); if (!isMove()) { try (InputStream in = Utils.findEntryInputStream(patchFile, getPath())) { if (in == null) { diff --git a/updater/src/com/intellij/updater/UpdateZipAction.java b/updater/src/com/intellij/updater/UpdateZipAction.java index 019a193fa294..b12e5b79f901 100644 --- a/updater/src/com/intellij/updater/UpdateZipAction.java +++ b/updater/src/com/intellij/updater/UpdateZipAction.java @@ -135,9 +135,11 @@ public class UpdateZipAction extends BaseUpdateAction { @Override protected void doApply(final ZipFile patchFile, File backupDir, File toFile) throws IOException { File temp = Utils.getTempFile(toFile.getName()); + //in case no backup is required + File source = backupDir == null ? toFile : getSource(backupDir); try (ZipOutputWrapper out = new ZipOutputWrapper(new FileOutputStream(temp), 0)) { - processZipFile(getSource(backupDir), (entry, in) -> { + processZipFile(source, (entry, in) -> { String path = entry.getName(); if (myFilesToUpdate.contains(path)) { try (OutputStream entryOut = out.zipStream(path)) {