public DirectoryPushedEventArgs(string path, PushDirectoryResult result) : base(path) { Result = result; }
// add separte method for top level call // and make recursive one private, so that different rules can apply // does this method need top level error handling? public static PushDirectoryResult PushDirectory(IFileSystem fileSystem, string sourcePath, string targetPath, string pattern = "*.*", FileAttributes attributes = FileAttributes.Normal) { // If the source directory is null, throw an exception. if (sourcePath == null) { throw new ArgumentNullException("sourcePath", "SourcePath null."); } // If the target directory is null, throw an exception. if (targetPath == null) { throw new ArgumentNullException("targetPath", "TargetPath null."); } // If the source directory doesn't exist, throw an exception. if (!fileSystem.Directory.Exists(sourcePath)) { throw new SyncDirectoryNotFoundException(sourcePath); } var sourceInfo = fileSystem.DirectoryInfo.FromDirectoryName(sourcePath); DirectoryInfoBase targetInfo; PushDirectoryResult result = new PushDirectoryResult(); // If the target directory doesn't exist, try to create it now. if (!fileSystem.Directory.Exists(targetPath)) { targetInfo = fileSystem.Directory.CreateDirectory(targetPath); targetInfo.Attributes = sourceInfo.Attributes; result.Result = PushEntryResult.Created; RaiseEntryEvent(DirectoryCreated, targetPath); } // If the target's attributes are different, update them now. else { targetInfo = fileSystem.DirectoryInfo.FromDirectoryName(targetPath); if (targetInfo.Attributes != sourceInfo.Attributes) { targetInfo.Attributes = sourceInfo.Attributes; result.Result = PushEntryResult.Updated; RaiseEntryEvent(DirectoryUpdated, targetPath); } } // Loop through each file in the source directory. foreach (string sourceFilePath in fileSystem.Directory.GetFiles(sourcePath, pattern))//, attributes)) { // If the source file doesn't have the required attributes, skip it. var sourceFileInfo = fileSystem.FileInfo.FromFileName(sourceFilePath); if ((sourceFileInfo.Attributes & attributes) != attributes) { continue; } string targetFilePath = Path.Combine(targetPath, sourceFileInfo.Name); try { switch (PushFile(fileSystem, sourceFilePath, targetFilePath)) { case PushEntryResult.Created: result.CreatedFiles.Add(sourceFileInfo.Name); RaiseEntryEvent(FileCreated, targetFilePath); break; case PushEntryResult.Updated: result.UpdatedFiles.Add(sourceFileInfo.Name); RaiseEntryEvent(FileUpdated, targetFilePath); break; case PushEntryResult.Verified: result.VerifiedFiles.Add(sourceFileInfo.Name); RaiseEntryEvent(FileVerified, targetFilePath); break; } } catch (Exception ex) { result.FailedEntries.Add(sourceFileInfo.Name, ex); RaiseErrorEvent(targetFilePath, ex); } } // Loop through each sub-directory in the source directory. foreach (string sourceDirectoryPath in fileSystem.Directory.GetDirectories(sourcePath)) { var sourceDirectory = fileSystem.DirectoryInfo.FromDirectoryName(sourceDirectoryPath); string targetDirectoryPath = Path.Combine(targetPath, sourceDirectory.Name); try { // I don't want the failure of one sub directory to kncker up all the others PushDirectoryResult subDirectoryResult = PushDirectory(fileSystem, sourceDirectoryPath, targetDirectoryPath, pattern, attributes); result.DirectoryResults.Add(sourceDirectory.Name, subDirectoryResult); } catch (Exception ex) { RaiseErrorEvent(targetDirectoryPath, ex); result.FailedEntries.Add(sourceDirectory.Name, ex); } } // Flag up the push. RaiseDirectorySynchronisedEvent(targetPath, result); return result; }
private static void RaiseDirectorySynchronisedEvent(string path, PushDirectoryResult result) { DirectoryPushedEventHandler temp = DirectoryPushed; if (temp != null) { DirectoryPushedEventArgs eventArgs = new DirectoryPushedEventArgs(path, result); temp(eventArgs); } }