From: Jocelyn Turcotte Date: Mon, 4 Sep 2017 17:06:13 +0000 (+0200) Subject: Use a C++ contructor and destructor for CSYNC X-Git-Tag: archive/raspbian/3.16.7-1_deb13u1+rpi1~1^2~701^2~129 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=e0fa5723eba894af8f812cab9aacf9a8048318df;p=nextcloud-desktop.git Use a C++ contructor and destructor for CSYNC Merge csync_create and csync_init into the constructor and replace csync_destroy with the destructor. Also use a QByteArray for csync_s::root_perms and flatten csync_rename_s as a rename sub-struct of csync_s since it can now handle C++ types. --- diff --git a/src/csync/csync.cpp b/src/csync/csync.cpp index 0f028731d..b37e2bdcd 100644 --- a/src/csync/csync.cpp +++ b/src/csync/csync.cpp @@ -82,50 +82,19 @@ static int _data_cmp(const void *key, const void *data) { return 0; } -void csync_create(CSYNC **csync, const char *local) { - CSYNC *ctx; +csync_s::csync_s(const char *localUri, const char *db_file) { size_t len = 0; - ctx = (CSYNC*)c_malloc(sizeof(CSYNC)); - - ctx->status_code = CSYNC_STATUS_OK; - /* remove trailing slashes */ - len = strlen(local); - while(len > 0 && local[len - 1] == '/') --len; - - ctx->local.uri = c_strndup(local, len); - - ctx->status_code = CSYNC_STATUS_OK; + len = strlen(localUri); + while(len > 0 && localUri[len - 1] == '/') --len; - ctx->current_fs = NULL; + local.uri = c_strndup(localUri, len); - ctx->abort = false; + c_rbtree_create(&local.tree, _key_cmp, _data_cmp); + c_rbtree_create(&remote.tree, _key_cmp, _data_cmp); - ctx->ignore_hidden_files = true; - - *csync = ctx; -} - -void csync_init(CSYNC *ctx, const char *db_file) { - assert(ctx); - /* Do not initialize twice */ - - assert(!(ctx->status & CSYNC_STATUS_INIT)); - ctx->status_code = CSYNC_STATUS_OK; - - SAFE_FREE(ctx->statedb.file); - ctx->statedb.file = c_strdup(db_file); - - c_rbtree_create(&ctx->local.tree, _key_cmp, _data_cmp); - c_rbtree_create(&ctx->remote.tree, _key_cmp, _data_cmp); - - ctx->remote.root_perms = 0; - - ctx->status = CSYNC_STATUS_INIT; - - /* initialize random generator */ - srand(time(NULL)); + statedb.file = c_strdup(db_file); } int csync_update(CSYNC *ctx) { @@ -438,88 +407,69 @@ static void _tree_destructor(void *data) { delete freedata; } -/* reset all the list to empty. - * used by csync_commit and csync_destroy */ -static void _csync_clean_ctx(CSYNC *ctx) -{ - /* destroy the rbtrees */ - if (c_rbtree_size(ctx->local.tree) > 0) { - c_rbtree_destroy(ctx->local.tree, _tree_destructor); - } - - if (c_rbtree_size(ctx->remote.tree) > 0) { - c_rbtree_destroy(ctx->remote.tree, _tree_destructor); - } - - csync_rename_destroy(ctx); - - /* free memory */ - c_rbtree_free(ctx->local.tree); - c_rbtree_free(ctx->remote.tree); - - SAFE_FREE(ctx->remote.root_perms); -} - -int csync_commit(CSYNC *ctx) { +int csync_s::reinitialize() { int rc = 0; - if (ctx == NULL) { - return -1; - } - - ctx->status_code = CSYNC_STATUS_OK; + status_code = CSYNC_STATUS_OK; - if (ctx->statedb.db != NULL - && csync_statedb_close(ctx) < 0) { + if (statedb.db != NULL + && csync_statedb_close(this) < 0) { CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "ERR: closing of statedb failed."); rc = -1; } - ctx->statedb.db = NULL; + statedb.db = NULL; + + /* destroy the rbtrees */ + if (c_rbtree_size(local.tree) > 0) { + c_rbtree_destroy(local.tree, _tree_destructor); + } - _csync_clean_ctx(ctx); + if (c_rbtree_size(remote.tree) > 0) { + c_rbtree_destroy(remote.tree, _tree_destructor); + } - ctx->remote.read_from_db = 0; - ctx->read_remote_from_db = true; - ctx->db_is_empty = false; + /* free memory */ + c_rbtree_free(local.tree); + c_rbtree_free(remote.tree); + remote.read_from_db = 0; + read_remote_from_db = true; + db_is_empty = false; /* Create new trees */ - c_rbtree_create(&ctx->local.tree, _key_cmp, _data_cmp); - c_rbtree_create(&ctx->remote.tree, _key_cmp, _data_cmp); - + c_rbtree_create(&local.tree, _key_cmp, _data_cmp); + c_rbtree_create(&remote.tree, _key_cmp, _data_cmp); - ctx->status = CSYNC_STATUS_INIT; - SAFE_FREE(ctx->error_string); + status = CSYNC_STATUS_INIT; + SAFE_FREE(error_string); rc = 0; return rc; } -int csync_destroy(CSYNC *ctx) { - int rc = 0; - - if (ctx == NULL) { - errno = EBADF; - return -1; - } - ctx->status_code = CSYNC_STATUS_OK; - - if (ctx->statedb.db != NULL - && csync_statedb_close(ctx) < 0) { +csync_s::~csync_s() { + if (statedb.db != NULL + && csync_statedb_close(this) < 0) { CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "ERR: closing of statedb failed."); - rc = -1; } - ctx->statedb.db = NULL; + statedb.db = NULL; - _csync_clean_ctx(ctx); + /* destroy the rbtrees */ + if (c_rbtree_size(local.tree) > 0) { + c_rbtree_destroy(local.tree, _tree_destructor); + } - SAFE_FREE(ctx->statedb.file); - SAFE_FREE(ctx->local.uri); - SAFE_FREE(ctx->error_string); + if (c_rbtree_size(remote.tree) > 0) { + c_rbtree_destroy(remote.tree, _tree_destructor); + } - SAFE_FREE(ctx); + /* free memory */ + c_rbtree_free(local.tree); + c_rbtree_free(remote.tree); - return rc; + SAFE_FREE(statedb.file); + SAFE_FREE(local.uri); + SAFE_FREE(error_string); } void *csync_get_userdata(CSYNC *ctx) { diff --git a/src/csync/csync.h b/src/csync/csync.h index b8d882ab8..e2a8bb62d 100644 --- a/src/csync/csync.h +++ b/src/csync/csync.h @@ -152,7 +152,7 @@ enum csync_ftw_type_e { typedef struct csync_file_stat_s csync_file_stat_t; -struct csync_file_stat_s { +struct OCSYNC_EXPORT csync_file_stat_s { uint64_t phash; time_t modtime; int64_t size; @@ -225,22 +225,6 @@ typedef int (*csync_vio_stat_hook) (csync_vio_handle_t *dhhandle, typedef QByteArray (*csync_checksum_hook)( const QByteArray &path, const QByteArray &otherChecksumHeader, void *userdata); -/** - * @brief Allocate a csync context. - * - * @param csync The context variable to allocate. - */ -void OCSYNC_EXPORT csync_create(CSYNC **csync, const char *local); - -/** - * @brief Initialize the file synchronizer. - * - * This function loads the configuration - * - * @param ctx The context to initialize. - */ -void OCSYNC_EXPORT csync_init(CSYNC *ctx, const char *db_file); - /** * @brief Update detection * @@ -259,26 +243,6 @@ int OCSYNC_EXPORT csync_update(CSYNC *ctx); */ int OCSYNC_EXPORT csync_reconcile(CSYNC *ctx); -/** - * @brief Re-initializes the csync context - * - * @param ctx The context to commit. - * - * @return 0 on success, less than 0 if an error occurred. - */ -int OCSYNC_EXPORT csync_commit(CSYNC *ctx); - -/** - * @brief Destroy the csync context - * - * frees the memory. - * - * @param ctx The context to destroy. - * - * @return 0 on success, less than 0 if an error occurred. - */ -int OCSYNC_EXPORT csync_destroy(CSYNC *ctx); - /** * @brief Get the userdata saved in the context. * diff --git a/src/csync/csync_private.h b/src/csync/csync_private.h index 3d0c30b6e..605e1b252 100644 --- a/src/csync/csync_private.h +++ b/src/csync/csync_private.h @@ -32,6 +32,7 @@ #ifndef _CSYNC_PRIVATE_H #define _CSYNC_PRIVATE_H +#include #include #include #include @@ -67,82 +68,89 @@ enum csync_replica_e { /** * @brief csync public structure */ -struct csync_s { +struct OCSYNC_EXPORT csync_s { struct { - csync_auth_callback auth_function; - void *userdata; - csync_update_callback update_callback; - void *update_callback_userdata; + csync_auth_callback auth_function = nullptr; + void *userdata = nullptr; + csync_update_callback update_callback = nullptr; + void *update_callback_userdata = nullptr; /* hooks for checking the white list (uses the update_callback_userdata) */ - int (*checkSelectiveSyncBlackListHook)(void*, const char*); - int (*checkSelectiveSyncNewFolderHook)(void*, const char* /* path */, const char* /* remotePerm */); + int (*checkSelectiveSyncBlackListHook)(void*, const char*) = nullptr; + int (*checkSelectiveSyncNewFolderHook)(void*, const char* /* path */, const char* /* remotePerm */) = nullptr; - csync_vio_opendir_hook remote_opendir_hook; - csync_vio_readdir_hook remote_readdir_hook; - csync_vio_closedir_hook remote_closedir_hook; - void *vio_userdata; + csync_vio_opendir_hook remote_opendir_hook = nullptr; + csync_vio_readdir_hook remote_readdir_hook = nullptr; + csync_vio_closedir_hook remote_closedir_hook = nullptr; + void *vio_userdata = nullptr; /* hook for comparing checksums of files during discovery */ - csync_checksum_hook checksum_hook; - void *checksum_userdata; + csync_checksum_hook checksum_hook = nullptr; + void *checksum_userdata = nullptr; } callbacks; - c_strlist_t *excludes; + c_strlist_t *excludes = nullptr; struct { - char *file; - sqlite3 *db; - int exists; + char *file = nullptr; + sqlite3 *db = nullptr; + bool exists = false; - sqlite3_stmt* by_hash_stmt; - sqlite3_stmt* by_fileid_stmt; - sqlite3_stmt* by_inode_stmt; + sqlite3_stmt* by_hash_stmt = nullptr; + sqlite3_stmt* by_fileid_stmt = nullptr; + sqlite3_stmt* by_inode_stmt = nullptr; int lastReturnValue; } statedb; struct { - char *uri; - c_rbtree_t *tree; + std::map folder_renamed_to; // map from->to + std::map folder_renamed_from; // map to->from + } renames; + + struct { + char *uri = nullptr; + c_rbtree_t *tree = nullptr; } local; struct { - c_rbtree_t *tree; - int read_from_db; - const char *root_perms; /* Permission of the root folder. (Since the root folder is not in the db tree, we need to keep a separate entry.) */ + c_rbtree_t *tree = nullptr; + bool read_from_db = false; + QByteArray root_perms; /* Permission of the root folder. (Since the root folder is not in the db tree, we need to keep a separate entry.) */ } remote; - /* replica we are currently walking */ - enum csync_replica_e current; + enum csync_replica_e current = LOCAL_REPLICA; /* Used in the update phase so changes in the sub directories can be notified to parent directories */ - csync_file_stat_t *current_fs; + csync_file_stat_t *current_fs = nullptr; /* csync error code */ - enum csync_status_codes_e status_code; + enum csync_status_codes_e status_code = CSYNC_STATUS_OK; - char *error_string; + char *error_string = nullptr; - int status; - volatile int abort; - void *rename_info; + int status = CSYNC_STATUS_INIT; + volatile bool abort = false; /** * Specify if it is allowed to read the remote tree from the DB (default to enabled) */ - bool read_remote_from_db; + bool read_remote_from_db = false; /** * If true, the DB is considered empty and all reads are skipped. (default is false) * This is useful during the initial local discovery as it speeds it up significantly. */ - bool db_is_empty; + bool db_is_empty = false; + + bool ignore_hidden_files = true; - bool ignore_hidden_files; + csync_s(const char *localUri, const char *db_file); + ~csync_s(); + int reinitialize(); }; /* diff --git a/src/csync/csync_rename.cpp b/src/csync/csync_rename.cpp index 1408e6783..5196020ee 100644 --- a/src/csync/csync_rename.cpp +++ b/src/csync/csync_rename.cpp @@ -21,7 +21,6 @@ #include "csync_private.h" #include "csync_rename.h" -#include #include #include #include @@ -33,36 +32,17 @@ static std::string _parentDir(const std::string &path) { return path.substr(0, len); } -struct csync_rename_s { - static csync_rename_s *get(CSYNC *ctx) { - if (!ctx->rename_info) { - ctx->rename_info = new csync_rename_s; - } - return reinterpret_cast(ctx->rename_info); - } - - std::map folder_renamed_to; // map from->to - std::map folder_renamed_from; // map to->from -}; - -void csync_rename_destroy(CSYNC* ctx) -{ - delete reinterpret_cast(ctx->rename_info); - ctx->rename_info = 0; -} - void csync_rename_record(CSYNC* ctx, const char* from, const char* to) { - csync_rename_s::get(ctx)->folder_renamed_to[from] = to; - csync_rename_s::get(ctx)->folder_renamed_from[to] = from; + ctx->renames.folder_renamed_to[from] = to; + ctx->renames.folder_renamed_from[to] = from; } char* csync_rename_adjust_path(CSYNC* ctx, const char* path) { - csync_rename_s* d = csync_rename_s::get(ctx); for (std::string p = _parentDir(path); !p.empty(); p = _parentDir(p)) { - std::map< std::string, std::string >::iterator it = d->folder_renamed_to.find(p); - if (it != d->folder_renamed_to.end()) { + std::map< std::string, std::string >::iterator it = ctx->renames.folder_renamed_to.find(p); + if (it != ctx->renames.folder_renamed_to.end()) { std::string rep = it->second + (path + p.length()); return c_strdup(rep.c_str()); } @@ -72,10 +52,9 @@ char* csync_rename_adjust_path(CSYNC* ctx, const char* path) char* csync_rename_adjust_path_source(CSYNC* ctx, const char* path) { - csync_rename_s* d = csync_rename_s::get(ctx); for (std::string p = _parentDir(path); !p.empty(); p = _parentDir(p)) { - std::map< std::string, std::string >::iterator it = d->folder_renamed_from.find(p); - if (it != d->folder_renamed_from.end()) { + std::map< std::string, std::string >::iterator it = ctx->renames.folder_renamed_from.find(p); + if (it != ctx->renames.folder_renamed_from.end()) { std::string rep = it->second + (path + p.length()); return c_strdup(rep.c_str()); } @@ -84,6 +63,5 @@ char* csync_rename_adjust_path_source(CSYNC* ctx, const char* path) } bool csync_rename_count(CSYNC *ctx) { - csync_rename_s* d = csync_rename_s::get(ctx); - return d->folder_renamed_from.size(); + return ctx->renames.folder_renamed_from.size(); } diff --git a/src/csync/csync_rename.h b/src/csync/csync_rename.h index 7702a1376..509b78afc 100644 --- a/src/csync/csync_rename.h +++ b/src/csync/csync_rename.h @@ -26,7 +26,6 @@ char OCSYNC_EXPORT *csync_rename_adjust_path(CSYNC *ctx, const char *path); /* Return the source of a given path in case of renames */ char OCSYNC_EXPORT *csync_rename_adjust_path_source(CSYNC *ctx, const char *path); -void OCSYNC_EXPORT csync_rename_destroy(CSYNC *ctx); void OCSYNC_EXPORT csync_rename_record(CSYNC *ctx, const char *from, const char *to); /* Return the amount of renamed item recorded */ bool OCSYNC_EXPORT csync_rename_count(CSYNC *ctx); diff --git a/src/libsync/discoveryphase.cpp b/src/libsync/discoveryphase.cpp index 3ba03cac9..db846e691 100644 --- a/src/libsync/discoveryphase.cpp +++ b/src/libsync/discoveryphase.cpp @@ -561,9 +561,9 @@ void DiscoveryMainThread::singleDirectoryJobFinishedWithErrorSlot(int csyncErrno void DiscoveryMainThread::singleDirectoryJobFirstDirectoryPermissionsSlot(const QString &p) { // Should be thread safe since the sync thread is blocked - if (!_discoveryJob->_csync_ctx->remote.root_perms) { + if (_discoveryJob->_csync_ctx->remote.root_perms.isEmpty()) { qCDebug(lcDiscovery) << "Permissions for root dir:" << p; - _discoveryJob->_csync_ctx->remote.root_perms = strdup(p.toUtf8()); + _discoveryJob->_csync_ctx->remote.root_perms = p.toUtf8(); } } diff --git a/src/libsync/syncengine.cpp b/src/libsync/syncengine.cpp index dac3cde37..6483ca435 100644 --- a/src/libsync/syncengine.cpp +++ b/src/libsync/syncengine.cpp @@ -87,10 +87,8 @@ SyncEngine::SyncEngine(AccountPtr account, const QString &localPath, // Everything in the SyncEngine expects a trailing slash for the localPath. ASSERT(localPath.endsWith(QLatin1Char('/'))); - csync_create(&_csync_ctx, localPath.toUtf8().data()); - const QString dbFile = _journal->databaseFilePath(); - csync_init(_csync_ctx, dbFile.toUtf8().data()); + _csync_ctx.reset(new CSYNC(localPath.toUtf8().data(), dbFile.toUtf8().data())); _excludedFiles.reset(new ExcludedFiles(&_csync_ctx->excludes)); _syncFileStatusTracker.reset(new SyncFileStatusTracker(this)); @@ -108,7 +106,6 @@ SyncEngine::~SyncEngine() _thread.quit(); _thread.wait(); _excludedFiles.reset(); - csync_destroy(_csync_ctx); } //Convert an error code from csync to a user readable string. @@ -786,7 +783,7 @@ void SyncEngine::startSync() _syncItemMap.clear(); _needsUpdate = false; - csync_resume(_csync_ctx); + csync_resume(_csync_ctx.data()); int fileRecordCount = -1; if (!_journal->exists()) { @@ -831,7 +828,7 @@ void SyncEngine::startSync() finalize(false); return; } - csync_set_userdata(_csync_ctx, this); + csync_set_userdata(_csync_ctx.data(), this); // Set up checksumming hook _csync_ctx->callbacks.checksum_hook = &CSyncChecksumHook::hook; @@ -861,7 +858,7 @@ void SyncEngine::startSync() connect(_discoveryMainThread, SIGNAL(etagConcatenation(QString)), this, SLOT(slotRootEtagReceived(QString))); } - DiscoveryJob *discoveryJob = new DiscoveryJob(_csync_ctx); + DiscoveryJob *discoveryJob = new DiscoveryJob(_csync_ctx.data()); discoveryJob->_selectiveSyncBlackList = selectiveSyncBlackList; discoveryJob->_selectiveSyncWhiteList = _journal->getSelectiveSyncList(SyncJournalDb::SelectiveSyncWhiteList, &ok); @@ -909,7 +906,7 @@ void SyncEngine::slotRootEtagReceived(const QString &e) void SyncEngine::slotDiscoveryJobFinished(int discoveryResult) { if (discoveryResult < 0) { - handleSyncError(_csync_ctx, "csync_update"); + handleSyncError(_csync_ctx.data(), "csync_update"); return; } qCInfo(lcEngine) << "#### Discovery end #################################################### " << _stopWatch.addLapTime(QLatin1String("Discovery Finished")) << "ms"; @@ -929,8 +926,8 @@ void SyncEngine::slotDiscoveryJobFinished(int discoveryResult) _progressInfo->_status = ProgressInfo::Reconcile; emit transmissionProgress(*_progressInfo); - if (csync_reconcile(_csync_ctx) < 0) { - handleSyncError(_csync_ctx, "csync_reconcile"); + if (csync_reconcile(_csync_ctx.data()) < 0) { + handleSyncError(_csync_ctx.data(), "csync_reconcile"); return; } @@ -947,21 +944,21 @@ void SyncEngine::slotDiscoveryJobFinished(int discoveryResult) _temporarilyUnavailablePaths.clear(); _renamedFolders.clear(); - if (csync_walk_local_tree(_csync_ctx, &treewalkLocal, 0) < 0) { + if (csync_walk_local_tree(_csync_ctx.data(), &treewalkLocal, 0) < 0) { qCWarning(lcEngine) << "Error in local treewalk."; walkOk = false; } - if (walkOk && csync_walk_remote_tree(_csync_ctx, &treewalkRemote, 0) < 0) { + if (walkOk && csync_walk_remote_tree(_csync_ctx.data(), &treewalkRemote, 0) < 0) { qCWarning(lcEngine) << "Error in remote treewalk."; } - if (_csync_ctx->remote.root_perms) { + if (!_csync_ctx->remote.root_perms.isEmpty()) { _remotePerms[QLatin1String("")] = _csync_ctx->remote.root_perms; qCInfo(lcEngine) << "Permissions of the root folder: " << _remotePerms[QLatin1String("")]; } // Re-init the csync context to free memory - csync_commit(_csync_ctx); + _csync_ctx->reinitialize(); // The map was used for merging trees, convert it to a list: SyncFileItemVector syncItems = _syncItemMap.values().toVector(); @@ -1154,7 +1151,7 @@ void SyncEngine::finalize(bool success) _thread.quit(); _thread.wait(); - csync_commit(_csync_ctx); + _csync_ctx->reinitialize(); _journal->close(); qCInfo(lcEngine) << "CSync run took " << _stopWatch.addLapTime(QLatin1String("Sync Finished")) << "ms"; @@ -1564,7 +1561,7 @@ void SyncEngine::abort() qCInfo(lcEngine) << "Aborting sync"; // Sets a flag for the update phase - csync_request_abort(_csync_ctx); + csync_request_abort(_csync_ctx.data()); // Aborts the discovery phase job if (_discoveryMainThread) { diff --git a/src/libsync/syncengine.h b/src/libsync/syncengine.h index ec4d6712f..30de1f631 100644 --- a/src/libsync/syncengine.h +++ b/src/libsync/syncengine.h @@ -194,7 +194,7 @@ private: QMap _syncItemMap; AccountPtr _account; - CSYNC *_csync_ctx; + QScopedPointer _csync_ctx; bool _needsUpdate; bool _syncRunning; QString _localPath; diff --git a/test/csync/CMakeLists.txt b/test/csync/CMakeLists.txt index 6b26eb96c..a735f8a0b 100644 --- a/test/csync/CMakeLists.txt +++ b/test/csync/CMakeLists.txt @@ -31,7 +31,6 @@ add_cmocka_test(check_std_c_time std_tests/check_std_c_time.c ${TEST_TARGET_LIBR # This will be rewritten soon anyway. #add_cmocka_test(check_logger log_tests/check_log.cpp ${TEST_TARGET_LIBRARIES}) -add_cmocka_test(check_csync_create csync_tests/check_csync_create.cpp ${TEST_TARGET_LIBRARIES}) add_cmocka_test(check_csync_log csync_tests/check_csync_log.cpp ${TEST_TARGET_LIBRARIES}) add_cmocka_test(check_csync_exclude csync_tests/check_csync_exclude.cpp ${TEST_TARGET_LIBRARIES}) add_cmocka_test(check_csync_statedb_load csync_tests/check_csync_statedb_load.cpp ${TEST_TARGET_LIBRARIES}) @@ -39,9 +38,7 @@ add_cmocka_test(check_csync_util csync_tests/check_csync_util.cpp ${TEST_TARGET_ add_cmocka_test(check_csync_misc csync_tests/check_csync_misc.cpp ${TEST_TARGET_LIBRARIES}) # csync tests which require init -add_cmocka_test(check_csync_init csync_tests/check_csync_init.cpp ${TEST_TARGET_LIBRARIES}) add_cmocka_test(check_csync_statedb_query csync_tests/check_csync_statedb_query.cpp ${TEST_TARGET_LIBRARIES}) -add_cmocka_test(check_csync_commit csync_tests/check_csync_commit.cpp ${TEST_TARGET_LIBRARIES}) # vio add_cmocka_test(check_vio vio_tests/check_vio.cpp ${TEST_TARGET_LIBRARIES}) diff --git a/test/csync/csync_tests/check_csync_commit.cpp b/test/csync/csync_tests/check_csync_commit.cpp deleted file mode 100644 index b3d77913b..000000000 --- a/test/csync/csync_tests/check_csync_commit.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - * libcsync -- a library to sync a directory with another - * - * Copyright (c) 2008-2013 by Andreas Schneider - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ -#include - -#include "torture.h" - -#include "csync_private.h" - -static int setup(void **state) { - CSYNC *csync; - int rc; - - rc = system("mkdir -p /tmp/check_csync1"); - assert_int_equal(rc, 0); - - csync_create(&csync, "/tmp/check_csync1"); - - *state = csync; - - return 0; -} - -static int setup_module(void **state) { - CSYNC *csync; - int rc; - - rc = system("mkdir -p /tmp/check_csync1"); - assert_int_equal(rc, 0); - - csync_create(&csync, "/tmp/check_csync1"); - - csync_init(csync, "foo"); - *state = csync; - - return 0; -} - -static int teardown(void **state) { - CSYNC *csync = (CSYNC*)*state; - int rc; - - rc = csync_destroy(csync); - - rc = system("rm -rf /tmp/check_csync"); - assert_int_equal(rc, 0); - - rc = system("rm -rf /tmp/check_csync1"); - assert_int_equal(rc, 0); - - *state = NULL; - - return 0; -} - -static void check_csync_commit(void **state) -{ - CSYNC *csync = (CSYNC*)*state; - int rc; - - rc = csync_commit(csync); - assert_int_equal(rc, 0); - - assert_int_equal(csync->status & CSYNC_STATUS_INIT, 1); -} - -static void check_csync_commit_dummy(void **state) -{ - CSYNC *csync = (CSYNC*)*state; - int rc; - - rc = csync_commit(csync); - assert_int_equal(rc, 0); - - assert_int_equal(csync->status & CSYNC_STATUS_INIT, 1); - -} - -int torture_run_tests(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test_setup_teardown(check_csync_commit, setup, teardown), - cmocka_unit_test_setup_teardown(check_csync_commit_dummy, setup_module, teardown), - }; - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/csync/csync_tests/check_csync_create.cpp b/test/csync/csync_tests/check_csync_create.cpp deleted file mode 100644 index 082ef26b8..000000000 --- a/test/csync/csync_tests/check_csync_create.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * libcsync -- a library to sync a directory with another - * - * Copyright (c) 2008-2013 by Andreas Schneider - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ -#include -#include - -#include "torture.h" - -#include "csync_private.h" - - -static void check_csync_destroy_null(void **state) -{ - int rc; - - (void) state; /* unused */ - - rc = csync_destroy(NULL); - assert_int_equal(rc, -1); -} - -static void check_csync_create(void **state) -{ - CSYNC *csync; - int rc; - - (void) state; /* unused */ - - csync_create(&csync, "/tmp/csync1"); - - rc = csync_destroy(csync); - assert_int_equal(rc, 0); -} - -int torture_run_tests(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(check_csync_destroy_null), - cmocka_unit_test(check_csync_create), - }; - - return cmocka_run_group_tests(tests, NULL, NULL); -} - diff --git a/test/csync/csync_tests/check_csync_exclude.cpp b/test/csync/csync_tests/check_csync_exclude.cpp index 5841eddf0..91982118c 100644 --- a/test/csync/csync_tests/check_csync_exclude.cpp +++ b/test/csync/csync_tests/check_csync_exclude.cpp @@ -32,7 +32,7 @@ static int setup(void **state) { CSYNC *csync; - csync_create(&csync, "/tmp/check_csync1"); + csync = new CSYNC("/tmp/check_csync1", ""); *state = csync; return 0; @@ -42,7 +42,7 @@ static int setup_init(void **state) { CSYNC *csync; int rc; - csync_create(&csync, "/tmp/check_csync1"); + csync = new CSYNC("/tmp/check_csync1", ""); rc = csync_exclude_load(EXCLUDE_LIST_FILE, &(csync->excludes)); assert_int_equal(rc, 0); @@ -67,8 +67,7 @@ static int teardown(void **state) { CSYNC *csync = (CSYNC*)*state; int rc; - rc = csync_destroy(csync); - assert_int_equal(rc, 0); + delete csync; rc = system("rm -rf /tmp/check_csync1"); assert_int_equal(rc, 0); diff --git a/test/csync/csync_tests/check_csync_init.cpp b/test/csync/csync_tests/check_csync_init.cpp deleted file mode 100644 index c0b4cdd6b..000000000 --- a/test/csync/csync_tests/check_csync_init.cpp +++ /dev/null @@ -1,88 +0,0 @@ -/* - * libcsync -- a library to sync a directory with another - * - * Copyright (c) 2008-2013 by Andreas Schneider - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ -#include - -#include "torture.h" - -#include "csync_private.h" - -static int setup(void **state) { - CSYNC *csync; - int rc; - - rc = system("mkdir -p /tmp/check_csync1"); - assert_int_equal(rc, 0); - - csync_create(&csync, "/tmp/check_csync1"); - - *state = csync; - return 0; -} - -static int setup_module(void **state) { - CSYNC *csync; - int rc; - - rc = system("mkdir -p /tmp/check_csync1"); - assert_int_equal(rc, 0); - - csync_create(&csync, "/tmp/check_csync1"); - - *state = csync; - return 0; -} - -static int teardown(void **state) { - CSYNC *csync = (CSYNC*)*state; - int rc; - - rc = csync_destroy(csync); - - rc = system("rm -rf /tmp/check_csync"); - assert_int_equal(rc, 0); - - rc = system("rm -rf /tmp/check_csync1"); - assert_int_equal(rc, 0); - - *state = NULL; - - return 0; -} - -static void check_csync_init(void **state) -{ - CSYNC *csync = (CSYNC*)*state; - - csync_init(csync, ""); - - assert_int_equal(csync->status & CSYNC_STATUS_INIT, 1); - -} - -int torture_run_tests(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test_setup_teardown(check_csync_init, setup, teardown), - cmocka_unit_test_setup_teardown(check_csync_init, setup_module, teardown), - }; - - return cmocka_run_group_tests(tests, NULL, NULL); -} - diff --git a/test/csync/csync_tests/check_csync_log.cpp b/test/csync/csync_tests/check_csync_log.cpp index b7c30c5a0..7953da730 100644 --- a/test/csync/csync_tests/check_csync_log.cpp +++ b/test/csync/csync_tests/check_csync_log.cpp @@ -28,25 +28,19 @@ #include "std/c_utf8.h" static int setup(void **state) { - CSYNC *csync; int rc; rc = system("mkdir -p /tmp/check_csync1"); assert_int_equal(rc, 0); - csync_create(&csync, "/tmp/check_csync1"); + *state = NULL; - *state = csync; - return 0; } static int teardown(void **state) { - CSYNC *csync = (CSYNC*)*state; int rc; - rc = csync_destroy(csync); - rc = system("rm -rf /tmp/check_csync"); assert_int_equal(rc, 0); diff --git a/test/csync/csync_tests/check_csync_statedb_load.cpp b/test/csync/csync_tests/check_csync_statedb_load.cpp index a7bccd7ea..e496b8222 100644 --- a/test/csync/csync_tests/check_csync_statedb_load.cpp +++ b/test/csync/csync_tests/check_csync_statedb_load.cpp @@ -36,9 +36,7 @@ static int setup(void **state) { rc = system("mkdir -p /tmp/check_csync1"); assert_int_equal(rc, 0); - csync_create(&csync, "/tmp/check_csync1"); - - csync->statedb.file = c_strdup( TESTDB ); + csync = new CSYNC("/tmp/check_csync1", TESTDB); *state = csync; sqlite3 *db = NULL; @@ -55,8 +53,7 @@ static int teardown(void **state) { CSYNC *csync = (CSYNC*)*state; int rc; - rc = csync_destroy(csync); - assert_int_equal(rc, 0); + delete csync; rc = system("rm -rf /tmp/check_csync1"); assert_int_equal(rc, 0); diff --git a/test/csync/csync_tests/check_csync_statedb_query.cpp b/test/csync/csync_tests/check_csync_statedb_query.cpp index d856d94ab..17c2a73e8 100644 --- a/test/csync/csync_tests/check_csync_statedb_query.cpp +++ b/test/csync/csync_tests/check_csync_statedb_query.cpp @@ -38,8 +38,7 @@ static int setup(void **state) assert_int_equal(rc, 0); rc = system("mkdir -p /tmp/check_csync"); assert_int_equal(rc, 0); - csync_create(&csync, "/tmp/check_csync1"); - csync_init(csync, TESTDB); + csync = new CSYNC("/tmp/check_csync1", TESTDB); sqlite3 *db = NULL; rc = sqlite3_open_v2(TESTDB, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL); @@ -100,8 +99,7 @@ static int teardown(void **state) { CSYNC *csync = (CSYNC*)*state; int rc = 0; - rc = csync_destroy(csync); - assert_int_equal(rc, 0); + delete csync; rc = system("rm -rf /tmp/check_csync"); assert_int_equal(rc, 0); rc = system("rm -rf /tmp/check_csync1"); diff --git a/test/csync/csync_tests/check_csync_update.cpp b/test/csync/csync_tests/check_csync_update.cpp index 1b260ed8b..70624ec11 100644 --- a/test/csync/csync_tests/check_csync_update.cpp +++ b/test/csync/csync_tests/check_csync_update.cpp @@ -98,12 +98,11 @@ static int setup(void **state) assert_int_equal(rc, 0); rc = system("mkdir -p /tmp/check_csync1"); assert_int_equal(rc, 0); - csync_create(&csync, "/tmp/check_csync1"); - csync_init(csync, TESTDB); + csync = new CSYNC("/tmp/check_csync1", TESTDB); /* Create a new db with metadata */ sqlite3 *db; - csync->statedb.file = c_strdup(TESTDB); + // csync->statedb.file = c_strdup(TESTDB); rc = sqlite3_open(csync->statedb.file, &db); statedb_create_metadata_table(db); if( firstrun ) { @@ -129,8 +128,7 @@ static int setup_ftw(void **state) assert_int_equal(rc, 0); rc = system("mkdir -p /tmp/check_csync1"); assert_int_equal(rc, 0); - csync_create(&csync, "/tmp"); - csync_init(csync, TESTDB); + csync = new CSYNC("/tmp", TESTDB); sqlite3 *db = NULL; rc = sqlite3_open_v2(TESTDB, &db, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL); @@ -151,11 +149,9 @@ static int setup_ftw(void **state) static int teardown(void **state) { CSYNC *csync = (CSYNC*)*state; - int rc; unlink( csync->statedb.file); - rc = csync_destroy(csync); - assert_int_equal(rc, 0); + delete csync; *state = NULL; diff --git a/test/csync/vio_tests/check_vio.cpp b/test/csync/vio_tests/check_vio.cpp index 6f4f3a9b4..006e3bc5c 100644 --- a/test/csync/vio_tests/check_vio.cpp +++ b/test/csync/vio_tests/check_vio.cpp @@ -49,7 +49,7 @@ static int setup(void **state) rc = system("rm -rf /tmp/csync_test"); assert_int_equal(rc, 0); - csync_create(&csync, "/tmp/csync1"); + csync = new CSYNC("/tmp/check_csync1", ""); csync->current = LOCAL_REPLICA; @@ -78,8 +78,7 @@ static int teardown(void **state) { CSYNC *csync = (CSYNC*)*state; int rc; - rc = csync_destroy(csync); - assert_int_equal(rc, 0); + delete csync; rc = chdir(wd_buffer); assert_int_equal(rc, 0); diff --git a/test/csync/vio_tests/check_vio_ext.cpp b/test/csync/vio_tests/check_vio_ext.cpp index faf5804e7..10de6eb7d 100644 --- a/test/csync/vio_tests/check_vio_ext.cpp +++ b/test/csync/vio_tests/check_vio_ext.cpp @@ -97,7 +97,7 @@ static int setup_testenv(void **state) { statevar *mystate = (statevar*)malloc( sizeof(statevar) ); mystate->result = NULL; - csync_create(&(mystate->csync), "/tmp/csync1"); + mystate->csync = new CSYNC("/tmp/check_csync1", ""); mystate->csync->current = LOCAL_REPLICA; @@ -124,8 +124,7 @@ static int teardown(void **state) { output("================== Tearing down!\n"); - rc = csync_destroy(csync); - assert_int_equal(rc, 0); + delete csync; rc = _tchdir(wd_buffer); assert_int_equal(rc, 0); diff --git a/test/testcsyncsqlite.cpp b/test/testcsyncsqlite.cpp index fb2767816..4a470e7b0 100644 --- a/test/testcsyncsqlite.cpp +++ b/test/testcsyncsqlite.cpp @@ -14,22 +14,20 @@ class TestCSyncSqlite : public QObject Q_OBJECT private: - CSYNC _ctx; + CSYNC *_ctx; private slots: void initTestCase() { int rc; - memset(&_ctx, 0, sizeof(CSYNC)); - QString db = QCoreApplication::applicationDirPath() + "/test_journal.db"; - _ctx.statedb.file = c_strdup(db.toLocal8Bit()); + _ctx = new CSYNC("/tmp/check_csync1", db.toLocal8Bit()); - rc = csync_statedb_load((CSYNC*)(&_ctx), _ctx.statedb.file, &(_ctx.statedb.db)); + rc = csync_statedb_load(_ctx, _ctx->statedb.file, &(_ctx->statedb.db)); QVERIFY(rc == 0); } void testFullResult() { - std::unique_ptr st = csync_statedb_get_stat_by_hash((CSYNC*)(&_ctx), 2081025720555645157 ); + std::unique_ptr st = csync_statedb_get_stat_by_hash( _ctx, 2081025720555645157 ); QVERIFY(st.get()); QCOMPARE( QString::number(st->phash), QString::number(2081025720555645157) ); QCOMPARE( QString::fromUtf8(st->path), QLatin1String("test2/zu/zuzu") ); @@ -42,38 +40,39 @@ private slots: } void testByHash() { - std::unique_ptr st = csync_statedb_get_stat_by_hash((CSYNC*)(&_ctx), -7147279406142960289); + std::unique_ptr st = csync_statedb_get_stat_by_hash(_ctx, -7147279406142960289); QVERIFY(st.get()); QCOMPARE(QString::fromUtf8(st->path), QLatin1String("documents/c1")); - st = csync_statedb_get_stat_by_hash((CSYNC*)(&_ctx), 5426481156826978940); + st = csync_statedb_get_stat_by_hash(_ctx, 5426481156826978940); QVERIFY(st.get()); QCOMPARE(QString::fromUtf8(st->path), QLatin1String("documents/c1/c2")); } void testByInode() { - std::unique_ptr st = csync_statedb_get_stat_by_inode((CSYNC*)(&_ctx), 1709555); + std::unique_ptr st = csync_statedb_get_stat_by_inode(_ctx, 1709555); QVERIFY(st.get()); QCOMPARE(QString::fromUtf8(st->path), QLatin1String("test2/zu/zuzu/zuzuzu")); - st = csync_statedb_get_stat_by_inode((CSYNC*)(&_ctx), 1706571); + st = csync_statedb_get_stat_by_inode(_ctx, 1706571); QVERIFY(st.get()); QCOMPARE(QString::fromUtf8(st->path), QLatin1String("Shared/for_kf/a2")); } void testByFileId() { - std::unique_ptr st = csync_statedb_get_stat_by_file_id((CSYNC*)(&_ctx), "00000556525d5af3d9625"); + std::unique_ptr st = csync_statedb_get_stat_by_file_id(_ctx, "00000556525d5af3d9625"); QVERIFY(st.get()); QCOMPARE(QString::fromUtf8(st->path), QLatin1String("test2/zu")); - st = csync_statedb_get_stat_by_file_id((CSYNC*)(&_ctx), "-0000001525d5af3d9625"); + st = csync_statedb_get_stat_by_file_id(_ctx, "-0000001525d5af3d9625"); QVERIFY(st.get()); QCOMPARE(QString::fromUtf8(st->path), QLatin1String("Shared")); } void cleanupTestCase() { - SAFE_FREE(_ctx.statedb.file); - csync_statedb_close((CSYNC*)(&_ctx)); + csync_statedb_close(_ctx); + delete _ctx; + _ctx = nullptr; } };