src/sync_actions.c
branchkeysync
changeset 959 c1c463ecf30c
parent 954 a72e5c669833
child 964 ef02296c41ca
     1.1 --- a/src/sync_actions.c	Mon Aug 01 12:03:00 2016 +0200
     1.2 +++ b/src/sync_actions.c	Mon Aug 01 12:32:04 2016 +0200
     1.3 @@ -24,11 +24,11 @@
     1.4      return 0;
     1.5  }
     1.6  
     1.7 -bool storedGroupKeys(PEP_SESSION session)
     1.8 +int storedGroupKeys(PEP_SESSION session)
     1.9  {
    1.10      assert(session);
    1.11      if (!session)
    1.12 -        return false;
    1.13 +        return -1; // error
    1.14  
    1.15      bool gc = false;
    1.16      int int_result = sqlite3_exec(
    1.17 @@ -39,34 +39,55 @@
    1.18          NULL
    1.19      );
    1.20      assert(int_result == SQLITE_OK);
    1.21 -    return gc;
    1.22 +    if (int_result != SQLITE_OK)
    1.23 +        return -1; // error
    1.24 +
    1.25 +    if (gc)
    1.26 +        return 1;
    1.27 +    else
    1.28 +        return 0;
    1.29  }
    1.30  
    1.31 -bool keyElectionWon(PEP_SESSION session, Identity partner)
    1.32 +int keyElectionWon(PEP_SESSION session, Identity partner)
    1.33  {
    1.34      assert(session);
    1.35      assert(partner);
    1.36      if (!(session && partner))
    1.37 -        return false;
    1.38 +        return -1; // error
    1.39  
    1.40      // an already existing group always wins
    1.41  
    1.42      if (storedGroupKeys(session)) {
    1.43          assert(!(partner->flags & PEP_idf_devicegroup));
    1.44 -        return true;
    1.45 +        return 1;
    1.46      }
    1.47  
    1.48      if (partner->flags & PEP_idf_devicegroup)
    1.49 -        return false;
    1.50 +        return 0;
    1.51  
    1.52      Identity me = NULL;
    1.53      PEP_STATUS status = get_identity(session, partner->address, PEP_OWN_USERID,
    1.54              &me);
    1.55      if (status != PEP_STATUS_OK)
    1.56 -        return false;
    1.57 +        return -1; // error
    1.58  
    1.59 -    bool result = false;
    1.60 +    int result = -1; // error state has to be overwritten
    1.61  
    1.62 +    time_t own_created;
    1.63 +    time_t partners_created;
    1.64 +
    1.65 +    status = key_created(session, me->fpr, &own_created);
    1.66 +    if (status != PEP_STATUS_OK)
    1.67 +        goto the_end;
    1.68 +
    1.69 +    status = key_created(session, partner->fpr, &partners_created);
    1.70 +    if (status != PEP_STATUS_OK)
    1.71 +        goto the_end;
    1.72 +
    1.73 +    if (own_created > partners_created)
    1.74 +        result = 0;
    1.75 +    else
    1.76 +        result = 1;
    1.77  
    1.78  the_end:
    1.79      free_identity(me);