public string StartDM(string name, bool mirrorMode, bool showFSWevents, bool showFSWErrors, bool showDMevents, bool showDMerrors, int rebuildOption, long identifier, bool trackChanged) { DmHolder dmHolder = _list.Find(d => d.Identifier == identifier); if (dmHolder != null) { if (!IOmethods.DirectoryExists(dmHolder.SourcePath)) { return("La carpeta principal no existe"); } if (!dmHolder.IsRunning) { if (mirrorMode) { if (!IOmethods.DirectoryExists(dmHolder.MirrorPath)) { return("La carpeta espejo no existe"); } if (rebuildOption == 0) { hardMirrorRebuild(dmHolder.SourcePath, dmHolder.MirrorPath); } else if (rebuildOption == 1) { softMirrorRebuild(dmHolder.SourcePath, dmHolder.MirrorPath); } else { deleteSourceAndMirror(dmHolder.SourcePath, dmHolder.MirrorPath); } } dmHolder.DM = new DirectoryMirror { FriendlyName = dmHolder.Name, SourceDirectory = dmHolder.SourcePath, MirrorDirectory = dmHolder.MirrorPath, InternalBufferSize = dmHolder.Buffer, Milliseconds = dmHolder.Timer, MirrorMode = mirrorMode }; if (showFSWevents) { dmHolder.DM.SourceEvent += new FSWabstract.SourceEventDelegate(SourceEvent); } if (showFSWErrors) { dmHolder.DM.SourceError += new FSWabstract.SourceErrorDelegate(SourceError); } if (showDMevents) { dmHolder.DM.DMinfoEvent += new DirectoryMirror.MirrorActionDelegate(MirrorEvent); } if (showDMerrors) { dmHolder.DM.DMerrorEvent += new DirectoryMirror.MirrorErrorDelegate(MirrorError); } } dmHolder.DM.Start(); dmHolder.DM.TrackChangedEvent(trackChanged); return("ok"); } return("No pudo comenzar"); }
/// <summary> /// Deletes all the contents of the source and mirror folders /// </summary> /// <param name="source"></param> /// <param name="mirror"></param> private static void deleteSourceAndMirror(string source, string mirror) { IOmethods.DeleteAll(source); IOmethods.DeleteAll(mirror); Thread.Sleep(1000); }
///<summary> /// Delete the mirror directory and recreates it and copies /// all the content from the source directory to it. ///</summary> /// <param name="source"></param> /// <param name="mirror"></param> private static void hardMirrorRebuild(string source, string mirror) { IOmethods.DeleteAll(mirror); //Directory.CreateDirectory(mirror); IOmethods.CopyDirectoryRecursively(source, mirror); }
///<summary> /// Compares The directory structure from the source and the mirror, /// and the files (file name and size comparison) and then makes the /// the nessecessary changes so that the mirror is identical to the source ///</summary> /// <param name="source"></param> /// <param name="mirror"></param> private static void softMirrorRebuild(string source, string mirror) { IOmethods.MirrorUpdate(source, mirror); }
private void updateMirror(List <SourceEventArgs> list) { MirrorEventArgs args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.TimeStamp = DateTime.Now; args.Action = "Comienzo de la actualización en el espejo"; args.Info = list.Count.ToString() + " evento(s) para procesar"; DMinfoEvent(args); string destination; List <SourceEventArgs> listF = new List <SourceEventArgs>(); try { //--- Created ---// listF = list.Where(x => x.EventType == "Created").ToList(); for (int i = 0; i < listF.Count; i++) { destination = listF[i].Path.Replace(SourceDirectory, MirrorDirectory); if (Directory.Exists(listF[i].Path)) { Directory.CreateDirectory(destination); IOmethods.CopyDirectoryRecursively(listF[i].Path, destination); list.Remove(listF[i]); // Remove all "Created" and "Changed" events of child folders and files from the master list list = list.Where(s => (s.EventType == "Created" || s.EventType == "Changed") && s.Path.Contains(listF[i].Path) == false).ToList(); // Update the sublist of "Created events" listF = list.Where(x => x.EventType == "Created").ToList(); i -= 1; args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.Action = "Create"; args.TimeStamp = DateTime.Now; args.Info = destination; DMinfoEvent(args); } else { File.Copy(listF[i].Path, destination, true); // Remove all "Changed" events for this file from the master list list = list.Where(s => (s.EventType != "Changed" && s.Path != listF[i].Path)).ToList(); // Update the sublist of "Created events" listF = list.Where(x => x.EventType == "Created").ToList(); i -= 1; args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.Action = "Create"; args.TimeStamp = DateTime.Now; args.Info = destination; DMinfoEvent(args); } } } catch (Exception x) { args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.TimeStamp = DateTime.Now; args.Action = "Error"; args.Info = x.Message; DMerrorEvent(args); } try { //--- Changed ---// listF = list.Where(z => z.EventType == "Changed").ToList(); listF = RemoveDuplicates(listF); foreach (SourceEventArgs f in listF) { if (File.Exists(f.Path)) { destination = f.Path.Replace(SourceDirectory, MirrorDirectory); File.Copy(f.Path, destination, true); args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.Action = "Copy"; args.TimeStamp = DateTime.Now; args.Info = destination; DMinfoEvent(args); } } } catch (Exception x) { args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.TimeStamp = DateTime.Now; args.Action = "Error"; args.Info = x.Message; DMerrorEvent(args); } try { //--- Renamed ---// listF = list.Where(x => x.EventType == "Renamed").ToList(); foreach (SourceEventArgs f in listF) { destination = f.Path.Replace(SourceDirectory, MirrorDirectory); if (System.IO.Directory.Exists(f.OldPath.Replace(SourceDirectory, MirrorDirectory))) { string oldFPath = f.OldPath.Replace(SourceDirectory, MirrorDirectory); string newFPath = f.Path.Replace(SourceDirectory, MirrorDirectory); System.IO.Directory.Move(oldFPath, newFPath); } else { string oldFPath = f.OldPath.Replace(SourceDirectory, MirrorDirectory); string newFPath = f.Path.Replace(SourceDirectory, MirrorDirectory); System.IO.File.Move(oldFPath, newFPath); } args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.Action = "Rename"; args.TimeStamp = DateTime.Now; args.Info = destination; DMinfoEvent(args); } } catch (Exception x) { args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.TimeStamp = DateTime.Now; args.Action = "Error"; args.Info = x.Message; DMerrorEvent(args); } try { //--- Deleted ---// listF = list.Where(x => x.EventType == "Deleted").ToList(); foreach (SourceEventArgs f in listF) { destination = f.Path.Replace(SourceDirectory, MirrorDirectory); if (Directory.Exists(destination)) { Directory.Delete(destination, true); } else { File.Delete(destination); } args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.Action = "Delete"; args.TimeStamp = DateTime.Now; args.Info = destination; DMinfoEvent(args); } } catch (Exception x) { args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.TimeStamp = DateTime.Now; args.Action = "Error"; args.Info = x.Message; DMerrorEvent(args); } args = new MirrorEventArgs(); args.FSWname = this.FriendlyName; args.TimeStamp = DateTime.Now; args.Action = "Fin de la actualización en el espejo"; DMinfoEvent(args); }