Ejemplo n.º 1
0
        private void DirectoryDifferentialCopy(string sourceDirName, string destDirName, string comparisonDirName, int index, List <BackupJobState> BUJS, bool doPrioritized)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dirsrc  = new DirectoryInfo(sourceDirName);
            DirectoryInfo dircomp = new DirectoryInfo(comparisonDirName);


            if (!dirsrc.Exists)
            {
                throw new DirectoryNotFoundException(
                          "Source or full backup directory does not exist or could not be found: "
                          + sourceDirName + "\nor\n" + comparisonDirName);
            }

            DirectoryInfo[] dirs = dirsrc.GetDirectories();
            //DirectoryInfo[] dirsComp = dirsrc.GetDirectories();

            // If the destination directory doesn't exist, create it.
            Directory.CreateDirectory(destDirName);

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dirsrc.GetFiles();
            foreach (FileInfo file in files)
            {
                if ((this.extensionPrioritized.Any(item => item == file.Extension) && doPrioritized) || (!this.extensionPrioritized.Any(item => item == file.Extension) && !doPrioritized))
                {
                    if (file.Length > maxFileSize && maxFileSize != 0)
                    {
                        // We wait for the mutex to accept us
                        copyBigFile.WaitOne();
                        this.controller.View.updateTracking(index, Int32.Parse(Math.Ceiling((BUJS[index].Progress) * 100).ToString()), BUJS[index].Name);
                        if (!File.Exists(Path.Combine(comparisonDirName, file.Name)))
                        {
                            Boolean didCryptIt = false;
                            foreach (string ext in BUJS[index].ToBeEncryptedFileExtensions)
                            {
                                if (ext == file.Extension)
                                {
                                    ProcessStartInfo psi = new ProcessStartInfo("CryptoSoft.exe");
                                    psi.WorkingDirectory = "../../../../CryptoSoft/CryptoSoft.scorp264/CryptoSoft/bin/Debug/netcoreapp3.1/";
                                    psi.Arguments        = "\"" + Path.Combine(sourceDirName, file.Name) + "\" \"" + Path.Combine(destDirName, file.Name) + "\"";
                                    Process proc = Process.Start(psi);
                                    proc.WaitForExit();
                                    logManager lm = logManager.getInstance();
                                    lm.writeLogFile("Encryption time for " + Path.Combine(destDirName, file.Name) + " was: " + proc.ExitCode + "ms");
                                    didCryptIt = true;
                                }
                            }
                            if (!didCryptIt)
                            {
                                file.CopyTo(Path.Combine(destDirName, file.Name), false);
                                logManager lm = logManager.getInstance();
                                lm.writeLogFile("Encryption time for " + Path.Combine(destDirName, file.Name) + " was: 0ms ( no encryption )");
                            }
                            BUJS[index].FilesTransfered.Add(new myOwnFileInfo(file.Length, file.FullName));
                            BUJS[index].Progress             = ((float)BUJS[index].FilesTransfered.Count) / ((float)BUJS[index].TotalElligibleFile);
                            BUJS[index].SizeOfRemainingFiles = BUJS[index].TotalSizeOfElligbleFiles - BUJS[index].FilesTransfered.Sum(item => item.fileSize);
                            stateManager.getInstance().writeStateFile(BUJS);
                            foreach (var item in backupToPause)
                            {
                                if (BUJS[index].Name == item)
                                {
                                    _ = waitHandle.WaitOne();
                                }
                            }
                        }
                        else if (File.Exists(Path.Combine(comparisonDirName, file.Name)))
                        {
                            if ((new FileInfo(Path.Combine(comparisonDirName, file.Name))).LastWriteTime < file.LastWriteTime)
                            {
                                Boolean didCryptIt = false;
                                foreach (string ext in BUJS[index].ToBeEncryptedFileExtensions)
                                {
                                    if (ext == file.Extension)
                                    {
                                        ProcessStartInfo psi = new ProcessStartInfo("CryptoSoft.exe");
                                        psi.WorkingDirectory = "../../../../CryptoSoft/CryptoSoft.scorp264/CryptoSoft/bin/Debug/netcoreapp3.1/";
                                        psi.Arguments        = "\"" + Path.Combine(sourceDirName, file.Name) + "\" \"" + Path.Combine(destDirName, file.Name) + "\"";
                                        Process proc = Process.Start(psi);
                                        proc.WaitForExit();
                                        logManager lm = logManager.getInstance();
                                        lm.writeLogFile("Encryption time for " + Path.Combine(destDirName, file.Name) + " was : " + proc.ExitCode + "ms");
                                        didCryptIt = true;
                                    }
                                }
                                if (!didCryptIt)
                                {
                                    file.CopyTo(Path.Combine(destDirName, file.Name), false);
                                    logManager lm = logManager.getInstance();
                                    lm.writeLogFile("Encryption time for " + Path.Combine(destDirName, file.Name) + " was: 0ms ( no encryption )");
                                }
                                BUJS[index].FilesTransfered.Add(new myOwnFileInfo(file.Length, file.FullName));
                                BUJS[index].Progress             = ((float)BUJS[index].FilesTransfered.Count) / ((float)BUJS[index].TotalElligibleFile);
                                BUJS[index].SizeOfRemainingFiles = BUJS[index].TotalSizeOfElligbleFiles - BUJS[index].FilesTransfered.Sum(item => item.fileSize);
                                stateManager.getInstance().writeStateFile(BUJS);
                                foreach (var item in backupToPause)
                                {
                                    if (BUJS[index].Name == item)
                                    {
                                        _ = waitHandle.WaitOne();
                                    }
                                }
                            }
                        }
                        // We release the mutex because we don't need one anymore
                        copyBigFile.ReleaseMutex();
                    }
                    else
                    {
                        this.controller.View.updateTracking(index, Int32.Parse(Math.Ceiling((BUJS[index].Progress) * 100).ToString()), BUJS[index].Name);
                        if (!File.Exists(Path.Combine(comparisonDirName, file.Name)))
                        {
                            Boolean didCryptIt = false;
                            foreach (string ext in BUJS[index].ToBeEncryptedFileExtensions)
                            {
                                if (ext == file.Extension)
                                {
                                    ProcessStartInfo psi = new ProcessStartInfo("CryptoSoft.exe");
                                    psi.WorkingDirectory = "../../../../CryptoSoft/CryptoSoft.scorp264/CryptoSoft/bin/Debug/netcoreapp3.1/";
                                    psi.Arguments        = "\"" + Path.Combine(sourceDirName, file.Name) + "\" \"" + Path.Combine(destDirName, file.Name) + "\"";
                                    Process proc = Process.Start(psi);
                                    proc.WaitForExit();
                                    logManager lm = logManager.getInstance();
                                    lm.writeLogFile("Encryption time for " + Path.Combine(destDirName, file.Name) + " was: " + proc.ExitCode + "ms");
                                    didCryptIt = true;
                                }
                            }
                            if (!didCryptIt)
                            {
                                file.CopyTo(Path.Combine(destDirName, file.Name), false);
                                logManager lm = logManager.getInstance();
                                lm.writeLogFile("Encryption time for " + Path.Combine(destDirName, file.Name) + " was: 0ms ( no encryption )");
                            }
                            BUJS[index].FilesTransfered.Add(new myOwnFileInfo(file.Length, file.FullName));
                            BUJS[index].Progress             = ((float)BUJS[index].FilesTransfered.Count) / ((float)BUJS[index].TotalElligibleFile);
                            BUJS[index].SizeOfRemainingFiles = BUJS[index].TotalSizeOfElligbleFiles - BUJS[index].FilesTransfered.Sum(item => item.fileSize);
                            stateManager.getInstance().writeStateFile(BUJS);
                            foreach (var item in backupToPause)
                            {
                                if (BUJS[index].Name == item)
                                {
                                    _ = waitHandle.WaitOne();
                                }
                            }
                        }
                        else if (File.Exists(Path.Combine(comparisonDirName, file.Name)))
                        {
                            if ((new FileInfo(Path.Combine(comparisonDirName, file.Name))).LastWriteTime < file.LastWriteTime)
                            {
                                Boolean didCryptIt = false;
                                foreach (string ext in BUJS[index].ToBeEncryptedFileExtensions)
                                {
                                    if (ext == file.Extension)
                                    {
                                        ProcessStartInfo psi = new ProcessStartInfo("CryptoSoft.exe");
                                        psi.WorkingDirectory = "../../../../CryptoSoft/CryptoSoft.scorp264/CryptoSoft/bin/Debug/netcoreapp3.1/";
                                        psi.Arguments        = "\"" + Path.Combine(sourceDirName, file.Name) + "\" \"" + Path.Combine(destDirName, file.Name) + "\"";
                                        Process proc = Process.Start(psi);
                                        proc.WaitForExit();
                                        logManager lm = logManager.getInstance();
                                        lm.writeLogFile("Encryption time for " + Path.Combine(destDirName, file.Name) + " was : " + proc.ExitCode + "ms");
                                        didCryptIt = true;
                                    }
                                }
                                if (!didCryptIt)
                                {
                                    file.CopyTo(Path.Combine(destDirName, file.Name), false);
                                    logManager lm = logManager.getInstance();
                                    lm.writeLogFile("Encryption time for " + Path.Combine(destDirName, file.Name) + " was: 0ms ( no encryption )");
                                }
                                BUJS[index].FilesTransfered.Add(new myOwnFileInfo(file.Length, file.FullName));
                                BUJS[index].Progress             = ((float)BUJS[index].FilesTransfered.Count) / ((float)BUJS[index].TotalElligibleFile);
                                BUJS[index].SizeOfRemainingFiles = BUJS[index].TotalSizeOfElligbleFiles - BUJS[index].FilesTransfered.Sum(item => item.fileSize);
                                stateManager.getInstance().writeStateFile(BUJS);
                                foreach (var item in backupToPause)
                                {
                                    if (BUJS[index].Name == item)
                                    {
                                        _ = waitHandle.WaitOne();
                                    }
                                }
                            }
                        }
                    }
                }
            }

            foreach (DirectoryInfo subdir in dirs)
            {
                DirectoryDifferentialCopy(subdir.FullName, Path.Combine(destDirName, subdir.Name), Path.Combine(comparisonDirName, subdir.Name), index, BUJS, doPrioritized);
                foreach (var item in backupToPause)
                {
                    if (BUJS[index].Name == item)
                    {
                        _ = waitHandle.WaitOne();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void executeBUJList(List <int> backupJobIDList, List <String> fullBackupListForDiff)
        {
            watchThread = new Thread(startWatchingProcess);
            watchThread.IsBackground = true;
            watchThread.Start();

            // The user can select one or multiple backup job to execute in the same time
            int numOfDiff = 0;
            List <BackupJobState> BUJStateList = new List <BackupJobState>();

            for (int i = 0; i < backupJobIDList.Count; i++)
            {
                // For each of them we will save them in our state file
                try
                {
                    BUJStateList.Add(new BackupJobState(this.BackupJobList[backupJobIDList[i]].Name, this.BackupJobList[backupJobIDList[i]].Source, this.BackupJobList[backupJobIDList[i]].Destination, (this.BackupJobList[backupJobIDList[i]].IsFull ? "/Full" : "/Diff") + "-" + backupJobIDList[i] + "-" + DateTime.Now.ToString("MM.dd.yyyy THH.mm.ss.fff"), (this.BackupJobList[backupJobIDList[i]].IsFull ? concernedFile(this.BackupJobList[backupJobIDList[i]].Source).Count : concernedFileDiff(this.BackupJobList[backupJobIDList[i]].Source, fullBackupListForDiff[numOfDiff]).Count), (this.BackupJobList[backupJobIDList[i]].IsFull ? concernedFile(this.BackupJobList[backupJobIDList[i]].Source) : concernedFileDiff(this.BackupJobList[backupJobIDList[i]].Source, fullBackupListForDiff[numOfDiff])), this.BackupJobList[backupJobIDList[i]].IsFull, false, this.BackupJobList[backupJobIDList[i]].ToBeEncryptedFileExtensions));
                }
                catch (Exception e)
                {
                    //Console.WriteLine("BUG")
                    logManager lm = logManager.getInstance();
                    lm.writeLogFile(" /!\\/!\\/!\\ Error for the backup job [" + backupJobIDList[i] + "] " + this.BackupJobList[backupJobIDList[i]].Name);
                    lm.writeLogFile(" /!\\/!\\/!\\ Source : \\\\?\\" + this.BackupJobList[backupJobIDList[i]].Source.Replace(":", "$"));
                    lm.writeLogFile(" /!\\/!\\/!\\ Destination : \\\\?\\" + this.BackupJobList[backupJobIDList[i]].Destination.Replace(":", "$"));
                    lm.writeLogFile(" /!\\/!\\/!\\ Size : unavailable for failed jobs");
                    lm.writeLogFile(" /!\\/!\\/!\\ Transfer time : -1ms");

                    throw e;
                }
                if (!this.BackupJobList[backupJobIDList[i]].IsFull)
                {
                    numOfDiff++;
                }
            }
            stateManager.getInstance().writeStateFile(BUJStateList);
            numOfDiff = 0;
            // For each backup job the user wants to execute we will execute it based on it's type (full / differential)
            for (int i = 0; i < backupJobIDList.Count; i++)
            {
                this.controller.View.addProgressBar(i);
                this.controller.View.sendText("AddProgressBar," + i);
                BUJStateList[i].ISACtive = true;
                // Update the state file
                stateManager.getInstance().writeStateFile(BUJStateList);
                logManager lm = logManager.getInstance();
                lm.writeLogFile("Starting the backup job [" + backupJobIDList[i] + "] " + BUJStateList[i].Name);
                lm.writeLogFile("Source : \\?\\" + BUJStateList[i].Source.Replace(":", "$"));
                lm.writeLogFile("Destination : \\\\?\\" + BUJStateList[i].Destination.Replace(":", "$"));
                lm.writeLogFile("Source directory size : " + CalculateFolderSize(BUJStateList[i].Source));
                lm.writeLogFile("Destination directory size : " + BUJStateList[i].TotalSizeOfElligbleFiles);
                if (this.BackupJobList[backupJobIDList[i]].IsFull)
                {
                    // Full copy
                    Thread fullThread = new Thread(() =>
                    {
                        int valueI = i;
                        DirectoryCopy(this.BackupJobList[backupJobIDList[valueI]].Source, BUJStateList[valueI].Destination + BUJStateList[valueI].FolderName, valueI, BUJStateList, true);
                        DirectoryCopy(this.BackupJobList[backupJobIDList[valueI]].Source, BUJStateList[valueI].Destination + BUJStateList[valueI].FolderName, valueI, BUJStateList, false);
                    });
                    fullThread.Name = "FULL_THREAD_ID_" + backupJobIDList[i];
                    fullThread.Start();
                    backupThreadList.Add(fullThread);
                    this.controller.View.UpdateBackupPauseList(BackupJobList[backupJobIDList[i]].Name);
                }
                else
                {
                    // Differential copy
                    int    current_backupIndex = numOfDiff;
                    Thread diffThread          = new Thread(() =>
                    {
                        int valueI = i;
                        DirectoryDifferentialCopy(this.BackupJobList[backupJobIDList[valueI]].Source, BUJStateList[valueI].Destination + BUJStateList[valueI].FolderName, fullBackupListForDiff[current_backupIndex], valueI, BUJStateList, true);
                        DirectoryDifferentialCopy(this.BackupJobList[backupJobIDList[valueI]].Source, BUJStateList[valueI].Destination + BUJStateList[valueI].FolderName, fullBackupListForDiff[current_backupIndex], valueI, BUJStateList, false);
                    });
                    diffThread.Name = "DIFF_THREAD_ID_" + backupJobIDList[i];
                    diffThread.Start();
                    backupThreadList.Add(diffThread);
                    // The number is increasing before the thread has start
                    numOfDiff++;
                    this.controller.View.UpdateBackupPauseList(BackupJobList[backupJobIDList[i]].Name);
                }
                // At the end the work at the index i is no more active
                BUJStateList[i].ISACtive = false;
                // We reupdate it one last time
                stateManager.getInstance().writeStateFile(BUJStateList);
                lm.writeLogFile("Transfer time : " + BUJStateList[i].Stopwatch.Elapsed);
            }
            stopWatchThread = new Thread(stopWatchingProcess);
            stopWatchThread.IsBackground = true;
            stopWatchThread.Start();
        }