static void _csync_exclude_add(CSYNC *ctx, const char *string) { if (ctx->excludes == NULL) { ctx->excludes = c_strlist_new(32); } if (ctx->excludes->count == ctx->excludes->size) { ctx->excludes = c_strlist_expand(ctx->excludes, 2 * ctx->excludes->size); } c_strlist_add(ctx->excludes, string); }
static int _csync_exclude_add(CSYNC *ctx, const char *string) { c_strlist_t *list; if (ctx->excludes == NULL) { ctx->excludes = c_strlist_new(32); if (ctx->excludes == NULL) { return -1; } } if (ctx->excludes->count == ctx->excludes->size) { list = c_strlist_expand(ctx->excludes, 2 * ctx->excludes->size); if (list == NULL) { return -1; } ctx->excludes = list; } return c_strlist_add(ctx->excludes, string); }
END_TEST START_TEST (check_c_strlist_expand) { size_t i = 0; c_strlist_t *strlist = NULL; strlist = c_strlist_new(42); fail_if(strlist == NULL, NULL); fail_unless(strlist->size == 42, NULL); fail_unless(strlist->count == 0, NULL); strlist = c_strlist_expand(strlist, 84); fail_if(strlist == NULL, NULL); fail_unless(strlist->size == 84, NULL); for (i = 0; i < strlist->size; i++) { fail_unless(c_strlist_add(strlist, (char *) "foobar") == 0, NULL); } c_strlist_destroy(strlist); }
END_TEST START_TEST (check_c_strlist_add) { size_t i = 0; c_strlist_t *strlist = NULL; strlist = c_strlist_new(42); fail_if(strlist == NULL, NULL); fail_unless(strlist->size == 42, NULL); fail_unless(strlist->count == 0, NULL); for (i = 0; i < strlist->size; i++) { fail_unless(c_strlist_add(strlist, (char *) "foobar") == 0, NULL); } fail_unless(strlist->count == 42, NULL); fail_unless(strcmp(strlist->vector[0], "foobar") == 0, NULL); fail_unless(strcmp(strlist->vector[41], "foobar") == 0, NULL); c_strlist_destroy(strlist); }
/* query the statedb, caller must free the memory */ c_strlist_t *csync_statedb_query(sqlite3 *db, const char *statement) { int err = SQLITE_OK; int rc = SQLITE_OK; 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; const char *field = NULL; c_strlist_t *result = NULL; int row = 0; 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: %zu", busy_count); } err = sqlite3_prepare(db, statement, -1, &stmt, &tail); } 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"); } CSYNC_LOG(CSYNC_LOG_PRIORITY_WARN, "sqlite3_compile error: %s - on query %s", sqlite3_errmsg(db), statement); 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); CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "sqlite3_step: BUSY counter: %zu", busy_count); continue; } if (err == SQLITE_MISUSE) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite3_step: MISUSE!!"); } if (err == SQLITE_DONE) { if (result == NULL) { result = c_strlist_new(1); } break; } if (err == SQLITE_ERROR) { break; } row++; if( result ) { result = c_strlist_expand(result, row*column_count); } else { result = c_strlist_new(column_count); } if (result == NULL) { return NULL; } /* iterate over columns */ for (i = 0; i < column_count; i++) { field = (const char *) sqlite3_column_text(stmt, i); if (!field) field = ""; // CSYNC_LOG(CSYNC_LOG_PRIORITY_TRACE, "sqlite3_column_text: %s", field); if (c_strlist_add(result, field) < 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) { CSYNC_LOG(CSYNC_LOG_PRIORITY_ERROR, "sqlite_step error: %s - on query: %s", sqlite3_errmsg(db), statement); if (result != NULL) { c_strlist_destroy(result); } return NULL; } 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); if (result != NULL) { c_strlist_destroy(result); } result = c_strlist_new(1); } } } } while (rc == SQLITE_SCHEMA && retry_count < 10); return result; }