/// <summary></summary> /// <param name=""></param> /// <param name=""></param> private void ReadSourceDirectories(PairOfFiles ParentDirectory, List <PairOfFiles> ltPairsRead) { PairOfFiles NewPair; if (_eState == nState.Working) // test if cancel was requested { _quReturn.Enqueue(new BackgroundMessage(BackgroundMessage.nType.Status, ParentDirectory.sSourcePath)); ReadSourceFiles(ParentDirectory, ltPairsRead); // read all files in this directory try { foreach (string DirectoryName in Directory.GetDirectories(ParentDirectory.sSourcePath)) { NewPair = new PairOfFiles(ParentDirectory, ParentDirectory.SourceDrive.RemoveRootPath(DirectoryName), PairOfFiles.nComparison.UnknownDestination, true, _TextConverter); ltPairsRead.Add(NewPair); if (NewPair.eComparison != PairOfFiles.nComparison.Error) { ReadSourceDirectories(NewPair, ltPairsRead); // recurse to the next level and search for directories, files and access errors there } } } catch (Exception ex) // if something goes wrong, we still have the parent pair to store the error message for us { ParentDirectory.eComparison = PairOfFiles.nComparison.Error; ParentDirectory.sErrorMessage = ex.Message; } } }
/// <summary></summary> /// <param name=""></param> public void RemovePair(PairOfFiles PairToRemove) { if ((_ltEncryptedPairs != null) && _ltEncryptedPairs.Contains(PairToRemove)) { _ltEncryptedPairs.Remove(PairToRemove); } }
/// <summary></summary> /// <param name=""></param> /// <param name=""></param> private void ReadDestinationFiles(PairOfFiles ParentDirectory, List <PairOfFiles> ltPairsRead) { PairOfFiles FoundPair, NewPair; try { foreach (string FileName in Directory.GetFiles(ParentDirectory.sDestinationPath)) { NewPair = new PairOfFiles(ParentDirectory, ParentDirectory.DestinationDrive.RemoveRootPath(FileName), PairOfFiles.nComparison.UnknownSource, false, _TextConverter); FoundPair = ltPairsRead.SingleOrDefault(p => (p.sRelativePath == NewPair.sRelativePath) && !p.isDirectory); if (FoundPair == null) // there is no source file with this relative path { ltPairsRead.Add(NewPair); if (NewPair.eComparison != PairOfFiles.nComparison.Error) { NewPair.eComparison = PairOfFiles.nComparison.DestinationOnly; } } else if (NewPair.eComparison == PairOfFiles.nComparison.Error) { FoundPair.eComparison = NewPair.eComparison; } else { FoundPair.eComparison = PairOfFiles.nComparison.BothExist; } } } catch (Exception ex) { ParentDirectory.eComparison = PairOfFiles.nComparison.Error; ParentDirectory.sErrorMessage = ex.Message; } }
/// <summary></summary> /// <param name=""></param> public void AddPair(PairOfFiles PairToAdd) { if ((_eEncryptionType != nEncryptionType.DirectoryUnencrypted) && (_ltEncryptedPairs != null) && !_ltEncryptedPairs.Contains(PairToAdd)) { _ltEncryptedPairs.Add(PairToAdd); } }
/// <summary>A constructor to initialize a <c>new BackgroundMessage</c>.</summary> /// <param name=""></param> public BackgroundMessage(nType eType) { _eType = eType; _eReturnCode = nReturnCode.Empty; _iValue = _iProgressMaximum = 0; _sText = string.Empty; _TimeStamp = DateTime.MinValue; _DriveProperty = null; _PairProperty = null; }
/// <summary></summary> /// <param name=""></param> private void Copy(PairOfFiles PairToCopy) { if (PairToCopy.isDirectory) { CopyDirectory(PairToCopy); } else { CopyFile(PairToCopy); } }
/// <summary></summary> /// <param name=""></param> private void DestinationOnly(PairOfFiles PairToSynchronize) { switch (PairToSynchronize.eSynchronizationMode) { case PairOfFiles.nSynchronizationMode.NoDelete: Skip(PairToSynchronize); break; case PairOfFiles.nSynchronizationMode.WithDelete: Delete(PairToSynchronize); break; case PairOfFiles.nSynchronizationMode.TwoWay: PairToSynchronize.SwapSourceAndDestination(); Copy(PairToSynchronize); break; } }
/// <summary></summary> /// <param name=""></param> private void Delete(PairOfFiles PairToDelete) { if (PairToDelete.isDirectory) { DeleteDirectory(PairToDelete); } else { DeleteFile(PairToDelete); } _quReturn.Enqueue(new BackgroundMessage(BackgroundMessage.nType.ReportProgress, 1)); }
/// <summary></summary> /// <param name=""></param> /// <param name=""></param> private void ReadSourceFiles(PairOfFiles ParentDirectory, List <PairOfFiles> ltPairsRead) { PairOfFiles NewPair; try { foreach (string FileName in Directory.GetFiles(ParentDirectory.sSourcePath)) { NewPair = new PairOfFiles(ParentDirectory, ParentDirectory.SourceDrive.RemoveRootPath(FileName), PairOfFiles.nComparison.UnknownDestination, false, _TextConverter); ltPairsRead.Add(NewPair); } } catch (Exception ex) { ParentDirectory.eComparison = PairOfFiles.nComparison.Error; ParentDirectory.sErrorMessage = ex.Message; } }
/// <summary></summary> /// <param name=""></param> /// <param name=""></param> private void ReadDestinationDirectories(PairOfFiles ParentDirectory, List <PairOfFiles> ltPairsRead) { PairOfFiles FoundPair, NewPair; if (_eState == nState.Working) // test if cancel was requested { _quReturn.Enqueue(new BackgroundMessage(BackgroundMessage.nType.Status, ParentDirectory.sDestinationPath)); ReadDestinationFiles(ParentDirectory, ltPairsRead); // read all files in this directory try { foreach (string DirectoryName in Directory.GetDirectories(ParentDirectory.sDestinationPath)) { NewPair = new PairOfFiles(ParentDirectory, ParentDirectory.DestinationDrive.RemoveRootPath(DirectoryName), PairOfFiles.nComparison.UnknownSource, true, _TextConverter); FoundPair = ltPairsRead.SingleOrDefault(p => (p.sRelativePath == NewPair.sRelativePath) && p.isDirectory); if (FoundPair == null) // there is no source directory with this relative path { ltPairsRead.Add(NewPair); if (NewPair.eComparison != PairOfFiles.nComparison.Error) { NewPair.eComparison = PairOfFiles.nComparison.DestinationOnly; ReadDestinationDirectories(NewPair, ltPairsRead); // recurse to the next level and search for directories, files and access errors there } } else if (NewPair.eComparison == PairOfFiles.nComparison.Error) { FoundPair.eComparison = NewPair.eComparison; } else { FoundPair.eComparison = PairOfFiles.nComparison.BothExist; ReadDestinationDirectories(FoundPair, ltPairsRead); // recurse to the next level and search for directories, files and access errors there } } } catch (Exception ex) // if something goes wrong, we still have the parent pair to store the error message for us { ParentDirectory.eComparison = PairOfFiles.nComparison.Error; ParentDirectory.sErrorMessage = ex.Message; } } }
/// <summary></summary> /// <param name=""></param> private void CopyDirectory(PairOfFiles PairToCopy) { if (PairToCopy.eComparison == PairOfFiles.nComparison.DestinationOnly) { PairToCopy.eComparison = PairOfFiles.nComparison.Error; PairToCopy.sErrorMessage = "No source directory to copy at '" + PairToCopy.sRelativePath + "'."; } else if (PairToCopy.DestinationDrive.eEncryptionType == Drive.nEncryptionType.DirectoryUnencrypted) { DirectoryInfo DirectoryInfoDestination = new DirectoryInfo(PairToCopy.sDestinationPath); try { if (!DirectoryInfoDestination.Exists) { DirectoryInfoDestination.Create(); } } catch (Exception ex) { PairToCopy.eComparison = PairOfFiles.nComparison.Error; PairToCopy.sErrorMessage = ex.Message; } PairToCopy.CopyProperties(); try { DirectoryInfoDestination.Attributes = (FileAttributes)PairToCopy.uAttributesSource; DirectoryInfoDestination.CreationTime = Directory.GetCreationTime(PairToCopy.sSourcePath); DirectoryInfoDestination.LastAccessTime = Directory.GetLastAccessTime(PairToCopy.sSourcePath); DirectoryInfoDestination.LastWriteTime = PairToCopy.LastWriteTimeSource; } catch { } PairToCopy.eComparison = PairOfFiles.nComparison.Identical; _quReturn.Enqueue(new BackgroundMessage(BackgroundMessage.nType.ReportProgress, PairToCopy)); } else { throw new NotImplementedException("File system encryption is not yet implemented."); } }
/// <summary></summary> /// <param name=""></param> private void DeleteFile(PairOfFiles PairToDelete) { if (PairToDelete.eComparison == PairOfFiles.nComparison.DestinationOnly) { if (PairToDelete.DestinationDrive.eEncryptionType == Drive.nEncryptionType.DirectoryUnencrypted) { try { File.Delete(PairToDelete.sDestinationPath); PairToDelete.eComparison = PairOfFiles.nComparison.Deleted; } catch (Exception ex) { PairToDelete.eComparison = PairOfFiles.nComparison.Error; PairToDelete.sErrorMessage = ex.Message; } } } else { PairToDelete.eComparison = PairOfFiles.nComparison.Error; PairToDelete.sErrorMessage = "Deleting file '" + PairToDelete.sRelativePath + "' is not allowed."; } }
/// <summary></summary> /// <param name=""></param> private void Skip(PairOfFiles PairToSkip) { // PairToSkip.eComparison = PairOfFiles.nComparison.Skipped; PairToSkip.DestinationDrive.AddPair(PairToSkip); }
/// <summary></summary> /// <param name=""></param> private void CopyFile(PairOfFiles PairToCopy) { FileInfo FileInfoDestination; if (PairToCopy.eComparison == PairOfFiles.nComparison.DestinationOnly) { PairToCopy.eComparison = PairOfFiles.nComparison.Error; PairToCopy.sErrorMessage = "No source file to copy: '" + PairToCopy.sRelativePath + "'."; } else if (PairToCopy.DestinationDrive.eEncryptionType == Drive.nEncryptionType.DirectoryUnencrypted) { FileInfoDestination = new FileInfo(PairToCopy.sDestinationPath); try { if (PairToCopy.SourceDrive.eEncryptionType == Drive.nEncryptionType.DirectoryUnencrypted) { if (PairToCopy.DestinationDrive.kFreeSpace < PairToCopy.kSourceSize) { PairToCopy.eComparison = PairOfFiles.nComparison.Error; PairToCopy.sErrorMessage = "Zu wenig Speicherplatz auf dem Ziellaufwerk."; } else { using (FileStream SourceStream = new FileStream(PairToCopy.sSourcePath, FileMode.Open, FileAccess.Read)) { using (FileStream DestinationStream = new FileStream(PairToCopy.sDestinationPath, FileMode.Create, FileAccess.Write)) CopyWithProgress(SourceStream, DestinationStream); } } } } catch (Exception ex) { PairToCopy.eComparison = PairOfFiles.nComparison.Error; PairToCopy.sErrorMessage = ex.Message; } if (_eState == nState.Working) { if (PairToCopy.eComparison != PairOfFiles.nComparison.Error) { PairToCopy.CopyProperties(); try { FileInfoDestination.Attributes = (FileAttributes)PairToCopy.uAttributesSource; FileInfoDestination.CreationTime = PairToCopy.CreationTimeSource; FileInfoDestination.LastAccessTime = PairToCopy.LastAccessTimeSource; FileInfoDestination.LastWriteTime = PairToCopy.LastWriteTimeSource; // synchronize the write time of the containing directory back to its original value // there is no point of resetting LastAccessTime as it also gets changed by reading access if (PairToCopy.ParentDirectory != null) { Directory.SetLastWriteTime(PairToCopy.ParentDirectory.sDestinationPath, PairToCopy.ParentDirectory.LastWriteTimeDestination); } } catch { } PairToCopy.eComparison = PairOfFiles.nComparison.Identical; } } else // _eState == nState.CancelRequested { try { if (FileInfoDestination.Exists) { FileInfoDestination.Delete(); } } catch (Exception ex) { PairToCopy.eComparison = PairOfFiles.nComparison.Error; PairToCopy.sErrorMessage = ex.Message; } PairToCopy.eComparison = PairOfFiles.nComparison.Error; PairToCopy.sErrorMessage = "Das Kopieren der Datei wurde vom Benutzer abgebrochen."; } _quReturn.Enqueue(new BackgroundMessage(BackgroundMessage.nType.ReportProgress, PairToCopy)); } else { throw new NotImplementedException("File system encryption is not yet implemented."); } }
/// <summary>A constructor to initialize a <c>new BackgroundMessage</c>.</summary> /// <param name=""></param> /// <param name=""></param> public BackgroundMessage(nType eType, PairOfFiles PairProperty) : this(eType) { _PairProperty = PairProperty; }