Search in sources :

Example 1 with UpdateInstaller

use of com.biglybt.pif.update.UpdateInstaller in project BiglyBT by BiglySoftware.

the class PlatformManagerUpdateChecker method installUpdate.

protected void installUpdate(UpdateChecker checker, Update update, ResourceDownloader rd, InputStream data) {
    ZipInputStream zip = null;
    try {
        data = update.verifyData(data, true);
        rd.reportActivity("Data verified successfully");
        UpdateInstaller installer = checker.createInstaller();
        zip = new ZipInputStream(data);
        ZipEntry entry = null;
        while ((entry = zip.getNextEntry()) != null) {
            String name = entry.getName();
            if (name.toLowerCase().startsWith("osx/")) {
                // OSX only files
                name = name.substring(4);
                if (name.length() > 0) {
                    rd.reportActivity("Adding update action for '" + name + "'");
                    if (Logger.isEnabled())
                        Logger.log(new LogEvent(LOGID, "PlatformManager:OSX adding action for '" + name + "'"));
                    // handle sub-dirs
                    String resource_name = name.replaceAll("/", "-");
                    installer.addResource(resource_name, zip, false);
                    String appDir = installer.getInstallDir();
                    String contentsResourceJava = "Contents/Resources/Java/";
                    if (name.startsWith(contentsResourceJava)) {
                        // trying to install something into the "Java" dir
                        // New installs have the "Java" dir as the Install dir.
                        name = name.substring(contentsResourceJava.length());
                    }
                    String target = appDir + File.separator + name;
                    installer.addMoveAction(resource_name, target);
                    if (name.endsWith(".jnilib") || name.endsWith("JavaApplicationStub")) {
                        installer.addChangeRightsAction("755", target);
                    }
                }
            }
        }
        update.complete(true);
    } catch (Throwable e) {
        update.complete(false);
        rd.reportActivity("Update install failed:" + e.getMessage());
    } finally {
        if (zip != null) {
            try {
                zip.close();
            } catch (Throwable e) {
            }
        }
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) LogEvent(com.biglybt.core.logging.LogEvent) UpdateInstaller(com.biglybt.pif.update.UpdateInstaller) ZipEntry(java.util.zip.ZipEntry)

Example 2 with UpdateInstaller

use of com.biglybt.pif.update.UpdateInstaller in project BiglyBT by BiglySoftware.

the class BackupManagerImpl method restoreSupport.

void restoreSupport(File backup_folder, BackupListener listener) {
    try {
        UpdateInstaller installer = null;
        File temp_dir = null;
        boolean ok = false;
        try {
            listener.reportProgress("Reading from " + backup_folder.getAbsolutePath());
            if (!backup_folder.isDirectory()) {
                throw (new Exception("Location '" + backup_folder.getAbsolutePath() + "' must be a directory"));
            }
            listener.reportProgress("Analysing backup");
            File config = new File(backup_folder, ConfigurationManager.CONFIG_FILENAME);
            if (!config.exists()) {
                throw (new Exception("Invalid backup: " + ConfigurationManager.CONFIG_FILENAME + " not found"));
            }
            Map config_map = BDecoder.decode(FileUtil.readFileAsByteArray(config));
            byte[] temp = (byte[]) config_map.get("azureus.user.directory");
            if (temp == null) {
                throw (new Exception("Invalid backup: " + ConfigurationManager.CONFIG_FILENAME + " doesn't contain user directory details"));
            }
            File current_user_dir = new File(SystemProperties.getUserPath());
            File backup_user_dir = new File(new String(temp, "UTF-8"));
            listener.reportProgress("Current user directory:\t" + current_user_dir.getAbsolutePath());
            listener.reportProgress("Backup's user directory:\t" + backup_user_dir.getAbsolutePath());
            temp_dir = AETemporaryFileHandler.createTempDir();
            PluginInterface pi = core.getPluginManager().getDefaultPluginInterface();
            installer = pi.getUpdateManager().createInstaller();
            File[] files = backup_folder.listFiles();
            if (current_user_dir.equals(backup_user_dir)) {
                listener.reportProgress("Directories are the same, no patching required");
                for (File f : files) {
                    File source = new File(temp_dir, f.getName());
                    listener.reportProgress("Creating restore action for '" + f.getName() + "'");
                    copyFiles(f, source);
                    File target = new File(current_user_dir, f.getName());
                    addActions(installer, source, target);
                }
            } else {
                listener.reportProgress("Directories are different, backup requires patching");
                for (File f : files) {
                    File source = new File(temp_dir, f.getName());
                    listener.reportProgress("Creating restore action for '" + f.getName() + "'");
                    if (f.isDirectory() || !f.getName().contains(".config")) {
                        copyFiles(f, source);
                    } else {
                        boolean patched = false;
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f), 1024 * 1024);
                        try {
                            Map m = BDecoder.decode(bis);
                            bis.close();
                            bis = null;
                            if (m.size() > 0) {
                                int applied = patch(m, backup_user_dir.getAbsolutePath(), current_user_dir.getAbsolutePath());
                                if (applied > 0) {
                                    listener.reportProgress("    Applied " + applied + " patches");
                                    patched = FileUtil.writeBytesAsFile2(source.getAbsolutePath(), BEncoder.encode(m));
                                    if (!patched) {
                                        throw (new Exception("Failed to write " + source));
                                    }
                                }
                            }
                        } catch (Throwable e) {
                            String name = f.getName();
                            if (name.contains(".bad") || name.contains(".bak")) {
                                listener.reportProgress("    Ignored failure to patch bad configuration file");
                            } else {
                                throw (e);
                            }
                        } finally {
                            if (bis != null) {
                                try {
                                    bis.close();
                                } catch (Throwable e) {
                                }
                            }
                        }
                        if (!patched) {
                            copyFiles(f, source);
                        }
                    }
                    File target = new File(current_user_dir, f.getName());
                    addActions(installer, source, target);
                }
            }
            listener.reportProgress("Restore action creation complete, restart required to complete the operation");
            listener.reportComplete();
            ok = true;
        } finally {
            if (!ok) {
                if (installer != null) {
                    installer.destroy();
                }
                if (temp_dir != null) {
                    FileUtil.recursiveDeleteNoCheck(temp_dir);
                }
            }
        }
    } catch (Throwable e) {
        listener.reportError(e);
    }
}
Also used : PluginInterface(com.biglybt.pif.PluginInterface) FileInputStream(java.io.FileInputStream) BufferedInputStream(java.io.BufferedInputStream) UpdateInstaller(com.biglybt.pif.update.UpdateInstaller) File(java.io.File)

Example 3 with UpdateInstaller

use of com.biglybt.pif.update.UpdateInstaller in project BiglyBT by BiglySoftware.

the class UI method processArgs.

@Override
public String[] processArgs(CommandLine commands, String[] args) {
    boolean showMainWindow = args.length == 0 || COConfigurationManager.getBooleanParameter("Activate Window On External Download");
    boolean open = true;
    if (commands.hasOption("closedown") || commands.hasOption("shutdown")) {
        try {
            UpdateManager um = core.getPluginManager().getDefaultPluginInterface().getUpdateManager();
            UpdateInstaller[] installers = um.getInstallers();
            for (UpdateInstaller installer : installers) {
                installer.destroy();
            }
        } catch (Throwable e) {
        }
        UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
        if (uiFunctions != null) {
            uiFunctions.dispose(false, false);
        }
        return null;
    }
    if (commands.hasOption("restart")) {
        UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
        if (uiFunctions != null) {
            uiFunctions.dispose(true, false);
        }
        return null;
    }
    if (commands.hasOption("share")) {
        showMainWindow = true;
        open = false;
    }
    if (commands.hasOption("open")) {
        showMainWindow = true;
    }
    String[] rest = commands.getArgs();
    for (int i = 0; i < rest.length; i++) {
        String filename = rest[i];
        File file = new File(filename);
        boolean isURI;
        if (!file.exists() && !isURI(filename)) {
            String magnet_uri = UrlUtils.normaliseMagnetURI(filename);
            isURI = magnet_uri != null;
            if (isURI) {
                filename = magnet_uri;
            }
        } else {
            isURI = isURI(filename);
        }
        if (isURI) {
            if (Logger.isEnabled())
                Logger.log(new LogEvent(LOGID, "StartServer: args[" + i + "] handling as a URI: " + filename));
        } else {
            try {
                if (!file.exists()) {
                    throw (new Exception("File '" + file + "' not found"));
                }
                filename = file.getCanonicalPath();
                Logger.log(new LogEvent(LOGID, "StartServer: file = " + filename));
            } catch (Throwable e) {
                Logger.log(new LogAlert(LogAlert.REPEATABLE, LogAlert.AT_ERROR, "Failed to access torrent file '" + filename + "'. Ensure sufficient temporary " + "file space available (check browser cache usage)."));
            }
        }
        boolean queued = false;
        try {
            this_mon.enter();
            if (queueTorrents) {
                queued_torrents.add(new Object[] { filename, Boolean.valueOf(open) });
                queued = true;
            }
        } finally {
            this_mon.exit();
        }
        if (!queued) {
            handleFile(filename, open);
        }
    }
    if (showMainWindow) {
        showMainWindow();
    }
    return args;
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) LogAlert(com.biglybt.core.logging.LogAlert) UIFunctions(com.biglybt.ui.UIFunctions) UpdateInstaller(com.biglybt.pif.update.UpdateInstaller) UpdateManager(com.biglybt.pif.update.UpdateManager) File(java.io.File)

Example 4 with UpdateInstaller

use of com.biglybt.pif.update.UpdateInstaller in project BiglyBT by BiglySoftware.

the class SWTUpdateChecker method processData.

private boolean processData(UpdateChecker checker, Update update, ResourceDownloader rd, InputStream data) {
    ZipInputStream zip = null;
    try {
        data = update.verifyData(data, true);
        rd.reportActivity("Data verified successfully");
        UpdateInstaller installer = checker.createInstaller();
        zip = new ZipInputStream(data);
        ZipEntry entry = null;
        while ((entry = zip.getNextEntry()) != null) {
            String name = entry.getName();
            if (name.endsWith(".jar")) {
                installer.addResource(name, zip, false);
                if (Constants.isUnix) {
                    // unix SWT goes in <appdir>/swt
                    installer.addMoveAction(name, installer.getInstallDir() + File.separator + "swt" + File.separator + name);
                } else {
                    installer.addMoveAction(name, installer.getInstallDir() + File.separator + name);
                }
            } else if (name.endsWith(".jnilib") && Constants.isOSX) {
                // on OS X, any .jnilib
                installer.addResource(name, zip, false);
                installer.addMoveAction(name, installer.getInstallDir() + "/dll/" + name);
            } else if (name.endsWith(".dll") || name.endsWith(".so") || name.contains(".so.")) {
                // native stuff for windows and linux
                installer.addResource(name, zip, false);
                installer.addMoveAction(name, installer.getInstallDir() + File.separator + name);
            } else if (name.equals("javaw.exe.manifest") || name.equals("azureus.sig")) {
            // silently ignore this one
            } else {
                Debug.outNoStack("SWTUpdate: ignoring zip entry '" + name + "'");
            }
        }
        update.complete(true);
    } catch (Throwable e) {
        update.complete(false);
        Logger.log(new LogAlert(LogAlert.UNREPEATABLE, "SWT Update failed", e));
        return false;
    } finally {
        if (zip != null) {
            try {
                zip.close();
            } catch (Throwable e) {
            }
        }
    }
    return true;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) UpdateInstaller(com.biglybt.pif.update.UpdateInstaller) ZipEntry(java.util.zip.ZipEntry)

Example 5 with UpdateInstaller

use of com.biglybt.pif.update.UpdateInstaller in project BiglyBT by BiglySoftware.

the class UpdateInstallerImpl method installNow.

@Override
public void installNow(final UpdateInstallerListener listener) throws UpdateException {
    try {
        UpdateInstaller[] installers = manager.getInstallers();
        if (installers.length != 1 || installers[0] != this) {
            throw (new UpdateException("Other installers exist - aborting"));
        }
        listener.reportProgress("Update starts");
        ClientRestarter ar = ClientRestarterFactory.create(manager.getCore());
        ar.updateNow();
        new AEThread2("installNow:waiter", true) {

            @Override
            public void run() {
                try {
                    long start = SystemTime.getMonotonousTime();
                    UpdateException pending_error = null;
                    while (true) {
                        Thread.sleep(1000);
                        listener.reportProgress("Checking progress");
                        if (!install_dir.exists()) {
                            break;
                        }
                        File fail_file = new File(install_dir, "install.fail");
                        if (fail_file.exists()) {
                            try {
                                String error = FileUtil.readFileAsString(fail_file, 1024);
                                throw (new UpdateException(error));
                            } catch (Throwable e) {
                                if (e instanceof UpdateException) {
                                    throw (e);
                                }
                                if (pending_error != null) {
                                    throw (pending_error);
                                }
                                pending_error = new UpdateException("Install failed, reason unknown");
                            }
                        }
                        if (SystemTime.getMonotonousTime() - start >= 5 * 60 * 1000) {
                            listener.reportProgress("Timeout");
                            throw (new UpdateException("Timeout waiting for update to apply"));
                        }
                    }
                    listener.reportProgress("Complete");
                    listener.complete();
                } catch (Throwable e) {
                    UpdateException fail;
                    if (e instanceof UpdateException) {
                        fail = (UpdateException) e;
                    } else {
                        fail = new UpdateException("install failed", e);
                    }
                    listener.reportProgress(fail.getMessage());
                    listener.failed(fail);
                } finally {
                    deleteInstaller();
                }
            }
        }.start();
    } catch (Throwable e) {
        deleteInstaller();
        UpdateException fail;
        if (e instanceof UpdateException) {
            fail = (UpdateException) e;
        } else {
            fail = new UpdateException("install failed", e);
        }
        listener.reportProgress(fail.getMessage());
        listener.failed(fail);
        throw (fail);
    }
}
Also used : UpdateInstaller(com.biglybt.pif.update.UpdateInstaller) ClientRestarter(com.biglybt.core.update.ClientRestarter) UpdateException(com.biglybt.pif.update.UpdateException)

Aggregations

UpdateInstaller (com.biglybt.pif.update.UpdateInstaller)8 LogEvent (com.biglybt.core.logging.LogEvent)4 File (java.io.File)3 ZipEntry (java.util.zip.ZipEntry)3 ZipInputStream (java.util.zip.ZipInputStream)3 PluginInterface (com.biglybt.pif.PluginInterface)2 UpdateException (com.biglybt.pif.update.UpdateException)2 UpdateManager (com.biglybt.pif.update.UpdateManager)2 LogAlert (com.biglybt.core.logging.LogAlert)1 LogIDs (com.biglybt.core.logging.LogIDs)1 NetworkAdmin (com.biglybt.core.networkmanager.admin.NetworkAdmin)1 ClientRestarter (com.biglybt.core.update.ClientRestarter)1 AEThread2 (com.biglybt.core.util.AEThread2)1 IndentWriter (com.biglybt.core.util.IndentWriter)1 UpdateInstallerListener (com.biglybt.pif.update.UpdateInstallerListener)1 UIFunctions (com.biglybt.ui.UIFunctions)1 ImageLoader (com.biglybt.ui.swt.imageloader.ImageLoader)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1