because a friend was asking me to do so
authorvb
Sun, 22 Feb 2015 15:16:57 +0100
changeset 65b4b2f063a7c4
parent 64 d393c205d201
child 66 805bfae3cdc4
because a friend was asking me to do so
src/pEpEngine.c
     1.1 --- a/src/pEpEngine.c	Sun Feb 22 13:49:19 2015 +0100
     1.2 +++ b/src/pEpEngine.c	Sun Feb 22 15:16:57 2015 +0100
     1.3 @@ -6,6 +6,7 @@
     1.4  
     1.5  DYNAMIC_API PEP_STATUS init(PEP_SESSION *session)
     1.6  {
     1.7 +    PEP_STATUS status = PEP_STATUS_OK;
     1.8  	int int_result;
     1.9  	static const char *sql_log;
    1.10  	static const char *sql_safeword;
    1.11 @@ -35,14 +36,14 @@
    1.12      pEpSession *_session = (pEpSession *) calloc(1, sizeof(pEpSession));
    1.13  	assert(_session);
    1.14  	if (_session == NULL)
    1.15 -		return PEP_OUT_OF_MEMORY;
    1.16 +		goto enomem;
    1.17  
    1.18  	_session->version = PEP_ENGINE_VERSION;
    1.19  
    1.20      assert(LOCAL_DB);
    1.21      if (LOCAL_DB == NULL) {
    1.22 -        free(_session);
    1.23 -        return PEP_INIT_CANNOT_OPEN_DB;
    1.24 +        status = PEP_INIT_CANNOT_OPEN_DB;
    1.25 +        goto pep_error;
    1.26      }
    1.27  
    1.28  	int_result = sqlite3_open_v2(
    1.29 @@ -56,18 +57,16 @@
    1.30  		);
    1.31  
    1.32  	if (int_result != SQLITE_OK) {
    1.33 -		sqlite3_close_v2(_session->db);
    1.34 -        free(_session);
    1.35 -		return PEP_INIT_CANNOT_OPEN_DB;
    1.36 +		status = PEP_INIT_CANNOT_OPEN_DB;
    1.37 +        goto pep_error;
    1.38  	}
    1.39  
    1.40  	sqlite3_busy_timeout(_session->db, BUSY_WAIT_TIME);
    1.41  
    1.42      assert(SYSTEM_DB);
    1.43      if (SYSTEM_DB == NULL) {
    1.44 -		sqlite3_close_v2(_session->db);
    1.45 -        free(_session);
    1.46 -		return PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
    1.47 +		status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
    1.48 +        goto pep_error;
    1.49      }
    1.50  
    1.51  	int_result = sqlite3_open_v2(
    1.52 @@ -79,10 +78,8 @@
    1.53  		);
    1.54  
    1.55  	if (int_result != SQLITE_OK) {
    1.56 -		sqlite3_close_v2(_session->system_db);
    1.57 -		sqlite3_close_v2(_session->db);
    1.58 -        free(_session);
    1.59 -		return PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
    1.60 +		status = PEP_INIT_CANNOT_OPEN_SYSTEM_DB;
    1.61 +        goto pep_error;
    1.62  	}
    1.63  
    1.64  	sqlite3_busy_timeout(_session->system_db, 1000);
    1.65 @@ -227,21 +224,27 @@
    1.66              strlen(sql_get_trust), &_session->get_trust, NULL);
    1.67      assert(int_result == SQLITE_OK);
    1.68  
    1.69 -	sqlite3_reset(_session->log);
    1.70 -    sqlite3_bind_text(_session->log, 1, "init", -1, SQLITE_STATIC);
    1.71 -    sqlite3_bind_text(_session->log, 2, "pEp " PEP_ENGINE_VERSION, -1,
    1.72 -            SQLITE_STATIC);
    1.73 -	do {
    1.74 -		int_result = sqlite3_step(_session->log);
    1.75 -		assert(int_result == SQLITE_DONE || int_result == SQLITE_BUSY);
    1.76 -	} while (int_result == SQLITE_BUSY);
    1.77 -    sqlite3_reset(_session->log);
    1.78 +    status = init_cryptotech(_session, in_first);
    1.79 +    if (status != PEP_STATUS_OK)
    1.80 +        goto pep_error;
    1.81  
    1.82 -    init_cryptotech(_session, in_first);
    1.83 -    init_transport_system(_session, in_first);
    1.84 +    status = init_transport_system(_session, in_first);
    1.85 +    if (status != PEP_STATUS_OK)
    1.86 +        goto pep_error;
    1.87  
    1.88 -	*session = (void *) _session;
    1.89 +    status = log_event(_session, "init", "pEp " PEP_ENGINE_VERSION, NULL, NULL);
    1.90 +    if (status != PEP_STATUS_OK)
    1.91 +        goto pep_error;
    1.92 +
    1.93 +	*session = _session;
    1.94  	return PEP_STATUS_OK;
    1.95 +
    1.96 +enomem:
    1.97 +    status = PEP_OUT_OF_MEMORY;
    1.98 +
    1.99 +pep_error:
   1.100 +    release(_session);
   1.101 +    return status;
   1.102  }
   1.103  
   1.104  DYNAMIC_API void release(PEP_SESSION session)
   1.105 @@ -260,24 +263,34 @@
   1.106  
   1.107  	if (session) {
   1.108  		if (session->db) {
   1.109 -			sqlite3_finalize(session->safeword);
   1.110 -			sqlite3_finalize(session->log);
   1.111 -			sqlite3_finalize(session->get_identity);
   1.112 -			sqlite3_finalize(session->set_identity);
   1.113 -            sqlite3_finalize(session->set_person);
   1.114 -            sqlite3_finalize(session->set_pgp_keypair);
   1.115 -            sqlite3_finalize(session->set_trust);
   1.116 -            sqlite3_finalize(session->get_trust);
   1.117 +            if (session->safeword)
   1.118 +                sqlite3_finalize(session->safeword);
   1.119 +            if (session->log)
   1.120 +                sqlite3_finalize(session->log);
   1.121 +            if (session->get_identity)
   1.122 +                sqlite3_finalize(session->get_identity);
   1.123 +            if (session->set_identity)
   1.124 +                sqlite3_finalize(session->set_identity);
   1.125 +            if (session->set_person)
   1.126 +                sqlite3_finalize(session->set_person);
   1.127 +            if (session->set_pgp_keypair)
   1.128 +                sqlite3_finalize(session->set_pgp_keypair);
   1.129 +            if (session->set_trust)
   1.130 +                sqlite3_finalize(session->set_trust);
   1.131 +            if (session->get_trust)
   1.132 +                sqlite3_finalize(session->get_trust);
   1.133  
   1.134 -			sqlite3_close_v2(session->db);
   1.135 -			sqlite3_close_v2(session->system_db);
   1.136 +            if (session->db)
   1.137 +                sqlite3_close_v2(session->db);
   1.138 +            if (session->system_db)
   1.139 +                sqlite3_close_v2(session->system_db);
   1.140  		}
   1.141 +
   1.142 +        release_transport_system(session, out_last);
   1.143 +        release_cryptotech(session, out_last);
   1.144 +
   1.145 +        free(session);
   1.146      }
   1.147 -
   1.148 -    release_transport_system(session, out_last);
   1.149 -    release_cryptotech(session, out_last);
   1.150 -
   1.151 -	free(session);
   1.152  }
   1.153  
   1.154  stringlist_t *new_stringlist(const char *value)