[Pkg-owncloud-commits] [owncloud-client] 154/211: CSync: statedb sanity check changes
Sandro Knauß
hefee-guest at moszumanska.debian.org
Sat Oct 25 09:10:40 UTC 2014
This is an automated email from the git hooks/post-receive script.
hefee-guest pushed a commit to branch master
in repository owncloud-client.
commit 61e72ef50b03f10216267b3efb4d42f8f5a46efd
Author: Markus Goetz <markus at woboq.com>
Date: Tue Oct 21 18:09:23 2014 +0200
CSync: statedb sanity check changes
---
csync/src/csync_statedb.c | 106 ++++++----------------------------------------
csync/src/csync_update.c | 5 ++-
2 files changed, 15 insertions(+), 96 deletions(-)
diff --git a/csync/src/csync_statedb.c b/csync/src/csync_statedb.c
index cda6e92..6f2e49a 100644
--- a/csync/src/csync_statedb.c
+++ b/csync/src/csync_statedb.c
@@ -91,88 +91,10 @@ static int _csync_check_db_integrity(sqlite3 *db) {
if( sqlite3_threadsafe() == 0 ) {
CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "* WARNING: SQLite module is not threadsafe!");
+ rc = -1;
}
return rc;
-
-}
-
-static int _csync_statedb_check(const char *statedb) {
- int fd = -1, rc;
- ssize_t r;
- char buf[BUF_SIZE] = {0};
- sqlite3 *db = NULL;
- csync_stat_t sb;
-
- mbchar_t *wstatedb = c_utf8_to_locale(statedb);
-
- if (wstatedb == NULL) {
- return -1;
- }
-
- /* check db version */
-#ifdef _WIN32
- _fmode = _O_BINARY;
-#endif
-
- fd = _topen(wstatedb, O_RDONLY);
-
- if (fd >= 0) {
- /* Check size. Size of zero is a valid database actually. */
- rc = _tfstat(fd, &sb);
-
- if (rc == 0) {
- if (sb.st_size == 0) {
- CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Database size is zero byte!");
- close(fd);
- } else {
- r = read(fd, (void *) buf, sizeof(buf) - 1);
- close(fd);
- if (r >= 0) {
- buf[BUF_SIZE - 1] = '\0';
- if (c_streq(buf, "SQLite format 3")) {
- if( sqlite_open(statedb, &db ) == SQLITE_OK ) {
- rc = _csync_check_db_integrity(db);
- if( sqlite3_close(db) != 0 ) {
- CSYNC_LOG(CSYNC_LOG_PRIORITY_NOTICE, "WARN: sqlite3_close error!");
- }
-
- if( rc >= 0 ) {
- /* everything is fine */
- c_free_locale_string(wstatedb);
- return 0;
- }
- CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "Integrity check failed!");
- } else {
- /* resources need to be freed even when open failed */
- sqlite3_close(db);
- CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "database corrupted, removing!");
- }
- } else {
- CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "sqlite version mismatch");
- }
- }
- }
- } else {
- close(fd);
- }
- /* if it comes here, the database is broken and should be recreated. */
- _tunlink(wstatedb);
- }
-
- c_free_locale_string(wstatedb);
-
- /* create database, use the original sqlite3_open function here as opening
- * read only is not sufficient because that does not create a new db but
- * bails out with error. */
- rc = sqlite3_open(statedb, &db);
- if (rc == SQLITE_OK) {
- sqlite3_close(db);
- return 1;
- }
- sqlite3_close(db);
- CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite3_open failed: %s %s", sqlite3_errmsg(db), statedb);
- return -1;
}
static int _csync_statedb_is_empty(sqlite3 *db) {
@@ -210,18 +132,7 @@ int csync_statedb_load(CSYNC *ctx, const char *statedb, sqlite3 **pdb) {
ctx->statedb.lastReturnValue = SQLITE_OK;
- /* 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 */
+ /* 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.",
@@ -231,9 +142,16 @@ int csync_statedb_load(CSYNC *ctx, const char *statedb, sqlite3 **pdb) {
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");
+ 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;
+ 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);
diff --git a/csync/src/csync_update.c b/csync/src/csync_update.c
index 61b31c3..84ff21c 100644
--- a/csync/src/csync_update.c
+++ b/csync/src/csync_update.c
@@ -375,8 +375,9 @@ static int _csync_detect_update(CSYNC *ctx, const char *file,
}
}
} else {
- CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Unable to open statedb, setting inst to NEW" );
- st->instruction = CSYNC_INSTRUCTION_NEW;
+ CSYNC_LOG(CSYNC_LOG_PRIORITY_DEBUG, "Unable to open statedb" );
+ ctx->status_code = CSYNC_STATUS_UNSUCCESSFUL;
+ return -1;
}
out:
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git
More information about the Pkg-owncloud-commits
mailing list