public override bool Update(SyncJob job) { if (this.SyncJobExists(job.Name, job.ID)) { throw new SyncJobNameExistException(String.Format(m_ResourceManager.GetString("err_syncjobCreated"), job.Name)); } SQLiteSyncSourceProvider provider = (SQLiteSyncSourceProvider)SyncClient.GetSyncSourceProvider(job.IntermediaryStorage.Path); if (provider.GetSyncSourceCount() > 2) { throw new SyncSourcesNumberExceededException(m_ResourceManager.GetString("err_onlyTwoSyncSourceFolders")); } // Update a profile requires update 2 tables at the same time, // If one update on a table fails, the total update action must fail too. string updateProfileText = "UPDATE " + SYNCJOB_TABLE + " SET " + COL_METADATA_SOURCE_LOCATION + " = @mdSource, " + COL_SYNCJOB_NAME + " = @name WHERE " + COL_SYNCJOB_ID + " = @id;"; SqliteParameterCollection paramList = new SqliteParameterCollection(); // Add parameters for 1st Update statement paramList.Add(new SqliteParameter("@mdSource", System.Data.DbType.String) { Value = job.IntermediaryStorage.Path }); paramList.Add(new SqliteParameter("@name", System.Data.DbType.String) { Value = job.Name }); paramList.Add(new SqliteParameter("@id", System.Data.DbType.String) { Value = job.ID }); SQLiteAccess db = new SQLiteAccess(Path.Combine(this.StoragePath, DATABASE_NAME), false); using (SqliteConnection con = db.NewSQLiteConnection()) { if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, DATABASE_NAME))); } SqliteTransaction transaction = (SqliteTransaction)con.BeginTransaction(); try { SQLiteSyncSourceProvider.Update(job.SyncSource, con); db.ExecuteNonQuery(updateProfileText, paramList); transaction.Commit(); return(true); } catch (Exception) { transaction.Rollback(); throw; } } }
// 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); }
/// <summary> /// Copy a file from sync folder and update action table /// </summary> /// <param name="action"></param> /// <param name="profile"></param> public static void CopyToSyncFolderAndUpdateActionTable(SyncAction action, SyncJob job) { if (!Directory.Exists(job.SyncSource.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.SyncSource.Path)); } if (!Directory.Exists(job.IntermediaryStorage.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.IntermediaryStorage.Path)); } // TODO: atomic.... string absolutePathInIntermediateStorage = job.IntermediaryStorage.DirtyFolderPath + action.RelativeFilePath; string absolutePathInSyncSource = job.SyncSource.Path + action.RelativeFilePath; SQLiteAccess dbAccess = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME), true); SqliteConnection con = dbAccess.NewSQLiteConnection(); if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); } SqliteTransaction trasaction = (SqliteTransaction)con.BeginTransaction(); try { SQLiteSyncActionsProvider actProvider = (SQLiteSyncActionsProvider)SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action, con); if (!Files.FileUtils.Copy(absolutePathInIntermediateStorage, absolutePathInSyncSource, true)) { throw new Exception(String.Format(m_ResourceManager.GetString("err_cannotCopyFile"), absolutePathInIntermediateStorage)); } trasaction.Commit(); Files.FileUtils.DeleteFileAndFolderIfEmpty(job.IntermediaryStorage.DirtyFolderPath, absolutePathInIntermediateStorage, true); } catch (OutOfDiskSpaceException) { trasaction.Rollback(); throw; } catch (Exception) { trasaction.Rollback(); throw; } finally { if (con != null) { con.Dispose(); } } }
public static bool IntermediaryMovable(string baseFolder, string sourceId) { string absolutePath = Path.Combine(baseFolder, Configuration.DATABASE_NAME); if (!File.Exists(absolutePath)) { return(true); } SyncSourceProvider sourceProvider = SyncClient.GetSyncSourceProvider(baseFolder); return(sourceProvider.SourceExist(sourceId)); }
/// <summary> /// Rename a file in sync source folder and update action table /// </summary> /// <param name="action"></param> /// <param name="profile"></param> /// <exception cref="System.ComponentModel.Win32Exception"></exception> public static void RenameInSyncFolderAndUpdateActionTable(RenameAction action, SyncJob job) { if (!Directory.Exists(job.SyncSource.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.SyncSource.Path)); } if (!Directory.Exists(job.IntermediaryStorage.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.IntermediaryStorage.Path)); } string oldAbsolutePathInSyncSource = job.SyncSource.Path + action.PreviousRelativeFilePath; string newAbsolutePathInSyncSource = job.SyncSource.Path + action.RelativeFilePath; SQLiteAccess access = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME), true); SqliteConnection con = access.NewSQLiteConnection(); if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); } SqliteTransaction transaction = (SqliteTransaction)con.BeginTransaction(); try { SyncActionsProvider actProvider = SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action); if (File.Exists(oldAbsolutePathInSyncSource) && !Files.FileUtils.Move(oldAbsolutePathInSyncSource, newAbsolutePathInSyncSource, true)) { throw new Exception(String.Format(m_ResourceManager.GetString("err_cannotRenameFile"), oldAbsolutePathInSyncSource)); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } finally { if (con != null) { con.Dispose(); } } }
public override bool Add(SyncJob job) { if (this.SyncJobExists(job.Name, job.ID)) { throw new SyncJobNameExistException(String.Format(m_ResourceManager.GetString("err_syncjobCreated"), job.Name)); } SQLiteAccess dbAccess1 = new SQLiteAccess(Path.Combine(this.StoragePath, Configuration.DATABASE_NAME), false); SQLiteAccess dbAccess2 = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME), false); SqliteConnection con1 = dbAccess1.NewSQLiteConnection(); SqliteConnection con2 = dbAccess2.NewSQLiteConnection(); if (con1 == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(this.StoragePath, DATABASE_NAME))); } if (con2 == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); } SqliteTransaction transaction1 = (SqliteTransaction)con1.BeginTransaction(); SqliteTransaction transaction2 = (SqliteTransaction)con2.BeginTransaction(); try { string insertJobText = "INSERT INTO " + SYNCJOB_TABLE + " (" + COL_SYNCJOB_ID + ", " + COL_SYNCJOB_NAME + " ," + COL_METADATA_SOURCE_LOCATION + ", " + COL_SYNC_SOURCE_ID + ") VALUES (@id, @name, @meta, @source)"; SqliteParameterCollection paramList = new SqliteParameterCollection(); paramList.Add(new SqliteParameter("@id", System.Data.DbType.String) { Value = job.ID }); paramList.Add(new SqliteParameter("@name", System.Data.DbType.String) { Value = job.Name }); paramList.Add(new SqliteParameter("@meta", System.Data.DbType.String) { Value = job.IntermediaryStorage.Path }); paramList.Add(new SqliteParameter("@source", System.Data.DbType.String) { Value = job.SyncSource.ID }); dbAccess1.ExecuteNonQuery(insertJobText, paramList); SQLiteSyncSourceProvider.Add(job.SyncSource, con1); SQLiteSyncSourceProvider provider = (SQLiteSyncSourceProvider)SyncClient.GetSyncSourceProvider(job.IntermediaryStorage.Path); if (provider.GetSyncSourceCount() == 2) { throw new SyncSourcesNumberExceededException(m_ResourceManager.GetString("err_onlyTwoSyncSourceFolders")); } SQLiteSyncSourceProvider.Add(job.SyncSource, con2); transaction1.Commit(); transaction2.Commit(); return(true); } catch (Exception) { transaction1.Rollback(); transaction2.Rollback(); throw; } finally { if (con1 != null) { con1.Dispose(); } if (con2 != null) { con2.Dispose(); } } }
public static void CreateDataStore(string pathToJobFolder, SyncSource syncSource, IntermediaryStorage metaDataSource) { if (!Directory.Exists(pathToJobFolder)) { Directory.CreateDirectory(pathToJobFolder); } if (!Directory.Exists(metaDataSource.Path)) { Directory.CreateDirectory(metaDataSource.Path); } SqliteConnection con1 = null; SqliteConnection con2 = null; SqliteTransaction transaction1 = null; SqliteTransaction transaction2 = null; try { SQLiteAccess dbAccess1 = new SQLiteAccess(Path.Combine(pathToJobFolder, Configuration.DATABASE_NAME), true); SQLiteAccess dbAccess2 = new SQLiteAccess(Path.Combine(metaDataSource.Path, Configuration.DATABASE_NAME), true); con1 = dbAccess1.NewSQLiteConnection(); con2 = dbAccess2.NewSQLiteConnection(); if (con1 == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(pathToJobFolder, Configuration.DATABASE_NAME))); } if (con2 == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(metaDataSource.Path, Configuration.DATABASE_NAME))); } transaction2 = (SqliteTransaction)con2.BeginTransaction(); transaction1 = (SqliteTransaction)con1.BeginTransaction(); //Create schema for source info table in job folder SQLiteSyncSourceProvider.CreateSchema(con1); //Create schema for profile table in job folder SQLiteSyncJobManager.CreateSchema(con1); //create schema for source info table in intermediate storage folder SQLiteSyncSourceProvider.CreateSchema(con2); //create schema for metadata table in intermediate storage folder SQLiteMetaDataProvider mdProvider = (SQLiteMetaDataProvider)SyncClient.GetMetaDataProvider(metaDataSource.Path, Configuration.DATABASE_NAME); mdProvider.CreateSchema(con2); //create schema for action table in intermediate storage folder SQLiteSyncActionsProvider actionProvider = (SQLiteSyncActionsProvider)SyncClient.GetSyncActionsProvider(metaDataSource.Path); actionProvider.CreateSchema(con2); transaction2.Commit(); transaction1.Commit(); } catch (Exception ex) { Console.WriteLine(ex.Message); if (transaction2 != null) { transaction2.Rollback(); } if (transaction1 != null) { transaction1.Rollback(); } throw new DatabaseException(m_ResourceManager.GetString("err_databaseException")); } finally { if (con1 != null) { con1.Dispose(); } if (con2 != null) { con2.Dispose(); } } }
public static void UpdateTableAction(SyncAction action, SyncJob job) { var actProvider = (SQLiteSyncActionsProvider)SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Add(action); }
public static void DeleteFromActionTable(SyncAction action, SyncJob job) { SyncActionsProvider actProvider = SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action); }
/// <summary> /// Renames conflicted file so that RenameAction can be executed /// </summary> /// <param name="renameAction">Rename action that cannot be executed due to conflict.</param> public static void ConflictRenameAndUpdateActionTable(RenameAction action, SyncJob job, bool keepConflictedFile) { if (!Directory.Exists(job.SyncSource.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.SyncSource.Path)); } if (!Directory.Exists(job.IntermediaryStorage.Path)) { throw new SyncSourceException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), job.IntermediaryStorage.Path)); } string absPathInIStorage = job.IntermediaryStorage.DirtyFolderPath + action.RelativeFilePath; string absPathInSyncSource = job.SyncSource.Path + action.RelativeFilePath; string absOldPathInSyncSource = job.SyncSource.Path + action.PreviousRelativeFilePath; SQLiteAccess access = new SQLiteAccess(Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME), true); SqliteConnection con = access.NewSQLiteConnection(); if (con == null) { throw new DatabaseException(String.Format(m_ResourceManager.GetString("err_somethingNotFound"), Path.Combine(job.IntermediaryStorage.Path, Configuration.DATABASE_NAME))); } SqliteTransaction trasaction = (SqliteTransaction)con.BeginTransaction(); try { SQLiteSyncActionsProvider actProvider = (SQLiteSyncActionsProvider)SyncClient.GetSyncActionsProvider(job.IntermediaryStorage.Path); actProvider.Delete(action, con); if (!keepConflictedFile) { Files.FileUtils.Delete(absPathInSyncSource, true); } else { Files.FileUtils.DuplicateRename(absPathInSyncSource, absPathInSyncSource); } if (!Files.FileUtils.Move(absOldPathInSyncSource, absPathInSyncSource, true)) { throw new Exception(String.Format(m_ResourceManager.GetString("err_cannotRenameFile"), absPathInIStorage)); } trasaction.Commit(); Files.FileUtils.DeleteFileAndFolderIfEmpty(job.IntermediaryStorage.DirtyFolderPath, absPathInIStorage, true); } catch (Exception) { trasaction.Rollback(); throw; } finally { if (con != null) { con.Dispose(); } } }