2006-02-12 21:52:18 +00:00
|
|
|
/* tempfile.c
|
|
|
|
* Routines to create temporary files
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
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
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "tempfile.h"
|
2007-01-12 03:05:28 +00:00
|
|
|
#include "mkstemp.h"
|
2008-05-22 15:46:27 +00:00
|
|
|
#include <wsutil/file_util.h>
|
2006-02-12 21:52:18 +00:00
|
|
|
|
2009-07-01 23:36:51 +00:00
|
|
|
#define INITIAL_PATH_SIZE 128
|
|
|
|
#define TMP_FILE_SUFFIX "XXXXXXXXXX"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a tempfile with the given prefix (e.g. "wireshark").
|
|
|
|
*
|
|
|
|
* @param namebuf If not NULL, receives the full path of the temp file.
|
|
|
|
* Should NOT be freed.
|
|
|
|
* @param pfx A prefix for the temporary file.
|
|
|
|
* @return The file descriptor of the new tempfile, from mkstemp().
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
create_tempfile(char **namebuf, const char *pfx)
|
2006-02-12 21:52:18 +00:00
|
|
|
{
|
2009-07-01 23:36:51 +00:00
|
|
|
static char *tf_path[3];
|
|
|
|
static int tf_path_len[3];
|
|
|
|
static int idx;
|
|
|
|
const char *tmp_dir;
|
|
|
|
int old_umask;
|
|
|
|
int fd;
|
2006-02-12 21:52:18 +00:00
|
|
|
|
2009-07-01 23:36:51 +00:00
|
|
|
idx = (idx + 1) % 3;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate the buffer if it's not already allocated.
|
|
|
|
*/
|
|
|
|
if (tf_path[idx] == NULL) {
|
|
|
|
tf_path_len[idx] = INITIAL_PATH_SIZE;
|
|
|
|
tf_path[idx] = g_malloc(tf_path_len[idx]);
|
2006-02-12 21:52:18 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 23:36:51 +00:00
|
|
|
/*
|
|
|
|
* We can't use get_tempfile_path here because we're called from dumpcap.c.
|
|
|
|
*/
|
|
|
|
tmp_dir = g_get_tmp_dir();
|
2006-02-12 21:52:18 +00:00
|
|
|
|
2009-07-01 23:36:51 +00:00
|
|
|
while (g_snprintf(tf_path[idx], tf_path_len[idx], "%s%c%s" TMP_FILE_SUFFIX, tmp_dir, G_DIR_SEPARATOR, pfx) > tf_path_len[idx]) {
|
|
|
|
tf_path_len[idx] *= 2;
|
|
|
|
tf_path[idx] = g_realloc(tf_path[idx], tf_path_len[idx]);
|
2006-02-12 21:52:18 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 23:36:51 +00:00
|
|
|
if (namebuf) {
|
|
|
|
*namebuf = tf_path[idx];
|
|
|
|
}
|
2006-02-12 21:52:18 +00:00
|
|
|
/* The Single UNIX Specification doesn't say that "mkstemp()"
|
|
|
|
creates the temporary file with mode rw-------, so we
|
|
|
|
won't assume that all UNIXes will do so; instead, we set
|
|
|
|
the umask to 0077 to take away all group and other
|
|
|
|
permissions, attempt to create the file, and then put
|
|
|
|
the umask back. */
|
|
|
|
old_umask = umask(0077);
|
2009-07-01 23:36:51 +00:00
|
|
|
fd = mkstemp(tf_path[idx]);
|
2006-02-12 21:52:18 +00:00
|
|
|
umask(old_umask);
|
2009-07-01 23:36:51 +00:00
|
|
|
return fd;
|
2006-02-12 21:52:18 +00:00
|
|
|
}
|