public void InitializeFull(ConsoleEx consoleEx, HelixFileVersion fileVersion = null) { PreInitializationCheck(); //Initialize Encr Directory consoleEx?.WriteLine(VerbosityLevel.Detailed, 0, "Initializing Encrypted Directory..."); Header = DirectoryHeader.New(); EncrDirectory.Create(); if (WhatIf) { EncrDirectory.WhatIfAddFile(HelixConsts.HeaderFileName, 10); } else { Header.Save(EncrDirectory.PathFull(HelixConsts.HeaderFileName), DerivedBytesProvider, fileVersion); EncrDirectory.RefreshEntry(HelixConsts.HeaderFileName); } this.FileNameEncoder = new FileNameEncoder(Header.FileNameKey); consoleEx?.WriteLine(VerbosityLevel.Detailed, 1, "Encrypted Directory Initialized (" + Header.DirectoryId.Substring(0, 6) + "...)"); InitializeDecr(consoleEx); }
public void OpenEncr(ConsoleEx consoleEx) { if (Header == null) { consoleEx?.WriteLine(VerbosityLevel.Detailed, 0, "Opening Encrypted Directory..."); Header = DirectoryHeader.Load(EncrDirectory.PathFull(HelixConsts.HeaderFileName), DerivedBytesProvider); consoleEx?.WriteLine(VerbosityLevel.Detailed, 0, "Opened Encrypted Directory (" + Header.DirectoryId.Substring(0, 6) + "...)"); } this.FileNameEncoder = new FileNameEncoder(Header.FileNameKey); }
/// <summary> /// Loads the EncrHeaders for new and updated encripted files /// </summary> private void FindChanges_St05_RefreshEncrHeaders(ConsoleEx console, List <ChangeBuilder> matchesA) { int statsRefreshHeaderCount = 0; foreach (ChangeBuilder preSyncDetails in matchesA.Where(m => m.EncrInfo != null && m.EncrInfo.LastWriteTimeUtc != m.LogEntry?.EncrModified)) { string encrFullPath = Path.Combine(EncrDirectory.FullName, HelixUtil.PathNative(preSyncDetails.EncrFileName)); if (File.Exists(encrFullPath)) { preSyncDetails.EncrHeader = HelixFile.DecryptHeader(encrFullPath, this.DerivedBytesProvider); //Updates the DecrFileName (if necessary) if (string.IsNullOrEmpty(preSyncDetails.DecrFileName) && FileNameEncoder.EncodeName(preSyncDetails.EncrHeader.FileName) == preSyncDetails.EncrInfo.RelativePath) { preSyncDetails.DecrFileName = preSyncDetails.EncrHeader.FileName; } } statsRefreshHeaderCount++; } console?.WriteLine(VerbosityLevel.Diagnostic, 2, $"Updated {statsRefreshHeaderCount} headers"); }
/// <summary> /// Matches files from Encr, Decr and Log Entry /// </summary> private List <ChangeBuilder> FindChanges_St04_ThreeWayJoin(List <FSEntry> encrDirectoryFiles, List <FSEntry> decrDirectoryFiles, SyncLog syncLog, ConsoleEx console) { List <ChangeBuilder> preSyncDetails = new List <ChangeBuilder>(); //Adds Logs console?.WriteLine(VerbosityLevel.Diagnostic, 2, "Merging in log..."); preSyncDetails.AddRange(syncLog.Select(entry => new ChangeBuilder { LogEntry = entry })); console?.WriteLine(VerbosityLevel.Diagnostic, 3, $"{preSyncDetails.Count} added"); //Updates/Adds Decrypted File Information console?.WriteLine(VerbosityLevel.Diagnostic, 2, "Merging in decrypted information..."); int decrStatAdd = 0; int decrStatMerge = 0; var decrJoin = decrDirectoryFiles .GroupJoin(preSyncDetails, o => o.RelativePath, i => i?.LogEntry?.DecrFileName, (o, i) => new Tuple <FSEntry, ChangeBuilder>(o, i.FirstOrDefault())); foreach (var entry in decrJoin.ToList()) { if (entry.Item2 == null) { //New Entry (not in log) preSyncDetails.Add(new ChangeBuilder { DecrInfo = entry.Item1 }); decrStatAdd++; } else { //Existing Entry (update only) entry.Item2.DecrInfo = entry.Item1; if (entry.Item1 != null) { decrStatMerge++; } } } console?.WriteLine(VerbosityLevel.Diagnostic, 3, $"{decrStatAdd} added, {decrStatMerge} merged"); //find encrypted file names foreach (var entry in preSyncDetails) { entry.DecrFileName = entry.LogEntry?.DecrFileName ?? entry.DecrInfo.RelativePath; entry.EncrFileName = FileNameEncoder.EncodeName(entry.DecrFileName); } //Updates/adds encrypted File Information console?.WriteLine(VerbosityLevel.Diagnostic, 2, "Merging in encrypted information..."); int encrStatAdd = 0; int encrStatMerge = 0; var encrJoin = encrDirectoryFiles .GroupJoin(preSyncDetails, o => o.RelativePath, i => i.EncrFileName, (o, i) => new Tuple <FSEntry, ChangeBuilder>(o, i.FirstOrDefault())); foreach (var entry in encrJoin.ToList()) { if (entry.Item2 == null) { //New Entry (not in log or decrypted file) preSyncDetails.Add(new ChangeBuilder { EncrInfo = entry.Item1, EncrFileName = entry.Item1.RelativePath }); encrStatAdd++; } else { //Existing Entry entry.Item2.EncrInfo = entry.Item1; if (entry.Item1 != null) { encrStatMerge++; } } } console?.WriteLine(VerbosityLevel.Diagnostic, 3, $"{encrStatAdd} added, {encrStatMerge} merged"); return(preSyncDetails); }