2006-02-12 21:52:18 +00:00
|
|
|
/* tempfile.c
|
|
|
|
* Routines to create temporary files
|
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
2006-02-12 21:52:18 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-02-07 11:26:45 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2006-02-12 21:52:18 +00:00
|
|
|
*/
|
|
|
|
|
2012-09-20 01:29:52 +00:00
|
|
|
#include "config.h"
|
2006-02-12 21:52:18 +00:00
|
|
|
|
2019-09-01 18:37:38 +00:00
|
|
|
#include <glib.h>
|
2006-02-12 21:52:18 +00:00
|
|
|
#include "tempfile.h"
|
2009-07-02 17:20:27 +00:00
|
|
|
|
2019-09-01 18:37:38 +00:00
|
|
|
/**
|
|
|
|
* Create a tempfile with the given prefix (e.g. "wireshark"). The path
|
|
|
|
* is created using g_file_open_tmp.
|
2013-01-26 15:41:16 +00:00
|
|
|
*
|
2019-09-01 18:37:38 +00:00
|
|
|
* @param namebuf [in,out] If not NULL, receives the full path of the temp file.
|
|
|
|
* Must be freed.
|
|
|
|
* @param pfx [in] A prefix for the temporary file.
|
2016-05-29 17:52:19 +00:00
|
|
|
* @param sfx [in] A file extension for the temporary file. NULL can be passed
|
|
|
|
* if no file extension is needed
|
2021-04-14 14:01:49 +00:00
|
|
|
* @param err [out] Any error returned by g_file_open_tmp. May be NULL
|
2016-05-29 17:52:19 +00:00
|
|
|
* @return The file descriptor of the new tempfile, from mkstemps().
|
2009-07-01 23:36:51 +00:00
|
|
|
*/
|
|
|
|
int
|
2019-09-01 18:37:38 +00:00
|
|
|
create_tempfile(gchar **namebuf, const char *pfx, const char *sfx, GError **err)
|
2006-02-12 21:52:18 +00:00
|
|
|
{
|
2014-10-14 15:12:16 +00:00
|
|
|
int fd;
|
2020-01-06 14:22:42 +00:00
|
|
|
gchar *safe_pfx = NULL;
|
2014-10-14 15:12:16 +00:00
|
|
|
|
2020-01-06 14:22:42 +00:00
|
|
|
if (pfx) {
|
|
|
|
/* The characters in "delimiters" come from:
|
|
|
|
* https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions.
|
|
|
|
* Add to the list as necessary for other OS's.
|
|
|
|
*/
|
|
|
|
const gchar *delimiters = "<>:\"/\\|?*"
|
|
|
|
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a"
|
|
|
|
"\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14"
|
|
|
|
"\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f";
|
|
|
|
|
|
|
|
/* Sanitize the pfx to resolve bug 7877 */
|
|
|
|
safe_pfx = g_strdup(pfx);
|
|
|
|
safe_pfx = g_strdelimit(safe_pfx, delimiters, '-');
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar* filetmpl = g_strdup_printf("%sXXXXXX%s", safe_pfx ? safe_pfx : "", sfx ? sfx : "");
|
|
|
|
g_free(safe_pfx);
|
2006-02-12 21:52:18 +00:00
|
|
|
|
2019-09-01 18:37:38 +00:00
|
|
|
fd = g_file_open_tmp(filetmpl, namebuf, err);
|
2014-10-14 15:12:16 +00:00
|
|
|
|
2019-09-01 18:37:38 +00:00
|
|
|
g_free(filetmpl);
|
2014-10-14 15:12:16 +00:00
|
|
|
return fd;
|
2006-02-12 21:52:18 +00:00
|
|
|
}
|
2009-07-02 17:20:27 +00:00
|
|
|
|
2014-10-14 15:12:16 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2014-10-14 15:12:16 +00:00
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 2
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=2 tabstop=8 expandtab:
|
|
|
|
* :indentSize=2:tabSize=8:noTabs=true:
|
|
|
|
*/
|