Beispiel #1
0
        public MovedDirectory(string dirPath, bool isLoopingTest)
        {
            if (isLoopingTest)
            {
                SrcDirPath = dirPath;

                // Only move, if there is an existing Tools directory
                if (Directory.Exists(SrcDirPath))
                {
                    DestDirPath = Path.Combine(Directory.GetParent(dirPath).FullName,
                                               MOVED_PREFIX + Path.GetFileName(dirPath));
                    if (Directory.Exists(DestDirPath))
                    {
                        DestDirPath = DirectoryEx.GetUniqueName(DestDirPath);
                    }

                    Helpers.TryTwice(() => Directory.Move(SrcDirPath, DestDirPath));
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Function for unpacking zipped External tools.
        /// </summary>
        /// <param name="pathToZip">Path to the zipped file that contains the tool and all its assicaited files.</param>
        /// <param name="unpackSupport"> Interface that implements required functions that are dependent on context.</param>
        /// <returns></returns>
        public static UnzipToolReturnAccumulator UnpackZipTool(string pathToZip, IUnpackZipToolSupport unpackSupport)
        {
            if (!Directory.Exists(pathToZip) && !File.Exists(pathToZip))
            {
                throw new FileNotFoundException(pathToZip);
            }

            //Removes any old folders that dont have Tools associated with them
            CheckToolDirConsistency();

            var    retval = new UnzipToolReturnAccumulator();
            string name   = Path.GetFileNameWithoutExtension(pathToZip);

            if (name == null)
            {
                throw new ToolExecutionException(Resources.ConfigureToolsDlg_unpackZipTool_Invalid_file_selected__No_tools_added_);
            }
            // This helps with zipfiles that have spaces in the titles.
            // Consider: We may want to add quotes around usages of the $(ToolDir) macro incase the Tool directory has spaces in one of its directory names.
            name = name.Replace(' ', '_');

            string outerToolsFolderPath = ToolDescriptionHelpers.GetToolsDirectory();

            if (string.IsNullOrEmpty(outerToolsFolderPath))
            {
                throw new ToolExecutionException(Resources.ConfigureToolsDlg_unpackZipTool_Error_unpacking_zipped_tools);
            }
            string tempFolderPath = Path.Combine(outerToolsFolderPath, @"Temp");

            var toolDir = new DirectoryInfo(tempFolderPath);

            if (!toolDir.Exists)
            {
                toolDir.Create();
            }

            // This naming conflict shouldn't happen. The temp file should be empty.
            // Consider: Try to delete the existing directory in the temp directory.
            string tempToolPath = Path.Combine(tempFolderPath, name);

            if (Directory.Exists(tempToolPath))
            {
                tempToolPath = DirectoryEx.GetUniqueName(tempToolPath);
            }

            using (new TemporaryDirectory(tempToolPath))
            {
                if (Directory.Exists(pathToZip))
                {
                    DirectoryCopy(pathToZip, tempToolPath);
                }
                else
                {
                    using (var zipFile = new ZipFile(pathToZip))
                    {
                        try
                        {
                            zipFile.ExtractAll(tempToolPath, ExtractExistingFileAction.OverwriteSilently);
                        }
                        catch (Exception)
                        {
                            throw new ToolExecutionException(Resources.ConfigureToolsDlg_unpackZipTool_There_is_a_naming_conflict_in_unpacking_the_zip__Tool_importing_canceled_);
                        }
                    }
                }

                var dirInfo = new DirectoryInfo(tempToolPath);
                if (!dirInfo.Exists)
                {
                    // Case where they try to load tools from an empty zipfile then the folder is never created.
                    dirInfo.Create();
                }

                var toolInfDir = new DirectoryInfo(Path.Combine(tempToolPath, TOOL_INF));
                if (!toolInfDir.Exists)
                {
                    throw new ToolExecutionException(TextUtil.LineSeparate(
                                                         Resources.ToolInstaller_UnpackZipTool_The_selected_zip_file_is_not_an_installable_tool_,
                                                         string.Format(Resources.ToolInstaller_UnpackZipTool_Error__It_does_not_contain_the_required__0__directory_, TOOL_INF)));
                }

                // Handle info.properties
                var toolInfo = GetToolInfo(toolInfDir, retval);

                if (!HandleAnnotations(unpackSupport.ShouldOverwriteAnnotations, toolInfDir))
                {
                    return(null);
                }

                HandleLegacyQuaSAR(toolInfo);

                var toolsToBeOverwritten = GetToolsToBeOverwritten(toolInfo.PackageIdentifier);

                List <ReportOrViewSpec> newReports;
                var existingReports = FindReportConflicts(toolInfDir, tempToolPath, out newReports);

                bool?overwrite = IsOverwrite(unpackSupport.ShouldOverwrite, toolsToBeOverwritten, existingReports, toolInfo);
                if (!overwrite.HasValue)
                {
                    // User canceled installation.
                    return(null);
                }
                string DirectoryToRemove = null;
                if (overwrite.Value)
                {
                    // Delete the tools and their containing folder
                    if (toolsToBeOverwritten.Count > 0)
                    {
                        foreach (var tool in toolsToBeOverwritten)
                        {
                            Settings.Default.ToolList.Remove(tool);
                        }
                        // The tools are all guarenteed to be from the same directory by GetToolsToBeOverwritten
                        // and all toolDescriptions in a directory come from the same installation
                        DirectoryToRemove = toolsToBeOverwritten.First().ToolDirPath;
                    }

                    // Overwrite all existing reports.
                    foreach (ReportOrViewSpec item in existingReports)
                    {
                        ReportSharing.SaveReport(PersistedViews.ExternalToolsGroup, item);
                    }
                }

                // Add all new reports.
                foreach (ReportOrViewSpec item in newReports)
                {
                    ReportSharing.SaveReport(PersistedViews.ExternalToolsGroup, item);
                }
                var reportRenameMapping = new Dictionary <string, string>();
                if (overwrite == false) // Dont overwrite so rename reports.
                {
                    // Deal with renaming reports!
                    foreach (ReportOrViewSpec item in existingReports)
                    {
                        string oldname = item.GetKey();
                        string newname = GetUniqueReportName(oldname);
                        reportRenameMapping.Add(oldname, newname);
                        ReportSharing.SaveReportAs(PersistedViews.ExternalToolsGroup, item, newname);
                    }
                }

                foreach (FileInfo file in toolInfDir.GetFiles(@"*.properties"))
                {
                    // We will replace the tool Directory value (null below) later when we know the import is sucessful.
                    AddToolFromProperties(file, retval, toolInfo, null, tempToolPath, reportRenameMapping);
                }

                // Check if we need to install a program
                if (retval.Installations.Count > 0)
                {
                    foreach (var ppc in retval.Installations.Keys)
                    {
                        string pathToPackageInstallScript = null;
                        if (ppc.ProgramName.Equals(@"R") && retval.Installations[ppc].Count != 0)
                        {
                            pathToPackageInstallScript = Path.Combine(tempToolPath, TOOL_INF, INSTALL_R_PACKAGES);
                            if (!File.Exists(pathToPackageInstallScript))
                            {
                                throw new ToolExecutionException(TextUtil.LineSeparate(string.Format(Resources.ToolInstaller_UnpackZipTool_Error__There_is_a_file_missing_the__0__zip, name),
                                                                                       string.Empty,
                                                                                       string.Format(Resources.ToolInstaller_UnpackZipTool_Tool_Uses_R_and_specifies_Packages_without_an__0__file_in_the_tool_inf_directory_, INSTALL_R_PACKAGES)));
                            }
                        }

                        string path = unpackSupport.InstallProgram(ppc, retval.Installations[ppc], pathToPackageInstallScript);
                        if (path == null)
                        {
                            // Cancel installation
                            return(null);
                        }
                        else if (path != string.Empty)
                        {
                            if (Settings.Default.ToolFilePaths.ContainsKey(ppc))
                            {
                                Settings.Default.ToolFilePaths.Remove(ppc);
                            }
                            Settings.Default.ToolFilePaths.Add(ppc, path);
                        }
                    }
                }
                // We don't decide the final toolDirPath until we make it to here.
                // This will require some fixing of the tooldir and path to dll in each of the tools in retval.validtoolsfound
                // It also enables us to not delete the tools from the tool list unless we have a sucessful installation.

                // Decide the permToolPath.
                if (DirectoryToRemove != null)
                {
                    DirectoryEx.SafeDelete(DirectoryToRemove);
                }

                // Final Directory Location.
                string permToolPath = DirectoryEx.GetUniqueName(Path.Combine(outerToolsFolderPath, name));

                foreach (var tool in retval.ValidToolsFound)
                {
                    tool.ToolDirPath = permToolPath;
                    if (!string.IsNullOrEmpty(tool.ArgsCollectorDllPath))
                    {
                        tool.ArgsCollectorDllPath = Path.Combine(permToolPath, tool.ArgsCollectorDllPath);
                    }
                    Settings.Default.ToolList.Add(tool);
                }

                if (retval.ValidToolsFound.Count != 0)
                {
                    Helpers.TryTwice(() => Directory.Move(tempToolPath, permToolPath));
                }
            }
            return(retval);
        }
Beispiel #3
0
        private static void UpdateAppInfoFile()
        {
            ResetAppInfoFile();
            if (_appInfo?.Count > 430)
            {
                goto Shareware;
            }

            foreach (var mirror in AppSupply.GetMirrors(AppSuppliers.Internal))
            {
                var link = PathEx.AltCombine(mirror, ".free", "AppInfo.ini");
                if (Log.DebugMode > 0)
                {
                    Log.Write($"Cache: Looking for '{link}'.");
                }
                if (NetEx.FileIsAvailable(link, 30000, UserAgents.Internal))
                {
                    WebTransfer.DownloadFile(link, CachePaths.AppInfo, 60000, UserAgents.Internal, false);
                }
                if (!File.Exists(CachePaths.AppInfo))
                {
                    continue;
                }
                break;
            }

            var blacklist = Array.Empty <string>();

            if (File.Exists(CachePaths.AppInfo))
            {
                blacklist = Ini.GetSections(CachePaths.AppInfo).Where(x => Ini.Read(x, "Disabled", false, CachePaths.AppInfo)).ToArray();
                UpdateAppInfoData(CachePaths.AppInfo, blacklist);
            }

            var tmpDir = Path.Combine(CorePaths.TempDir, DirectoryEx.GetUniqueName());

            if (!DirectoryEx.Create(tmpDir))
            {
                return;
            }
            var tmpZip = Path.Combine(tmpDir, "AppInfo.7z");

            foreach (var mirror in AppSupply.GetMirrors(AppSuppliers.Internal))
            {
                var link = PathEx.AltCombine(mirror, ".free", "AppInfo.7z");
                if (Log.DebugMode > 0)
                {
                    Log.Write($"Cache: Looking for '{link}'.");
                }
                if (NetEx.FileIsAvailable(link, 30000, UserAgents.Internal))
                {
                    WebTransfer.DownloadFile(link, tmpZip, 60000, UserAgents.Internal, false);
                }
                if (!File.Exists(tmpZip))
                {
                    continue;
                }
                break;
            }
            if (!File.Exists(tmpZip))
            {
                var link = PathEx.AltCombine(AppSupplierHosts.PortableApps, "updater", "update.7z");
                if (Log.DebugMode > 0)
                {
                    Log.Write($"Cache: Looking for '{link}'.");
                }
                if (NetEx.FileIsAvailable(link, 60000, UserAgents.Empty))
                {
                    WebTransfer.DownloadFile(link, tmpZip, 60000, UserAgents.Empty, false);
                }
            }
            if (File.Exists(tmpZip))
            {
                using (var process = SevenZip.DefaultArchiver.Extract(tmpZip, tmpDir))
                    if (process?.HasExited == false)
                    {
                        process.WaitForExit();
                    }
                FileEx.TryDelete(tmpZip);
            }
            var tmpIni = DirectoryEx.GetFiles(tmpDir, "*.ini").FirstOrDefault();

            if (!File.Exists(tmpIni))
            {
                DirectoryEx.TryDelete(tmpDir);
                return;
            }
            UpdateAppInfoData(tmpIni, blacklist);

            FileEx.Serialize(CachePaths.AppInfo, AppInfo, true);
            DirectoryEx.TryDelete(tmpDir);

Shareware:
            if (!Shareware.Enabled)
            {
                return;
            }

            foreach (var srv in Shareware.GetAddresses())
            {
                var key = Shareware.FindAddressKey(srv);
                var usr = Shareware.GetUser(srv);
                var pwd = Shareware.GetPassword(srv);
                var url = PathEx.AltCombine(srv, "AppInfo.ini");
                if (Log.DebugMode > 0)
                {
                    Log.Write($"Shareware: Looking for '{{{key.Encode()}}}/AppInfo.ini'.");
                }
                if (!NetEx.FileIsAvailable(url, usr, pwd, 60000, UserAgents.Default))
                {
                    continue;
                }
                var appInfo = WebTransfer.DownloadString(url, usr, pwd, 60000, UserAgents.Default);
                if (string.IsNullOrWhiteSpace(appInfo))
                {
                    continue;
                }
                var srvKey = key?.Decode(BinaryToTextEncoding.Base85);
                UpdateAppInfoData(appInfo, null, srvKey?.Any() == true ? new ReadOnlyCollection <byte>(srvKey) : default);
            }
        }