Пример #1
0
int csync_statedb_load(CSYNC *ctx, const char *statedb, sqlite3 **pdb) {
  int rc = -1;
  int check_rc = -1;
  c_strlist_t *result = NULL;
  sqlite3 *db = NULL;

  if( !ctx ) {
      return -1;
  }

  /* csync_statedb_check tries to open the statedb and creates it in case
   * its not there.
   */
  check_rc = _csync_statedb_check(statedb);
  if (check_rc < 0) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "ERR: checking csync database failed - bail out.");

    rc = -1;
    goto out;
  }

  /* Open or create the temporary database */
  if (sqlite3_open(statedb, &db) != SQLITE_OK) {
    const char *errmsg= sqlite3_errmsg(ctx->statedb.db);
    CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "ERR: Failed to sqlite3 open statedb - bail out: %s.",
              errmsg ? errmsg : "<no sqlite3 errormsg>");

    rc = -1;
    goto out;
  }

  /* If check_rc == 1 the database is new and empty as a result. */
  if ((check_rc == 1) || _csync_statedb_is_empty(db)) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "statedb doesn't exist");
    csync_set_statedb_exists(ctx, 0);
  } else {
    csync_set_statedb_exists(ctx, 1);
  }

  /* optimization for speeding up SQLite */
  result = csync_statedb_query(db, "PRAGMA synchronous = FULL;");
  c_strlist_destroy(result);
  result = csync_statedb_query(db, "PRAGMA case_sensitive_like = ON;");
  c_strlist_destroy(result);

#ifndef NDEBUG
  sqlite3_profile(db, sqlite_profile, 0 );
#endif
  *pdb = db;

  return 0;
out:
  sqlite3_close(db);
  return rc;
}
Пример #2
0
int csync_statedb_load(CSYNC *ctx, const char *statedb) {
  int rc = -1;
  c_strlist_t *result = NULL;
  char *statedb_tmp = NULL;

  if (_csync_statedb_check(statedb) < 0) {
    rc = -1;
    goto out;
  }

  /*
   * We want a two phase commit for the jounal, so we create a temporary copy
   * of the database.
   * The intention is that if something goes wrong we will not loose the
   * statedb.
   */
  if (asprintf(&statedb_tmp, "%s.ctmp", statedb) < 0) {
    rc = -1;
    goto out;
  }

  if (c_copy(statedb, statedb_tmp, 0644) < 0) {
    rc = -1;
    goto out;
  }

  /* Open the temporary database */
  if (sqlite3_open(statedb_tmp, &ctx->statedb.db) != SQLITE_OK) {
    rc = -1;
    goto out;
  }

  if (_csync_statedb_is_empty(ctx)) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "statedb doesn't exist");
    csync_set_statedb_exists(ctx, 0);
  } else {
    csync_set_statedb_exists(ctx, 1);
  }

  /* optimization for speeding up SQLite */
  result = csync_statedb_query(ctx, "PRAGMA default_synchronous = OFF;");
  c_strlist_destroy(result);

  rc = 0;
out:
  SAFE_FREE(statedb_tmp);
  return rc;
}
Пример #3
0
int csync_statedb_load(CSYNC *ctx, const char *statedb, sqlite3 **pdb) {
    int rc = -1;
    c_strlist_t *result = NULL;
    sqlite3 *db = NULL;

    if( !ctx ) {
        return -1;
    }

    if (ctx->statedb.db) {
        CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "ERR: DB already open");
        ctx->status_code = CSYNC_STATUS_PARAM_ERROR;
        return -1;
    }

    ctx->statedb.lastReturnValue = SQLITE_OK;

    /* Openthe database */
    if (sqlite_open(statedb, &db) != SQLITE_OK) {
        const char *errmsg= sqlite3_errmsg(ctx->statedb.db);
        CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "ERR: Failed to sqlite3 open statedb - bail out: %s.",
                  errmsg ? errmsg : "<no sqlite3 errormsg>");

        rc = -1;
        ctx->status_code = CSYNC_STATUS_STATEDB_LOAD_ERROR;
        goto out;
    }

    if (_csync_check_db_integrity(db) != 0) {
        const char *errmsg= sqlite3_errmsg(db);
        CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "ERR: sqlite3 integrity check failed - bail out: %s.",
                  errmsg ? errmsg : "<no sqlite3 errormsg>");
        rc = -1;
        ctx->status_code = CSYNC_STATUS_STATEDB_CORRUPTED;
        goto out;
    }

    if (_csync_statedb_is_empty(db)) {
        CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "statedb contents doesn't exist");
        csync_set_statedb_exists(ctx, 0);
    } else {
        csync_set_statedb_exists(ctx, 1);
    }

    /* Print out the version */
    //
    result = csync_statedb_query(db, "SELECT sqlite_version();");
    if (result && result->count >= 1) {
        CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "sqlite3 version \"%s\"", *result->vector);
    }
    c_strlist_destroy(result);

    /* optimization for speeding up SQLite */
    result = csync_statedb_query(db, "PRAGMA synchronous = NORMAL;");
    c_strlist_destroy(result);
    result = csync_statedb_query(db, "PRAGMA case_sensitive_like = ON;");
    c_strlist_destroy(result);

    /* set a busy handler with 5 seconds timeout */
    sqlite3_busy_timeout(db, 5000);

#ifndef NDEBUG
    sqlite3_profile(db, sqlite_profile, 0 );
#endif
    *pdb = db;

    CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "Success");

    return 0;
out:
    sqlite3_close(db);
    return rc;
}
Пример #4
0
int csync_statedb_load(CSYNC *ctx, const char *statedb, sqlite3 **pdb) {
  int rc = -1;
  int check_rc = -1;
  c_strlist_t *result = NULL;
  char *statedb_tmp = NULL;
  sqlite3 *db = NULL;

  /* csync_statedb_check tries to open the statedb and creates it in case
   * its not there.
   */
  check_rc = _csync_statedb_check(statedb);
  if (check_rc < 0) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "ERR: checking csync database failed - bail out.");

    rc = -1;
    goto out;
  }

  /*
   * We want a two phase commit for the jounal, so we create a temporary copy
   * of the database.
   * The intention is that if something goes wrong we will not loose the
   * statedb.
   */
  rc = asprintf(&statedb_tmp, "%s.ctmp", statedb);
  if (rc < 0) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "ERR: could not create statedb name - bail out.");
    rc = -1;
    goto out;
  }

  if (c_copy(statedb, statedb_tmp, 0644) < 0) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "ERR: Failed to copy statedb -> statedb_tmp - bail out.");

    rc = -1;
    goto out;
  }

  _csync_win32_hide_file( statedb_tmp );

  /* Open or create the temporary database */
  if (sqlite3_open(statedb_tmp, &db) != SQLITE_OK) {
    const char *errmsg= sqlite3_errmsg(ctx->statedb.db);
    CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "ERR: Failed to sqlite3 open statedb - bail out: %s.",
              errmsg ? errmsg : "<no sqlite3 errormsg>");

    rc = -1;
    goto out;
  }
  SAFE_FREE(statedb_tmp);

  /* If check_rc == 1 the database is new and empty as a result. */
  if ((check_rc == 1) || _csync_statedb_is_empty(db)) {
    CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "statedb doesn't exist");
    csync_set_statedb_exists(ctx, 0);
  } else {
    csync_set_statedb_exists(ctx, 1);
  }

  /* optimization for speeding up SQLite */
  result = csync_statedb_query(db, "PRAGMA synchronous = FULL;");
  c_strlist_destroy(result);
  result = csync_statedb_query(db, "PRAGMA case_sensitive_like = ON;");
  c_strlist_destroy(result);

#ifndef NDEBUG
  sqlite3_profile(db, sqlite_profile, 0 );
#endif
  *pdb = db;

  return 0;
out:
  sqlite3_close(db);
  SAFE_FREE(statedb_tmp);
  return rc;
}