public bool TryGetPayloadFromCache(string packageId, string packageVersion, out MsiPayload payload) { string packageCacheDirectory = GetPackageDirectory(packageId, packageVersion); string manifestPath = Path.Combine(packageCacheDirectory, "msi.json"); payload = default; // It's possible that the MSI is cached, but without the JSON manifest we cannot // trust that the MSI in the cache directory is the correct file. if (!File.Exists(manifestPath)) { Log?.LogMessage($"Manifest file does not exist, '{manifestPath}'"); return(false); } // The msi.json manifest contains the name of the actual MSI. The filename does not necessarily match the package // ID as it may have been shortened to support VS caching. MsiManifest msiManifest = JsonConvert.DeserializeObject <MsiManifest>(File.ReadAllText(manifestPath)); string msiPath = Path.Combine(Path.GetDirectoryName(manifestPath), msiManifest.Payload); if (!File.Exists(msiPath)) { Log?.LogMessage($"MSI package is not cached, '{msiPath}'"); return(false); } payload = new MsiPayload(manifestPath, msiPath); return(true); }
public void CachePayload(string packageId, string packageVersion, string manifestPath) { if (!File.Exists(manifestPath)) { throw new FileNotFoundException($"Manifest file not found: {manifestPath}"); } Elevate(); if (IsElevated) { CreateRootDirectory(); string packageDirectory = GetPackageDirectory(packageId, packageVersion); // Delete the directory and create a new one that's secure. If the files were properly // cached, the client won't request this action. if (Directory.Exists(packageDirectory)) { Directory.Delete(packageDirectory, recursive: true); } CreateSecureDirectory(packageDirectory); // We cannot assume that the MSI adjacent to the manifest is the one to cache. We'll trust // the manifest to provide the MSI filename. MsiManifest msiManifest = JsonConvert.DeserializeObject <MsiManifest>(File.ReadAllText(manifestPath)); // Only use the filename+extension of the payload property in case the manifest has been altered. string msiPath = Path.Combine(Path.GetDirectoryName(manifestPath), Path.GetFileName(msiManifest.Payload)); string cachedMsiPath = Path.Combine(packageDirectory, Path.GetFileName(msiPath)); string cachedManifestPath = Path.Combine(packageDirectory, Path.GetFileName(manifestPath)); MoveFile(manifestPath, cachedManifestPath); MoveFile(msiPath, cachedMsiPath); } else if (IsClient) { Dispatcher.SendCacheRequest(InstallRequestType.CachePayload, manifestPath, packageId, packageVersion); } }