Search in sources :

Example 1 with AEWin32Access

use of com.biglybt.platform.win32.access.AEWin32Access in project BiglyBT by BiglySoftware.

the class CoreUpdateChecker method launchUpdate.

protected void launchUpdate(File file, String[] args) {
    try {
        if (file.getName().endsWith(".exe")) {
            try {
                AEWin32Access accessor = AEWin32Manager.getAccessor(true);
                // accessor.createProcess( , false );
                String s_args = null;
                if (args.length > 0) {
                    s_args = "";
                    for (String s : args) {
                        s_args += (s_args.length() == 0 ? "" : " ") + s;
                    }
                }
                accessor.shellExecute(null, file.getAbsolutePath(), s_args, SystemProperties.getApplicationPath(), AEWin32Access.SW_NORMAL);
            } catch (Throwable e) {
                Logger.log(new LogEvent(LogIDs.LOGGER, "AEWin32Access failed", e));
                if (args.length > 0) {
                    String[] s_args = new String[args.length + 1];
                    s_args[0] = file.getAbsolutePath();
                    System.arraycopy(args, 0, s_args, 1, args.length);
                    Runtime.getRuntime().exec(s_args);
                } else {
                    Runtime.getRuntime().exec(new String[] { file.getAbsolutePath() });
                }
            }
        } else {
            // osx, need to unzip .app and launch
            File dir = file.getParentFile();
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
            Throwable unzip_error = null;
            String chmod_command = findCommand("chmod");
            try {
                while (true) {
                    ZipEntry entry = zis.getNextEntry();
                    if (entry == null) {
                        break;
                    }
                    if (entry.isDirectory()) {
                        continue;
                    }
                    String name = entry.getName();
                    FileOutputStream entry_os = null;
                    File entry_file = null;
                    if (!name.endsWith("/")) {
                        entry_file = new File(dir, name.replace('/', File.separatorChar));
                        entry_file.getParentFile().mkdirs();
                        entry_os = new FileOutputStream(entry_file);
                    }
                    try {
                        byte[] buffer = new byte[65536];
                        while (true) {
                            int len = zis.read(buffer);
                            if (len <= 0) {
                                break;
                            }
                            if (entry_os != null) {
                                entry_os.write(buffer, 0, len);
                            }
                        }
                    } finally {
                        if (entry_os != null) {
                            entry_os.close();
                            if (name.endsWith(".jnilib") || name.endsWith("JavaApplicationStub")) {
                                try {
                                    String[] to_run = { chmod_command, "a+x", entry_file.getAbsolutePath() };
                                    runCommand(to_run, true);
                                } catch (Throwable e) {
                                    unzip_error = e;
                                }
                            }
                        }
                    }
                }
            } finally {
                zis.close();
            }
            if (unzip_error != null) {
                throw (unzip_error);
            }
            File[] files = dir.listFiles();
            boolean launched = false;
            for (File f : files) {
                if (f.getName().endsWith(".app")) {
                    String[] to_run;
                    if (args.length == 0 || !Constants.isOSX) {
                        to_run = new String[] { "/bin/sh", "-c", "open \"" + f.getAbsolutePath() + "\"" };
                    } else {
                        to_run = new String[3 + args.length];
                        to_run[0] = findCommand("open");
                        to_run[1] = f.getAbsolutePath();
                        to_run[2] = "--args";
                        System.arraycopy(args, 0, to_run, 3, args.length);
                    }
                    runCommand(to_run, false);
                    launched = true;
                }
            }
            if (!launched) {
                throw (new Exception("No .app files found in '" + dir + "'"));
            }
        }
    } catch (Throwable e) {
        Logger.log(new LogEvent(LogIDs.LOGGER, "Failed to launch update '" + file + "'", e));
    }
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) ZipEntry(java.util.zip.ZipEntry) ZipInputStream(java.util.zip.ZipInputStream) AEWin32Access(com.biglybt.platform.win32.access.AEWin32Access)

Example 2 with AEWin32Access

use of com.biglybt.platform.win32.access.AEWin32Access in project BiglyBT by BiglySoftware.

the class ClientRestarterImpl method restartViaEXE.

private boolean restartViaEXE(PrintWriter log, String exeUpdater, String[] properties, String[] parameters, String backupJavaRunString, boolean update_only) {
    String azRunner = null;
    File fileRestart = null;
    if (!update_only) {
        try {
            azRunner = PlatformManagerFactory.getPlatformManager().getApplicationCommandLine();
        } catch (PlatformManagerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        int result;
        AEWin32Access accessor = AEWin32Manager.getAccessor(true);
        if (accessor == null) {
            result = -123;
        } else {
            if (azRunner != null) {
                // create a batch file to run the updater, then to restart client
                // because the updater would restart client as administrator user
                // and confuse the user
                fileRestart = FileUtil.getUserFile("restart.bat");
                String s = "title BiglyBT Updater Runner\r\n";
                s += exeUpdater + " \"updateonly\"";
                for (int i = 1; i < parameters.length; i++) {
                    s += " \"" + parameters[i].replaceAll("\\\"", "") + "\"";
                }
                s += "\r\n";
                s += "start \"\" \"" + azRunner + "\"";
                byte[] bytes;
                String encoding = FileUtil.getScriptCharsetEncoding();
                if (encoding == null) {
                    bytes = s.getBytes();
                } else {
                    try {
                        bytes = s.getBytes(encoding);
                    } catch (Throwable e) {
                        e.printStackTrace();
                        bytes = s.getBytes();
                    }
                }
                FileUtil.writeBytesAsFile(fileRestart.getAbsolutePath(), bytes);
                result = accessor.shellExecute(null, fileRestart.getAbsolutePath(), null, SystemProperties.getApplicationPath(), AEWin32Access.SW_SHOWMINIMIZED);
            } else {
                String execEXE = "\"-J" + getClassPath().replaceAll("\\\"", "") + "\" ";
                for (int i = 0; i < properties.length; i++) {
                    execEXE += "\"-J" + properties[i].replaceAll("\\\"", "") + "\" ";
                }
                for (int i = 0; i < parameters.length; i++) {
                    execEXE += " \"" + parameters[i].replaceAll("\\\"", "") + "\"";
                }
                log.println("Launch via " + exeUpdater + " params " + execEXE);
                result = accessor.shellExecute(null, exeUpdater, execEXE, SystemProperties.getApplicationPath(), AEWin32Access.SW_NORMAL);
            }
        }
        /*
			 * Some results:
			 * 0: OOM
			 * 2: FNF
			 * 3: Path Not Foud
			 * 5: Access Denied (User clicked cancel on admin access dialog)
			 * 8: OOM
			 * 11: Bad Format
			 * 26: Sharing Violation
			 * 27: Association incomplete
			 * 28: DDE Timeout
			 * 29: DDE Fail
			 * 30: DDE Busy
			 * 31: No Association
			 * 32: DLL Not found
			 * >32: OK!
			 */
        log.println("   -> " + result);
        if (result <= 32) {
            String sErrorReason = "";
            String key = null;
            switch(result) {
                case 0:
                case 8:
                    key = "oom";
                    break;
                case 2:
                    key = "fnf";
                    break;
                case 3:
                    key = "pnf";
                    break;
                case 5:
                    key = "denied";
                    break;
                case 11:
                    key = "bad";
                    break;
                case -123:
                    key = "nowin32";
                    break;
                default:
                    sErrorReason = "" + result;
                    break;
            }
            if (key != null) {
                sErrorReason = MessageText.getString("restart.error." + key, new String[] { exeUpdater, SystemProperties.getApplicationPath() });
            }
            Logger.log(new LogAlert(false, LogAlert.AT_ERROR, MessageText.getString("restart.error", new String[] { sErrorReason })));
            return false;
        }
    } catch (Throwable f) {
        f.printStackTrace(log);
        return javaSpawn(log, backupJavaRunString);
    }
    return true;
}
Also used : PlatformManagerException(com.biglybt.pif.platform.PlatformManagerException) File(java.io.File) AEWin32Access(com.biglybt.platform.win32.access.AEWin32Access) LogAlert(com.biglybt.core.logging.LogAlert)

Aggregations

AEWin32Access (com.biglybt.platform.win32.access.AEWin32Access)2 LogAlert (com.biglybt.core.logging.LogAlert)1 LogEvent (com.biglybt.core.logging.LogEvent)1 PlatformManagerException (com.biglybt.pif.platform.PlatformManagerException)1 File (java.io.File)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1