forked from cory/tildefriends
Replace all printfs with tf_printf, which redirects to android logging. Change into the files directory so that sqlite can do its thing. Getting closer.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4203 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
77
src/ssb.db.c
77
src/ssb.db.c
@ -1,5 +1,6 @@
|
||||
#include "ssb.db.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "mem.h"
|
||||
#include "ssb.h"
|
||||
#include "trace.h"
|
||||
@ -20,7 +21,7 @@ static void _tf_ssb_db_exec(sqlite3* db, const char* statement)
|
||||
int result = sqlite3_exec(db, statement, NULL, NULL, &error);
|
||||
if (result != SQLITE_OK)
|
||||
{
|
||||
printf("Error running '%s': %s.\n", statement, error);
|
||||
tf_printf("Error running '%s': %s.\n", statement, error);
|
||||
abort();
|
||||
}
|
||||
}
|
||||
@ -38,14 +39,14 @@ static bool _tf_ssb_db_has_rows(sqlite3* db, const char* query)
|
||||
}
|
||||
if (result != SQLITE_DONE)
|
||||
{
|
||||
printf("%s\n", sqlite3_errmsg(db));
|
||||
tf_printf("%s\n", sqlite3_errmsg(db));
|
||||
abort();
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%s\n", sqlite3_errmsg(db));
|
||||
tf_printf("%s\n", sqlite3_errmsg(db));
|
||||
abort();
|
||||
}
|
||||
return found;
|
||||
@ -123,19 +124,19 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
|
||||
|
||||
if (!populate_fts && /* HACK */ false)
|
||||
{
|
||||
printf("Checking FTS5 integrity...\n");
|
||||
tf_printf("Checking FTS5 integrity...\n");
|
||||
if (sqlite3_exec(db, "INSERT INTO messages_fts(messages_fts, rank) VALUES ('integrity-check', 0)", NULL, NULL, NULL) == SQLITE_CORRUPT_VTAB)
|
||||
{
|
||||
populate_fts = true;
|
||||
}
|
||||
printf("Done.\n");
|
||||
tf_printf("Done.\n");
|
||||
}
|
||||
|
||||
if (populate_fts)
|
||||
{
|
||||
printf("Populating full-text search...\n");
|
||||
tf_printf("Populating full-text search...\n");
|
||||
_tf_ssb_db_exec(db, "INSERT INTO messages_fts (rowid, content) SELECT rowid, content FROM messages");
|
||||
printf("Done.\n");
|
||||
tf_printf("Done.\n");
|
||||
}
|
||||
|
||||
_tf_ssb_db_exec(db, "CREATE TRIGGER IF NOT EXISTS messages_ai AFTER INSERT ON messages BEGIN INSERT INTO messages_fts(rowid, content) VALUES (new.rowid, new.content); END");
|
||||
@ -149,14 +150,14 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
|
||||
" ref TEXT, "
|
||||
" UNIQUE(message, ref)"
|
||||
")");
|
||||
printf("Populating messages_refs...\n");
|
||||
tf_printf("Populating messages_refs...\n");
|
||||
_tf_ssb_db_exec(db, "INSERT INTO messages_refs(message, ref) "
|
||||
"SELECT messages.id, j.value FROM messages, json_tree(messages.content) as j WHERE "
|
||||
"j.value LIKE '&%.sha256' OR "
|
||||
"j.value LIKE '%%%.sha256' OR "
|
||||
"j.value LIKE '@%.ed25519' "
|
||||
"ON CONFLICT DO NOTHING");
|
||||
printf("Done.\n");
|
||||
tf_printf("Done.\n");
|
||||
}
|
||||
|
||||
_tf_ssb_db_exec(db, "DROP TRIGGER IF EXISTS messages_ai_refs");
|
||||
@ -205,7 +206,7 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
|
||||
|
||||
if (need_convert_timestamp_to_real)
|
||||
{
|
||||
printf("Converting timestamp column from INTEGER to REAL.\n");
|
||||
tf_printf("Converting timestamp column from INTEGER to REAL.\n");
|
||||
_tf_ssb_db_exec(db, "BEGIN TRANSACTION");
|
||||
_tf_ssb_db_exec(db, "DROP INDEX IF EXISTS messages_author_timestamp_index");
|
||||
_tf_ssb_db_exec(db, "ALTER TABLE messages ADD COLUMN timestamp_real REAL");
|
||||
@ -217,7 +218,7 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
|
||||
}
|
||||
if (need_add_sequence_before_author)
|
||||
{
|
||||
printf("Adding sequence_before_author column.\n");
|
||||
tf_printf("Adding sequence_before_author column.\n");
|
||||
_tf_ssb_db_exec(db, "ALTER TABLE messages ADD COLUMN sequence_before_author INTEGER");
|
||||
}
|
||||
}
|
||||
@ -291,7 +292,7 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
|
||||
int r = sqlite3_step(statement);
|
||||
if (r != SQLITE_DONE)
|
||||
{
|
||||
printf("%s\n", sqlite3_errmsg(db));
|
||||
tf_printf("%s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
stored = r == SQLITE_DONE && sqlite3_changes(db) != 0;
|
||||
if (stored)
|
||||
@ -301,18 +302,18 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("bind failed\n");
|
||||
tf_printf("bind failed\n");
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("prepare failed: %s\n", sqlite3_errmsg(db));
|
||||
tf_printf("prepare failed: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Previous message doesn't exist.\n");
|
||||
tf_printf("Previous message doesn't exist.\n");
|
||||
}
|
||||
|
||||
if (last_row_id != -1)
|
||||
@ -330,14 +331,14 @@ bool tf_ssb_db_store_message(tf_ssb_t* ssb, JSContext* context, const char* id,
|
||||
}
|
||||
if (r != SQLITE_DONE)
|
||||
{
|
||||
printf("%s\n", sqlite3_errmsg(db));
|
||||
tf_printf("%s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("prepare failed: %s\n", sqlite3_errmsg(db));
|
||||
tf_printf("prepare failed: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
}
|
||||
|
||||
@ -456,13 +457,13 @@ bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char*
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("bind failed: %s\n", sqlite3_errmsg(db));
|
||||
tf_printf("bind failed: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("prepare failed: %s\n", sqlite3_errmsg(db));
|
||||
tf_printf("prepare failed: %s\n", sqlite3_errmsg(db));
|
||||
}
|
||||
|
||||
if (rows)
|
||||
@ -470,7 +471,7 @@ bool tf_ssb_db_blob_store(tf_ssb_t* ssb, const uint8_t* blob, size_t size, char*
|
||||
tf_ssb_notify_blob_stored(ssb, id);
|
||||
if (!out_new)
|
||||
{
|
||||
printf("blob stored %s %zd => %d\n", id, size, result);
|
||||
tf_printf("blob stored %s %zd => %d\n", id, size, result);
|
||||
}
|
||||
}
|
||||
|
||||
@ -514,7 +515,7 @@ bool tf_ssb_db_get_message_by_author_and_sequence(tf_ssb_t* ssb, const char* aut
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("prepare failed: %s\n", sqlite3_errmsg(tf_ssb_get_db(ssb)));
|
||||
tf_printf("prepare failed: %s\n", sqlite3_errmsg(tf_ssb_get_db(ssb)));
|
||||
}
|
||||
return found;
|
||||
}
|
||||
@ -543,7 +544,7 @@ bool tf_ssb_db_get_latest_message_by_author(tf_ssb_t* ssb, const char* author, i
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("prepare failed: %s\n", sqlite3_errmsg(tf_ssb_get_db(ssb)));
|
||||
tf_printf("prepare failed: %s\n", sqlite3_errmsg(tf_ssb_get_db(ssb)));
|
||||
}
|
||||
return found;
|
||||
}
|
||||
@ -671,7 +672,7 @@ static int _tf_ssb_sqlite_authorizer(void* user_data, int action_code, const cha
|
||||
}
|
||||
if (result != SQLITE_OK)
|
||||
{
|
||||
printf("Denying sqlite access to %d %s %s %s %s\n", action_code, arg0, arg1, arg2, arg3);
|
||||
tf_printf("Denying sqlite access to %d %s %s %s %s\n", action_code, arg0, arg1, arg2, arg3);
|
||||
fflush(stdout);
|
||||
}
|
||||
return result;
|
||||
@ -796,12 +797,12 @@ bool tf_ssb_db_check(sqlite3* db, const char* check_author)
|
||||
bool delete_following = false;
|
||||
if (strcmp(author, previous_author))
|
||||
{
|
||||
printf("%s\n", author);
|
||||
tf_printf("%s\n", author);
|
||||
}
|
||||
|
||||
if (strcmp(author, previous_author) == 0 && sequence != previous_sequence + 1)
|
||||
{
|
||||
printf("Detected gap in messages for %s at sequence = %" PRId64 " => %" PRId64 ".\n", author, previous_sequence, sequence);
|
||||
tf_printf("Detected gap in messages for %s at sequence = %" PRId64 " => %" PRId64 ".\n", author, previous_sequence, sequence);
|
||||
delete_following = true;
|
||||
}
|
||||
else
|
||||
@ -810,30 +811,30 @@ bool tf_ssb_db_check(sqlite3* db, const char* check_author)
|
||||
{
|
||||
if (previous && strcmp(previous, previous_id))
|
||||
{
|
||||
printf("%s:%d previous was %s should be %s\n", id, (int)sequence, previous_id, previous);
|
||||
tf_printf("%s:%d previous was %s should be %s\n", id, (int)sequence, previous_id, previous);
|
||||
}
|
||||
if (strcmp(id, actual_id))
|
||||
{
|
||||
if (_tf_ssb_update_message_id(db, id, actual_id))
|
||||
{
|
||||
printf("updated %s to %s\n", id, actual_id);
|
||||
tf_printf("updated %s to %s\n", id, actual_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("failed to update %s to %s\n", id, actual_id);
|
||||
tf_printf("failed to update %s to %s\n", id, actual_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%s sequence=%" PRId64 " unable to verify signature for %s sequence_before_author=%d message=[%.*s]\n", author, sequence, id, sequence_before_author, (int)strlen(jv), jv);
|
||||
tf_printf("%s sequence=%" PRId64 " unable to verify signature for %s sequence_before_author=%d message=[%.*s]\n", author, sequence, id, sequence_before_author, (int)strlen(jv), jv);
|
||||
delete_following = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (delete_following)
|
||||
{
|
||||
printf("Deleting author = %s sequence >= %" PRId64 ".\n", author, sequence);
|
||||
tf_printf("Deleting author = %s sequence >= %" PRId64 ".\n", author, sequence);
|
||||
sqlite3_stmt* delete_statement = NULL;
|
||||
if (sqlite3_prepare(db, "DELETE FROM messages WHERE author = ? AND sequence >= ?", -1, &delete_statement, NULL) == SQLITE_OK)
|
||||
{
|
||||
@ -842,7 +843,7 @@ bool tf_ssb_db_check(sqlite3* db, const char* check_author)
|
||||
{
|
||||
if (sqlite3_step(delete_statement) != SQLITE_DONE)
|
||||
{
|
||||
printf("Error deleting author = %s sequence >= %" PRId64 ".\n", author, sequence);
|
||||
tf_printf("Error deleting author = %s sequence >= %" PRId64 ".\n", author, sequence);
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(delete_statement);
|
||||
@ -918,7 +919,7 @@ bool tf_ssb_db_identity_add(tf_ssb_t* ssb, const char* user, const char* public_
|
||||
sqlite3_changes(db) != 0;
|
||||
if (!added)
|
||||
{
|
||||
printf("Unable to add identity: %s.\n", sqlite3_errmsg(db));
|
||||
tf_printf("Unable to add identity: %s.\n", sqlite3_errmsg(db));
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
@ -1171,20 +1172,20 @@ static void _test_private(sqlite3* db, const uint8_t* private_key)
|
||||
uint8_t* key = out + 1;
|
||||
if (crypto_secretbox_open_easy(result, body, body_size, nonce, key) != -1)
|
||||
{
|
||||
printf("%.*s\n", (int)body_size, result);
|
||||
tf_printf("%.*s\n", (int)body_size, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("scalarmult failed\n");
|
||||
tf_printf("scalarmult failed\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("base64 failed\n");
|
||||
tf_printf("base64 failed\n");
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
@ -1199,14 +1200,14 @@ void tf_ssb_db_private(sqlite3* db)
|
||||
while (sqlite3_step(statement) == SQLITE_ROW)
|
||||
{
|
||||
uint8_t private_key[crypto_sign_SECRETKEYBYTES] = { 0 };
|
||||
printf("-> %s\n", sqlite3_column_text(statement, 0));
|
||||
tf_printf("-> %s\n", sqlite3_column_text(statement, 0));
|
||||
int r = tf_base64_decode((const char*)sqlite3_column_text(statement, 1), sqlite3_column_bytes(statement, 1) - strlen(".ed25519"), private_key, sizeof(private_key));
|
||||
if (r == sizeof(private_key))
|
||||
{
|
||||
uint8_t key[crypto_sign_SECRETKEYBYTES] = { 0 };
|
||||
if (crypto_sign_ed25519_sk_to_curve25519(key, private_key) != 0)
|
||||
{
|
||||
printf("key convert failed\n");
|
||||
tf_printf("key convert failed\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -1298,7 +1299,7 @@ void tf_ssb_db_forget_stored_connection(tf_ssb_t* ssb, const char* address, int
|
||||
sqlite3_bind_text(statement, 3, pubkey, -1, NULL) != SQLITE_OK ||
|
||||
sqlite3_step(statement) != SQLITE_DONE)
|
||||
{
|
||||
printf("Delete stored connection: %s.\n", sqlite3_errmsg(db));
|
||||
tf_printf("Delete stored connection: %s.\n", sqlite3_errmsg(db));
|
||||
}
|
||||
sqlite3_finalize(statement);
|
||||
}
|
||||
|
Reference in New Issue
Block a user