summaryrefslogtreecommitdiffstats
path: root/divesite.c
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2015-08-24 10:37:18 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-08-25 10:43:48 -0700
commite03b553e80a00b07757f51f7866bc666b807dce8 (patch)
tree5c3ec6d5cc5bdc556f01d4a524a3df44113fbbb4 /divesite.c
parent6eed3155e6a84f1b27b5340b45d6deb801fee42d (diff)
downloadsubsurface-e03b553e80a00b07757f51f7866bc666b807dce8.tar.gz
Make created dive site uuid deterministic
Having random uuids seemed like a good idea, but there are several situations where they really cause problems. One is merging dive file imports from V2 logfiles. Another is testing such imports. Instead of making the uuid random we now hash the name and add the timestamp of the first dive associated with this dive site to the hash (first in this context is "first encountered" with no guarantee that it is the chronologically first). This way V2 imports create deterministic uuids but uuid conflicts are still extremely unlikely, even if the user has multiple dive sites with the same name. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'divesite.c')
-rw-r--r--divesite.c35
1 files changed, 26 insertions, 9 deletions
diff --git a/divesite.c b/divesite.c
index 4ffdcd78f..035c1b82d 100644
--- a/divesite.c
+++ b/divesite.c
@@ -104,10 +104,13 @@ struct dive_site *alloc_dive_site(uint32_t uuid)
exit(1);
sites[nr] = ds;
dive_site_table.nr = nr + 1;
- if (uuid)
+ if (uuid) {
+ if (get_dive_site_by_uuid(uuid))
+ fprintf(stderr, "PROBLEM: duplicate uuid %08x\n", uuid);
ds->uuid = uuid;
- else
+ } else {
ds->uuid = dive_site_getUniqId();
+ }
return ds;
}
@@ -157,19 +160,33 @@ void delete_dive_site(uint32_t id)
}
}
+uint32_t create_divesite_uuid(const char *name, timestamp_t divetime)
+{
+ unsigned char hash[20];
+ SHA_CTX ctx;
+ SHA1_Init(&ctx);
+ SHA1_Update(&ctx, &divetime, sizeof(timestamp_t));
+ SHA1_Update(&ctx, name, strlen(name));
+ SHA1_Final(hash, &ctx);
+ // now return the first 32 of the 160 bit hash
+ return *(uint32_t *)hash;
+}
+
/* allocate a new site and add it to the table */
-uint32_t create_dive_site(const char *name)
+uint32_t create_dive_site(const char *name, timestamp_t divetime)
{
- struct dive_site *ds = alloc_dive_site(0);
+ uint32_t uuid = create_divesite_uuid(name, divetime);
+ struct dive_site *ds = alloc_dive_site(uuid);
ds->name = copy_string(name);
- return ds->uuid;
+ return uuid;
}
/* same as before, but with GPS data */
-uint32_t create_dive_site_with_gps(const char *name, degrees_t latitude, degrees_t longitude)
+uint32_t create_dive_site_with_gps(const char *name, degrees_t latitude, degrees_t longitude, timestamp_t divetime)
{
- struct dive_site *ds = alloc_dive_site(0);
+ uint32_t uuid = create_divesite_uuid(name, divetime);
+ struct dive_site *ds = alloc_dive_site(uuid);
ds->name = copy_string(name);
ds->latitude = latitude;
ds->longitude = longitude;
@@ -231,7 +248,7 @@ void clear_dive_site(struct dive_site *ds)
free_taxonomy(&ds->taxonomy);
}
-uint32_t find_or_create_dive_site_with_name(const char *name)
+uint32_t find_or_create_dive_site_with_name(const char *name, timestamp_t divetime)
{
int i;
struct dive_site *ds;
@@ -244,5 +261,5 @@ uint32_t find_or_create_dive_site_with_name(const char *name)
}
if (ds)
return ds->uuid;
- return create_dive_site(name);
+ return create_dive_site(name, divetime);
}