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>
|
|
|
|
|
2011-04-21 13:56:06 +00:00
|
|
|
#include <time.h>
|
2006-02-12 21:52:18 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2009-07-02 17:20:27 +00:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2006-02-12 21:52:18 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
2009-07-02 17:20:27 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <process.h> /* For getpid() */
|
|
|
|
#endif
|
|
|
|
|
2006-02-12 21:52:18 +00:00
|
|
|
#include "tempfile.h"
|
2008-05-22 15:46:27 +00:00
|
|
|
#include <wsutil/file_util.h>
|
2006-02-12 21:52:18 +00:00
|
|
|
|
2009-07-02 17:20:27 +00:00
|
|
|
#ifndef __set_errno
|
|
|
|
#define __set_errno(x) errno=(x)
|
|
|
|
#endif
|
|
|
|
|
2010-11-16 15:37:37 +00:00
|
|
|
#define INITIAL_PATH_SIZE 128
|
|
|
|
#define TMP_FILE_SUFFIX "XXXXXX"
|
|
|
|
|
2009-07-02 17:20:27 +00:00
|
|
|
#ifndef HAVE_MKSTEMP
|
|
|
|
/* Generate a unique temporary file name from TEMPLATE.
|
2010-11-16 15:37:37 +00:00
|
|
|
The last six characters of TEMPLATE must be TMP_FILE_SUFFIX;
|
2009-07-02 17:20:27 +00:00
|
|
|
they are replaced with a string that makes the filename unique.
|
|
|
|
Returns a file descriptor open on the file for reading and writing. */
|
|
|
|
static int
|
|
|
|
mkstemp (template)
|
|
|
|
char *template;
|
|
|
|
{
|
|
|
|
static const char letters[]
|
|
|
|
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
size_t len;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
len = strlen (template);
|
2010-11-16 15:37:37 +00:00
|
|
|
if (len < 6 || strcmp (&template[len - 6], TMP_FILE_SUFFIX))
|
2009-07-02 17:20:27 +00:00
|
|
|
{
|
|
|
|
__set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_snprintf (&template[len - 5], 6, "%.5u",
|
|
|
|
(unsigned int) getpid () % 100000) != 5)
|
|
|
|
/* Inconceivable lossage. */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof (letters); ++i)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
template[len - 6] = letters[i];
|
|
|
|
|
|
|
|
fd = ws_open (template, O_RDWR|O_BINARY|O_CREAT|O_EXCL, 0600);
|
|
|
|
if (fd >= 0)
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We return the null string if we can't find a unique file name. */
|
|
|
|
|
|
|
|
template[0] = '\0';
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_MKSTEMP */
|
|
|
|
|
|
|
|
#ifndef HAVE_MKDTEMP
|
|
|
|
/* Generate a unique temporary directory name from TEMPLATE.
|
2010-11-16 15:37:37 +00:00
|
|
|
The last six characters of TEMPLATE must be TMP_FILE_SUFFIX;
|
2009-07-02 17:20:27 +00:00
|
|
|
they are replaced with a string that makes the filename unique.
|
|
|
|
Returns 0 on success or -1 on error (from mkdir(2)). */
|
|
|
|
char *
|
|
|
|
mkdtemp (template)
|
|
|
|
char *template;
|
|
|
|
{
|
|
|
|
static const char letters[]
|
|
|
|
= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
|
size_t len;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
len = strlen (template);
|
2010-11-16 15:37:37 +00:00
|
|
|
if (len < 6 || strcmp (&template[len - 6], TMP_FILE_SUFFIX))
|
2009-07-02 17:20:27 +00:00
|
|
|
{
|
|
|
|
__set_errno (EINVAL);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_snprintf (&template[len - 5], 6, "%.5u",
|
|
|
|
(unsigned int) getpid () % 100000) != 5)
|
|
|
|
/* Inconceivable lossage. */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < sizeof (letters); ++i)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
template[len - 6] = letters[i];
|
|
|
|
|
|
|
|
ret = ws_mkdir(template, 0700);
|
|
|
|
if (ret >= 0)
|
|
|
|
return template;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We return the null string if we can't find a unique file name. */
|
|
|
|
|
|
|
|
template[0] = '\0';
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_MKDTEMP */
|
|
|
|
|
2010-11-16 15:37:37 +00:00
|
|
|
#define MAX_TEMPFILES 3
|
2009-07-01 23:36:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2010-11-16 15:37:37 +00:00
|
|
|
static struct _tf {
|
|
|
|
char *path;
|
2010-11-16 16:40:16 +00:00
|
|
|
unsigned long len;
|
2010-11-16 15:37:37 +00:00
|
|
|
} tf[MAX_TEMPFILES];
|
2009-07-01 23:36:51 +00:00
|
|
|
static int idx;
|
2010-11-16 15:37:37 +00:00
|
|
|
|
2009-07-01 23:36:51 +00:00
|
|
|
const char *tmp_dir;
|
|
|
|
int old_umask;
|
|
|
|
int fd;
|
2010-11-16 15:37:37 +00:00
|
|
|
time_t current_time;
|
|
|
|
char timestr[14 + 1];
|
|
|
|
gchar *tmp_file;
|
|
|
|
gchar sep[2] = {0, 0};
|
2006-02-12 21:52:18 +00:00
|
|
|
|
2010-11-16 15:37:37 +00:00
|
|
|
idx = (idx + 1) % MAX_TEMPFILES;
|
2009-07-01 23:36:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate the buffer if it's not already allocated.
|
|
|
|
*/
|
2010-11-16 15:37:37 +00:00
|
|
|
if (tf[idx].path == NULL) {
|
|
|
|
tf[idx].len = INITIAL_PATH_SIZE;
|
|
|
|
tf[idx].path = (char *)g_malloc(tf[idx].len);
|
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
|
|
|
|
2010-11-16 15:37:37 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
_tzset();
|
|
|
|
#endif
|
|
|
|
current_time = time(NULL);
|
|
|
|
strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time));
|
|
|
|
sep[0] = G_DIR_SEPARATOR;
|
|
|
|
tmp_file = g_strconcat(tmp_dir, sep, pfx, "_", timestr, "_", TMP_FILE_SUFFIX, NULL);
|
|
|
|
if (strlen(tmp_file) > tf[idx].len) {
|
2010-11-16 20:08:31 +00:00
|
|
|
tf[idx].len = (int)strlen(tmp_file) + 1;
|
2010-11-16 15:37:37 +00:00
|
|
|
tf[idx].path = (char *)g_realloc(tf[idx].path, tf[idx].len);
|
2006-02-12 21:52:18 +00:00
|
|
|
}
|
2010-11-16 15:37:37 +00:00
|
|
|
g_strlcpy(tf[idx].path, tmp_file, tf[idx].len);
|
|
|
|
g_free(tmp_file);
|
2006-02-12 21:52:18 +00:00
|
|
|
|
2009-07-01 23:36:51 +00:00
|
|
|
if (namebuf) {
|
2010-11-16 15:37:37 +00:00
|
|
|
*namebuf = tf[idx].path;
|
2009-07-01 23:36:51 +00:00
|
|
|
}
|
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);
|
2010-11-16 15:37:37 +00:00
|
|
|
fd = mkstemp(tf[idx].path);
|
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
|
|
|
}
|
2009-07-02 17:20:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a directory with the given prefix (e.g. "wireshark"). The path
|
|
|
|
* is created using g_get_tmp_dir and mkdtemp.
|
|
|
|
*
|
2010-08-26 20:31:09 +00:00
|
|
|
* @param namebuf
|
2009-07-02 17:20:27 +00:00
|
|
|
* @param pfx A prefix for the temporary directory.
|
|
|
|
* @return The temporary directory path on success, or NULL on failure.
|
|
|
|
* Must NOT be freed.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
create_tempdir(char **namebuf, const char *pfx)
|
|
|
|
{
|
|
|
|
static char *td_path[3];
|
|
|
|
static int td_path_len[3];
|
|
|
|
static int idx;
|
|
|
|
const char *tmp_dir;
|
|
|
|
|
|
|
|
idx = (idx + 1) % 3;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate the buffer if it's not already allocated.
|
|
|
|
*/
|
|
|
|
if (td_path[idx] == NULL) {
|
|
|
|
td_path_len[idx] = INITIAL_PATH_SIZE;
|
2010-03-11 00:36:45 +00:00
|
|
|
td_path[idx] = (char *)g_malloc(td_path_len[idx]);
|
2009-07-02 17:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can't use get_tempfile_path here because we're called from dumpcap.c.
|
|
|
|
*/
|
|
|
|
tmp_dir = g_get_tmp_dir();
|
|
|
|
|
|
|
|
while (g_snprintf(td_path[idx], td_path_len[idx], "%s%c%s" TMP_FILE_SUFFIX, tmp_dir, G_DIR_SEPARATOR, pfx) > td_path_len[idx]) {
|
|
|
|
td_path_len[idx] *= 2;
|
2010-03-11 00:36:45 +00:00
|
|
|
td_path[idx] = (char *)g_realloc(td_path[idx], td_path_len[idx]);
|
2009-07-02 17:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (namebuf) {
|
|
|
|
*namebuf = td_path[idx];
|
|
|
|
}
|
|
|
|
return mkdtemp(td_path[idx]);
|
|
|
|
}
|