mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
DLD upload: Avoid using open_memstream() and non-portable functions
patch contains: - replacement for open_memstream() - storage of the temporary zip file in the OS temporary directory - replacement usage of mktemp() with g_mkstemp() patch based on work by Miika Turkia. [Dirk Hohndel: as suggested by Miika I commented out the deletion of the DLD file] Acked-by: Miika Turkia <miika.turkia@gmail.com> Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
87fe20600f
commit
099953e776
1 changed files with 27 additions and 11 deletions
38
divelist.c
38
divelist.c
|
@ -10,6 +10,7 @@
|
||||||
* void mark_divelist_changed(int changed)
|
* void mark_divelist_changed(int changed)
|
||||||
* int unsaved_changes()
|
* int unsaved_changes()
|
||||||
*/
|
*/
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -1977,7 +1978,7 @@ static void export_selected_dives_cb(GtkWidget *menuitem, GtkTreePath *path)
|
||||||
int i;
|
int i;
|
||||||
struct dive *dive;
|
struct dive *dive;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
char filename[PATH_MAX], *tempfile, *template;
|
char filename[PATH_MAX], *tempfile;
|
||||||
size_t streamsize;
|
size_t streamsize;
|
||||||
char *membuf;
|
char *membuf;
|
||||||
xmlDoc *doc;
|
xmlDoc *doc;
|
||||||
|
@ -1985,14 +1986,17 @@ static void export_selected_dives_cb(GtkWidget *menuitem, GtkTreePath *path)
|
||||||
xmlDoc *transformed;
|
xmlDoc *transformed;
|
||||||
struct zip_source *s[dive_table.nr];
|
struct zip_source *s[dive_table.nr];
|
||||||
struct zip *zip;
|
struct zip *zip;
|
||||||
|
const gchar *tmpdir = g_get_tmp_dir();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Creating a temporary .DLD file to be eventually uploaded to
|
* Creating a temporary .DLD file to be eventually uploaded to
|
||||||
* divelogs.de. I wonder if this could be done in-memory.
|
* divelogs.de. I wonder if this could be done in-memory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
template = strdup("/tmp/export.DLD-XXXXXX");
|
tempfile = g_build_filename(tmpdir, "export.DLD-XXXXXX", NULL);
|
||||||
tempfile = mktemp(template);
|
int fd = g_mkstemp(tempfile);
|
||||||
|
if (fd != -1)
|
||||||
|
close(fd);
|
||||||
zip = zip_open(tempfile, ZIP_CREATE, NULL);
|
zip = zip_open(tempfile, ZIP_CREATE, NULL);
|
||||||
|
|
||||||
if (!zip)
|
if (!zip)
|
||||||
|
@ -2010,14 +2014,17 @@ static void export_selected_dives_cb(GtkWidget *menuitem, GtkTreePath *path)
|
||||||
if (!dive->selected)
|
if (!dive->selected)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/*
|
f = tmpfile();
|
||||||
* Saving the dive is done into a memory buffer
|
|
||||||
*/
|
|
||||||
|
|
||||||
f = open_memstream(&membuf, &streamsize);
|
|
||||||
if (!f)
|
if (!f)
|
||||||
return;
|
return;
|
||||||
save_dive(f, dive);
|
save_dive(f, dive);
|
||||||
|
fseek(f, 0, SEEK_END);
|
||||||
|
streamsize = ftell(f);
|
||||||
|
rewind(f);
|
||||||
|
membuf = malloc(streamsize + 1);
|
||||||
|
if (!membuf || !fread(membuf, streamsize, 1, f))
|
||||||
|
return;
|
||||||
|
membuf[streamsize] = 0;
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -2030,7 +2037,10 @@ static void export_selected_dives_cb(GtkWidget *menuitem, GtkTreePath *path)
|
||||||
if (!doc)
|
if (!doc)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
free((void *)membuf);
|
||||||
xslt = get_stylesheet("divelogs-export.xslt");
|
xslt = get_stylesheet("divelogs-export.xslt");
|
||||||
|
if (!xslt)
|
||||||
|
return;
|
||||||
transformed = xsltApplyStylesheet(xslt, doc, NULL);
|
transformed = xsltApplyStylesheet(xslt, doc, NULL);
|
||||||
xsltFreeStylesheet(xslt);
|
xsltFreeStylesheet(xslt);
|
||||||
xmlDocDumpMemory(transformed, (xmlChar **) &membuf, (int *)&streamsize);
|
xmlDocDumpMemory(transformed, (xmlChar **) &membuf, (int *)&streamsize);
|
||||||
|
@ -2044,7 +2054,8 @@ static void export_selected_dives_cb(GtkWidget *menuitem, GtkTreePath *path)
|
||||||
snprintf(filename, PATH_MAX, "%d.xml", i + 1);
|
snprintf(filename, PATH_MAX, "%d.xml", i + 1);
|
||||||
if ((s[i]=zip_source_buffer(zip, membuf, streamsize, 0)) == NULL || zip_add(zip, filename, s[i]) == -1)
|
if ((s[i]=zip_source_buffer(zip, membuf, streamsize, 0)) == NULL || zip_add(zip, filename, s[i]) == -1)
|
||||||
fprintf(stderr, "failed to include dive %d\n", i);
|
fprintf(stderr, "failed to include dive %d\n", i);
|
||||||
|
if (membuf)
|
||||||
|
free((void *)membuf);
|
||||||
}
|
}
|
||||||
zip_close(zip);
|
zip_close(zip);
|
||||||
|
|
||||||
|
@ -2053,9 +2064,14 @@ static void export_selected_dives_cb(GtkWidget *menuitem, GtkTreePath *path)
|
||||||
* message and use proper dialog to inform user of the success
|
* message and use proper dialog to inform user of the success
|
||||||
* or failure of the upload
|
* or failure of the upload
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fprintf(stderr, "Created .DLD file %s\n", tempfile);
|
fprintf(stderr, "Created .DLD file %s\n", tempfile);
|
||||||
free(template);
|
|
||||||
|
/* TODO: the file needs to be deleted after the upload
|
||||||
|
|
||||||
|
g_unlink(tempfile);
|
||||||
|
|
||||||
|
*/
|
||||||
|
g_free(tempfile);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue