From 7cec263940df4159e89693857bee004f674674ed Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 4 Apr 2006 21:45:04 +0000 Subject: [PATCH] Migrated upgradeGetdown() out of Yohoho and into a Getdown utility class. --- .../threerings/getdown/util/LaunchUtil.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/java/com/threerings/getdown/util/LaunchUtil.java b/src/java/com/threerings/getdown/util/LaunchUtil.java index cbb21b0..a8ec21d 100644 --- a/src/java/com/threerings/getdown/util/LaunchUtil.java +++ b/src/java/com/threerings/getdown/util/LaunchUtil.java @@ -4,6 +4,11 @@ package com.threerings.getdown.util; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +import org.apache.commons.io.CopyUtils; import com.threerings.getdown.Log; @@ -52,6 +57,65 @@ public class LaunchUtil return apbase + "java"; } + /** + * Upgrades Getdown by moving an installation managed copy of the Getdown + * jar file over the non-managed copy (which would be used to run Getdown + * itself). + * + *

If the upgrade fails for a variety of reasons, warnings are logged + * but no other actions are taken. There's not much else one can do other + * than try again next time around. + */ + public static void upgradeGetdown (File oldgd, File curgd, File newgd) + { + // we assume getdown's jar file size changes with every upgrade, this + // is not guaranteed, but in reality it will, and it allows us to avoid + // pointlessly upgrading getdown every time the client is updated which + // is unnecessarily flirting with danger + if (!newgd.exists() || newgd.length() == curgd.length()) { + return; + } + + Log.info("Updating Getdown with " + newgd + "..."); + + // clear out any old getdown + if (oldgd.exists()) { + oldgd.delete(); + } + + // now try updating using renames + if (!curgd.exists() || curgd.renameTo(oldgd)) { + if (newgd.renameTo(curgd)) { + oldgd.delete(); // yay! + try { + // copy the moved file back to getdown-dop-new.jar so that + // we don't end up downloading another copy next time + CopyUtils.copy(new FileInputStream(curgd), + new FileOutputStream(newgd)); + } catch (IOException e) { + Log.warning("Error copying updated Getdown back: " + e); + } + return; + } + + Log.warning("Unable to renameTo(" + oldgd + ")."); + // try to unfuck ourselves + if (!oldgd.renameTo(curgd)) { + Log.warning("Oh God, why dost thee scorn me so."); + } + } + + // that didn't work, let's try copying it + Log.info("Attempting to upgrade by copying over " + curgd + "..."); + try { + CopyUtils.copy(new FileInputStream(newgd), + new FileOutputStream(curgd)); + } catch (IOException ioe) { + Log.warning("Mayday! Brute force copy method also failed."); + Log.logStackTrace(ioe); + } + } + /** * Returns true if, on this operating system, we have to stick around * and read the stderr from our children processes to prevent them