From 570b4c8be244de329b76780a819db425e1c33bb7 Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Sun, 14 Apr 2019 16:39:12 +0700 Subject: [PATCH] libmsc/db.c: get rid of hard-coded SMS expiry threshold The initial idea of the SMS expiry threshold was to avoid storing SMS messages with too long validity time (e.g. 63 weeks). Unfortunately, neither this feature was properly documented, nor the expiry threshold is configurable. Moreover, it has been implemented in a wrong way, so instead of deleting the oldest expired message, it would delete the youngest one or nothing: SELECT ... FROM SMS ORDER BY created LIMIT 1; while it should be sorted by 'valid_until' in ascending order: SELECT .. FROM SMS ORDER BY valid_until LIMIT 1; Thus, if the oldest message is expired, it gets deleted. If the oldest message is not expired yet, there is nothing to delete. Change-Id: I0ce6b1ab50986dc69a2be4ea62b6a24c7f3f8f0a --- src/libmsc/db.c | 28 +++++++++++----------------- tests/db_sms/db_sms_test.ok | 3 +-- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/src/libmsc/db.c b/src/libmsc/db.c index 705af6195..241bc866e 100644 --- a/src/libmsc/db.c +++ b/src/libmsc/db.c @@ -1021,20 +1021,15 @@ int db_sms_delete_sent_message_by_id(unsigned long long sms_id) } -static int delete_expired_sms(unsigned long long sms_id, time_t created, time_t validity_timestamp) +static int delete_expired_sms(unsigned long long sms_id, time_t validity_timestamp) { dbi_result result; - time_t now, min_created; + time_t now; now = time(NULL); - if (validity_timestamp > now) - return -1; - /* Our SMS expiry threshold is hard-coded to roughly 2 weeks at the moment. */ - min_created = now - (time_t)(60 * 60 * 24 * 7 * 2); - if (min_created < 0) /* bogus system clock? */ - return -1; - if (created >= min_created) /* not yet expired */ + /* Net yet expired */ + if (validity_timestamp > now) return -1; result = dbi_conn_queryf(conn, "DELETE FROM SMS WHERE id = %llu", sms_id); @@ -1049,9 +1044,9 @@ static int delete_expired_sms(unsigned long long sms_id, time_t created, time_t int db_sms_delete_expired_message_by_id(unsigned long long sms_id) { dbi_result result; - time_t created, validity_timestamp; + time_t validity_timestamp; - result = dbi_conn_queryf(conn, "SELECT created,valid_until FROM SMS WHERE id = %llu", sms_id); + result = dbi_conn_queryf(conn, "SELECT valid_until FROM SMS WHERE id = %llu", sms_id); if (!result) return -1; if (!next_row(result)) { @@ -1059,29 +1054,28 @@ int db_sms_delete_expired_message_by_id(unsigned long long sms_id) return -1; } - created = dbi_result_get_datetime(result, "created"); validity_timestamp = dbi_result_get_datetime(result, "valid_until"); dbi_result_free(result); - return delete_expired_sms(sms_id, created, validity_timestamp); + return delete_expired_sms(sms_id, validity_timestamp); } void db_sms_delete_oldest_expired_message(void) { dbi_result result; - result = dbi_conn_queryf(conn, "SELECT id,created,valid_until FROM SMS ORDER BY created LIMIT 1"); + result = dbi_conn_queryf(conn, "SELECT id,valid_until FROM SMS " + "ORDER BY valid_until LIMIT 1"); if (!result) return; if (next_row(result)) { unsigned long long sms_id; - time_t created, validity_timestamp; + time_t validity_timestamp; sms_id = dbi_result_get_ulonglong(result, "id"); - created = dbi_result_get_datetime(result, "created"); validity_timestamp = dbi_result_get_datetime(result, "valid_until"); - delete_expired_sms(sms_id, created, validity_timestamp); + delete_expired_sms(sms_id, validity_timestamp); } dbi_result_free(result); diff --git a/tests/db_sms/db_sms_test.ok b/tests/db_sms/db_sms_test.ok index c61b1f47c..486dcc356 100644 --- a/tests/db_sms/db_sms_test.ok +++ b/tests/db_sms/db_sms_test.ok @@ -68,8 +68,7 @@ DDB NOTICE test_db_sms_get('Truncated TP-UD (255 octets, 8-bit encoding)'): succ DDB NOTICE verify_sms('Truncated TP-UD (255 octets, 8-bit encoding)'): TP-User-Data mismatch DDB NOTICE test_db_sms_get('Same MSISDN #1'): failure, as expected DDB NOTICE test_db_sms_get('Same MSISDN #2'): failure, as expected -DDB NOTICE test_db_sms_get('Expired SMS'): unexpected result -DDB NOTICE verify_sms('Expired SMS'): match +DDB NOTICE test_db_sms_get('Expired SMS'): failure, as expected DDB NOTICE test_db_sms_get('Empty TP-UD'): success, as expected DDB NOTICE verify_sms('Empty TP-UD'): match full talloc report on 'null_context' (total 0 bytes in 1 blocks)