mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-29 15:43:56 +00:00
Issue #69: replace sqlite3_bind_xxx() calls
Use the new sqlite_bind() and sqlite_prepare_bind() calls instead. Add cmp_sid_t() function, use it instead of memcmp(). Use alloca_tohex_sid_t() in preference to alloca_tohex_sid() when the argument is a sid_t.
This commit is contained in:
parent
7e3a552011
commit
41e18e587d
@ -22,6 +22,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
#include "str.h"
|
||||
#include <ctype.h>
|
||||
|
||||
int cmp_sid_t(const sid_t *a, const sid_t *b)
|
||||
{
|
||||
return memcmp(a, b, sizeof a->binary);
|
||||
}
|
||||
|
||||
int str_to_sid_t(sid_t *sid, const char *hex)
|
||||
{
|
||||
if (strcmp(hex, "broadcast") == 0) {
|
||||
|
94
meshms.c
94
meshms.c
@ -90,13 +90,14 @@ static int get_my_conversation_bundle(const sid_t *my_sid, rhizome_manifest *m)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct conversations *add_conv(struct conversations **conv, const sid_t *them){
|
||||
struct conversations **ptr=conv;
|
||||
static struct conversations *add_conv(struct conversations **conv, const sid_t *them)
|
||||
{
|
||||
struct conversations **ptr = conv;
|
||||
while(*ptr){
|
||||
int cmp = memcmp((*ptr)->them.binary, them, sizeof((*ptr)->them));
|
||||
if (cmp==0)
|
||||
int cmp = cmp_sid_t(&(*ptr)->them, them);
|
||||
if (cmp == 0)
|
||||
break;
|
||||
if (cmp<0)
|
||||
if (cmp < 0)
|
||||
ptr = &(*ptr)->_left;
|
||||
else
|
||||
ptr = &(*ptr)->_right;
|
||||
@ -104,38 +105,34 @@ static struct conversations *add_conv(struct conversations **conv, const sid_t *
|
||||
if (!*ptr){
|
||||
*ptr = emalloc_zero(sizeof(struct conversations));
|
||||
if (*ptr)
|
||||
memcpy((*ptr)->them.binary, them->binary, sizeof((*ptr)->them));
|
||||
(*ptr)->them = *them;
|
||||
}
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
// find matching conversations
|
||||
// if their_sid_hex == my_sid_hex, return all conversations with any recipient
|
||||
static int get_database_conversations(const sid_t *my_sid, const sid_t *their_sid, struct conversations **conv){
|
||||
const char *my_sid_hex = alloca_tohex_sid(my_sid->binary);
|
||||
const char *their_sid_hex = alloca_tohex_sid(their_sid?their_sid->binary:my_sid->binary);
|
||||
|
||||
// if their_sid == my_sid, return all conversations with any recipient
|
||||
static int get_database_conversations(const sid_t *my_sid, const sid_t *their_sid, struct conversations **conv)
|
||||
{
|
||||
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
||||
sqlite3_stmt *statement = sqlite_prepare(&retry,
|
||||
"SELECT id, version, filesize, tail, sender, recipient "
|
||||
"FROM manifests "
|
||||
"WHERE service = '"RHIZOME_SERVICE_MESHMS2"' "
|
||||
"AND (sender=?1 or recipient=?1) "
|
||||
"AND (sender=?2 or recipient=?2)");
|
||||
sqlite3_stmt *statement = sqlite_prepare_bind(&retry,
|
||||
"SELECT id, version, filesize, tail, sender, recipient"
|
||||
" FROM manifests"
|
||||
" WHERE service = ?3"
|
||||
" AND (sender=?1 or recipient=?1)"
|
||||
" AND (sender=?2 or recipient=?2)",
|
||||
SID_T, my_sid,
|
||||
SID_T, their_sid ? their_sid : my_sid,
|
||||
STATIC_TEXT, RHIZOME_SERVICE_MESHMS2,
|
||||
END
|
||||
);
|
||||
if (!statement)
|
||||
return -1;
|
||||
|
||||
int ret = sqlite3_bind_text(statement, 1, my_sid_hex, -1, SQLITE_STATIC);
|
||||
if (ret!=SQLITE_OK)
|
||||
goto end;
|
||||
|
||||
ret = sqlite3_bind_text(statement, 2, their_sid_hex, -1, SQLITE_STATIC);
|
||||
if (ret!=SQLITE_OK)
|
||||
goto end;
|
||||
|
||||
if (config.debug.meshms)
|
||||
if (config.debug.meshms) {
|
||||
const char *my_sid_hex = alloca_tohex_sid_t(*my_sid);
|
||||
const char *their_sid_hex = alloca_tohex_sid_t(*(their_sid ? their_sid : my_sid));
|
||||
DEBUGF("Looking for conversations for %s, %s", my_sid_hex, their_sid_hex);
|
||||
|
||||
}
|
||||
while (sqlite_step_retry(&retry, statement) == SQLITE_ROW) {
|
||||
const char *id = (const char *)sqlite3_column_text(statement, 0);
|
||||
long long version = sqlite3_column_int64(statement, 1);
|
||||
@ -143,21 +140,24 @@ static int get_database_conversations(const sid_t *my_sid, const sid_t *their_si
|
||||
long long tail = sqlite3_column_int64(statement, 3);
|
||||
const char *sender = (const char *)sqlite3_column_text(statement, 4);
|
||||
const char *recipient = (const char *)sqlite3_column_text(statement, 5);
|
||||
const char *them = recipient;
|
||||
|
||||
if (strcasecmp(them, my_sid_hex)==0)
|
||||
them=sender;
|
||||
|
||||
sid_t their_sid;
|
||||
fromhex(their_sid.binary, them, sizeof(their_sid));
|
||||
|
||||
if (config.debug.meshms)
|
||||
DEBUGF("found id %s, sender %s, recipient %s", id, sender, recipient);
|
||||
|
||||
const char *them = recipient;
|
||||
sid_t their_sid;
|
||||
if (str_to_sid_t(&their_sid, them) == -1) {
|
||||
WHYF("invalid SID hex: %s -- skipping", alloca_str_toprint(them));
|
||||
continue;
|
||||
}
|
||||
if (cmp_sid_t(&their_sid, my_sid) == 0) {
|
||||
them = sender;
|
||||
if (str_to_sid_t(&their_sid, them) == -1) {
|
||||
WHYF("invalid SID hex: %s -- skipping", alloca_str_toprint(them));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
struct conversations *ptr = add_conv(conv, &their_sid);
|
||||
if (!ptr)
|
||||
goto end;
|
||||
|
||||
break;
|
||||
struct ply *p;
|
||||
if (them==sender){
|
||||
ptr->found_their_ply=1;
|
||||
@ -171,15 +171,8 @@ static int get_database_conversations(const sid_t *my_sid, const sid_t *their_si
|
||||
p->tail = tail;
|
||||
p->size = size;
|
||||
}
|
||||
|
||||
end:
|
||||
if (ret!=SQLITE_OK){
|
||||
WHYF("Query failed: %s", sqlite3_errmsg(rhizome_db));
|
||||
free_conversations(*conv);
|
||||
*conv=NULL;
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
return (ret==SQLITE_OK)?0:-1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct conversations * find_or_create_conv(const sid_t *my_sid, const sid_t *their_sid){
|
||||
@ -464,7 +457,8 @@ static int update_conversations(const sid_t *my_sid, struct conversations *conv)
|
||||
|
||||
// read our cached conversation list from our rhizome payload
|
||||
// if we can't load the existing data correctly, just ignore it.
|
||||
static int read_known_conversations(rhizome_manifest *m, const sid_t *their_sid, struct conversations **conv){
|
||||
static int read_known_conversations(rhizome_manifest *m, const sid_t *their_sid, struct conversations **conv)
|
||||
{
|
||||
if (m->haveSecret==NEW_BUNDLE_ID)
|
||||
return 0;
|
||||
|
||||
@ -491,7 +485,7 @@ static int read_known_conversations(rhizome_manifest *m, const sid_t *their_sid,
|
||||
break;
|
||||
if (config.debug.meshms)
|
||||
DEBUGF("Reading existing conversation for %s", alloca_tohex_sid(sid.binary));
|
||||
if (their_sid && memcmp(sid.binary, their_sid->binary, sizeof(sid)))
|
||||
if (their_sid && cmp_sid_t(&sid, their_sid) != 0)
|
||||
continue;
|
||||
struct conversations *ptr = add_conv(conv, &sid);
|
||||
if (!ptr)
|
||||
@ -881,7 +875,7 @@ end:
|
||||
static int mark_read(struct conversations *conv, const sid_t *their_sid, const char *offset_str){
|
||||
int ret=0;
|
||||
if (conv){
|
||||
int cmp = their_sid?memcmp(conv->them.binary, their_sid->binary, sizeof(sid_t)):0;
|
||||
int cmp = their_sid ? cmp_sid_t(&conv->them, their_sid) : 0;
|
||||
if (!their_sid || cmp<0){
|
||||
ret+=mark_read(conv->_left, their_sid, offset_str);
|
||||
}
|
||||
|
@ -344,6 +344,9 @@ int _sqlite_vexec_strbuf_retry(struct __sourceloc, sqlite_retry_state *retry, st
|
||||
#define sqlite_bind_loglevel(ll,rs,stmt,arg,...) _sqlite_bind(__WHENCE__, (ll), (rs), (stmt), arg, ##__VA_ARGS__)
|
||||
#define sqlite_retry(rs,action) _sqlite_retry(__WHENCE__, (rs), (action))
|
||||
#define sqlite_retry_done(rs,action) _sqlite_retry_done(__WHENCE__, (rs), (action))
|
||||
#define sqlite_exec(stmt) _sqlite_exec(__WHENCE__, LOG_LEVEL_ERROR, NULL, (stmt))
|
||||
#define sqlite_exec_retry(rs,stmt) _sqlite_exec(__WHENCE__, LOG_LEVEL_ERROR, (rs), (stmt))
|
||||
#define sqlite_exec_retry_loglevel(ll,rs,stmt) _sqlite_exec(__WHENCE__, (ll), (rs), (stmt))
|
||||
#define sqlite_step(stmt) _sqlite_step(__WHENCE__, LOG_LEVEL_ERROR, NULL, (stmt))
|
||||
#define sqlite_step_retry(rs,stmt) _sqlite_step(__WHENCE__, LOG_LEVEL_ERROR, (rs), (stmt))
|
||||
#define sqlite_exec_void(sql,arg,...) _sqlite_exec_void(__WHENCE__, LOG_LEVEL_ERROR, (sql), arg, ##__VA_ARGS__)
|
||||
|
@ -1613,11 +1613,13 @@ int rhizome_retrieve_manifest(const char *manifestid, rhizome_manifest *m)
|
||||
|
||||
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
||||
|
||||
sqlite3_stmt *statement = sqlite_prepare(&retry, "SELECT manifest, version, inserttime, author FROM manifests WHERE id like ?");
|
||||
sqlite3_stmt *statement = sqlite_prepare_bind(&retry,
|
||||
"SELECT manifest, version, inserttime, author FROM manifests WHERE id like ?",
|
||||
TEXT_TOUPPER, manifestid,
|
||||
END);
|
||||
if (!statement)
|
||||
return -1;
|
||||
|
||||
sqlite3_bind_text(statement, 1, manifestid, -1, SQLITE_STATIC);
|
||||
if (sqlite_step_retry(&retry, statement) == SQLITE_ROW){
|
||||
const char *manifestblob = (char *) sqlite3_column_blob(statement, 0);
|
||||
int64_t q_version = sqlite3_column_int64(statement, 1);
|
||||
@ -1651,10 +1653,12 @@ done:
|
||||
|
||||
static int rhizome_delete_manifest_retry(sqlite_retry_state *retry, const char *manifestid)
|
||||
{
|
||||
sqlite3_stmt *statement = sqlite_prepare(retry, "DELETE FROM manifests WHERE id = ?");
|
||||
sqlite3_stmt *statement = sqlite_prepare_bind(retry,
|
||||
"DELETE FROM manifests WHERE id = ?",
|
||||
TEXT_TOUPPER, manifestid,
|
||||
END);
|
||||
if (!statement)
|
||||
return -1;
|
||||
sqlite3_bind_text(statement, 1, manifestid, -1, SQLITE_STATIC);
|
||||
if (_sqlite_exec(__WHENCE__, LOG_LEVEL_ERROR, retry, statement) == -1)
|
||||
return -1;
|
||||
return sqlite3_changes(rhizome_db) ? 0 : 1;
|
||||
@ -1663,25 +1667,13 @@ static int rhizome_delete_manifest_retry(sqlite_retry_state *retry, const char *
|
||||
static int rhizome_delete_file_retry(sqlite_retry_state *retry, const char *fileid)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
rhizome_delete_external(fileid);
|
||||
|
||||
sqlite3_stmt *statement = sqlite_prepare(retry, "DELETE FROM files WHERE id = ?");
|
||||
if (!statement)
|
||||
sqlite3_stmt *statement = sqlite_prepare_bind(retry, "DELETE FROM files WHERE id = ?", TEXT_TOUPPER, fileid, END);
|
||||
if (!statement || sqlite_exec_retry(retry, statement) == -1)
|
||||
ret = -1;
|
||||
else {
|
||||
sqlite3_bind_text(statement, 1, fileid, -1, SQLITE_STATIC);
|
||||
if (_sqlite_exec(__WHENCE__, LOG_LEVEL_ERROR, retry, statement) == -1)
|
||||
ret = -1;
|
||||
}
|
||||
statement = sqlite_prepare(retry, "DELETE FROM fileblobs WHERE id = ?");
|
||||
if (!statement)
|
||||
statement = sqlite_prepare_bind(retry, "DELETE FROM fileblobs WHERE id = ?", TEXT_TOUPPER, fileid, END);
|
||||
if (!statement || sqlite_exec_retry(retry, statement) == -1)
|
||||
ret = -1;
|
||||
else {
|
||||
sqlite3_bind_text(statement, 1, fileid, -1, SQLITE_STATIC);
|
||||
if (_sqlite_exec(__WHENCE__, LOG_LEVEL_ERROR, retry, statement) == -1)
|
||||
ret = -1;
|
||||
}
|
||||
return ret == -1 ? -1 : sqlite3_changes(rhizome_db) ? 0 : 1;
|
||||
}
|
||||
|
||||
@ -1765,15 +1757,13 @@ static int is_interesting(const char *id_hex, int64_t version)
|
||||
|
||||
// do we have this bundle [or later]?
|
||||
sqlite_retry_state retry = SQLITE_RETRY_STATE_DEFAULT;
|
||||
sqlite3_stmt *statement = sqlite_prepare(&retry,
|
||||
"SELECT filehash FROM manifests WHERE id like ? and version >= ?");
|
||||
|
||||
sqlite3_stmt *statement = sqlite_prepare_bind(&retry,
|
||||
"SELECT filehash FROM manifests WHERE id like ? and version >= ?",
|
||||
TEXT_TOUPPER, id_hex,
|
||||
INT64, version,
|
||||
END);
|
||||
if (!statement)
|
||||
RETURN(-1);
|
||||
|
||||
sqlite3_bind_text(statement, 1, id_hex, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int64(statement, 2, version);
|
||||
|
||||
if (sqlite_step_retry(&retry, statement) == SQLITE_ROW){
|
||||
const char *q_filehash = (const char *) sqlite3_column_text(statement, 0);
|
||||
ret=0;
|
||||
@ -1781,7 +1771,6 @@ static int is_interesting(const char *id_hex, int64_t version)
|
||||
ret=1;
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
|
||||
RETURN(ret);
|
||||
OUT();
|
||||
}
|
||||
|
1
serval.h
1
serval.h
@ -552,6 +552,7 @@ typedef struct sid_binary {
|
||||
|
||||
#define alloca_tohex_sid_t(sid) alloca_tohex((sid).binary, sizeof (*(sid_t*)0).binary)
|
||||
|
||||
int cmp_sid_t(const sid_t *a, const sid_t *b);
|
||||
int str_to_sid_t(sid_t *sid, const char *hex);
|
||||
int strn_to_sid_t(sid_t *sid, const char *hex, const char **endp);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user