Use a C++ contructor and destructor for CSYNC
authorJocelyn Turcotte <jturcotte@woboq.com>
Mon, 4 Sep 2017 17:06:13 +0000 (19:06 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Thu, 5 Oct 2017 20:01:04 +0000 (22:01 +0200)
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.

20 files changed:
src/csync/csync.cpp
src/csync/csync.h
src/csync/csync_private.h
src/csync/csync_rename.cpp
src/csync/csync_rename.h
src/libsync/discoveryphase.cpp
src/libsync/syncengine.cpp
src/libsync/syncengine.h
test/csync/CMakeLists.txt
test/csync/csync_tests/check_csync_commit.cpp [deleted file]
test/csync/csync_tests/check_csync_create.cpp [deleted file]
test/csync/csync_tests/check_csync_exclude.cpp
test/csync/csync_tests/check_csync_init.cpp [deleted file]
test/csync/csync_tests/check_csync_log.cpp
test/csync/csync_tests/check_csync_statedb_load.cpp
test/csync/csync_tests/check_csync_statedb_query.cpp
test/csync/csync_tests/check_csync_update.cpp
test/csync/vio_tests/check_vio.cpp
test/csync/vio_tests/check_vio_ext.cpp
test/testcsyncsqlite.cpp

index 0f028731daf5e277c86ecc3fa7fcfe5a1e2b9af4..b37e2bdcd8ccea58ee26a8be25cc63119b6da8a5 100644 (file)
@@ -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) {
index b8d882ab872e712732fbecfe680759600d1ae855..e2a8bb62d06d61d97f01b5c4eb01e1a86743a23c 100644 (file)
@@ -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.
  *
index 3d0c30b6e530d920eafbf78e48b31ede8ccb7ab1..605e1b2523c69c0481d1fbd68c7eddb279c5480f 100644 (file)
@@ -32,6 +32,7 @@
 #ifndef _CSYNC_PRIVATE_H
 #define _CSYNC_PRIVATE_H
 
+#include <map>
 #include <stdint.h>
 #include <stdbool.h>
 #include <sqlite3.h>
@@ -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<std::string, std::string> folder_renamed_to; // map from->to
+    std::map<std::string, std::string> 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();
 };
 
 /*
index 1408e67836d9b88953ba84458c0c62538d353531..5196020eed0c38ab4772d26222883975ac86a168 100644 (file)
@@ -21,7 +21,6 @@
 #include "csync_private.h"
 #include "csync_rename.h"
 
-#include <map>
 #include <string>
 #include <vector>
 #include <algorithm>
@@ -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<csync_rename_s *>(ctx->rename_info);
-    }
-
-    std::map<std::string, std::string> folder_renamed_to; // map from->to
-    std::map<std::string, std::string> folder_renamed_from; // map to->from
-};
-
-void csync_rename_destroy(CSYNC* ctx)
-{
-    delete reinterpret_cast<csync_rename_s *>(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();
 }
index 7702a137680fd0478d6c124d3bda8bad1a374cf8..509b78afcc61f78929647d0b7c29fa5178531a9d 100644 (file)
@@ -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);
index 3ba03cac94cb7c5f5324a4d422cc1a0b0500ef16..db846e69102ffe4904faca50c5ef1efbf623c4e1 100644 (file)
@@ -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();
     }
 }
 
index dac3cde37c90b2047d389e912cd71f5c0196a8d1..6483ca435c6e4f69f3ce5d073dc94f6e74758d16 100644 (file)
@@ -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) {
index ec4d6712ff84dbb25a7959fc5e42f4da1690be53..30de1f631c42aacb56db6930afaf362733d88526 100644 (file)
@@ -194,7 +194,7 @@ private:
     QMap<QString, SyncFileItemPtr> _syncItemMap;
 
     AccountPtr _account;
-    CSYNC *_csync_ctx;
+    QScopedPointer<CSYNC> _csync_ctx;
     bool _needsUpdate;
     bool _syncRunning;
     QString _localPath;
index 6b26eb96cd9e71499122186b05ec3b519011c557..a735f8a0b9e01723e7863c51fb42e3da4cdb9be7 100644 (file)
@@ -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 (file)
index b3d7791..0000000
+++ /dev/null
@@ -1,103 +0,0 @@
-/*
- * libcsync -- a library to sync a directory with another
- *
- * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
- *
- * 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 <string.h>
-
-#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 (file)
index 082ef26..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * libcsync -- a library to sync a directory with another
- *
- * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
- *
- * 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 <stdlib.h>
-#include <stdio.h>
-
-#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);
-}
-
index 5841eddf093a71ad04026a6670397539a8aaddf3..91982118c1d8a8af0571dc98767f7b5bff7d0909 100644 (file)
@@ -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 (file)
index c0b4cdd..0000000
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * libcsync -- a library to sync a directory with another
- *
- * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
- *
- * 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 <string.h>
-
-#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);
-}
-
index b7c30c5a088470136ae533e3537c8902ec0e8fb5..7953da73064a3c39ba0aa5ca7e979bcb34c3c31a 100644 (file)
 #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);
 
index a7bccd7eab89a5ccf9f732527b3fe3e96130637a..e496b8222313e94235f948a2f1bd44133ee4c2f1 100644 (file)
@@ -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);
index d856d94ab28514db50aea6d650448584def64d71..17c2a73e8ba2e781c4fa2ae389f35015cf2b9354 100644 (file)
@@ -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");
index 1b260ed8b69c6ee646710655b6eb1f1bb0108753..70624ec11df59b7667be6bdd9d05923e0942c592 100644 (file)
@@ -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;
     
index 6f4f3a9b48aba55ecc2757bf1ef8a6e29b0909cf..006e3bc5c35f3c2bb95dc9ee5b788016fd3f1864 100644 (file)
@@ -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);
index faf5804e77f68c14491ef21b98c158b611c010c0..10de6eb7d0e30d05f4f5e6eb8e12dc6e1f2a08cd 100644 (file)
@@ -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);
index fb2767816b4bf71a64b02552aa360f1a5241e1a8..4a470e7b06fbbc52ce921411436fef53e86da4e6 100644 (file)
@@ -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<csync_file_stat_t> st = csync_statedb_get_stat_by_hash((CSYNC*)(&_ctx), 2081025720555645157 );
+        std::unique_ptr<csync_file_stat_t> 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<csync_file_stat_t> st = csync_statedb_get_stat_by_hash((CSYNC*)(&_ctx), -7147279406142960289);
+        std::unique_ptr<csync_file_stat_t> 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<csync_file_stat_t> st = csync_statedb_get_stat_by_inode((CSYNC*)(&_ctx), 1709555);
+        std::unique_ptr<csync_file_stat_t> 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<csync_file_stat_t> st = csync_statedb_get_stat_by_file_id((CSYNC*)(&_ctx), "00000556525d5af3d9625");
+        std::unique_ptr<csync_file_stat_t> 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;
     }
 
 };