public void Load(IProtocolDeserializerContext context) { FullDependency = context.ReadBool(); int count = context.ReadInt(); Files = new Dictionary<string, FileFingerprint>(); for (int i = 0; i < count; i++) { var name = context.ReadString(); if (FullDependency) { var date = context.ReadDateTime(); var size = context.ReadLong(); var fileFingerprint = new FileFingerprint { LastSize = size, LastModifiedDate = date }; Files.Add(name, fileFingerprint); } else { Files.Add(name, new FileFingerprint()); } } }
/// <summary> /// Gets the file fingerprints from the application folder /// As the sha1 is calculated based on the content of the file, /// there is a possibility that one key can have multiple fingerprints (duplicate files) /// </summary> /// <param name="appPath">The path to the application folder</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns>Return a dictionary of file fingerprints, with sha1 as key and a list of file fingerprints as value.</returns> public async Task<Dictionary<string, List<FileFingerprint>>> GetFileFingerprints(string appPath, System.Threading.CancellationToken cancellationToken) { Dictionary<string, List<FileFingerprint>> fingerprints = new Dictionary<string, List<FileFingerprint>>(); appPath = Path.GetFullPath(appPath); foreach (string file in Directory.GetFiles(appPath, "*", SearchOption.AllDirectories)) { FileInfo fileInfo = new FileInfo(file); FileFingerprint print = new FileFingerprint(); print.Size = fileInfo.Length; print.FileName = fileInfo.FullName.Replace(appPath, string.Empty).TrimStart('\\'); print.SHA1 = await this.CalculateSHA1(fileInfo.FullName, cancellationToken); if (fingerprints.ContainsKey(print.SHA1)) { fingerprints[print.SHA1].Add(print); } else { fingerprints.Add(print.SHA1, new List<FileFingerprint>() { print }); } } return fingerprints; }
public void UploadInvalidFingerprintsTest() { var staticContent = "alabala"; var contentPath = Path.Combine(tempAppPath, "content.zip"); using (var fs = File.OpenWrite(contentPath)) { using (var za = new ZipArchive(fs, ZipArchiveMode.Create, true)) { var ze = za.CreateEntry("content.txt", CompressionLevel.NoCompression); using (var sw = new StreamWriter(ze.Open())) { sw.Write(staticContent); } } } CreateAppResponse app = client.Apps.CreateApp(apprequest).Result; Guid appGuid = app.EntityMetadata.Guid; using (var fs = File.OpenRead(contentPath)) { var fp1 = new FileFingerprint() { FileName = "file1", SHA1 = "xxxxxxxxxxx", Size = -1 }; var fp2 = new FileFingerprint() { FileName = "file2", SHA1 = "yyyyyyyyyyyyy", Size = 1024 * 1024 * 100 }; var fpl = new List <FileFingerprint>() { fp1, fp2 }; client.Apps.UploadBits(appGuid, fs, fpl).Wait(); } var zipFromServerPath = Path.GetTempFileName(); DownloadAppZip(appGuid.ToString(), zipFromServerPath); if (new FileInfo(zipFromServerPath).Length == 0) { Assert.Inconclusive("API endpoint doesn't support package downloads"); } using (var fs = File.OpenRead(zipFromServerPath)) { using (var arch = new ZipArchive(fs, ZipArchiveMode.Read, true)) { // Indeed, this is very strange. Even if the SHA is not found in the cache the files will still be packed. // The contents of the file differ based on CC's cache store. If the store is backed on local file system (or NFS) // it will be an empty file. If the store is backed on S3, it will contain the HTTP response error. Assert.IsTrue(arch.Entries.Count() == 3); } } client.Apps.DeleteApp(appGuid).Wait(); Directory.Delete(tempAppPath, true); }