/// <summary> /// Function that performs action of synchronization based on the corresponding SyncData which has to contain SyncActionState and SyncDirection. /// </summary> /// <param name="SourceData">The directory being synchronized.</param> /// <param name="TargetData">The comparing directory to be synchronized with.</param> /// <param name="SD">Corresponding SyncData with parameters to decide what to do with the file.</param> private static void SolveStatedPrioritizedConflict(DirectoryData SourceData, DirectoryData TargetData, SyncData SD) { switch (SD.SAS) { case SyncActionState.Skip: return; case SyncActionState.Delete: File.Delete(TargetData.FindFileGetLocalPath(SD.FD.RelativePath)); File.Delete(SourceData.FindFileGetLocalPath(SD.FD.RelativePath)); break; case SyncActionState.Share: if (SD.SD != SyncDirection.None) { if (SD.SD == SyncDirection.Target) { UpdateExistingFile(TargetData.FindFileGetLocalPath(SD.FD.RelativePath), SourceData.FindFileGetLocalPath(SD.FD.RelativePath)); } else if (SD.SD == SyncDirection.Source) { UpdateExistingFile(SourceData.FindFileGetLocalPath(SD.FD.RelativePath), TargetData.FindFileGetLocalPath(SD.FD.RelativePath)); } } break; case SyncActionState.Copy: break; default: break; } }
/// <summary> /// Function that forms a list of statuses compared to a specific DirectoryData data passed. /// </summary> /// <param name="currentDD">Specific DirectoryData to form a file statuses from.</param> /// <returns>LoadedFileStatus list which tells if the file datas read from Fhsks are correct or if its not tell whats wrong with it.</returns> public static List <LoadedFileStatus> CheckFileStatusesOfDirectoryData(DirectoryData currentDD) { List <LoadedFileStatus> result = new List <LoadedFileStatus>(); foreach (var item in currentDD.Files) { string currentLocalPath = currentDD.FindFileGetLocalPath(item.RelativePath); if (File.Exists(currentLocalPath)) { FileInfo fi = new FileInfo(currentLocalPath); if (fi.LastWriteTime != item.LastModifiedTime) { result.Add(LoadedFileStatus.Modified); } else { result.Add(LoadedFileStatus.NotTouched); } } else { result.Add(LoadedFileStatus.DoesntExist); } } return(result); }
/// <summary> /// Function that updates existant files data it of specified DirectoryData FileData list based on pre-computed LoadedFileStatus list by re-computing it. /// </summary> /// <param name="lfs">Pre-computed LoadedFileStatus list</param> /// <param name="currentDD">Specific DirectoryData to correct.</param> public static void UpdateModifiedFilesData(List <LoadedFileStatus> lfs, DirectoryData currentDD) { using (var md5 = MD5.Create()) { for (int i = 0; i < currentDD.Files.Count; i++) { if (lfs[i] == LoadedFileStatus.Modified) { FileData crnt = currentDD.Files[i]; FileInfo fi = new FileInfo(currentDD.FindFileGetLocalPath(crnt.RelativePath)); crnt.LastModifiedTime = fi.LastWriteTime; using (var stream = File.OpenRead(currentDD.FindFileGetLocalPath(crnt.RelativePath))) { crnt.Hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLowerInvariant(); } } } } }
/// <summary> /// Function that performs action of synchronization to a file which doesnt exist in one of the two directories based on SyncData parameters. /// </summary> /// <param name="SourceData">The directory being synchronized.</param> /// <param name="TargetData">The comparing directory to be synchronized with.</param> /// <param name="SD">Corresponding SyncData with parameters to decide what to do with the file.</param> private static void SolveNonExistantFileToOtherDirectory(DirectoryData SourceData, DirectoryData TargetData, SyncData SD) { switch (SD.SAS) { case SyncActionState.Skip: return; case SyncActionState.Delete: File.Delete(TargetData.FindFileGetLocalPath(SD.FD.RelativePath)); break; case SyncActionState.Share: File.Copy(TargetData.FindFileGetLocalPath(SD.FD.RelativePath), SourceData.RootPath + "\\" + SD.FD.RelativePath); break; case SyncActionState.Copy: break; default: break; } }