// Constructor public FileSyncAgent(SyncJob job) { this._job = job; // Instantiates providers actProvider = SyncClient.GetSyncActionsProvider(this._job.IntermediaryStorage.Path); mdProvider = SyncClient.GetMetaDataProvider(_job.IntermediaryStorage.Path, _job.SyncSource.ID); }
private Metadata UpdateSyncSourceMetadata() { //read metadata of the current folder stored in the database Metadata mdCurrentOld = mdProvider.Load(_job.SyncSource.ID, SourceOption.SOURCE_ID_EQUALS); //read metadata of the current folder in file system Metadata mdCurrent = MetaDataProvider.Generate(_job.SyncSource.Path, _job.SyncSource.ID, false, false, true); //Update metadata mdProvider.Update(mdCurrentOld, mdCurrent); return(mdCurrent); }
/// <summary> /// Generate the sync preview containing actions to be executed. /// The returned SyncPreviewResult can then be passed to Synchronize method /// as an argument for the actions to be executed. /// </summary> /// <exception cref="DirectoryNotFoundException"></exception> public SyncPreviewResult GenerateSyncPreview(StatusCallbackDelegate statusCallback) { OnStatusChanged(new SyncStatusChangedEventArgs(m_ResourceManager.GetString("lbl_peparingToSync"))); // Load actions to be executed. IList <SyncAction> actions = actProvider.Load(_job.SyncSource.ID, SourceOption.SOURCE_ID_NOT_EQUALS); // Generate current folder's metadata FileMetaData currentItems = MetaDataProvider.GenerateFileMetadata(_job.SyncSource.Path, _job.SyncSource.ID, false, statusCallback); // Load current folder metadata from database FileMetaData oldCurrentItems = mdProvider.LoadFileMetadata(_job.SyncSource.ID, SourceOption.SOURCE_ID_EQUALS); // Load the other folder metadata from database FileMetaData oldOtherItems = mdProvider.LoadFileMetadata(_job.SyncSource.ID, SourceOption.SOURCE_ID_NOT_EQUALS); return(new SyncPreviewResult(actions, currentItems, oldCurrentItems, oldOtherItems)); }
private void SaveActionsAndDirtyFiles(IList <SyncAction> actions) { FileMetaData fileMetaData = MetaDataProvider.GenerateFileMetadata(_job.IntermediaryStorage.DirtyFolderPath, "", false, true); int totalProgress = actions.Count; int currProgress = 0; foreach (SyncAction a in actions) { if (!Validator.SyncJobParamsValidated(_job.Name, _job.IntermediaryStorage.Path, _job.SyncSource.Path, null)) { return; } try { OnProgressChanged(new SyncProgressChangedEventArgs(++currProgress, totalProgress)); OnSyncFileChanged(new SyncFileChangedEventArgs(a.ChangeType, a.RelativeFilePath)); FileMetaDataItem item = new FileMetaDataItem("", a.RelativeFilePath, a.FileHash, DateTime.Now, 0, 0); if (a.ChangeType == ChangeType.NEWLY_CREATED && !fileMetaData.MetaDataItems.Contains(item, new FileMetaDataItemComparer())) { SyncExecutor.CopyToDirtyFolderAndUpdateActionTable(a, _job); } else { actProvider.Add(a); } log.Add(new LogActivity(a.RelativeFilePath, a.ChangeType.ToString(), "SUCCESS")); } catch (OutOfDiskSpaceException) { log.Add(new LogActivity(a.RelativeFilePath, a.ChangeType.ToString(), "FAIL")); throw; } catch (Exception) { log.Add(new LogActivity(a.RelativeFilePath, a.ChangeType.ToString(), "FAIL")); throw; } } }
private void SyncEmptyFolders() { //read metadata of the current folder in file system FolderMetadata currentItems = MetaDataProvider.GenerateFolderMetadata(_job.SyncSource.Path, _job.SyncSource.ID, false, false, true); //read the folder metadata of current folder stored in database FolderMetadata oldCurrentItems = mdProvider.LoadFolderMetadata(_job.SyncSource.ID, SourceOption.SOURCE_ID_EQUALS); //read folder metadata of the other source stored in the database FolderMetadata otherItems = mdProvider.LoadFolderMetadata(_job.SyncSource.ID, SourceOption.SOURCE_ID_NOT_EQUALS); //get the difference between current folder metadata with its previous state (from previous sync session). FolderMetadataComparer comparer1 = new FolderMetadataComparer(oldCurrentItems, currentItems); //get the difference between current folder metadata with the other folder's previous metadata FolderMetadataComparer compare2 = new FolderMetadataComparer(currentItems, otherItems); FolderMetadataComparer comparer3 = new FolderMetadataComparer(oldCurrentItems.FolderMetadataItems, compare2.LeftOnly); FolderMetadataComparer comparer4 = new FolderMetadataComparer(compare2.RightOnly, comparer1.LeftOnly); List <FolderMetadataItem> temps = comparer3.Both.ToList(); temps.Sort(new FolderMetadataItemComparer()); temps.Reverse(); foreach (FolderMetadataItem item in temps) { SyncExecutor.DeleteFolder(this._job.SyncSource.Path, item.RelativePath, true); } foreach (FolderMetadataItem item in comparer4.LeftOnly) { if (item.IsEmpty == 1) { SyncExecutor.CreateFolder(this._job.SyncSource.Path, item.RelativePath); } } currentItems = MetaDataProvider.GenerateFolderMetadata(_job.SyncSource.Path, _job.SyncSource.ID, false, false, true); mdProvider.UpdateFolderMetadata(oldCurrentItems, currentItems, false); }
/// <summary> /// Generates SyncActions to be executed by other PC by comparing the metadata /// of current SyncSource and the metadata of other PC. /// </summary> private IList <SyncAction> GenerateActions() { //Metadata mdCurrent = UpdateSyncSourceMetadata(); //Metadata mdOther = mdProvider.Load(_job.SyncSource.ID, SourceOption.SOURCE_ID_NOT_EQUALS); FileMetaData mdCurrent = MetaDataProvider.GenerateFileMetadata( _job.SyncSource.Path, _job.SyncSource.ID, false, false); FileMetaData mdOldCurrent = mdProvider.LoadFileMetadata(_job.SyncSource.ID, SourceOption.SOURCE_ID_EQUALS); mdProvider.UpdateFileMetadata(mdOldCurrent, mdCurrent); FileMetaData mdOther = mdProvider.LoadFileMetadata(_job.SyncSource.ID, SourceOption.SOURCE_ID_NOT_EQUALS); //generate list of sync actions by comparing 2 metadata var differences = new FileMetaDataComparer(mdCurrent, mdOther); return(actProvider.Generate(mdCurrent.SourceId, differences.LeftOnly, differences.RightOnly, differences.BothModified)); }