public override void SaveAlgorithm(string name, string sourceDirectory, string destinationDirectory, string lastCompleteDirectory, Nko obj) { // Before the save begin we verify if our temp directory is well deleted Trace.WriteLine(name); Trace.WriteLine(sourceDirectory); Trace.WriteLine(destinationDirectory); Trace.WriteLine(lastCompleteDirectory); string CalculatorOn = ""; // We check if the calculator is active foreach (var process in ProcessList.GetProcessList()) { if (process.Name.Equals("Calculator")) { CalculatorOn = process.Name; } } if (CalculatorOn == "Calculator") { Trace.WriteLine("Your calculator is on please close him"); } else { Trace.WriteLine(name); Trace.WriteLine(sourceDirectory); //Thread.Sleep(2000); Trace.WriteLine(destinationDirectory); Trace.WriteLine(lastCompleteDirectory); Thread.Sleep(10000); String date = NowDate.ToString("yyyy-MM-dd,HH.mm.ss"); if (Directory.Exists("..\\..\\..\\tempSource\\")) { string pathTemp = "..\\..\\..\\tempSource\\"; Directory.Delete(pathTemp, true); } // We get the informations of the backup that the user write string SourceDirectory = sourceDirectory; string completeDirectory = lastCompleteDirectory; string DestinationDirectory = destinationDirectory + name + "Le." + date + ".zip"; string TempSourcedDirectory = "..\\..\\..\\tempSource\\"; string sourceFileName; string savedFileName; // We create the temp directory and we get all the file in the source and we also get every file from the last complete backups DirectoryInfo di = Directory.CreateDirectory(TempSourcedDirectory); var source = new DirectoryInfo(SourceDirectory); var directory = new DirectoryInfo(completeDirectory); var lastSave = directory.GetDirectories().OrderByDescending(f => f.LastWriteTime).First(); string LastCompleteDirectory = lastSave.ToString(); string[] sourcesFiles = Directory.GetFiles(SourceDirectory, "*", SearchOption.AllDirectories); string[] savedfile = Directory.GetFiles(LastCompleteDirectory, "*", SearchOption.AllDirectories); foreach (string s in sourcesFiles) { foreach (string d in savedfile) { while (test == 1) { Thread.Sleep(1000); Trace.WriteLine("PAUSE"); } // We use this to manipulate the information of the file sourceFileName = System.IO.Path.GetFileName(s); savedFileName = System.IO.Path.GetFileName(d); FileInfo SourceFile = new System.IO.FileInfo(sourceFileName); FileInfo SavedFile = new System.IO.FileInfo(savedFileName); Console.WriteLine(SavedFile); FileInfo SourceFilePath = new System.IO.FileInfo(System.IO.Path.Combine(SourceDirectory, SourceFile.Name)); FileInfo SavedFilePath = new System.IO.FileInfo(System.IO.Path.Combine(LastCompleteDirectory, SavedFile.Name)); FileInfo SemiPath = new System.IO.FileInfo(System.IO.Path.Combine(LastCompleteDirectory, SourceFile.Name)); // If the name of the source file and the file from the last is the name, we compare the last write time if (SourceFile.Name.Equals(SavedFile.Name)) { if (SourceFilePath.LastWriteTime > SavedFilePath.LastWriteTime) { System.IO.File.Copy(s, Path.Combine(TempSourcedDirectory, SourceFile.Name), true); } else { Trace.WriteLine(" Any file to copy"); } } else if (SemiPath.Exists) { } else if (!SavedFile.Exists) { // If the file don't exit we create it because it mean that its a new save System.IO.File.Copy(s, Path.Combine(TempSourcedDirectory, SourceFile.Name), true); } } } if (Directory.GetDirectories(TempSourcedDirectory).Length == 0 && Directory.GetFiles(TempSourcedDirectory).Length == 0) { Console.WriteLine("Any file changed since the last complete Backup, impossible to create empty backup"); } else { // At the end we create a zip from the temp directory to the destination directory and we delete our temp directory CryptoSoft.CryptoSoft.Crypto(TempSourcedDirectory); ZipFile.CreateFromDirectory(TempSourcedDirectory, DestinationDirectory); } Console.ReadLine(); string PathSaved = "..\\..\\..\\tempSource\\"; Directory.Delete(PathSaved, true); long size = 0; long currentSize = 0; int i = 0; int j = 0; foreach (string srcFile in sourcesFiles) { size = new FileInfo(srcFile).Length; } bool status = true; // Write Info for StateFile when Job is On while (i < j) { for (j = 0; j <= sourcesFiles.Length; j++) { string srcFile = sourcesFiles[i]; currentSize = new FileInfo(srcFile).Length; SaveState.OpenFile(); SaveState.SaveTime(date); SaveState.SaveName(destinationDirectory); SaveState.SaveStatus(status); SaveState.FileCount(sourcesFiles); SaveState.SaveSize(size); SaveState.SaveProgress(j, sourcesFiles); SaveState.FileCountLeft(j, sourcesFiles); SaveState.SaveSizeLeft(size, currentSize); SaveState.SaveSourceFile(j, sourcesFiles); SaveState.SaveDestination(destinationDirectory); SaveState.CloseFile(); i++; long msbefore = DateTimeOffset.Now.ToUnixTimeMilliseconds(); using (StreamWriter w = File.AppendText(logFilePath)) { Log.Logger(name, srcFile, destinationDirectory, currentSize, DateTimeOffset.Now.ToUnixTimeMilliseconds() - msbefore, w); } } } // Write Info for StateFile when Job is Off status = false; SaveState.OpenFile(); SaveState.SaveTime(date); SaveState.SaveName(destinationDirectory); SaveState.SaveStatus(status); SaveState.CloseFile(); } }
public override void SaveAlgorithm(string name, string sourceDirectory, string destinationDirectory, string lastCompleteDirectory, Nko obj) { string CalculatorOn = ""; // Check if the calculator is running if (CalculatorOn == "Calculator") { Trace.WriteLine("Your calculator is on, "); } else { Trace.WriteLine(name); Trace.WriteLine(sourceDirectory); Thread.Sleep(2000); Trace.WriteLine(destinationDirectory); Trace.WriteLine(lastCompleteDirectory); Thread.Sleep(2000); // Start the backup // We get the today date for the name of our backups String date = NowDate.ToString("yyyy-MM-dd,HH.mm.ss"); // Here we get the informations about the source directory and the Destination Directory and he we get all the source file from the source directory string SourceDirectory = sourceDirectory; string DestinationDirectory = destinationDirectory + name + " Le." + date; string[] originalFiles = Directory.GetFiles(SourceDirectory, "*", SearchOption.AllDirectories); // We check if the source directory is empty to prevent the creation of empty backups Thread.Sleep(2000); Trace.WriteLine("on continu"); if (originalFiles.Length != 0) { System.IO.Directory.CreateDirectory(DestinationDirectory); Nko.limit = 1000; // Here we copy the file in the directory that we created just above Array.ForEach(originalFiles, (originalFileLocation) => { Thread.Sleep(2000); while (test == 1) { Trace.WriteLine("PAUSE"); Thread.Sleep(2000); } if (test == 3) { Trace.WriteLine("stop"); } bool isProcessAlive = ProcessList.IsProcessExist(); Trace.WriteLine(isProcessAlive); while (isProcessAlive == true) { Thread.Sleep(2000); isProcessAlive = ProcessList.IsProcessExist(); if (isProcessAlive == false) { Trace.WriteLine("ON PEUT CONTINUER"); } } Trace.WriteLine("paS PAUSE"); FileInfo sourceFile = new FileInfo(originalFileLocation); FileInfo destFile = new FileInfo(originalFileLocation.Replace(SourceDirectory, DestinationDirectory)); // Here we copy everything and we create directory if needed if (destFile.Exists && sourceFile.Length < Nko.limit) { sourceFile.CopyTo(destFile.FullName, true); } else if (!destFile.Exists && sourceFile.Length < Nko.limit) { Directory.CreateDirectory(destFile.DirectoryName); sourceFile.CopyTo(destFile.FullName, false); } if (sourceFile.Length > Nko.limit) { Monitor.Enter(obj); try { Trace.WriteLine("Je suis dans le monitor"); if (destFile.Exists) { sourceFile.CopyTo(destFile.FullName, true); } else if (!destFile.Exists) { Directory.CreateDirectory(destFile.DirectoryName); sourceFile.CopyTo(destFile.FullName, false); } // Thread.Sleep(10000); } // catch blocks go here. finally { Trace.WriteLine("Fin du monitor"); Monitor.Exit(obj); } } }); } else if (originalFiles.Length == 0) // We check if the source directory is empty { Trace.WriteLine("Source directory empty"); } else { Trace.WriteLine("error"); } CryptoSoft.CryptoSoft.Crypto(DestinationDirectory); long size = 0; long currentSize = 0; int i = -1; int j = 0; // Counts the number of files in the source folder foreach (string srcFile in originalFiles) { size = new FileInfo(srcFile).Length; } bool status = true; // Write Info for StateFile when Job is On while (i < j) { for (j = 0; j <= originalFiles.Length; j++) { string srcFile = originalFiles[j]; currentSize = new FileInfo(srcFile).Length; SaveState.OpenFile(); SaveState.SaveTime(date); SaveState.SaveName(DestinationDirectory); SaveState.SaveStatus(status); SaveState.FileCount(originalFiles); SaveState.SaveSize(size); SaveState.SaveProgress(j, originalFiles); SaveState.FileCountLeft(j, originalFiles); SaveState.SaveSizeLeft(size, currentSize); SaveState.SaveSourceFile(j, originalFiles); SaveState.SaveDestination(DestinationDirectory); SaveState.CloseFile(); i++; } } // Write Info for StateFile when Job is Off status = false; SaveState.OpenFile(); SaveState.SaveTime(date); SaveState.SaveName(DestinationDirectory); SaveState.SaveStatus(status); SaveState.CloseFile(); for (int x = 0; x < originalFiles.Length; x++) { string srcFile = originalFiles[x]; currentSize = new FileInfo(srcFile).Length; long msbefore = DateTimeOffset.Now.ToUnixTimeMilliseconds(); using (StreamWriter w = File.AppendText(logFilePath)) { Log.Logger(name, srcFile, DestinationDirectory, currentSize, DateTimeOffset.Now.ToUnixTimeMilliseconds() - msbefore, w); } } } }