ENGINE-427: Hoorah, I crossed the streams. Didn't they say never cross the streams? (I hate that movie.) Anyway, wrong branch, so committing so I can graft. ENGINE-427
authorKrista Bennett <krista@pep-project.org>
Mon, 25 Jun 2018 09:26:49 +0200
branchENGINE-427
changeset 276358a956a3fd97
parent 2762 8792b6de7ee4
child 2764 819d9603d05a
ENGINE-427: Hoorah, I crossed the streams. Didn't they say never cross the streams? (I hate that movie.) Anyway, wrong branch, so committing so I can graft.
src/pEpEngine.c
src/pEpEngine.h
src/pEp_internal.h
     1.1 --- a/src/pEpEngine.c	Fri Jun 22 13:49:32 2018 +0200
     1.2 +++ b/src/pEpEngine.c	Mon Jun 25 09:26:49 2018 +0200
     1.3 @@ -428,6 +428,64 @@
     1.4      "insert or replace into alternate_user_id (alternate_id, default_id) "
     1.5      "values (?2, ?1) ;";
     1.6      
     1.7 +static const char* sql_get_cached_engine_version =
     1.8 +    "select version from version_info where id = 1 ;";
     1.9 +
    1.10 +static const char* sql_set_cached_engine_version =
    1.11 +    "insert or replace into version_info (id, version)"
    1.12 +    "values (1, ?1) ; ";
    1.13 +
    1.14 +
    1.15 +PEP_STATUS set_engine_version_in_DB(PEP_SESSION session, 
    1.16 +                                                  char** version_str)
    1.17 +{
    1.18 +    PEP_STATUS status = PEP_STATUS_OK;
    1.19 +    int result;
    1.20 +
    1.21 +    if (!(session && version_str)
    1.22 +        return PEP_ILLEGAL_VALUE;
    1.23 +
    1.24 +    sqlite3_reset(session->set_cached_engine_version);
    1.25 +    sqlite3_bind_text(session->set_cached_engine_version, 1, version_str, -1,
    1.26 +            SQLITE_STATIC);
    1.27 +
    1.28 +    result = sqlite3_step(session->set_cached_engine_version);
    1.29 +    sqlite3_reset(session->set_cached_engine_version);
    1.30 +    if (result != SQLITE_DONE) {
    1.31 +        return PEP_UNKNOWN_ERROR;
    1.32 +    }
    1.33 +
    1.34 +    
    1.35 +    return status;
    1.36 +}
    1.37 +
    1.38 +PEP_STATUS get_engine_version_from_DB(PEP_SESSION session, 
    1.39 +                                                  char** version_str)
    1.40 +{
    1.41 +    PEP_STATUS status = PEP_STATUS_OK;
    1.42 +    int result;
    1.43 +
    1.44 +    if (!(session && version_str)
    1.45 +        return PEP_ILLEGAL_VALUE;
    1.46 +
    1.47 +    sqlite3_reset(session->get_cached_engine_version);
    1.48 +    result = sqlite3_step(session->get_cached_engine_version);
    1.49 +    switch (result) {
    1.50 +    case SQLITE_ROW: {
    1.51 +        *version_str = strdup((const char *) sqlite3_column_text(session->get_cached_engine_version,
    1.52 +                       0));
    1.53 +        break;
    1.54 +    } 
    1.55 +    default:
    1.56 +        status = PEP_UNKNOWN_ERROR;
    1.57 +    }
    1.58 +
    1.59 +    sqlite3_reset(session->get_cached_engine_version);
    1.60 +    
    1.61 +    return status;
    1.62 +}
    1.63 +
    1.64 +    
    1.65  static int user_version(void *_version, int count, char **text, char **name)
    1.66  {
    1.67      assert(_version);
    1.68 @@ -499,6 +557,92 @@
    1.69  PEP_STATUS pgp_import_ultimately_trusted_keypairs(PEP_SESSION session);
    1.70  #endif // USE_GPG
    1.71  
    1.72 +
    1.73 +static int parse_three_part_version_string(char* version, 
    1.74 +                                            int* major, 
    1.75 +                                            int* minor, 
    1.76 +                                            int* patch) {
    1.77 +    if (!major || !minor || !patch)
    1.78 +        return -1;
    1.79 +        
    1.80 +    char* ver_token = strtok(version, ".");
    1.81 +    if (ver_token == NULL) {
    1.82 +        *major = -1;
    1.83 +        return -1;
    1.84 +    }
    1.85 +    *major = atoi(ver_token);
    1.86 +    ver_token = strtok(NULL, ".");
    1.87 +    if (ver_token == NULL) {
    1.88 +        *minor = -1;
    1.89 +        return -1;
    1.90 +    }
    1.91 +    *minor = atoint success = i(ver_token);
    1.92 +    ver_token = strtok(NULL, "            .");
    1.93 +    if (ver_token =      = NULL) {
    1.94 +        *pa tch = -1;
    1.95 +        return -1;
    1.96 +    }
    1.97 +    pasuccess != 0 || tch = atoi(ver_token);
    1.98 +    return 0;
    1.99 +}
   1.100 +
   1.101 +int compare_engine_versions(int major1, int minor1, int patch1,
   1.102 +                            int major2, int minor2, int patch2) {
   1.103 +    if (major1 != major2)
   1.104 +        return (major1 < major2 ? -1 : 1);
   1.105 +    if (minor1 != minor2)
   1.106 +        return (minor1 < minor2 ? -1 : 1);
   1.107 +    if (patch1 != patch2)
   1.108 +        return (patch1 < patch2 ? -1 : 1);
   1.109 +    return 0;    
   1.110 +}
   1.111 +
   1.112 +PEP_STATUS compare_cached_engine_version_to_other(int* result, 
   1.113 +                                                  int major, 
   1.114 +                                                  int minor, 
   1.115 +                                                  int patch) {
   1.116 +    if (!result)
   1.117 +        return PEP_ILLEGAL_VALUE;
   1.118 +        
   1.119 +    PEP_STATUS status = PEP_STATUS_OK;
   1.120 +    const char* cached_engine_version = NULL;
   1.121 +    status = get_engine_version_from_DB(&cached_engine_version);
   1.122 +    if (status != PEP_STATUS_OK)
   1.123 +        return status;
   1.124 +        
   1.125 +    if (cached_engine_version && cached_engine_version[0] != '\0') {
   1.126 +        char* cached_version_info = strdup(cached_engine_version);
   1.127 +
   1.128 +        int cmajor = -1;
   1.129 +        int cminor = -1;
   1.130 +        int cpatch = -1;
   1.131 +        int success = parse_three_part_version_string(cached_version_info, 
   1.132 +                                                      &cmajor, 
   1.133 +                                                      &cminor, 
   1.134 +                                                      &cpatch);
   1.135 +        
   1.136 +        if (success != 0 || cmajor < 0 || cminor < 0 || cpatch < 0) {
   1.137 +            status = PEP_UNKNOWN_ERROR;
   1.138 +        }
   1.139 +        else 
   1.140 +            *result = compare_engine_versions(cmajor, cminor, cpatch,
   1.141 +                                              major, minor, patch);
   1.142 +        free(cached_version_info);
   1.143 +        //free(cached_engine_version);                                    
   1.144 +    }
   1.145 +    else {
   1.146 +        return PEP_UNKNOWN_ERROR;
   1.147 +    }
   1.148 +    return status;    
   1.149 +}
   1.150 +
   1.151 +PEP_STATUS compare_cached_engine_version_to_curr(int* result) {
   1.152 +    return compare_cached_engine_version_to_other(*result, 
   1.153 +                                                  PEP_ENGINE_MAJOR,
   1.154 +                                                  PEP_ENGINE_MINOR,
   1.155 +                                                  PEP_ENGINE_PATCH);
   1.156 +}
   1.157 +
   1.158  DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
   1.159  {
   1.160      PEP_STATUS status = PEP_STATUS_OK;
   1.161 @@ -1057,6 +1201,14 @@
   1.162              (int)strlen(sql_trustword), &_session->trustword, NULL);
   1.163      assert(int_result == SQLITE_OK);
   1.164  
   1.165 +    int_result = sqlite3_prepare_v2(_session->db, sql_get_cached_engine_version,
   1.166 +            (int)strlen(sql_get_cached_engine_version), &_session->get_cached_engine_version, NULL);
   1.167 +    assert(int_result == SQLITE_OK);
   1.168 +
   1.169 +    int_result = sqlite3_prepare_v2(_session->db, sql_set_cached_engine_version,
   1.170 +            (int)strlen(sql_set_cached_engine_version), &_session->set_cached_engine_version, NULL);
   1.171 +    assert(int_result == SQLITE_OK);
   1.172 +
   1.173      int_result = sqlite3_prepare_v2(_session->db, sql_get_identity,
   1.174              (int)strlen(sql_get_identity), &_session->get_identity, NULL);
   1.175      assert(int_result == SQLITE_OK);
   1.176 @@ -1373,6 +1525,10 @@
   1.177                  sqlite3_finalize(session->log);
   1.178              if (session->trustword)
   1.179                  sqlite3_finalize(session->trustword);
   1.180 +            if (session->get_cached_engine_version)
   1.181 +                sqlite3_finalize(session->get_cached_engine_version);                
   1.182 +            if (session->set_cached_engine_version)
   1.183 +                sqlite3_finalize(session->set_cached_engine_version);                                
   1.184              if (session->get_identity)
   1.185                  sqlite3_finalize(session->get_identity);
   1.186              if (session->get_identity_without_trust_check)
     2.1 --- a/src/pEpEngine.h	Fri Jun 22 13:49:32 2018 +0200
     2.2 +++ b/src/pEpEngine.h	Mon Jun 25 09:26:49 2018 +0200
     2.3 @@ -1275,6 +1275,11 @@
     2.4  
     2.5  PEP_STATUS set_pgp_keypair(PEP_SESSION session, const char* fpr);
     2.6  
     2.7 +PEP_STATUS compare_cached_engine_version_to_other(int* result, 
     2.8 +                                                  int major, 
     2.9 +                                                  int minor, 
    2.10 +                                                  int patch);
    2.11 +
    2.12  #ifdef __cplusplus
    2.13  }
    2.14  #endif
     3.1 --- a/src/pEp_internal.h	Fri Jun 22 13:49:32 2018 +0200
     3.2 +++ b/src/pEp_internal.h	Mon Jun 25 09:26:49 2018 +0200
     3.3 @@ -133,6 +133,7 @@
     3.4  
     3.5      sqlite3_stmt *log;
     3.6      sqlite3_stmt *trustword;
     3.7 +    sqlite3_stmt *get_cached_engine_version;
     3.8      sqlite3_stmt *get_identity;
     3.9      sqlite3_stmt *get_identity_without_trust_check;
    3.10      sqlite3_stmt *get_identities_by_address;