private void ProcessDirectoryForActions(DirectoryInfo di) { //Get the destination directory string newFolder = di.FullName.Substring(SourceDirectory.Length); if (newFolder.StartsWith(@"\")) { newFolder = newFolder.Substring(1); } string destDirectory = Path.Combine(DestinationDirectory, newFolder); var destDir = new DirectoryInfo(destDirectory); //Create the destination directory if it doesn't already exist //if (!Directory.Exists(destDirectory)) if (!destDir.Exists) { _log.InfoFormat("Add Create Folder Action to Action Queue: {0}", destDir.FullName); ActionQueue.Enqueue(new CreateFolderAction(destDir.FullName)); OnActionAddedToQueue(new EventArgs()); } //Now check each file in the source directory for the following conditions // * In the source directory but not in the destination directory // * file in source and destination directory are different try { foreach (FileInfo fi in di.GetFiles("*.*", SearchOption.TopDirectoryOnly)) { //FileBeginProcessingEventArgs ea = new FileBeginProcessingEventArgs(fi.FullName); //OnFileBeginProcessing(ea); string destPath = Path.Combine(destDir.FullName, fi.Name); //Check file attributes if ((!IncludeHidden && fi.Attributes.ToString().Contains("Hidden")) || (!IncludeReadOnly && fi.Attributes.ToString().Contains("ReadOnly")) || (!IncludeSystem && fi.Attributes.ToString().Contains("System"))) { _log.InfoFormat("Add Skip File Action to Action Queue: {0}", fi.FullName); SkipFileActionQueue.Enqueue(new SkipFileAction(fi.FullName)); OnActionAddedToQueue(new EventArgs()); } else { //Check if the file exists ///TODO: Probably only need copy action if (!File.Exists(destPath)) { _log.InfoFormat("Add Copy File Action to Action Queue: {0} {1}", fi.FullName, destPath); ActionQueue.Enqueue(new CopyFileAction(fi.FullName, destPath)); OnActionAddedToQueue(new EventArgs()); } else //Only copy if it has changed { var fiD = new FileInfo(destPath); if (fi.LastWriteTimeUtc < fiD.LastWriteTimeUtc.AddSeconds(-5) || fi.Length != fiD.Length) { _log.InfoFormat("Add Overwrite File Action to Action Queue: {0}", fi.FullName, destPath); ActionQueue.Enqueue(new OverwriteFileAction(fi.FullName, destPath)); OnActionAddedToQueue(new EventArgs()); } else { _log.InfoFormat("Add Skip File Action to Action Queue: {0}", fi.FullName); SkipFileActionQueue.Enqueue(new SkipFileAction(fi.FullName)); OnActionAddedToQueue(new EventArgs()); } } } } } catch (Exception ex) { _log.ErrorFormat("{0}: {1} {2}", "ProcessDirectoryForActions", di.FullName, ex.Message); } }
private void BuildActionQueue() { _log.Info("Building Action Queue"); ProcessingStatus = BackupProcessingStatus.BuildingActionQueue; //Clear the action Queues ActionQueue.Clear(); SkipFileActionQueue.Clear(); DeleteActionStack.Clear(); if (_cancelToken.IsCancellationRequested) { _log.Info("Building Action Queue Cancelled"); ProcessingStatus = BackupProcessingStatus.Cancelled; return; } if (CheckDriveExists(SourceDirectory.Substring(0, 1)) && CheckSourceRootExists() && CheckDriveExists(DestinationDirectory.Substring(0, 1))) { if (BackupMode == BackupSetMode.Backup && RemoveDeleted) { List <DirectoryInfo> destDirs = IOHelper.GetDirectoriesPostOrder(DestinationDirectory); if (destDirs != null) { foreach (DirectoryInfo di in destDirs) { if (_cancelToken.IsCancellationRequested) { ProcessingStatus = BackupProcessingStatus.Cancelled; return; } ProcessDestinationDirectoryForPossibleDelete(di); } } } var dirs = IOHelper.GetDirectories(SourceDirectory); if (dirs != null) { foreach (DirectoryInfo di in dirs) { //Check if the directory should be excluded if (ExcludedDirectories == null || !ExcludedDirectories.Contains(di.FullName)) { if (_cancelToken.IsCancellationRequested) { ProcessingStatus = BackupProcessingStatus.Cancelled; return; } ProcessDirectoryForActions(di); } else { IOHelper.GetFiles(di.FullName) .ForEach(fi => SkipFileActionQueue.Enqueue(new SkipFileAction(fi.FullName))); } } } } _log.Info("Finished Building Action Queue"); ProcessingStatus = BackupProcessingStatus.ActionQueueBuilt; }