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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
48 #include <process.h> /* For getpid() */
52 #include <wsutil/file_util.h>
55 #define __set_errno(x) errno=(x)
58 #define INITIAL_PATH_SIZE 128
59 #define TMP_FILE_SUFFIX "XXXXXX"
62 /* Generate a unique temporary file name from TEMPLATE.
63 The last six characters of TEMPLATE must be TMP_FILE_SUFFIX;
64 they are replaced with a string that makes the filename unique.
65 Returns a file descriptor open on the file for reading and writing. */
67 mkstemp (char *template)
69 static const char letters[]
70 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
74 len = strlen (template);
75 if (len < 6 || strcmp (&template[len - 6], TMP_FILE_SUFFIX))
81 if (g_snprintf (&template[len - 5], 6, "%.5u",
82 (unsigned int) getpid () % 100000) != 5)
83 /* Inconceivable lossage. */
86 for (i = 0; i < sizeof (letters); ++i)
90 template[len - 6] = letters[i];
92 fd = ws_open (template, O_RDWR|O_BINARY|O_CREAT|O_EXCL, 0600);
97 /* We return the null string if we can't find a unique file name. */
103 #endif /* HAVE_MKSTEMP */
106 /* Generate a unique temporary directory name from TEMPLATE.
107 The last six characters of TEMPLATE must be TMP_FILE_SUFFIX;
108 they are replaced with a string that makes the filename unique.
109 Returns 0 on success or -1 on error (from mkdir(2)). */
111 mkdtemp (char *template)
113 static const char letters[]
114 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
118 len = strlen (template);
119 if (len < 6 || strcmp (&template[len - 6], TMP_FILE_SUFFIX))
121 __set_errno (EINVAL);
125 if (g_snprintf (&template[len - 5], 6, "%.5u",
126 (unsigned int) getpid () % 100000) != 5)
127 /* Inconceivable lossage. */
130 for (i = 0; i < sizeof (letters); ++i)
134 template[len - 6] = letters[i];
136 ret = ws_mkdir(template, 0700);
141 /* We return the null string if we can't find a unique file name. */
147 #endif /* HAVE_MKDTEMP */
149 #define MAX_TEMPFILES 3
152 * Create a tempfile with the given prefix (e.g. "wireshark").
154 * @param namebuf If not NULL, receives the full path of the temp file.
155 * Should NOT be freed.
156 * @param pfx A prefix for the temporary file.
157 * @return The file descriptor of the new tempfile, from mkstemp().
160 create_tempfile(char **namebuf, const char *pfx)
172 char timestr[14 + 1];
174 gchar sep[2] = {0, 0};
176 idx = (idx + 1) % MAX_TEMPFILES;
179 * Allocate the buffer if it's not already allocated.
181 if (tf[idx].path == NULL) {
182 tf[idx].len = INITIAL_PATH_SIZE;
183 tf[idx].path = (char *)g_malloc(tf[idx].len);
187 * We can't use get_tempfile_path here because we're called from dumpcap.c.
189 tmp_dir = g_get_tmp_dir();
194 current_time = time(NULL);
195 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S", localtime(¤t_time));
196 sep[0] = G_DIR_SEPARATOR;
197 tmp_file = g_strconcat(tmp_dir, sep, pfx, "_", timestr, "_", TMP_FILE_SUFFIX, NULL);
198 if (strlen(tmp_file) > tf[idx].len) {
199 tf[idx].len = (int)strlen(tmp_file) + 1;
200 tf[idx].path = (char *)g_realloc(tf[idx].path, tf[idx].len);
202 g_strlcpy(tf[idx].path, tmp_file, tf[idx].len);
206 *namebuf = tf[idx].path;
208 /* The Single UNIX Specification doesn't say that "mkstemp()"
209 creates the temporary file with mode rw-------, so we
210 won't assume that all UNIXes will do so; instead, we set
211 the umask to 0077 to take away all group and other
212 permissions, attempt to create the file, and then put
214 old_umask = umask(0077);
215 fd = mkstemp(tf[idx].path);
221 * Create a directory with the given prefix (e.g. "wireshark"). The path
222 * is created using g_get_tmp_dir and mkdtemp.
225 * @param pfx A prefix for the temporary directory.
226 * @return The temporary directory path on success, or NULL on failure.
230 create_tempdir(char **namebuf, const char *pfx)
232 static char *td_path[3];
233 static int td_path_len[3];
240 * Allocate the buffer if it's not already allocated.
242 if (td_path[idx] == NULL) {
243 td_path_len[idx] = INITIAL_PATH_SIZE;
244 td_path[idx] = (char *)g_malloc(td_path_len[idx]);
248 * We can't use get_tempfile_path here because we're called from dumpcap.c.
250 tmp_dir = g_get_tmp_dir();
252 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]) {
253 td_path_len[idx] *= 2;
254 td_path[idx] = (char *)g_realloc(td_path[idx], td_path_len[idx]);
258 *namebuf = td_path[idx];
260 return mkdtemp(td_path[idx]);