src/pgp_sequoia.c
branchsync
changeset 3212 fb4124923112
parent 3211 8ca54e726b3c
child 3213 e56efa502f08
     1.1 --- a/src/pgp_sequoia.c	Wed Dec 26 20:48:02 2018 +0100
     1.2 +++ b/src/pgp_sequoia.c	Fri Dec 28 12:40:23 2018 +0100
     1.3 @@ -166,31 +166,31 @@
     1.4  
     1.5      sqlite_result
     1.6          = sqlite3_prepare_v2(session->key_db, "begin transaction",
     1.7 -                             -1, &session->begin_transaction, NULL);
     1.8 +                             -1, &session->sq_sql.begin_transaction, NULL);
     1.9      assert(sqlite_result == SQLITE_OK);
    1.10  
    1.11      sqlite_result
    1.12          = sqlite3_prepare_v2(session->key_db, "commit transaction",
    1.13 -                             -1, &session->commit_transaction, NULL);
    1.14 +                             -1, &session->sq_sql.commit_transaction, NULL);
    1.15      assert(sqlite_result == SQLITE_OK);
    1.16  
    1.17      sqlite_result
    1.18          = sqlite3_prepare_v2(session->key_db, "rollback transaction",
    1.19 -                             -1, &session->rollback_transaction, NULL);
    1.20 +                             -1, &session->sq_sql.rollback_transaction, NULL);
    1.21      assert(sqlite_result == SQLITE_OK);
    1.22  
    1.23      sqlite_result
    1.24          = sqlite3_prepare_v2(session->key_db,
    1.25                               "SELECT tpk, secret FROM keys"
    1.26                               " WHERE primary_key == ?",
    1.27 -                             -1, &session->tpk_find, NULL);
    1.28 +                             -1, &session->sq_sql.tpk_find, NULL);
    1.29      assert(sqlite_result == SQLITE_OK);
    1.30  
    1.31      sqlite_result
    1.32          = sqlite3_prepare_v2(session->key_db,
    1.33                               "SELECT tpk, secret FROM keys"
    1.34                               " WHERE primary_key == ? and secret == 1",
    1.35 -                             -1, &session->tsk_find, NULL);
    1.36 +                             -1, &session->sq_sql.tsk_find, NULL);
    1.37      assert(sqlite_result == SQLITE_OK);
    1.38  
    1.39      sqlite_result
    1.40 @@ -199,7 +199,7 @@
    1.41                               " LEFT JOIN keys"
    1.42                               "  ON subkeys.primary_key == keys.primary_key"
    1.43                               " WHERE subkey == ?",
    1.44 -                             -1, &session->tpk_find_by_keyid, NULL);
    1.45 +                             -1, &session->sq_sql.tpk_find_by_keyid, NULL);
    1.46      assert(sqlite_result == SQLITE_OK);
    1.47  
    1.48      sqlite_result
    1.49 @@ -208,7 +208,7 @@
    1.50                               " LEFT JOIN keys"
    1.51                               "  ON subkeys.primary_key == keys.primary_key"
    1.52                               " WHERE subkey == ?",
    1.53 -                             -1, &session->tpk_find_by_keyid, NULL);
    1.54 +                             -1, &session->sq_sql.tpk_find_by_keyid, NULL);
    1.55      assert(sqlite_result == SQLITE_OK);
    1.56  
    1.57      sqlite_result
    1.58 @@ -217,7 +217,7 @@
    1.59                               " LEFT JOIN keys"
    1.60                               "  ON subkeys.primary_key == keys.primary_key"
    1.61                               " WHERE subkey == ? and keys.secret == 1",
    1.62 -                             -1, &session->tsk_find_by_keyid, NULL);
    1.63 +                             -1, &session->sq_sql.tsk_find_by_keyid, NULL);
    1.64      assert(sqlite_result == SQLITE_OK);
    1.65  
    1.66      sqlite_result
    1.67 @@ -226,7 +226,7 @@
    1.68                               " LEFT JOIN keys"
    1.69                               "  ON userids.primary_key == keys.primary_key"
    1.70                               " WHERE userid == ?",
    1.71 -                             -1, &session->tpk_find_by_email, NULL);
    1.72 +                             -1, &session->sq_sql.tpk_find_by_email, NULL);
    1.73      assert(sqlite_result == SQLITE_OK);
    1.74  
    1.75      sqlite_result
    1.76 @@ -235,19 +235,19 @@
    1.77                               " LEFT JOIN keys"
    1.78                               "  ON userids.primary_key == keys.primary_key"
    1.79                               " WHERE userid == ? and keys.secret == 1",
    1.80 -                             -1, &session->tsk_find_by_email, NULL);
    1.81 +                             -1, &session->sq_sql.tsk_find_by_email, NULL);
    1.82      assert(sqlite_result == SQLITE_OK);
    1.83  
    1.84      sqlite_result
    1.85          = sqlite3_prepare_v2(session->key_db,
    1.86                               "select tpk, secret from keys",
    1.87 -                             -1, &session->tpk_all, NULL);
    1.88 +                             -1, &session->sq_sql.tpk_all, NULL);
    1.89      assert(sqlite_result == SQLITE_OK);
    1.90  
    1.91      sqlite_result
    1.92          = sqlite3_prepare_v2(session->key_db,
    1.93                               "select tpk, secret from keys where secret = 1",
    1.94 -                             -1, &session->tsk_all, NULL);
    1.95 +                             -1, &session->sq_sql.tsk_all, NULL);
    1.96      assert(sqlite_result == SQLITE_OK);
    1.97  
    1.98      sqlite_result
    1.99 @@ -255,7 +255,7 @@
   1.100                               "INSERT OR REPLACE INTO keys"
   1.101                               "   (primary_key, secret, tpk)"
   1.102                               " VALUES (?, ?, ?)",
   1.103 -                             -1, &session->tpk_save_insert_primary, NULL);
   1.104 +                             -1, &session->sq_sql.tpk_save_insert_primary, NULL);
   1.105      assert(sqlite_result == SQLITE_OK);
   1.106  
   1.107      sqlite_result
   1.108 @@ -263,7 +263,7 @@
   1.109                               "INSERT OR REPLACE INTO subkeys"
   1.110                               "   (subkey, primary_key)"
   1.111                               " VALUES (?, ?)",
   1.112 -                             -1, &session->tpk_save_insert_subkeys, NULL);
   1.113 +                             -1, &session->sq_sql.tpk_save_insert_subkeys, NULL);
   1.114      assert(sqlite_result == SQLITE_OK);
   1.115  
   1.116      sqlite_result
   1.117 @@ -271,7 +271,7 @@
   1.118                               "INSERT OR REPLACE INTO userids"
   1.119                               "   (userid, primary_key)"
   1.120                               " VALUES (?, ?)",
   1.121 -                             -1, &session->tpk_save_insert_userids, NULL);
   1.122 +                             -1, &session->sq_sql.tpk_save_insert_userids, NULL);
   1.123      assert(sqlite_result == SQLITE_OK);
   1.124  
   1.125   out:
   1.126 @@ -282,53 +282,12 @@
   1.127  
   1.128  void pgp_release(PEP_SESSION session, bool out_last)
   1.129  {
   1.130 -    if (session->begin_transaction)
   1.131 -        sqlite3_finalize(session->begin_transaction);
   1.132 -    session->begin_transaction = NULL;
   1.133 -    if (session->commit_transaction)
   1.134 -        sqlite3_finalize(session->commit_transaction);
   1.135 -    session->commit_transaction = NULL;
   1.136 -    if (session->rollback_transaction)
   1.137 -        sqlite3_finalize(session->rollback_transaction);
   1.138 -    session->rollback_transaction = NULL;
   1.139 -
   1.140 -    if (session->tpk_find)
   1.141 -        sqlite3_finalize(session->tpk_find);
   1.142 -    session->tpk_find = NULL;
   1.143 -    if (session->tsk_find)
   1.144 -        sqlite3_finalize(session->tsk_find);
   1.145 -    session->tsk_find = NULL;
   1.146 -
   1.147 -    if (session->tpk_find_by_keyid)
   1.148 -        sqlite3_finalize(session->tpk_find_by_keyid);
   1.149 -    session->tpk_find_by_keyid = NULL;
   1.150 -    if (session->tsk_find_by_keyid)
   1.151 -        sqlite3_finalize(session->tsk_find_by_keyid);
   1.152 -    session->tsk_find_by_keyid = NULL;
   1.153 -
   1.154 -    if (session->tpk_find_by_email)
   1.155 -        sqlite3_finalize(session->tpk_find_by_email);
   1.156 -    session->tpk_find_by_email = NULL;
   1.157 -    if (session->tsk_find_by_email)
   1.158 -        sqlite3_finalize(session->tsk_find_by_email);
   1.159 -    session->tsk_find_by_email = NULL;
   1.160 -
   1.161 -    if (session->tpk_all)
   1.162 -        sqlite3_finalize(session->tpk_all);
   1.163 -    session->tpk_all = NULL;
   1.164 -    if (session->tsk_all)
   1.165 -        sqlite3_finalize(session->tsk_all);
   1.166 -    session->tsk_all = NULL;
   1.167 -
   1.168 -    if (session->tpk_save_insert_primary)
   1.169 -        sqlite3_finalize(session->tpk_save_insert_primary);
   1.170 -    session->tpk_save_insert_primary = NULL;
   1.171 -    if (session->tpk_save_insert_subkeys)
   1.172 -        sqlite3_finalize(session->tpk_save_insert_subkeys);
   1.173 -    session->tpk_save_insert_subkeys = NULL;
   1.174 -    if (session->tpk_save_insert_userids)
   1.175 -        sqlite3_finalize(session->tpk_save_insert_userids);
   1.176 -    session->tpk_save_insert_userids = NULL;
   1.177 +    sqlite3_stmt **stmts = (sqlite3_stmt **) &session->sq_sql;
   1.178 +    for (int i = 0; i < sizeof(session->sq_sql) / sizeof(*stmts); i ++)
   1.179 +        if (stmts[i]) {
   1.180 +            sqlite3_finalize(stmts[i]);
   1.181 +            stmts[i] = NULL;
   1.182 +        }
   1.183  
   1.184      if (session->key_db) {
   1.185          int result = sqlite3_close_v2(session->key_db);
   1.186 @@ -508,7 +467,7 @@
   1.187  
   1.188      T("(%s, %d)", fpr_str, private_only);
   1.189  
   1.190 -    sqlite3_stmt *stmt = private_only ? session->tsk_find : session->tpk_find;
   1.191 +    sqlite3_stmt *stmt = private_only ? session->sq_sql.tsk_find : session->sq_sql.tpk_find;
   1.192      sqlite3_bind_text(stmt, 1, fpr_str, -1, SQLITE_STATIC);
   1.193  
   1.194      status = key_load(session, stmt, tpk, secret);
   1.195 @@ -542,7 +501,7 @@
   1.196      T("(%s, %d)", keyid_hex, private_only);
   1.197  
   1.198      sqlite3_stmt *stmt
   1.199 -        = private_only ? session->tsk_find_by_keyid : session->tpk_find_by_keyid;
   1.200 +        = private_only ? session->sq_sql.tsk_find_by_keyid : session->sq_sql.tpk_find_by_keyid;
   1.201      sqlite3_bind_text(stmt, 1, keyid_hex, -1, SQLITE_STATIC);
   1.202  
   1.203      status = key_load(session, stmt, tpkp, secretp);
   1.204 @@ -608,7 +567,7 @@
   1.205  static PEP_STATUS tpk_all(PEP_SESSION session, int private_only,
   1.206                            sq_tpk_t **tpksp, int *tpks_countp) {
   1.207      PEP_STATUS status = PEP_STATUS_OK;
   1.208 -    sqlite3_stmt *stmt = private_only ? session->tsk_all : session->tpk_all;
   1.209 +    sqlite3_stmt *stmt = private_only ? session->sq_sql.tsk_all : session->sq_sql.tpk_all;
   1.210      status = key_loadn(session, stmt, tpksp, tpks_countp);
   1.211      ERROR_OUT(session, status, "loading TPKs");
   1.212   out:
   1.213 @@ -629,7 +588,7 @@
   1.214      T("(%s)", pattern);
   1.215  
   1.216      sqlite3_stmt *stmt
   1.217 -        = private_only ? session->tsk_find_by_email : session->tpk_find_by_email;
   1.218 +        = private_only ? session->sq_sql.tsk_find_by_email : session->sq_sql.tpk_find_by_email;
   1.219      sqlite3_bind_text(stmt, 1, pattern, -1, SQLITE_STATIC);
   1.220  
   1.221      status = key_loadn(session, stmt, tpksp, countp);
   1.222 @@ -690,7 +649,7 @@
   1.223  
   1.224  
   1.225      // Insert the TSK into the DB.
   1.226 -    sqlite3_stmt *stmt = session->begin_transaction;
   1.227 +    sqlite3_stmt *stmt = session->sq_sql.begin_transaction;
   1.228      int sqlite_result = sqlite3_step(stmt);
   1.229      sqlite3_reset(stmt);
   1.230      if (sqlite_result != SQLITE_DONE)
   1.231 @@ -698,7 +657,7 @@
   1.232                    "begin transaction failed: %s",
   1.233                    sqlite3_errmsg(session->key_db));
   1.234  
   1.235 -    stmt = session->tpk_save_insert_primary;
   1.236 +    stmt = session->sq_sql.tpk_save_insert_primary;
   1.237      sqlite3_bind_text(stmt, 1, fpr, -1, SQLITE_STATIC);
   1.238      sqlite3_bind_int(stmt, 2, is_tsk);
   1.239      sqlite3_bind_blob(stmt, 3, tsk_buffer, tsk_buffer_len, SQLITE_STATIC);
   1.240 @@ -710,7 +669,7 @@
   1.241                    "Saving TPK: %s", sqlite3_errmsg(session->key_db));
   1.242  
   1.243      // Insert the "subkeys" (the primary key and the subkeys).
   1.244 -    stmt = session->tpk_save_insert_subkeys;
   1.245 +    stmt = session->sq_sql.tpk_save_insert_subkeys;
   1.246      key_iter = sq_tpk_key_iter(tpk);
   1.247      sq_p_key_t key;
   1.248      while ((key = sq_tpk_key_iter_next(key_iter, NULL, NULL))) {
   1.249 @@ -733,7 +692,7 @@
   1.250      key_iter = NULL;
   1.251  
   1.252      // Insert the "userids".
   1.253 -    stmt = session->tpk_save_insert_userids;
   1.254 +    stmt = session->sq_sql.tpk_save_insert_userids;
   1.255      user_id_iter = sq_tpk_user_id_binding_iter(tpk);
   1.256      sq_user_id_binding_t binding;
   1.257      int first = 1;
   1.258 @@ -794,7 +753,8 @@
   1.259      if (! tried_commit) {
   1.260          tried_commit = 1;
   1.261          stmt = status == PEP_STATUS_OK
   1.262 -            ? session->commit_transaction : session->rollback_transaction;
   1.263 +            ? session->sq_sql.commit_transaction
   1.264 +            : session->sq_sql.rollback_transaction;
   1.265          int sqlite_result = sqlite3_step(stmt);
   1.266          sqlite3_reset(stmt);
   1.267          if (sqlite_result != SQLITE_DONE)