open_memstream on non-linux?

Martin Gysel me at bearsh.org
Wed Mar 13 02:08:04 PDT 2013


Am 12.03.2013 20:24, schrieb Miika Turkia:
> On Tue, Mar 12, 2013 at 2:48 PM, Lubomir I. Ivanov <neolit123 at gmail.com> wrote:
>> On 12 March 2013 14:45, Lubomir I. Ivanov <neolit123 at gmail.com> wrote:
>>> On 12 March 2013 10:35, Martin Gysel <me at bearsh.org> wrote:
>>>> Am 11.03.2013 18:09, schrieb Lubomir I. Ivanov:
>>>>> On 11 March 2013 18:19, Dirk Hohndel <dirk at hohndel.org> wrote:
>>>>>> There is some discussion and code on working around that here…
>>>>>>
>>>>>> http://stackoverflow.com/questions/10305095/can-i-replace-a-call-to-open-memstream-with-a-malloc-and-an-implicit-cast
>>>>>>
>>>>>> which looks way overkill to me.
>>>>>>
>>>>>> Miika - any way you could reimplement this differently? :-)
>>>>>
>>>>> here is my attempt for miika's reference.
>>>>>
>>>>> it also now, uses ./ for the temp zip file, since obviously /tmp/
>>>>> isn't portable to non-UNIX OS. instead i would write the temp file in
>>>>> the home directory, which we should retrieve in a OS specific way
>>>>> using e.g. subsurface_get_home_path().
>>>>
>>>> For me it seems to be a bad idea to create a tmp file in the home
>>>> directory, just image, if subsurface crashes once a tmp file has been
>>>> created, it will never get deleted again. there's a reason places like
>>>> /tmp are there.
>>>>
>>>
>>> that's certainly true.
>>>
>>>> looking through the glib's documentation I found
>>>> g_mkstemp() or g_file_open_tmp().
>>>> g_file_open_tmp() for example takes a filename template (no directory)
>>>> creates and opens a tmp file in the appropriate location. it also gives
>>>> you the actual name of the file based on the template. there's no
>>>> mention that this doesn't work on windows...
>>>>
>>>
>>> yep, g_file_open_tmp() should do it, given g_file_open_tmp() succeeds.
>>>
>>
>> i meant g_get_tmp_dir(), which searches for the temp directory...
> 
> Here is a version (on top of Lubomir's patch) that uses
> g_get_tmp_dir() and g_build_filename() to create the temporary file
> name.
> 
> miika
> 

maybe it is a little bit nip-picking...

from your patch:

 	struct zip_source *s[dive_table.nr];
 	struct zip *zip;
+	const gchar *tmpdir = g_get_tmp_dir();

 	/*
 	 * Creating a temporary .DLD file to be eventually uploaded to
 	 * divelogs.de. I wonder if this could be done in-memory.
 	 */

-	template = strdup("./export.DLD-XXXXXX");
+	template = g_build_filename(tmpdir, "export.DLD-XXXXXX", NULL);
 	tempfile = (char *)mktemp(template);

just wondering why you call g_get_tmp_dir() followed by
g_build_filename() followed by mktemp() and not just simple use
g_file_open_tmp()? supposing the glib guys did a good job it seems to be
less error prone to me...

/martin


More information about the subsurface mailing list