2 * Routines to create temporary files
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
49 #include <process.h> /* For getpid() */
53 #include <wsutil/file_util.h>
56 #define __set_errno(x) errno=(x)
59 #define INITIAL_PATH_SIZE 128
60 #define TMP_FILE_SUFFIX "XXXXXX"
63 /* Generate a unique temporary file name from TEMPLATE.
64 The last six characters of TEMPLATE must be TMP_FILE_SUFFIX;
65 they are replaced with a string that makes the filename unique.
66 Returns a file descriptor open on the file for reading and writing. */
71 static const char letters[]
72 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
76 len = strlen (template);
77 if (len < 6 || strcmp (&template[len - 6], TMP_FILE_SUFFIX))
83 if (g_snprintf (&template[len - 5], 6, "%.5u",
84 (unsigned int) getpid () % 100000) != 5)
85 /* Inconceivable lossage. */
88 for (i = 0; i < sizeof (letters); ++i)
92 template[len - 6] = letters[i];
94 fd = ws_open (template, O_RDWR|O_BINARY|O_CREAT|O_EXCL, 0600);
99 /* We return the null string if we can't find a unique file name. */
105 #endif /* HAVE_MKSTEMP */
108 /* Generate a unique temporary directory name from TEMPLATE.
109 The last six characters of TEMPLATE must be TMP_FILE_SUFFIX;
110 they are replaced with a string that makes the filename unique.
111 Returns 0 on success or -1 on error (from mkdir(2)). */
116 static const char letters[]
117 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
121 len = strlen (template);
122 if (len < 6 || strcmp (&template[len - 6], TMP_FILE_SUFFIX))
124 __set_errno (EINVAL);
128 if (g_snprintf (&template[len - 5], 6, "%.5u",
129 (unsigned int) getpid () % 100000) != 5)
130 /* Inconceivable lossage. */
133 for (i = 0; i < sizeof (letters); ++i)
137 template[len - 6] = letters[i];
139 ret = ws_mkdir(template, 0700);
144 /* We return the null string if we can't find a unique file name. */
150 #endif /* HAVE_MKDTEMP */
152 #define MAX_TEMPFILES 3
155 * Create a tempfile with the given prefix (e.g. "wireshark").
157 * @param namebuf If not NULL, receives the full path of the temp file.
158 * Should NOT be freed.
159 * @param pfx A prefix for the temporary file.
160 * @return The file descriptor of the new tempfile, from mkstemp().
163 create_tempfile(char **namebuf, const char *pfx)
175 char timestr[14 + 1];
177 gchar sep[2] = {0, 0};
179 idx = (idx + 1) % MAX_TEMPFILES;
182 * Allocate the buffer if it's not already allocated.
184 if (tf[idx].path == NULL) {
185 tf[idx].len = INITIAL_PATH_SIZE;
186 tf[idx].path = (char *)g_malloc(tf[idx].len);
190 * We can't use get_tempfile_path here because we're called from dumpcap.c.
192 tmp_dir = g_get_tmp_dir();
197 current_time = time(NULL);
198 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time));
199 sep[0] = G_DIR_SEPARATOR;
200 tmp_file = g_strconcat(tmp_dir, sep, pfx, "_", timestr, "_", TMP_FILE_SUFFIX, NULL);
201 if (strlen(tmp_file) > tf[idx].len) {
202 tf[idx].len = (int)strlen(tmp_file) + 1;
203 tf[idx].path = (char *)g_realloc(tf[idx].path, tf[idx].len);
205 g_strlcpy(tf[idx].path, tmp_file, tf[idx].len);
209 *namebuf = tf[idx].path;
211 /* The Single UNIX Specification doesn't say that "mkstemp()"
212 creates the temporary file with mode rw-------, so we
213 won't assume that all UNIXes will do so; instead, we set
214 the umask to 0077 to take away all group and other
215 permissions, attempt to create the file, and then put
217 old_umask = umask(0077);
218 fd = mkstemp(tf[idx].path);
224 * Create a directory with the given prefix (e.g. "wireshark"). The path
225 * is created using g_get_tmp_dir and mkdtemp.
228 * @param pfx A prefix for the temporary directory.
229 * @return The temporary directory path on success, or NULL on failure.
233 create_tempdir(char **namebuf, const char *pfx)
235 static char *td_path[3];
236 static int td_path_len[3];
243 * Allocate the buffer if it's not already allocated.
245 if (td_path[idx] == NULL) {
246 td_path_len[idx] = INITIAL_PATH_SIZE;
247 td_path[idx] = (char *)g_malloc(td_path_len[idx]);
251 * We can't use get_tempfile_path here because we're called from dumpcap.c.
253 tmp_dir = g_get_tmp_dir();
255 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]) {
256 td_path_len[idx] *= 2;
257 td_path[idx] = (char *)g_realloc(td_path[idx], td_path_len[idx]);
261 *namebuf = td_path[idx];
263 return mkdtemp(td_path[idx]);