private void Disconnect() { _bufferReady.Set(); lock (this) { if (!Connected) { return; } Connected = false; } Logger.Message("disposing of pipes: [{0}]-[{1}]".format(_clientId, _sessionId)); try { if (_serverPipe != null) { _serverPipe.Close(); } _serverPipe = null; if (!_isAsychronous && _responsePipe != null) { _responsePipe.Close(); } _responsePipe = null; } catch (Exception e) { Logger.Error(e); } // clean up anything that can be cleaned up. FilesystemExtensions.RemoveTemporaryFiles(); }
protected virtual int Startup(IEnumerable <string> args) { var task = Task.Factory.StartNew(() => { Main(args); }, CancellationTokenSource.Token); try { Console.CancelKeyPress += (x, y) => { if (!CancellationTokenSource.IsCancellationRequested) { Console.WriteLine("Operation Canceled..."); CancellationTokenSource.Cancel(); if (y.SpecialKey == ConsoleSpecialKey.ControlBreak) { // can't cancel so we just block on the task. return; // task.Wait(); } } y.Cancel = true; }; task.Wait(CancellationTokenSource.Token); } catch (Exception e) { HandleException(e.Unwrap()); return(1); } FilesystemExtensions.RemoveTemporaryFiles(); return(0); }
/// <summary> /// Extract zipped package and return the unzipped folder /// </summary> /// <param name="zippedPackagePath"></param> /// <returns></returns> private string ExtractZipPackage(string zippedPackagePath) { if (zippedPackagePath != null && zippedPackagePath.FileExists()) { // extracted folder string extractedFolder = FilesystemExtensions.GenerateTemporaryFileOrDirectoryNameInTempDirectory(); try { //unzip the file ZipFile.ExtractToDirectory(zippedPackagePath, extractedFolder); // extraction fails if (!Directory.Exists(extractedFolder)) { Verbose(string.Format(CultureInfo.CurrentCulture, Resources.Messages.FailToExtract, zippedPackagePath, extractedFolder)); return(string.Empty); } // the zipped folder var zippedDirectory = Directory.EnumerateDirectories(extractedFolder).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(zippedDirectory) && Directory.Exists(zippedDirectory)) { return(zippedDirectory); } } catch (Exception ex) { Verbose(string.Format(CultureInfo.CurrentCulture, Resources.Messages.FailToInstallZipFolder, zippedPackagePath, ex.Message)); Debug(ex.StackTrace); // remove the extracted folder extractedFolder.TryHardToDelete(); } } return(string.Empty); }
internal string DownLoadFileFromLinks(IEnumerable <Link> links) { string file = null; foreach (var link in links) { file = RetryDownload( // the download function takes in a uri link and download it (uri) => { var tmpFile = FilesystemExtensions.GenerateTemporaryFileOrDirectoryNameInTempDirectory(); return(ProviderServices.DownloadFile(uri, tmpFile, -1, true, this)); }, link.HRef); // got a valid file! if (file != null && file.FileExists()) { // if file is zip, unpack it and return the unpacked folder if (link.MediaType == Iso19770_2.MediaType.ZipPackage) { try { // let's extract the zipped file return(ExtractZipPackage(file)); } finally { // delete the zipped file file.TryHardToDelete(); } } return(file); } } return(file); }
internal string DownLoadFileFromLinks(IEnumerable <Link> links) { string file = null; foreach (var link in links) { file = RetryDownload( // the download function takes in a uri link and download it (uri) => { var tmpFile = FilesystemExtensions.GenerateTemporaryFileOrDirectoryNameInTempDirectory(); return(ProviderServices.DownloadFile(uri, tmpFile, -1, true, this)); }, link.HRef); // got a valid file! if (file != null && file.FileExists()) { return(file); } } return(file); }
private Swidtag DownloadSwidtag(IEnumerable <Uri> locations, BootstrapRequest request) { foreach (var location in locations.WhereNotNull()) { try { var filename = FilesystemExtensions.GenerateTemporaryFileOrDirectoryNameInTempDirectory(); DownloadSwidtagToFile(filename, location); if (_timedOut) { // try one more time... DownloadSwidtagToFile(filename, location); } if (filename.FileExists()) { try { var document = XDocument.Load(filename); if (Swidtag.IsSwidtag(document.Root)) { Location = location; return(new Swidtag(document)); } } finally { System.IO.File.Delete(filename); } } } catch (Exception e) { e.Dump(request); } } return(null); }
private bool InstallNugetPackage(Package provider, Link link, string fastPath, BootstrapRequest request) { // download the nuget package string downloadedNupkg = request.DownloadAndValidateFile(provider._swidtag); if (downloadedNupkg != null) { // extracted folder string extractedFolder = String.Concat(FilesystemExtensions.GenerateTemporaryFileOrDirectoryNameInTempDirectory()); try { //unzip the file ZipFile.ExtractToDirectory(downloadedNupkg, extractedFolder); if (Directory.Exists(extractedFolder)) { string versionFolder = Path.Combine(request.DestinationPath(request), provider.Name, provider.Version); // tool folder is where we find things like nuget.exe string toolFolder = Path.Combine(extractedFolder, "tools"); string libFolder = Path.Combine(extractedFolder, "lib"); // create the directory version folder if not exist if (!Directory.Exists(versionFolder)) { Directory.CreateDirectory(versionFolder); } // copy the tools directory if (Directory.Exists(toolFolder)) { string destinationToolFolder = Path.Combine(versionFolder, "tools"); if (!Directory.Exists(destinationToolFolder)) { Directory.CreateDirectory(destinationToolFolder); } foreach (string child in Directory.EnumerateFiles(toolFolder)) { try { // try copy and overwrite File.Copy(child, Path.Combine(destinationToolFolder, Path.GetFileName(child)), true); } catch (Exception e) { request.Debug(e.StackTrace); if (!(e is UnauthorizedAccessException || e is IOException)) { // something wrong, delete the version folder versionFolder.TryHardToDelete(); return(false); } // otherwise this means the file is just being used. so just moves on to copy other files } } } // copy files from lib if (Directory.Exists(libFolder)) { // check that the lib folder has at most 1 dll if (Directory.EnumerateFiles(libFolder).Count(file => String.Equals(Path.GetExtension(file), ".dll", StringComparison.OrdinalIgnoreCase)) > 1) { request.Warning(String.Format(CultureInfo.CurrentCulture, Resources.Messages.MoreThanOneDllExists, provider.Name)); return(false); } foreach (string child in Directory.EnumerateFiles(libFolder)) { try { File.Copy(child, Path.Combine(versionFolder, Path.GetFileName(child)), true); } catch (Exception e) { request.Debug(e.StackTrace); if (!(e is UnauthorizedAccessException || e is IOException)) { // something wrong, delete the version folder versionFolder.TryHardToDelete(); return(false); } // otherwise this means the file is just being used. so just moves on to copy other files } } } // target file name is the assembly provider string targetFile = Path.Combine(versionFolder, Path.GetFileName(link.Attributes[Iso19770_2.Discovery.TargetFilename])); if (File.Exists(targetFile)) { request.Verbose(Resources.Messages.InstalledPackage, provider.Name, targetFile); request.YieldFromSwidtag(provider, fastPath); return(true); } } } finally { downloadedNupkg.TryHardToDelete(); extractedFolder.TryHardToDelete(); } } return(false); }
public override bool ProcessRecordAsync() { if (Stopping) { return(false); } var packageProvider = SelectProviders(ProviderName).ReEnumerable(); if (ProviderName.IsNullOrEmpty()) { Error(Constants.Errors.ProviderNameNotSpecified, packageProvider.Select(p => p.ProviderName).JoinWithComma()); return(false); } switch (packageProvider.Count()) { case 0: Error(Constants.Errors.UnknownProvider, ProviderName); return(false); case 1: break; default: Error(Constants.Errors.MatchesMultipleProviders, packageProvider.Select(p => p.ProviderName).JoinWithComma()); return(false); } var provider = packageProvider.First(); using (var sources = provider.ResolvePackageSources(this).CancelWhen(CancellationEvent.Token)) { // first, check if there is a source by this name already. var existingSources = sources.Where(each => each.IsRegistered && each.Name.Equals(Name, StringComparison.OrdinalIgnoreCase)).ToArray(); if (existingSources.Any()) { // if there is, and the user has said -Force, then let's remove it. foreach (var existingSource in existingSources) { if (Force) { if (ShouldProcess(FormatMessageString(Constants.Messages.TargetPackageSource, existingSource.Name, existingSource.Location, existingSource.ProviderName), Constants.Messages.ActionReplacePackageSource).Result) { var removedSources = provider.RemovePackageSource(existingSource.Name, this).CancelWhen(CancellationEvent.Token); foreach (var removedSource in removedSources) { Verbose(Constants.Messages.OverwritingPackageSource, removedSource.Name); } } } else { Error(Constants.Errors.PackageSourceExists, existingSource.Name); return(false); } } } } string providerNameForProcessMessage = ProviderName.JoinWithComma(); if (ShouldProcess(FormatMessageString(Constants.Messages.TargetPackageSource, Name, Location, providerNameForProcessMessage), FormatMessageString(Constants.Messages.ActionRegisterPackageSource)).Result) { //Need to resolve the path created via psdrive. //e.g., New-PSDrive -Name x -PSProvider FileSystem -Root \\foobar\myfolder. Here we are resolving x:\ try { if (FilesystemExtensions.LooksLikeAFilename(Location)) { ProviderInfo providerInfo = null; var resolvedPaths = GetResolvedProviderPathFromPSPath(Location, out providerInfo); // Ensure the path is a single path from the file system provider if ((providerInfo != null) && (resolvedPaths.Count == 1) && String.Equals(providerInfo.Name, "FileSystem", StringComparison.OrdinalIgnoreCase)) { Location = resolvedPaths[0]; } } } catch (Exception) { //allow to continue handling the cases other than file system } using (var added = provider.AddPackageSource(Name, Location, Trusted, this).CancelWhen(CancellationEvent.Token)) { foreach (var addedSource in added) { WriteObject(addedSource); } } return(true); } return(false); }
public string DownloadFile(Uri remoteLocation, string localFilename, int timeoutMilliseconds, bool showProgress, Request request) { if (request == null) { throw new ArgumentNullException("request"); } if (remoteLocation == null) { throw new ArgumentNullException("remoteLocation"); } request.Debug("Calling 'WebDownloader::DownloadFile' '{0}','{1}','{2}','{3}'", remoteLocation, localFilename, timeoutMilliseconds, showProgress); if (remoteLocation.Scheme.ToLowerInvariant() != "http" && remoteLocation.Scheme.ToLowerInvariant() != "https" && remoteLocation.Scheme.ToLowerInvariant() != "ftp") { request.Error(ErrorCategory.InvalidResult, remoteLocation.ToString(), Constants.Messages.SchemeNotSupported, remoteLocation.Scheme); return(null); } if (localFilename == null) { localFilename = FilesystemExtensions.GenerateTemporaryFileOrDirectoryNameInTempDirectory(); } localFilename = Path.GetFullPath(localFilename); // did the caller pass us a directory name? if (Directory.Exists(localFilename)) { localFilename = Path.Combine(localFilename, "downloadedFile.tmp"); } // make sure that the parent folder is created first. var folder = Path.GetDirectoryName(localFilename); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } // clobber an existing file if it's already there. // todo: in the future, we could check the md5 of the file and if the remote server supports it // todo: we could skip the download. if (File.Exists(localFilename)) { localFilename.TryHardToDelete(); } // setup the progress tracker if the caller wanted one. int pid = 0; if (showProgress) { pid = request.StartProgress(0, "Downloading '{0}'", remoteLocation); } #if CORECLR var task = Download(remoteLocation, localFilename, showProgress, request, pid); task.Wait(timeoutMilliseconds); if (!task.IsCompleted) { request.Warning(Constants.Status.TimedOut); request.Debug("Timed out downloading '{0}'", remoteLocation.AbsoluteUri); } #else var webClient = new WebClient(); // Mozilla/5.0 is the general token that says the browser is Mozilla compatible, and is common to almost every browser today. webClient.Headers.Add("User-Agent", "Mozilla/5.0 PackageManagement"); if (request.WebProxy != null) { // if user supplies webproxy, use that webClient.Proxy = request.WebProxy; } else { // get ie settings webClient.Proxy = WebRequest.GetSystemWebProxy(); // set credentials to be user credentials webClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials; } var done = new ManualResetEvent(false); webClient.DownloadFileCompleted += (sender, args) => { if (args.Cancelled || args.Error != null) { localFilename = null; } done.Set(); }; var lastPercent = 0; if (showProgress) { webClient.DownloadProgressChanged += (sender, args) => { // Progress(requestObject, 2, (int)percent, "Downloading {0} of {1} bytes", args.BytesReceived, args.TotalBytesToReceive); var percent = (int)((args.BytesReceived * 100) / args.TotalBytesToReceive); if (percent > lastPercent) { lastPercent = percent; request.Progress(pid, (int)((args.BytesReceived * 100) / args.TotalBytesToReceive), "To {0}", localFilename); } }; } // start the download webClient.DownloadFileAsync(remoteLocation, localFilename); // wait for the completion if (timeoutMilliseconds > 0) { if (!done.WaitOne(timeoutMilliseconds)) { webClient.CancelAsync(); request.Warning(Constants.Status.TimedOut); request.Debug("Timed out downloading '{0}'", remoteLocation.AbsoluteUri); return(null); } } else { // wait until it completes or fails on it's own done.WaitOne(); } #endif // if we don't have the file by this point, we've failed. if (localFilename == null || !File.Exists(localFilename)) { request.CompleteProgress(pid, false); request.Warning(Constants.Messages.UnableToDownload, remoteLocation.ToString(), localFilename); return(null); } if (showProgress) { request.CompleteProgress(pid, true); } return(localFilename); }
public static void Start() { try { MessageText = "It will be just a moment while we \r\nremove old versions of the\r\nCoApp Package Manager."; StatusText = "Status: Shutting down CoApp Service"; OverallProgress = 5; OnPropertyChanged(); var tsk = Task.Factory.StartNew(FilesystemExtensions.RemoveTemporaryFiles); // try to gracefully kill coapp.service try { var psi = new ProcessStartInfo { FileName = "sc.exe", Arguments = @"stop ""CoApp Package Installer Service""", WindowStyle = ProcessWindowStyle.Hidden }; var p = Process.Start(psi); p.WaitForExit(); } catch { // it's ok. } // try to gracefully kill coapp.service ( new ) try { var psi = new ProcessStartInfo { FileName = "sc.exe", Arguments = @"stop ""CoApp""", WindowStyle = ProcessWindowStyle.Hidden }; var p = Process.Start(psi); p.WaitForExit(); } catch { // it's ok. } // let's just kill the processes if they exist var serviceProcs = Process.GetProcessesByName("CoApp.Service"); if (serviceProcs.Any()) { foreach (var proc in serviceProcs) { try { proc.Kill(); } catch { // it's ok. } } } StatusText = "Status: Removing Service"; OverallProgress = 10; OnPropertyChanged(); // remove service if it exists try { var psi = new ProcessStartInfo { FileName = "sc.exe", Arguments = @"delete ""CoApp Package Installer Service""", WindowStyle = ProcessWindowStyle.Hidden }; var p = Process.Start(psi); p.WaitForExit(); } catch { // it's ok. } try { var psi = new ProcessStartInfo { FileName = "sc.exe", Arguments = @"delete ""CoApp""", WindowStyle = ProcessWindowStyle.Hidden }; var p = Process.Start(psi); p.WaitForExit(); } catch { // it's ok. } MsiSetInternalUI(2, IntPtr.Zero); MsiSetExternalUI((context, messageType, message) => 1, 0x400, IntPtr.Zero); StatusText = "Status: Finding installed packages."; OverallProgress = 15; OnPropertyChanged(); var installedMSIs = GetInstalledCoAppMSIs().ToArray(); StatusText = string.Format("Status: Found {0} installed packages.", installedMSIs.Length); OverallProgress = 20; OnPropertyChanged(); // Remove CoApp toolkit MSIs var toolkits = installedMSIs.Where(each => (each.ProductName.Equals("CoApp.Toolkit", StringComparison.InvariantCultureIgnoreCase) || each.ProductName.Equals("CoApp", StringComparison.InvariantCultureIgnoreCase)) && each.Manufacturer.Equals("OUTERCURVE FOUNDATION", StringComparison.CurrentCultureIgnoreCase)).ToArray(); if (toolkits.Any()) { StatusText = "Status: Removing CoApp Toolkit."; OverallProgress = 25; OnPropertyChanged(); foreach (var pkg in toolkits) { OverallProgress++; OnPropertyChanged(); MsiInstallProduct(pkg.Path, @"REMOVE=ALL ALLUSERS=1 COAPP=1 COAPP_INSTALLED=1 REBOOT=REALLYSUPPRESS"); } } if (installedMSIs.Any()) { installedMSIs = GetInstalledCoAppMSIs().ToArray(); if (AllPackages && installedMSIs.Any()) { var eachProgress = 45 / installedMSIs.Count(); StatusText = "Status: Removing other packages."; OverallProgress = 30; OnPropertyChanged(); foreach (var pkg in installedMSIs) { OverallProgress += eachProgress; OnPropertyChanged(); MsiInstallProduct(pkg.Path, @"REMOVE=ALL ALLUSERS=1 COAPP=1 COAPP_INSTALLED=1 REBOOT=REALLYSUPPRESS"); } } } // // installedMSIs = GetInstalledCoAppMSIs().ToArray(); // if (!installedMSIs.Any()) { StatusText = "Status: Removing CoApp Folder."; OverallProgress = 75; OnPropertyChanged(); // get rid of c:\windows\coapp.exe var coappexe = Path.Combine(Environment.GetEnvironmentVariable("SystemRoot"), "coapp.exe"); if (File.Exists(coappexe)) { coappexe.TryHardToDelete(); } // try to get rid of c:\apps var apps = String.Format("{0}\\apps", Environment.GetEnvironmentVariable("SystemDrive")); if (Symlink.IsSymlink(apps)) { Symlink.DeleteSymlink(apps); } else if (Directory.Exists(apps)) { FilesystemExtensions.TryHardToDelete(String.Format("{0}\\apps", Environment.GetEnvironmentVariable("SystemDrive"))); } // no more packages installed-- remove the c:\apps directory var rootFolder = CoAppRootFolder.Value; FilesystemExtensions.TryHardToDelete(Path.Combine(rootFolder, ".cache")); FilesystemExtensions.TryHardToDelete(Path.Combine(rootFolder, "ReferenceAssemblies")); FilesystemExtensions.TryHardToDelete(Path.Combine(rootFolder, "x86")); FilesystemExtensions.TryHardToDelete(Path.Combine(rootFolder, "x64")); FilesystemExtensions.TryHardToDelete(Path.Combine(rootFolder, "bin")); FilesystemExtensions.TryHardToDelete(Path.Combine(rootFolder, "powershell")); FilesystemExtensions.TryHardToDelete(Path.Combine(rootFolder, "lib")); FilesystemExtensions.TryHardToDelete(Path.Combine(rootFolder, "include")); // FilesystemExtensions.TryHardToDelete(Path.Combine(rootFolder, "etc")); StatusText = "Status: Removing Dead Links."; OverallProgress = 80; OnPropertyChanged(); FilesystemExtensions.RemoveDeadLnks(rootFolder); StatusText = "Status: Removing Empty Folders."; OverallProgress = 81; OnPropertyChanged(); FilesystemExtensions.RemoveEssentiallyEmptyFolders(rootFolder); // } // clean out the CoApp registry keys try { StatusText = "Status: Removing CoApp Registry Settings."; OverallProgress = 83; OnPropertyChanged(); var registryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).CreateSubKey(@"Software"); if (registryKey != null) { registryKey.DeleteSubKeyTree("CoApp"); } } catch { } StatusText = "Status: Cleaning up Temp Folder."; OverallProgress = 85; OnPropertyChanged(); foreach (var f in Directory.EnumerateFiles(FilesystemExtensions.TempPath, "*.msi")) { FilesystemExtensions.TryHardToDelete(f); } OverallProgress = 88; OnPropertyChanged(); foreach (var f in Directory.EnumerateFiles(FilesystemExtensions.TempPath, "*.tmp")) { FilesystemExtensions.TryHardToDelete(f); } OverallProgress = 91; OnPropertyChanged(); foreach (var f in Directory.EnumerateFiles(FilesystemExtensions.TempPath, "*.exe")) { FilesystemExtensions.TryHardToDelete(f); } OverallProgress = 93; OnPropertyChanged(); foreach (var f in Directory.EnumerateFiles(FilesystemExtensions.TempPath, "*.dll")) { FilesystemExtensions.TryHardToDelete(f); } OverallProgress = 95; OnPropertyChanged(); MsiSetExternalUI(null, 0x400, IntPtr.Zero); FilesystemExtensions.RemoveTemporaryFiles(); StatusText = "Status: Complete"; OverallProgress = 100; OnPropertyChanged(); } catch { // meh. } Environment.Exit(0); }