public void SyncWithMaster(string masterDir, SyncOptions options, CancellationTokenSource cts) { var master = new Repository(masterDir, cts); if (cts.Token.IsCancellationRequested) { return; } var task = Task.Factory .StartNew(_ => PushTo(master, cts), cts) .ContinueWith(_ => PullFrom(master, options ?? SyncOptions.Default(), cts), TaskContinuationOptions.NotOnCanceled) .ContinueWith(_ => cts.Cancel()); task.Wait(); }
private void PullFrom(Repository master, SyncOptions options, CancellationTokenSource cts) { var mapByHash = Map.Values.ToDictionary(ci => ci.Sha1String); var filesToCopy = master.Map.Values .OrderBy(ci => ci.LastWriteTime) .Reverse(); if (options.MaxLocalStorage != null) { long totalSize = 0; filesToCopy = filesToCopy .TakeWhile(ci => { totalSize += ci.Length; return totalSize < options.MaxLocalStorage; }); // purge des fichiers en trop pour le repo local var mapToCopy = filesToCopy.ToDictionary(f => f.FileName); int countDeleted = 0; foreach (var f in this.Map.Values) { if (mapToCopy.ContainsKey(f.FileName) == false) { // suppression du fichier File.Delete(Path.Combine(this.RootPath, f.FileName)); countDeleted++; } } _logger.InfoFormat("{0} fichiers supprimés pour respecter la taille max du repository : {1}", countDeleted, this.RootPath); } foreach (var f in filesToCopy) { if (cts.Token.IsCancellationRequested) { break; } if (!mapByHash.ContainsKey(f.Sha1String)) { this.CopyFrom(master, f); } } }