2008-02-27 20:56:47 +03:00
|
|
|
/*
|
|
|
|
* libcsync -- a library to sync a directory with another
|
|
|
|
*
|
|
|
|
* Copyright (c) 2008 by Andreas Schneider <mail@cynapses.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2012-03-02 19:47:34 +04:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-06-09 19:19:12 +04:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
#include <sqlite3.h>
|
2008-02-29 13:24:14 +03:00
|
|
|
#include <stdio.h>
|
2008-02-27 20:56:47 +03:00
|
|
|
#include <unistd.h>
|
2008-03-20 12:45:05 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2008-03-04 12:42:02 +03:00
|
|
|
#include "c_lib.h"
|
2008-02-27 20:56:47 +03:00
|
|
|
#include "csync_private.h"
|
2008-07-09 11:57:19 +04:00
|
|
|
#include "csync_statedb.h"
|
2008-06-28 19:18:10 +04:00
|
|
|
#include "csync_util.h"
|
2013-05-20 14:24:01 +04:00
|
|
|
#include "csync_time.h"
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
#define CSYNC_LOG_CATEGORY_NAME "csync.statedb"
|
2008-02-27 20:56:47 +03:00
|
|
|
#include "csync_log.h"
|
|
|
|
|
2008-05-06 16:31:36 +04:00
|
|
|
#define BUF_SIZE 16
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
void csync_set_statedb_exists(CSYNC *ctx, int val) {
|
|
|
|
ctx->statedb.exists = val;
|
2008-06-24 13:13:17 +04:00
|
|
|
}
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
int csync_get_statedb_exists(CSYNC *ctx) {
|
|
|
|
return ctx->statedb.exists;
|
2008-06-24 13:13:17 +04:00
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
static int _csync_check_db_integrity(CSYNC *ctx, sqlite3 *db) {
|
2013-06-07 18:08:45 +04:00
|
|
|
c_strlist_t *result = NULL;
|
|
|
|
int rc = -1;
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "PRAGMA quick_check;");
|
2013-06-07 18:08:45 +04:00
|
|
|
if (result != NULL) {
|
|
|
|
/* There is a result */
|
|
|
|
if (result->count > 0) {
|
|
|
|
if (c_streq(result->vector[0], "ok")) {
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _csync_statedb_check(CSYNC *ctx, const char *statedb) {
|
2012-10-29 13:40:43 +04:00
|
|
|
int fd = -1, rc;
|
2012-10-19 22:14:23 +04:00
|
|
|
ssize_t r;
|
2008-05-06 16:31:36 +04:00
|
|
|
char buf[BUF_SIZE] = {0};
|
2008-02-27 20:56:47 +03:00
|
|
|
sqlite3 *db = NULL;
|
|
|
|
|
2013-06-05 19:59:34 +04:00
|
|
|
mbchar_t *wstatedb = c_utf8_to_locale(statedb);
|
|
|
|
|
|
|
|
if (wstatedb == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
/* check db version */
|
2012-03-07 17:46:47 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
_fmode = _O_BINARY;
|
|
|
|
#endif
|
2013-06-05 19:59:34 +04:00
|
|
|
fd = _topen(wstatedb, O_RDONLY);
|
|
|
|
|
2008-03-20 12:45:05 +03:00
|
|
|
if (fd >= 0) {
|
2012-10-19 22:14:23 +04:00
|
|
|
r = read(fd, (void *) buf, sizeof(buf) - 1);
|
|
|
|
close(fd);
|
|
|
|
if (r >= 0) {
|
2008-05-07 13:29:48 +04:00
|
|
|
buf[BUF_SIZE - 1] = '\0';
|
2008-02-27 20:56:47 +03:00
|
|
|
if (c_streq(buf, "SQLite format 3")) {
|
2013-06-26 01:14:08 +04:00
|
|
|
if (sqlite3_open(statedb, &db ) == SQLITE_OK) {
|
|
|
|
rc = _csync_check_db_integrity(ctx, db);
|
2013-06-07 18:08:45 +04:00
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
sqlite3_close(db);
|
2013-06-05 19:59:34 +04:00
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
if(rc >= 0) {
|
2013-06-07 18:08:45 +04:00
|
|
|
/* everything is fine */
|
|
|
|
c_free_locale_string(wstatedb);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
} else {
|
2013-06-21 22:49:43 +04:00
|
|
|
/* resources need to be freed even when open failed */
|
2013-06-26 01:14:08 +04:00
|
|
|
sqlite3_close(db);
|
2008-02-27 20:56:47 +03:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "database corrupted, removing!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "sqlite version mismatch");
|
|
|
|
}
|
|
|
|
}
|
2013-06-05 19:59:34 +04:00
|
|
|
_tunlink(wstatedb);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2013-06-05 19:59:34 +04:00
|
|
|
c_free_locale_string(wstatedb);
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
/* create database */
|
2012-10-29 13:40:43 +04:00
|
|
|
rc = sqlite3_open(statedb, &db);
|
|
|
|
if (rc == SQLITE_OK) {
|
2008-02-27 20:56:47 +03:00
|
|
|
sqlite3_close(db);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-29 13:40:43 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "%s %s", sqlite3_errmsg(db), statedb);
|
2008-04-30 14:32:35 +04:00
|
|
|
sqlite3_close(db);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
static int _csync_statedb_is_empty(CSYNC *ctx, sqlite3 *db) {
|
2008-02-27 20:56:47 +03:00
|
|
|
c_strlist_t *result = NULL;
|
|
|
|
int rc = 0;
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "SELECT COUNT(phash) FROM metadata LIMIT 1 OFFSET 0;");
|
2008-04-29 13:41:16 +04:00
|
|
|
if (result == NULL) {
|
2008-02-27 20:56:47 +03:00
|
|
|
rc = 1;
|
|
|
|
}
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
int csync_statedb_load(CSYNC *ctx, const char *statedb, sqlite3 **pdb) {
|
2008-02-29 13:24:14 +03:00
|
|
|
int rc = -1;
|
2008-04-29 16:19:50 +04:00
|
|
|
c_strlist_t *result = NULL;
|
2008-07-09 12:10:00 +04:00
|
|
|
char *statedb_tmp = NULL;
|
2013-06-26 01:14:08 +04:00
|
|
|
sqlite3 *db;
|
2008-02-29 13:24:14 +03:00
|
|
|
|
2013-06-07 18:08:45 +04:00
|
|
|
if (_csync_statedb_check(ctx, statedb) < 0) {
|
2008-02-29 13:24:14 +03:00
|
|
|
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
|
2008-07-09 12:10:00 +04:00
|
|
|
* statedb.
|
2008-02-29 13:24:14 +03:00
|
|
|
*/
|
2008-07-09 12:10:00 +04:00
|
|
|
if (asprintf(&statedb_tmp, "%s.ctmp", statedb) < 0) {
|
2008-02-29 13:24:14 +03:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
if (c_copy(statedb, statedb_tmp, 0644) < 0) {
|
2008-02-29 13:24:14 +03:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2008-02-29 13:24:14 +03:00
|
|
|
/* Open the temporary database */
|
2013-06-26 01:14:08 +04:00
|
|
|
if (sqlite3_open(statedb_tmp, &db) != SQLITE_OK) {
|
2008-02-29 13:24:14 +03:00
|
|
|
rc = -1;
|
|
|
|
goto out;
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
2013-06-26 01:14:08 +04:00
|
|
|
SAFE_FREE(statedb_tmp);
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
if (_csync_statedb_is_empty(ctx, db)) {
|
2008-07-09 12:10:00 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "statedb doesn't exist");
|
|
|
|
csync_set_statedb_exists(ctx, 0);
|
2008-02-27 20:56:47 +03:00
|
|
|
} else {
|
2008-07-09 12:10:00 +04:00
|
|
|
csync_set_statedb_exists(ctx, 1);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2008-04-29 16:19:50 +04:00
|
|
|
/* optimization for speeding up SQLite */
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "PRAGMA default_synchronous = FULL;");
|
2008-04-29 16:19:50 +04:00
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
*pdb = db;
|
|
|
|
|
|
|
|
return 0;
|
2008-02-29 13:24:14 +03:00
|
|
|
out:
|
2013-06-26 01:14:08 +04:00
|
|
|
sqlite3_close(db);
|
2008-07-09 12:10:00 +04:00
|
|
|
SAFE_FREE(statedb_tmp);
|
2008-04-29 16:19:50 +04:00
|
|
|
return rc;
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
int csync_statedb_write(CSYNC *ctx, sqlite3 *db)
|
|
|
|
{
|
2013-05-20 14:24:01 +04:00
|
|
|
struct timespec start, finish;
|
2013-06-26 01:14:08 +04:00
|
|
|
int rc;
|
2013-05-20 14:24:01 +04:00
|
|
|
|
|
|
|
csync_gettime(&start);
|
2008-04-29 13:21:43 +04:00
|
|
|
/* drop tables */
|
2013-06-26 01:14:08 +04:00
|
|
|
rc = csync_statedb_drop_tables(ctx, db);
|
|
|
|
if (rc < 0) {
|
2008-04-29 13:21:43 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2013-05-20 14:24:01 +04:00
|
|
|
csync_gettime(&finish);
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
|
|
|
|
"## DROPPING tables took %.2f seconds",
|
|
|
|
c_secdiff(finish, start));
|
2008-04-29 13:21:43 +04:00
|
|
|
|
|
|
|
/* create tables */
|
2013-05-20 14:24:01 +04:00
|
|
|
csync_gettime(&start);
|
2013-06-26 01:14:08 +04:00
|
|
|
rc = csync_statedb_create_tables(ctx, db);
|
|
|
|
if (rc < 0) {
|
2008-04-29 13:21:43 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2013-05-20 14:24:01 +04:00
|
|
|
csync_gettime(&finish);
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
|
|
|
|
"## CREATE tables took %.2f seconds",
|
|
|
|
c_secdiff(finish, start));
|
2008-04-29 13:21:43 +04:00
|
|
|
|
|
|
|
/* insert metadata */
|
2013-05-20 14:24:01 +04:00
|
|
|
csync_gettime(&start);
|
2013-06-26 01:14:08 +04:00
|
|
|
rc = csync_statedb_insert_metadata(ctx, db);
|
|
|
|
if (rc < 0) {
|
2008-04-29 13:21:43 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2013-05-20 14:24:01 +04:00
|
|
|
csync_gettime(&finish);
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
|
|
|
|
"## INSERT took %.2f seconds",
|
|
|
|
c_secdiff(finish, start));
|
2008-04-29 13:21:43 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
int csync_statedb_close(CSYNC *ctx, const char *statedb, sqlite3 *db, int jwritten) {
|
2008-07-09 12:10:00 +04:00
|
|
|
char *statedb_tmp = NULL;
|
2008-04-29 13:21:43 +04:00
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
/* close the temporary database */
|
2013-06-26 01:14:08 +04:00
|
|
|
sqlite3_close(db);
|
2008-04-29 13:21:43 +04:00
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
if (asprintf(&statedb_tmp, "%s.ctmp", statedb) < 0) {
|
2008-05-20 18:14:14 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
/* if we successfully synchronized, overwrite the original statedb */
|
2008-04-29 13:21:43 +04:00
|
|
|
if (jwritten) {
|
2008-07-09 12:10:00 +04:00
|
|
|
rc = c_copy(statedb_tmp, statedb, 0644);
|
2008-04-29 13:21:43 +04:00
|
|
|
if (rc == 0) {
|
2008-07-09 12:10:00 +04:00
|
|
|
unlink(statedb_tmp);
|
2008-04-29 13:21:43 +04:00
|
|
|
}
|
2008-05-20 18:14:14 +04:00
|
|
|
} else {
|
2008-07-09 12:10:00 +04:00
|
|
|
unlink(statedb_tmp);
|
2008-04-29 13:21:43 +04:00
|
|
|
}
|
2008-07-09 12:10:00 +04:00
|
|
|
SAFE_FREE(statedb_tmp);
|
2008-04-29 13:21:43 +04:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
int csync_statedb_create_tables(CSYNC *ctx, sqlite3 *db) {
|
2008-04-29 11:24:28 +04:00
|
|
|
c_strlist_t *result = NULL;
|
|
|
|
|
2008-04-29 16:21:16 +04:00
|
|
|
/*
|
|
|
|
* Create temorary table to work on, this speeds up the
|
2013-06-07 18:08:45 +04:00
|
|
|
* creation of the statedb if we later just rename it to its
|
|
|
|
* final name metadata
|
2008-04-29 16:21:16 +04:00
|
|
|
*/
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db,
|
2013-03-13 20:04:22 +04:00
|
|
|
"CREATE TABLE IF NOT EXISTS metadata_temp("
|
2008-04-29 11:24:28 +04:00
|
|
|
"phash INTEGER(8),"
|
|
|
|
"pathlen INTEGER,"
|
2008-04-29 13:21:43 +04:00
|
|
|
"path VARCHAR(4096),"
|
2008-04-29 11:24:28 +04:00
|
|
|
"inode INTEGER,"
|
|
|
|
"uid INTEGER,"
|
|
|
|
"gid INTEGER,"
|
2008-04-29 16:21:16 +04:00
|
|
|
"mode INTEGER,"
|
2008-04-29 11:24:28 +04:00
|
|
|
"modtime INTEGER(8),"
|
|
|
|
"PRIMARY KEY(phash)"
|
2008-04-29 13:21:43 +04:00
|
|
|
");"
|
|
|
|
);
|
2008-04-29 11:24:28 +04:00
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-04-29 16:21:16 +04:00
|
|
|
c_strlist_destroy(result);
|
2013-06-07 18:09:07 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the 'real' table in case it does not exist. This is important
|
|
|
|
* for first time sync. Otherwise other functions that query metadata
|
|
|
|
* table whine about the table not existing.
|
|
|
|
*/
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db,
|
2013-06-07 18:09:07 +04:00
|
|
|
"CREATE TABLE IF NOT EXISTS metadata("
|
|
|
|
"phash INTEGER(8),"
|
|
|
|
"pathlen INTEGER,"
|
|
|
|
"path VARCHAR(4096),"
|
|
|
|
"inode INTEGER,"
|
|
|
|
"uid INTEGER,"
|
|
|
|
"gid INTEGER,"
|
|
|
|
"mode INTEGER,"
|
|
|
|
"modtime INTEGER(8),"
|
|
|
|
"PRIMARY KEY(phash)"
|
|
|
|
");"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
|
|
|
|
2008-04-29 11:24:28 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
int csync_statedb_drop_tables(CSYNC *ctx, sqlite3* db) {
|
2008-04-29 13:21:43 +04:00
|
|
|
c_strlist_t *result = NULL;
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db,
|
2013-05-20 14:24:01 +04:00
|
|
|
"DROP TABLE IF EXISTS metadata_temp;"
|
2008-04-29 13:21:43 +04:00
|
|
|
);
|
2008-04-30 12:39:08 +04:00
|
|
|
if (result == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-04-29 13:21:43 +04:00
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-18 12:43:57 +04:00
|
|
|
static int _insert_metadata_visitor(void *obj, void *data) {
|
2008-04-29 13:21:43 +04:00
|
|
|
csync_file_stat_t *fs = NULL;
|
|
|
|
int rc = -1;
|
2013-03-13 20:04:22 +04:00
|
|
|
sqlite3_stmt* stmt;
|
2008-04-29 13:21:43 +04:00
|
|
|
|
|
|
|
fs = (csync_file_stat_t *) obj;
|
2013-06-23 16:50:49 +04:00
|
|
|
stmt = (sqlite3_stmt *) data;
|
2013-03-13 20:04:22 +04:00
|
|
|
if (stmt == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-04-29 13:21:43 +04:00
|
|
|
|
2008-06-28 19:18:10 +04:00
|
|
|
switch (fs->instruction) {
|
2013-03-13 20:04:22 +04:00
|
|
|
/*
|
2008-07-09 12:10:00 +04:00
|
|
|
* Don't write ignored, deleted or files with an error to the statedb.
|
2008-06-28 19:18:10 +04:00
|
|
|
* They will be visited on the next synchronization again as a new file.
|
2008-06-27 19:58:32 +04:00
|
|
|
*/
|
2013-03-13 20:04:22 +04:00
|
|
|
case CSYNC_INSTRUCTION_DELETED:
|
|
|
|
case CSYNC_INSTRUCTION_IGNORE:
|
|
|
|
case CSYNC_INSTRUCTION_ERROR:
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
case CSYNC_INSTRUCTION_NONE:
|
2008-06-28 19:18:10 +04:00
|
|
|
/* As we only sync the local tree we need this flag here */
|
2013-03-13 20:04:22 +04:00
|
|
|
case CSYNC_INSTRUCTION_UPDATED:
|
|
|
|
case CSYNC_INSTRUCTION_CONFLICT:
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE,
|
|
|
|
"SQL statement: INSERT INTO metadata_temp \n"
|
|
|
|
"\t\t\t(phash, pathlen, path, inode, uid, gid, mode, modtime) VALUES \n"
|
|
|
|
"\t\t\t(%llu, %lu, %s, %llu, %u, %u, %u, %lu);",
|
|
|
|
(long long unsigned int) fs->phash,
|
|
|
|
(long unsigned int) fs->pathlen,
|
|
|
|
fs->path,
|
|
|
|
(long long unsigned int) fs->inode,
|
|
|
|
fs->uid,
|
|
|
|
fs->gid,
|
|
|
|
fs->mode,
|
|
|
|
fs->modtime);
|
|
|
|
|
|
|
|
/*
|
2008-06-28 19:18:10 +04:00
|
|
|
* The phash needs to be long long unsigned int or it segfaults on PPC
|
|
|
|
*/
|
2013-03-13 20:04:22 +04:00
|
|
|
sqlite3_bind_int64(stmt, 1, (long long signed int) fs->phash);
|
|
|
|
sqlite3_bind_int64(stmt, 2, (long unsigned int) fs->pathlen);
|
|
|
|
sqlite3_bind_text( stmt, 3, fs->path, fs->pathlen, SQLITE_STATIC);
|
|
|
|
sqlite3_bind_int64(stmt, 4, (long long signed int) fs->inode);
|
|
|
|
sqlite3_bind_int( stmt, 5, fs->uid);
|
|
|
|
sqlite3_bind_int( stmt, 6, fs->gid);
|
|
|
|
sqlite3_bind_int( stmt, 7, fs->mode);
|
|
|
|
sqlite3_bind_int64(stmt, 8, fs->modtime);
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
if (sqlite3_step(stmt) != SQLITE_DONE) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "sqlite insert failed!");
|
|
|
|
rc = -1;
|
|
|
|
}
|
2008-04-29 13:21:43 +04:00
|
|
|
|
2013-03-13 20:04:22 +04:00
|
|
|
sqlite3_reset(stmt);
|
2008-06-28 19:18:10 +04:00
|
|
|
|
2013-03-13 20:04:22 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN,
|
|
|
|
"file: %s, instruction: %s (%d), not added to statedb!",
|
|
|
|
fs->path, csync_instruction_str(fs->instruction), fs->instruction);
|
|
|
|
rc = 1;
|
|
|
|
break;
|
2008-06-18 11:56:08 +04:00
|
|
|
}
|
2008-04-29 13:21:43 +04:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
int csync_statedb_insert_metadata(CSYNC *ctx, sqlite3 *db) {
|
2008-04-29 16:21:16 +04:00
|
|
|
c_strlist_t *result = NULL;
|
2013-05-20 14:24:01 +04:00
|
|
|
struct timespec start, step1, step2, finish;
|
2013-06-26 01:14:08 +04:00
|
|
|
int rc;
|
2013-05-20 14:24:01 +04:00
|
|
|
|
2013-03-13 20:04:22 +04:00
|
|
|
char buffer[] = "INSERT INTO metadata_temp VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)";
|
|
|
|
sqlite3_stmt* stmt;
|
2008-04-29 16:21:16 +04:00
|
|
|
|
2013-05-20 14:24:01 +04:00
|
|
|
csync_gettime(&start);
|
2008-04-29 16:21:16 +04:00
|
|
|
|
2013-03-13 20:04:22 +04:00
|
|
|
/* prepare the INSERT statement */
|
2013-06-26 01:14:08 +04:00
|
|
|
rc = sqlite3_prepare_v2(db, buffer, strlen(buffer), &stmt, NULL);
|
|
|
|
if (rc != SQLITE_OK) {
|
2008-04-29 16:21:16 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-20 14:24:01 +04:00
|
|
|
/* Use transactions as that really speeds up processing */
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "BEGIN TRANSACTION;");
|
2013-05-20 14:24:01 +04:00
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2013-06-23 16:50:49 +04:00
|
|
|
if (c_rbtree_walk(ctx->local.tree, stmt, _insert_metadata_visitor) < 0) {
|
2013-05-20 14:24:01 +04:00
|
|
|
/* inserting failed. Drop the metadata_temp table. */
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "ROLLBACK TRANSACTION;");
|
2013-05-20 14:24:01 +04:00
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "DROP TABLE IF EXISTS metadata_temp;");
|
2013-05-20 14:24:01 +04:00
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2008-04-30 14:40:31 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "COMMIT TRANSACTION;");
|
2013-05-20 14:24:01 +04:00
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2013-03-13 20:04:22 +04:00
|
|
|
sqlite3_finalize(stmt);
|
2013-05-20 14:24:01 +04:00
|
|
|
csync_gettime(&step1);
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
|
|
|
|
"Transaction1 took %.2f seconds",
|
|
|
|
c_secdiff(step1, start));
|
|
|
|
|
|
|
|
/* Drop table metadata */
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "BEGIN TRANSACTION;");
|
2013-05-20 14:24:01 +04:00
|
|
|
c_strlist_destroy(result);
|
2013-03-13 20:04:22 +04:00
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "DROP TABLE IF EXISTS metadata;");
|
2013-03-13 20:04:22 +04:00
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2013-05-20 14:24:01 +04:00
|
|
|
/* Rename temp table to real table. */
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "ALTER TABLE metadata_temp RENAME TO metadata;");
|
2013-03-13 20:04:22 +04:00
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "COMMIT TRANSACTION;");
|
2013-05-20 14:24:01 +04:00
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
|
|
|
csync_gettime(&step2);
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
|
|
|
|
"ALTERING tables took %.2f seconds",
|
|
|
|
c_secdiff(step2, step1));
|
|
|
|
|
|
|
|
/* Recreate indices */
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "BEGIN TRANSACTION;");
|
2008-04-30 14:40:31 +04:00
|
|
|
c_strlist_destroy(result);
|
2008-04-29 16:21:16 +04:00
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db,
|
2013-05-20 14:24:01 +04:00
|
|
|
"CREATE INDEX IF NOT EXISTS metadata_phash ON metadata(phash);");
|
|
|
|
if (result == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db,
|
2013-05-20 14:24:01 +04:00
|
|
|
"CREATE INDEX IF NOT EXISTS metadata_inode ON metadata(inode);");
|
|
|
|
if (result == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, "COMMIT TRANSACTION;");
|
2013-05-20 14:24:01 +04:00
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
|
|
|
csync_gettime(&finish);
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG,
|
|
|
|
"INDEXING took %.2f seconds",
|
|
|
|
c_secdiff(finish, step2));
|
|
|
|
|
|
|
|
|
2008-04-29 16:21:16 +04:00
|
|
|
return 0;
|
2008-04-29 13:21:43 +04:00
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
|
2008-05-09 13:25:21 +04:00
|
|
|
/* caller must free the memory */
|
2013-06-26 01:14:08 +04:00
|
|
|
csync_file_stat_t *csync_statedb_get_stat_by_hash(CSYNC *ctx,
|
|
|
|
sqlite3 *db,
|
|
|
|
uint64_t phash)
|
|
|
|
{
|
2008-04-30 18:23:42 +04:00
|
|
|
csync_file_stat_t *st = NULL;
|
|
|
|
c_strlist_t *result = NULL;
|
|
|
|
char *stmt = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2013-05-06 18:27:11 +04:00
|
|
|
/* Use %lld instead of %llu otherwise there is an overflow in sqlite
|
|
|
|
* which only supports signed */
|
|
|
|
stmt = sqlite3_mprintf("SELECT * FROM metadata WHERE phash='%lld'",
|
|
|
|
(long long int) phash);
|
2008-04-30 18:23:42 +04:00
|
|
|
if (stmt == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, stmt);
|
2008-04-30 18:23:42 +04:00
|
|
|
sqlite3_free(stmt);
|
|
|
|
if (result == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result->count <= 6) {
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* phash, pathlen, path, inode, uid, gid, mode, modtime */
|
|
|
|
len = strlen(result->vector[2]);
|
|
|
|
st = c_malloc(sizeof(csync_file_stat_t) + len + 1);
|
|
|
|
if (st == NULL) {
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-03 11:46:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME:
|
|
|
|
* We use an INTEGER(8) which is signed to the phash in the sqlite3 db,
|
|
|
|
* but the phash is an uint64_t. So for some values we get a string like
|
|
|
|
* "1.66514565505016e+19". For such a string strtoull() returns 1.
|
|
|
|
* phash = 1
|
|
|
|
*
|
|
|
|
* st->phash = strtoull(result->vector[0], NULL, 10);
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* The query suceeded so use the phash we pass to the function. */
|
|
|
|
st->phash = phash;
|
|
|
|
|
2008-04-30 18:23:42 +04:00
|
|
|
st->pathlen = atoi(result->vector[1]);
|
|
|
|
memcpy(st->path, (len ? result->vector[2] : ""), len + 1);
|
|
|
|
st->inode = atoi(result->vector[3]);
|
|
|
|
st->uid = atoi(result->vector[4]);
|
|
|
|
st->gid = atoi(result->vector[5]);
|
|
|
|
st->mode = atoi(result->vector[6]);
|
|
|
|
st->modtime = strtoul(result->vector[7], NULL, 10);
|
|
|
|
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2008-05-09 13:25:21 +04:00
|
|
|
/* caller must free the memory */
|
2013-06-26 01:14:08 +04:00
|
|
|
csync_file_stat_t *csync_statedb_get_stat_by_inode(CSYNC *ctx,
|
|
|
|
sqlite3 *db,
|
|
|
|
ino_t inode) {
|
2008-04-30 18:23:42 +04:00
|
|
|
csync_file_stat_t *st = NULL;
|
|
|
|
c_strlist_t *result = NULL;
|
|
|
|
char *stmt = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2012-03-29 15:04:42 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* no idea about inodes. */
|
|
|
|
return st;
|
|
|
|
#endif
|
|
|
|
|
2013-06-10 18:08:14 +04:00
|
|
|
stmt = sqlite3_mprintf("SELECT * FROM metadata WHERE inode='%llu'",
|
|
|
|
(long long unsigned int)inode);
|
2008-04-30 18:23:42 +04:00
|
|
|
if (stmt == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
result = csync_statedb_query(ctx, db, stmt);
|
2008-04-30 18:23:42 +04:00
|
|
|
sqlite3_free(stmt);
|
|
|
|
if (result == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result->count <= 6) {
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* phash, pathlen, path, inode, uid, gid, mode, modtime */
|
|
|
|
len = strlen(result->vector[2]);
|
|
|
|
st = c_malloc(sizeof(csync_file_stat_t) + len + 1);
|
|
|
|
if (st == NULL) {
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
st->phash = strtoull(result->vector[0], NULL, 10);
|
|
|
|
st->pathlen = atoi(result->vector[1]);
|
|
|
|
memcpy(st->path, (len ? result->vector[2] : ""), len + 1);
|
|
|
|
st->inode = atoi(result->vector[3]);
|
|
|
|
st->uid = atoi(result->vector[4]);
|
|
|
|
st->gid = atoi(result->vector[5]);
|
|
|
|
st->mode = atoi(result->vector[6]);
|
|
|
|
st->modtime = strtoul(result->vector[7], NULL, 10);
|
|
|
|
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2008-07-09 12:10:00 +04:00
|
|
|
/* query the statedb, caller must free the memory */
|
2013-06-26 01:14:08 +04:00
|
|
|
c_strlist_t *csync_statedb_query(CSYNC *ctx,
|
|
|
|
sqlite3 *db,
|
|
|
|
const char *statement) {
|
2008-12-23 15:54:57 +03:00
|
|
|
int err = SQLITE_OK;
|
|
|
|
int rc = SQLITE_OK;
|
2008-02-27 20:56:47 +03:00
|
|
|
size_t i = 0;
|
|
|
|
size_t busy_count = 0;
|
|
|
|
size_t retry_count = 0;
|
|
|
|
size_t column_count = 0;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
const char *tail = NULL;
|
|
|
|
c_strlist_t *result = NULL;
|
|
|
|
|
|
|
|
do {
|
|
|
|
/* compile SQL program into a virtual machine, reattempteing if busy */
|
|
|
|
do {
|
|
|
|
if (busy_count) {
|
|
|
|
/* sleep 100 msec */
|
|
|
|
usleep(100000);
|
2008-06-27 20:52:09 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "sqlite3_prepare: BUSY counter: %zu", busy_count);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
2013-06-26 01:14:08 +04:00
|
|
|
err = sqlite3_prepare(db, statement, -1, &stmt, &tail);
|
2008-02-27 20:56:47 +03:00
|
|
|
} while (err == SQLITE_BUSY && busy_count ++ < 120);
|
|
|
|
|
|
|
|
if (err != SQLITE_OK) {
|
|
|
|
if (err == SQLITE_BUSY) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Gave up waiting for lock to clear");
|
|
|
|
}
|
2013-06-26 01:14:08 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN,
|
|
|
|
"sqlite3_compile error: %s - on query %s",
|
|
|
|
sqlite3_errmsg(db), statement);
|
2008-02-27 20:56:47 +03:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
busy_count = 0;
|
|
|
|
column_count = sqlite3_column_count(stmt);
|
|
|
|
|
|
|
|
/* execute virtual machine by iterating over rows */
|
|
|
|
for(;;) {
|
|
|
|
err = sqlite3_step(stmt);
|
|
|
|
|
|
|
|
if (err == SQLITE_BUSY) {
|
|
|
|
if (busy_count++ > 120) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Busy counter has reached its maximum. Aborting this sql statement");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* sleep 100 msec */
|
|
|
|
usleep(100000);
|
2008-06-27 20:52:09 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "sqlite3_step: BUSY counter: %zu", busy_count);
|
2008-02-27 20:56:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == SQLITE_MISUSE) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite3_step: MISUSE!!");
|
|
|
|
}
|
|
|
|
|
2008-04-29 13:41:16 +04:00
|
|
|
if (err == SQLITE_DONE) {
|
|
|
|
if (result == NULL) {
|
|
|
|
result = c_strlist_new(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == SQLITE_ERROR) {
|
2008-02-27 20:56:47 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-10-19 22:09:00 +04:00
|
|
|
/* If something went wrong make sure we don't leak memory */
|
|
|
|
if (result != NULL) {
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
result = c_strlist_new(column_count);
|
|
|
|
if (result == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate over columns */
|
|
|
|
for (i = 0; i < column_count; i++) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "sqlite3_column_text: %s", (char *) sqlite3_column_text(stmt, i));
|
|
|
|
if (c_strlist_add(result, (char *) sqlite3_column_text(stmt, i)) < 0) {
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} /* end infinite for loop */
|
|
|
|
|
|
|
|
/* deallocate vm resources */
|
|
|
|
rc = sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
if (err != SQLITE_DONE && rc != SQLITE_SCHEMA) {
|
2013-06-26 01:14:08 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite_step error: %s - on query: %s", sqlite3_errmsg(db), statement);
|
2012-10-19 22:09:00 +04:00
|
|
|
if (result != NULL) {
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
result = c_strlist_new(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == SQLITE_SCHEMA) {
|
|
|
|
retry_count ++;
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "SQLITE_SCHEMA error occurred on query: %s", statement);
|
|
|
|
if (retry_count < 10) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Retrying now.");
|
|
|
|
} else {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "RETRY count has reached its maximum. Aborting statement: %s", statement);
|
2012-10-19 22:09:00 +04:00
|
|
|
if (result != NULL) {
|
|
|
|
c_strlist_destroy(result);
|
|
|
|
}
|
2008-02-27 20:56:47 +03:00
|
|
|
result = c_strlist_new(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (rc == SQLITE_SCHEMA && retry_count < 10);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
int csync_statedb_insert(CSYNC *ctx, sqlite3 *db, const char *statement) {
|
2008-02-27 20:56:47 +03:00
|
|
|
int err;
|
|
|
|
int rc = 0;
|
|
|
|
int busy_count = 0;
|
|
|
|
int retry_count = 0;
|
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
const char *tail;
|
|
|
|
|
2008-03-04 13:29:43 +03:00
|
|
|
if (!statement[0]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-27 20:56:47 +03:00
|
|
|
do {
|
|
|
|
/* compile SQL program into a virtual machine, reattempteing if busy */
|
|
|
|
do {
|
|
|
|
if (busy_count) {
|
|
|
|
/* sleep 100 msec */
|
|
|
|
usleep(100000);
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "sqlite3_prepare: BUSY counter: %d", busy_count);
|
|
|
|
}
|
2013-06-26 01:14:08 +04:00
|
|
|
err = sqlite3_prepare(db, statement, -1, &stmt, &tail);
|
2008-02-27 20:56:47 +03:00
|
|
|
} while (err == SQLITE_BUSY && busy_count++ < 120);
|
|
|
|
|
|
|
|
if (err != SQLITE_OK) {
|
|
|
|
if (err == SQLITE_BUSY) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Gave up waiting for lock to clear");
|
|
|
|
}
|
2013-06-26 01:14:08 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite3_compile error: %s on query %s", sqlite3_errmsg(db), statement);
|
2008-02-27 20:56:47 +03:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
busy_count = 0;
|
|
|
|
|
|
|
|
/* execute virtual machine by iterating over rows */
|
|
|
|
for(;;) {
|
|
|
|
err = sqlite3_step(stmt);
|
|
|
|
|
|
|
|
if (err == SQLITE_BUSY) {
|
|
|
|
if (busy_count++ > 120) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Busy counter has reached its maximum. Aborting this sql statement");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* sleep 100 msec */
|
|
|
|
usleep(100000);
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "sqlite3_step: BUSY counter: %d", busy_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == SQLITE_MISUSE) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite3_step: MISUSE!!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == SQLITE_DONE || err == SQLITE_ERROR) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} /* end infinite for loop */
|
|
|
|
|
|
|
|
/* deallocate vm resources */
|
|
|
|
rc = sqlite3_finalize(stmt);
|
|
|
|
|
|
|
|
if (err != SQLITE_DONE && rc != SQLITE_SCHEMA) {
|
2013-06-26 01:14:08 +04:00
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR,
|
|
|
|
"sqlite_step error: %s on insert: %s",
|
|
|
|
sqlite3_errmsg(db), statement);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == SQLITE_SCHEMA) {
|
|
|
|
retry_count++;
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "SQLITE_SCHEMA error occurred on insert: %s", statement);
|
|
|
|
if (retry_count < 10) {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Retrying now.");
|
|
|
|
} else {
|
|
|
|
CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "RETRY count has reached its maximum. Aborting statement: %s", statement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (rc == SQLITE_SCHEMA && retry_count < 10);
|
|
|
|
|
2013-06-26 01:14:08 +04:00
|
|
|
return sqlite3_last_insert_rowid(db);
|
2008-02-27 20:56:47 +03:00
|
|
|
}
|
|
|
|
|
2009-05-13 12:12:07 +04:00
|
|
|
/* vim: set ts=8 sw=2 et cindent: */
|