public void Download() { DataTable fileInfo; string directoryID = db.ExecuteScalar("SELECT id FROM directories WHERE fullpath = @path", new Dictionary <string, object>() { { "@path", vsrc.ParentPath.Path } }); fileInfo = db.GetDataTable("SELECT * from entities where isdir = @isdir and fname = @name and dir = @dir", new Dictionary <string, object>() { { "@name", vsrc.EntityName }, { "@isdir", 0 }, { "@dir", directoryID } }); // While we write the blocks to multiple - // We only need data from one file system - // I want to test the file system to make sure it is working before attempting access int fsToUse = 0; bool continueSearch = false; for (int i = 0; i < fsdst.Count; i++) { try { using (Stream s = fsdst[fsToUse].CreateFile(FileSystemPath.Parse("/test"))) { } continueSearch = false; } catch (Exception) { Console.WriteLine("[Error]: file system - " + fsdst[fsToUse].ToString() + " missing - trying a backup"); continueSearch = true; fsToUse++; } if (!continueSearch) { break; } } DataTable blocks = db.GetDataTable(@"SELECT * FROM fileblocks inner join blocks on blocks.id = fileblocks.block_id WHERE file_id = @fileid and blocks.location = @location order by block_order asc" , new Dictionary <string, object>() { { "@fileid", fileInfo.Rows[0]["id"] }, { "@location", fsdst[fsToUse].ToString() } }); using (ProgressBar pb = new ProgressBar(disableProgress)) { Int64 rowCount = 0; foreach (DataRow r in blocks.Rows) { pb.Report((((double)rowCount) / blocks.Rows.Count)); rowCount += 1; /*string remotename = db.ExecuteScalar("SELECT name FROM blocks where id = @blockID and location = @name", * new Dictionary<string, object>() * { * {"@blockID", r["block_id"]}, * {"@name", fsdst[fsToUse].ToString()} * });*/ string remotename = r["name"].ToString(); remotename = "/" + remotename; remotename = remotename.Replace("//", "/"); using (var fstream = fsdst[fsToUse].OpenFile(FileSystemPath.Parse($"/{DATAFILE}"), FileAccess.Read)) { DeDupeStorage storage = new DeDupeStorage(fstream, db); byte[] buffer = storage.GetFile(remotename); byte[] plainText = AESWrapper.DecryptToByte(buffer, key); using (FileStream f = System.IO.File.Open(this.Path, FileMode.Append, FileAccess.Write)) { f.Write(plainText); } } /*using (var fstream = fsdst[fsToUse].OpenFile(FileSystemPath.Parse($"/{DATAFILE}"), FileAccess.Read)) * { * DeDupeStorage storage = new DeDupeStorage(fstream, db); * byte[] buffer = storage.GetFile(remotename); * byte[] plainText = AESWrapper.DecryptToByte(buffer, key); * using (var zippedStream = new MemoryStream(plainText)) * { * using (var archive = new ZipArchive(zippedStream, ZipArchiveMode.Read)) * { * var ent = archive.GetEntry("data.bin"); * using (var zipent = ent.Open()) * { * byte[] bufferinner = zipent.ReadAllBytes(); * using (FileStream f = System.IO.File.Open(this.Path, FileMode.Append, FileAccess.Write)) * { * f.Write(bufferinner); * } * //fstream.Write(bufferinner); * } * } * } * }*/ /*using (var fstream = fsdst[fsToUse].OpenFile(FileSystemPath.Parse($"/{DATAFILE}"), FileAccess.Read)) * { * using (var zipfile = new ZipArchive(fstream, ZipArchiveMode.Read)) * { * var ent = zipfile.GetEntry(remotename); * using (var zipent = ent.Open()) * { * byte[] buffer = zipent.ReadAllBytes(); * byte[] plainText = AESWrapper.DecryptToByte(buffer, key); * * if (plainText == null) * { * throw new Exception("[Error]: Could not decrypt file"); * } * * using (FileStream f = System.IO.File.Open(this.Path, FileMode.Append, FileAccess.Write)) * { * f.Write(plainText); * } * } * } * }*/ /*using (Stream s = fsdst[fsToUse].OpenFile(FileSystemPath.Parse(remotename), FileAccess.Read)) * { * * using (FileStream f = System.IO.File.Open(this.Path, FileMode.Append, FileAccess.Write)) * { * byte[] buffer = s.ReadAllBytes(); * byte[] plainText = AESWrapper.DecryptToByte(buffer, key); * if (plainText == null) * { * throw new Exception("[Error]: Could not decrypt file"); * } * * f.Write(plainText); * } * }*/ } string fileHash = fileInfo.Rows[0]["filehash"].ToString(); if (fileHash != "") { string newHash = ""; using (FileStream fs = System.IO.File.Open(this.Path, FileMode.Open, FileAccess.Read)) { newHash = fs.GetSHA512(); } if (fileHash != newHash) { Console.WriteLine("[Warning]: File hashs do not match - data corruption possible!"); } } } }