src/pEpEngine.c
branchkeysync
changeset 1091 0571ce4b97a2
parent 1090 c15ef7d00170
child 1103 aba4f9ea5bce
     1.1 --- a/src/pEpEngine.c	Mon Aug 29 15:02:16 2016 +0200
     1.2 +++ b/src/pEpEngine.c	Mon Aug 29 17:39:35 2016 +0200
     1.3 @@ -6,6 +6,7 @@
     1.4  #include "sync_fsm.h"
     1.5  
     1.6  static int init_count = -1;
     1.7 +char sync_uuid[37];
     1.8  
     1.9  static int user_version(void *_version, int count, char **text, char **name)
    1.10  {
    1.11 @@ -132,6 +133,8 @@
    1.12  #define _DDL_USER_VERSION "3"
    1.13  
    1.14      if (in_first) {
    1.15 +        memset(sync_uuid, 0, 37);
    1.16 +
    1.17          int_result = sqlite3_exec(
    1.18              _session->db,
    1.19                  "create table if not exists version_info (\n"
    1.20 @@ -385,7 +388,7 @@
    1.21                                "(select coalesce((select value + 1 from sequences "
    1.22                                "where name = ?1), 1 )), ?2) ; ";
    1.23          sql_sequence_value2 = "select value, own from sequences where name = ?1 ;";
    1.24 -        sql_sequence_value3 = "update sequences set value = ?2 where name = ?1 ;";
    1.25 +        sql_sequence_value3 = "update sequences set value = ?2, own = ?3 where name = ?1 ;";
    1.26          
    1.27          sql_set_revoked =     "insert or replace into revoked_keys ("
    1.28                                "    revoked_fpr, replacement_fpr, revocation_date) "
    1.29 @@ -1727,6 +1730,9 @@
    1.30                  status = PEP_OWN_SEQUENCE;
    1.31              break;
    1.32          }
    1.33 +        case SQLITE_DONE:
    1.34 +            status = PEP_RECORD_NOT_FOUND;
    1.35 +            break;
    1.36          default:
    1.37              status = PEP_UNKNOWN_ERROR;
    1.38      }
    1.39 @@ -1755,7 +1761,7 @@
    1.40  }
    1.41  
    1.42  static PEP_STATUS _set_sequence_value(PEP_SESSION session,
    1.43 -        const char *name, int32_t value)
    1.44 +        const char *name, int32_t value, int own)
    1.45  {
    1.46      assert(session && name && value > 0);
    1.47      if (!(session && name && value > 0))
    1.48 @@ -1764,6 +1770,7 @@
    1.49      sqlite3_reset(session->sequence_value3);
    1.50      sqlite3_bind_text(session->sequence_value3, 1, name, -1, SQLITE_STATIC);
    1.51      sqlite3_bind_int(session->sequence_value3, 2, value);
    1.52 +    sqlite3_bind_int(session->sequence_value3, 3, own);
    1.53      int result = sqlite3_step(session->sequence_value3);
    1.54      assert(result == SQLITE_DONE);
    1.55      sqlite3_reset(session->sequence_value3);
    1.56 @@ -1794,18 +1801,22 @@
    1.57          uuid_unparse_upper(uuid, name);
    1.58          own = 1;
    1.59      }
    1.60 +    else {
    1.61 +        if (name == sync_uuid || strcmp(name, sync_uuid) == 0)
    1.62 +            own = 1;
    1.63 +    }
    1.64  
    1.65      if (*value) {
    1.66          int32_t old_value = 0;
    1.67          status = _get_sequence_value(session, name, &old_value);
    1.68 -        if (status != PEP_STATUS_OK)
    1.69 +        if (status != PEP_STATUS_OK && status != PEP_RECORD_NOT_FOUND)
    1.70              return status;
    1.71  
    1.72          if (old_value >= *value) {
    1.73              return PEP_SEQUENCE_VIOLATED;
    1.74          }
    1.75          else {
    1.76 -            status = _set_sequence_value(session, name, *value);
    1.77 +            status = _set_sequence_value(session, name, *value, own);
    1.78              return status;
    1.79          }
    1.80      }