Dive site: make UUID generation deterministic

Instead of using a random UUID, use an SHA1 hash of name, description
and notes (if defined). This is necessary for testing.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2019-04-05 21:33:27 +02:00 committed by Dirk Hohndel
parent d5953318ca
commit b024ca101e
2 changed files with 22 additions and 8 deletions

View file

@ -6,6 +6,7 @@
#include "divelist.h"
#include "membuffer.h"
#include "table.h"
#include "sha1.h"
#include <math.h>
@ -129,14 +130,27 @@ static MAKE_REMOVE(dive_site_table, struct dive_site *, dive_site)
int add_dive_site_to_table(struct dive_site *ds, struct dive_site_table *ds_table)
{
/* If the site doesn't yet have an UUID, create a new one.
* Make this deterministic for testing. */
if (!ds->uuid) {
SHA_CTX ctx;
uint32_t csum[5];
SHA1_Init(&ctx);
if (ds->name)
SHA1_Update(&ctx, ds->name, strlen(ds->name));
if (ds->description)
SHA1_Update(&ctx, ds->description, strlen(ds->description));
if (ds->notes)
SHA1_Update(&ctx, ds->notes, strlen(ds->notes));
SHA1_Final((unsigned char *)csum, &ctx);
ds->uuid = csum[0];
}
/* Take care to never have the same uuid twice. This could happen on
* reimport of a log where the dive sites have diverged */
while (ds->uuid == 0 || get_dive_site_by_uuid(ds->uuid, ds_table) != NULL) {
ds->uuid = rand() & 0xff;
ds->uuid |= (rand() & 0xff) << 8;
ds->uuid |= (rand() & 0xff) << 16;
ds->uuid |= (rand() & 0xff) << 24;
}
while (ds->uuid == 0 || get_dive_site_by_uuid(ds->uuid, ds_table) != NULL)
++ds->uuid;
int idx = dive_site_table_get_insertion_index(ds_table, ds);
add_to_dive_site_table(ds_table, idx, ds);

View file

@ -4,11 +4,11 @@
<divecomputerid model='Vyper Air' deviceid='ffffffff' serial='20400612'/>
</settings>
<divesites>
<site uuid='8c1836f0' name='USA, Hoodsport, WA / Sund Rock' gps='47.400269 -123.142632'>
<site uuid='64785a00' name='USA, Hoodsport, WA / Sund Rock' gps='47.400269 -123.142632'>
</site>
</divesites>
<dives>
<dive number='1' divesiteid='8c1836f0' date='2011-05-17' time='11:01:00' duration='32:00 min'>
<dive number='1' divesiteid='64785a00' date='2011-05-17' time='11:01:00' duration='32:00 min'>
<buddy>Linus</buddy>
<notes>Second deep dive
Linus and I planned and executed - Alex just watching