mirror of
https://github.com/subsurface/subsurface.git
synced 2024-11-30 22:20:21 +00:00
Properly handle dive sites loaded from XML
We used to always create a new dive site structure when loading dive site data from XML. That is completely bogus, because it can (and does) create duplicate dive sites with the same UUID. Which makes the whole UUID pointless. So instead, look up the existing dive site associated with the UUID loaded from the XML, and try to merge the data properly if we already had dive site information for that UUID. Reported-by: Alessandro Volpi <volpial@gmail.com> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:
parent
fc55620d2d
commit
4a550e4d7d
3 changed files with 42 additions and 17 deletions
|
@ -2,6 +2,7 @@
|
|||
#include "divesite.h"
|
||||
#include "dive.h"
|
||||
#include "divelist.h"
|
||||
#include "membuffer.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
|
@ -105,12 +106,10 @@ static uint32_t dive_site_getUniqId()
|
|||
struct dive_site *alloc_or_get_dive_site(uint32_t uuid)
|
||||
{
|
||||
struct dive_site *ds;
|
||||
if (uuid) {
|
||||
if ((ds = get_dive_site_by_uuid(uuid)) != NULL) {
|
||||
fprintf(stderr, "PROBLEM: refusing to create dive site with the same uuid %08x\n", uuid);
|
||||
return ds;
|
||||
}
|
||||
}
|
||||
|
||||
if (uuid && (ds = get_dive_site_by_uuid(uuid)) != NULL)
|
||||
return ds;
|
||||
|
||||
int nr = dive_site_table.nr;
|
||||
int allocated = dive_site_table.allocated;
|
||||
struct dive_site **sites = dive_site_table.dive_sites;
|
||||
|
@ -205,7 +204,6 @@ uint32_t create_dive_site(const char *name, timestamp_t divetime)
|
|||
uint32_t uuid = create_divesite_uuid(name, divetime);
|
||||
struct dive_site *ds = alloc_or_get_dive_site(uuid);
|
||||
ds->name = copy_string(name);
|
||||
|
||||
return uuid;
|
||||
}
|
||||
|
||||
|
@ -273,6 +271,36 @@ void copy_dive_site(struct dive_site *orig, struct dive_site *copy)
|
|||
}
|
||||
}
|
||||
|
||||
static void merge_string(char **a, char **b)
|
||||
{
|
||||
char *s1 = *a, *s2 = *b;
|
||||
|
||||
if (same_string(s1, s2))
|
||||
return;
|
||||
|
||||
if (!s1) {
|
||||
*a = strdup(s2);
|
||||
return;
|
||||
}
|
||||
|
||||
*a = format_string("(%s) or (%s)", s1, s2);
|
||||
free(s1);
|
||||
}
|
||||
|
||||
void merge_dive_site(struct dive_site *a, struct dive_site *b)
|
||||
{
|
||||
if (!a->latitude.udeg) a->latitude.udeg = b->latitude.udeg;
|
||||
if (!a->longitude.udeg) a->longitude.udeg = b->longitude.udeg;
|
||||
merge_string(&a->name, &b->name);
|
||||
merge_string(&a->notes, &b->notes);
|
||||
merge_string(&a->description, &b->description);
|
||||
|
||||
if (!a->taxonomy.category) {
|
||||
a->taxonomy = b->taxonomy;
|
||||
memset(&b->taxonomy, 0, sizeof(b->taxonomy));
|
||||
}
|
||||
}
|
||||
|
||||
void clear_dive_site(struct dive_site *ds)
|
||||
{
|
||||
free(ds->name);
|
||||
|
|
|
@ -64,6 +64,7 @@ uint32_t get_dive_site_uuid_by_gps_and_name(char *name, degrees_t latitude, degr
|
|||
uint32_t get_dive_site_uuid_by_gps_proximity(degrees_t latitude, degrees_t longitude, int distance, struct dive_site **dsp);
|
||||
bool dive_site_is_empty(struct dive_site *ds);
|
||||
void copy_dive_site(struct dive_site *orig, struct dive_site *copy);
|
||||
void merge_dive_site(struct dive_site *a, struct dive_site *b);
|
||||
void clear_dive_site(struct dive_site *ds);
|
||||
unsigned int get_distance(degrees_t lat1, degrees_t lon1, degrees_t lat2, degrees_t lon2);
|
||||
uint32_t find_or_create_dive_site_with_name(const char *name, timestamp_t divetime);
|
||||
|
|
|
@ -1569,17 +1569,13 @@ static void dive_site_end(void)
|
|||
{
|
||||
if (!cur_dive_site)
|
||||
return;
|
||||
if (cur_dive_site->taxonomy.nr == 0) {
|
||||
free(cur_dive_site->taxonomy.category);
|
||||
cur_dive_site->taxonomy.category = NULL;
|
||||
}
|
||||
if (cur_dive_site->uuid) {
|
||||
// we intentionally call this with '0' to ensure we get
|
||||
// a new structure and then copy things into that new
|
||||
// structure a few lines below (which sets the correct
|
||||
// uuid)
|
||||
struct dive_site *ds = alloc_or_get_dive_site(0);
|
||||
if (cur_dive_site->taxonomy.nr == 0) {
|
||||
free(cur_dive_site->taxonomy.category);
|
||||
cur_dive_site->taxonomy.category = NULL;
|
||||
}
|
||||
copy_dive_site(cur_dive_site, ds);
|
||||
struct dive_site *ds = alloc_or_get_dive_site(cur_dive_site->uuid);
|
||||
merge_dive_site(ds, cur_dive_site);
|
||||
|
||||
if (verbose > 3)
|
||||
printf("completed dive site uuid %x8 name {%s}\n", ds->uuid, ds->name);
|
||||
|
|
Loading…
Reference in a new issue