From 424aa5cd245446b64e88509fc85172615c22454a Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Fri, 13 Apr 2007 18:48:23 +0000 Subject: [PATCH] Fall back to renames by copying if normal renaming fails as Windows XP decided today that renaming is no longer allowed (at least in the directory in which we're installing our games). Also nixed the old JavaScript bridge stuff which we no longer use and was flaky in the first place. --- .../threerings/getdown/data/Application.java | 21 ++---- .../getdown/launcher/GetdownApplet.java | 14 ---- .../com/threerings/getdown/tools/Patcher.java | 6 +- .../com/threerings/getdown/util/FileUtil.java | 71 +++++++++++++++++++ src/java/netscape/javascript/JSException.java | 8 --- src/java/netscape/javascript/JSObject.java | 23 ------ 6 files changed, 81 insertions(+), 62 deletions(-) create mode 100644 src/java/com/threerings/getdown/util/FileUtil.java delete mode 100644 src/java/netscape/javascript/JSException.java delete mode 100644 src/java/netscape/javascript/JSObject.java diff --git a/src/java/com/threerings/getdown/data/Application.java b/src/java/com/threerings/getdown/data/Application.java index a409d95..0662cc1 100644 --- a/src/java/com/threerings/getdown/data/Application.java +++ b/src/java/com/threerings/getdown/data/Application.java @@ -67,6 +67,7 @@ import org.apache.commons.io.IOUtils; import com.threerings.getdown.Log; import com.threerings.getdown.util.ConfigUtil; +import com.threerings.getdown.util.FileUtil; import com.threerings.getdown.util.LaunchUtil; import com.threerings.getdown.util.MetaProgressObserver; import com.threerings.getdown.util.ProgressObserver; @@ -1082,24 +1083,16 @@ public class Application } } - // Windows is a wonderful operating system, it won't let you rename a file overtop of - // another one; thus to avoid running the risk of getting royally fucked, we have to do - // this complicated backup bullshit; this way if the shit hits the fan before we get the - // new copy into place, we should be able to read from the backup copy; yay! + // move the old file to a _old version File original = getLocalPath(path); - if (RunAnywhere.isWindows() && original.exists()) { - File backup = getLocalPath(path + "_old"); - if (backup.exists() && !backup.delete()) { - Log.warning("Failed to delete " + backup + "."); - } - if (!original.renameTo(backup)) { - Log.warning("Failed to move " + original + " to backup. We will likely fail " + - "to replace it with " + target + "."); + if (original.exists()) { + if (!FileUtil.renameTo(original, getLocalPath(path + "_old"))) { + Log.warning("Failed to move " + original + " to backup."); } } - // now attempt to replace the current file with the new one - if (!target.renameTo(original)) { + // now move the temporary file over the original + if (!FileUtil.renameTo(target, original)) { throw new IOException("Failed to rename(" + target + ", " + original + ")"); } } diff --git a/src/java/com/threerings/getdown/launcher/GetdownApplet.java b/src/java/com/threerings/getdown/launcher/GetdownApplet.java index ce9610c..ceb68dd 100644 --- a/src/java/com/threerings/getdown/launcher/GetdownApplet.java +++ b/src/java/com/threerings/getdown/launcher/GetdownApplet.java @@ -32,9 +32,6 @@ import java.io.FileOutputStream; import java.io.File; import java.io.PrintStream; -import netscape.javascript.JSObject; -import netscape.javascript.JSException; - import com.samskivert.util.RunAnywhere; import com.samskivert.util.StringUtil; @@ -125,17 +122,6 @@ public class GetdownApplet extends JApplet protected void exit (int exitCode) { // don't exit as we're in an applet } - @Override protected void setStatus ( - final String message, final int percent, final long remaining, boolean createUI) - { - super.setStatus(message, percent, remaining, createUI); - try { - JSObject.getWindow(GetdownApplet.this).call( - "getdownStatus", new Object[] { message, percent, remaining }); - } catch (Throwable t) { - Log.warning("Failed to communicate status to JavaScript: " + t); - } - } }; // set up our user interface immediately diff --git a/src/java/com/threerings/getdown/tools/Patcher.java b/src/java/com/threerings/getdown/tools/Patcher.java index 7e06df0..d1b7bb6 100644 --- a/src/java/com/threerings/getdown/tools/Patcher.java +++ b/src/java/com/threerings/getdown/tools/Patcher.java @@ -33,6 +33,7 @@ import com.samskivert.io.StreamUtil; import org.apache.commons.io.IOUtils; import com.threerings.getdown.Log; +import com.threerings.getdown.util.FileUtil; import com.threerings.getdown.util.ProgressObserver; /** @@ -166,7 +167,7 @@ public class Patcher fout = null; // move the current version of the jar to .old - if (!target.renameTo(otarget)) { + if (!FileUtil.renameTo(target, otarget)) { System.err.println("Failed to .oldify '" + target + "'."); return; } @@ -186,8 +187,7 @@ public class Patcher } catch (IOException ioe) { if (patcher == null) { - System.err.println("Failed to write patch file '" + patch + - "': " + ioe); + System.err.println("Failed to write patch file '" + patch + "': " + ioe); } else { System.err.println("Error patching '" + target + "': " + ioe); } diff --git a/src/java/com/threerings/getdown/util/FileUtil.java b/src/java/com/threerings/getdown/util/FileUtil.java new file mode 100644 index 0000000..94e3220 --- /dev/null +++ b/src/java/com/threerings/getdown/util/FileUtil.java @@ -0,0 +1,71 @@ +// +// $Id$ +// +// Getdown - application installer, patcher and launcher +// Copyright (C) 2004-2006 Three Rings Design, Inc. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 2 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the: Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + +package com.threerings.getdown.util; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; + +import com.samskivert.io.StreamUtil; + +import org.apache.commons.io.IOUtils; + +import com.threerings.getdown.Log; + +/** + * File related utilities. + */ +public class FileUtil +{ + /** + * Gets the specified source file to the specified destination file by hook or crook. Windows + * has all sorts of problems which we work around in this method. + * + * @return true if we managed to get the job done, false otherwise. + */ + public static boolean renameTo (File source, File dest) + { + // if we're on a civilized operating system we may be able to simple rename it + if (source.renameTo(dest)) { + return true; + } + + // otherwise try copying the old data over the new + FileInputStream fin = null; + FileOutputStream fout = null; + try { + fin = new FileInputStream(source); + fout = new FileOutputStream(dest); + IOUtils.copy(fin, fout); + source.delete(); + return true; + + } catch (IOException ioe) { + Log.warning("Failed to copy " + source + " to " + dest + ": " + ioe); + return false; + + } finally { + StreamUtil.close(fin); + StreamUtil.close(fout); + } + } +} diff --git a/src/java/netscape/javascript/JSException.java b/src/java/netscape/javascript/JSException.java deleted file mode 100644 index 49b2d46..0000000 --- a/src/java/netscape/javascript/JSException.java +++ /dev/null @@ -1,8 +0,0 @@ -package netscape.javascript; - -/** - * This just exists to compile against so that we don't have to link against plugin.jar - */ -public class JSException extends RuntimeException -{ -} diff --git a/src/java/netscape/javascript/JSObject.java b/src/java/netscape/javascript/JSObject.java deleted file mode 100644 index a3b5353..0000000 --- a/src/java/netscape/javascript/JSObject.java +++ /dev/null @@ -1,23 +0,0 @@ -package netscape.javascript; - -import java.applet.Applet; - -/** - * This just exists to compile against so that we don't have to link against plugin.jar - */ -public abstract class JSObject -{ - public abstract Object call (String func, Object[] args) throws JSException; - public abstract Object eval (String code) throws JSException; - public abstract Object getMember (String name) throws JSException; - public abstract void setMember (String name, Object value) throws JSException; - public abstract void removeMember (String name) throws JSException; - public abstract Object getSlot (int slot) throws JSException; - public abstract void setSlot (int slot, Object value) throws JSException; - - public static JSObject getWindow (Applet applet) - throws JSException - { - return null; - } -}