Пример #1
0
        private static void Update(string updateeFilePath, string packageContentDirPath, bool restartUpdatee)
        {
            // Wait until updatee is writable to ensure all running instances have exited
            WriteLog("Waiting for all running updatee instances to exit...");
            while (!FileEx.CheckWriteAccess(updateeFilePath))
            {
                Thread.Sleep(100);
            }

            // Copy over the package contents
            WriteLog("Copying package contents from storage to updatee's directory...");
            var updateeDirPath = Path.GetDirectoryName(updateeFilePath);

            DirectoryEx.Copy(packageContentDirPath, updateeDirPath);

            // Launch the updatee again if requested
            if (restartUpdatee)
            {
                WriteLog("Restarting updatee...");

                using (var restartedUpdateeProcess = Process.Start(updateeFilePath))
                    WriteLog($"Restarted as pid:{restartedUpdateeProcess?.Id}.");
            }

            // Delete package content directory
            WriteLog("Deleting package contents from storage...");
            Directory.Delete(packageContentDirPath, true);
        }
Пример #2
0
 private void EnsureUpdaterNotLaunched()
 {
     // Check whether we have write access to updater executable
     // (this is a reasonably accurate check for whether that process is running)
     if (File.Exists(_updaterFilePath) && !FileEx.CheckWriteAccess(_updaterFilePath))
     {
         throw new UpdaterAlreadyLaunchedException();
     }
 }
Пример #3
0
        private void RunCore()
        {
            var updateeDirPath = Path.GetDirectoryName(_updateeFilePath);

            // Wait until updatee is writable to ensure all running instances have exited
            WriteLog("Waiting for all running updatee instances to exit...");
            while (!FileEx.CheckWriteAccess(_updateeFilePath))
            {
                Thread.Sleep(100);
            }

            // Copy over the package contents
            WriteLog("Copying package contents from storage to updatee's directory...");
            DirectoryEx.Copy(_packageContentDirPath, updateeDirPath);

            // Restart updatee if requested
            if (_restartUpdatee)
            {
                var startInfo = new ProcessStartInfo
                {
                    WorkingDirectory = updateeDirPath,
                    Arguments        = _routedArgs,
                    UseShellExecute  = true // avoid sharing console window with updatee
                };

                // If updatee is an .exe file - start it directly
                if (string.Equals(Path.GetExtension(_updateeFilePath), ".exe", StringComparison.OrdinalIgnoreCase))
                {
                    startInfo.FileName = _updateeFilePath;
                }
                // If not - figure out what to do with it
                else
                {
                    // If there's an .exe file with same name - start it instead
                    // Security vulnerability?
                    if (File.Exists(Path.ChangeExtension(_updateeFilePath, ".exe")))
                    {
                        startInfo.FileName = Path.ChangeExtension(_updateeFilePath, ".exe");
                    }
                    // Otherwise - start the updatee using dotnet SDK
                    else
                    {
                        startInfo.FileName  = "dotnet";
                        startInfo.Arguments = $"{_updateeFilePath} {_routedArgs}";
                    }
                }

                WriteLog($"Restarting updatee [{startInfo.FileName} {startInfo.Arguments}]...");

                using var restartedUpdateeProcess = Process.Start(startInfo);
                WriteLog($"Restarted as pid:{restartedUpdateeProcess?.Id}.");
            }

            // Delete package content directory
            WriteLog("Deleting package contents from storage...");
            Directory.Delete(_packageContentDirPath, true);
        }
Пример #4
0
        private static void Update(string updateeFilePath, string packageContentDirPath, bool restartUpdatee)
        {
            var updateeDirPath = Path.GetDirectoryName(updateeFilePath);

            // Wait until updatee is writable to ensure all running instances have exited
            WriteLog("Waiting for all running updatee instances to exit...");
            while (!FileEx.CheckWriteAccess(updateeFilePath))
            {
                Thread.Sleep(100);
            }

            // Copy over the package contents
            WriteLog("Copying package contents from storage to updatee's directory...");
            DirectoryEx.Copy(packageContentDirPath, updateeDirPath);

            // Restart updatee if requested
            if (restartUpdatee)
            {
                WriteLog("Restarting updatee...");

                var startInfo = new ProcessStartInfo
                {
                    WorkingDirectory = updateeDirPath
                };

                // If updatee is an .exe file - start it directly
                if (string.Equals(Path.GetExtension(updateeFilePath), ".exe", StringComparison.OrdinalIgnoreCase))
                {
                    startInfo.FileName = updateeFilePath;
                }
                // If not - figure out what to do with it
                else
                {
                    //Start with dotnet
                    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                    {
                        var process = new Process
                        {
                            StartInfo = new ProcessStartInfo
                            {
                                FileName               = "dotnet",
                                Arguments              = Path.GetFileName(updateeFilePath),
                                WorkingDirectory       = Path.GetDirectoryName(updateeFilePath),
                                UseShellExecute        = false,
                                RedirectStandardOutput = false,
                                RedirectStandardError  = false,
                                CreateNoWindow         = true
                            }
                        };
                        WriteLog("Windows detected. Restarting with: " + process.StartInfo.FileName + " " + process.StartInfo.Arguments + " in WorkingDirectory: " + process.StartInfo.WorkingDirectory);
                        process.Start();
                    }
                    else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    {
                        var process = new Process
                        {
                            StartInfo = new ProcessStartInfo
                            {
                                FileName        = "/bin/bash",
                                Arguments       = "-c \" " + "dotnet " + updateeFilePath + " \"",
                                UseShellExecute = false,
                            }
                        };
                        WriteLog("Linux detected. Restarting with: " + process.StartInfo.FileName + " " + process.StartInfo.Arguments);
                        process.Start();
                    }
                }
            }

            // Delete package content directory
            WriteLog("Deleting package contents from storage...");
            Directory.Delete(packageContentDirPath, true);
        }