COOPY » Guide
version 0.6.5
|
00001 /* tempname.c - generate the name of a temporary file. 00002 00003 Copyright (C) 1991-2003, 2005-2007, 2009-2012 Free Software Foundation, Inc. 00004 00005 This program is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00017 00018 /* Extracted from glibc sysdeps/posix/tempname.c. See also tmpdir.c. */ 00019 00020 #if !_LIBC 00021 /* PFMOD # include <config.h> */ 00022 # include "tempname.h" 00023 #endif 00024 00025 #include <sys/types.h> 00026 #include <assert.h> 00027 00028 #include <errno.h> 00029 #ifndef __set_errno 00030 # define __set_errno(Val) errno = (Val) 00031 #endif 00032 00033 #include <stdio.h> 00034 #ifndef P_tmpdir 00035 # define P_tmpdir "/tmp" 00036 #endif 00037 #ifndef TMP_MAX 00038 # define TMP_MAX 238328 00039 #endif 00040 #ifndef __GT_FILE 00041 # define __GT_FILE 0 00042 # define __GT_DIR 1 00043 # define __GT_NOCREATE 2 00044 #endif 00045 #if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR \ 00046 || GT_NOCREATE != __GT_NOCREATE) 00047 # error report this to bug-gnulib@gnu.org 00048 #endif 00049 00050 #include <stddef.h> 00051 #include <stdlib.h> 00052 #include <string.h> 00053 00054 #include <fcntl.h> 00055 #include <sys/time.h> 00056 #include <stdint.h> 00057 #include <unistd.h> 00058 00059 #include <sys/stat.h> 00060 00061 #if _LIBC 00062 # define struct_stat64 struct stat64 00063 #else 00064 # define struct_stat64 struct stat 00065 # define __gen_tempname gen_tempname 00066 # define __getpid getpid 00067 # define __gettimeofday gettimeofday 00068 # define __mkdir mkdir 00069 # define __open open 00070 # define __lxstat64(version, file, buf) lstat (file, buf) 00071 #endif 00072 00073 #if ! (HAVE___SECURE_GETENV || _LIBC) 00074 # define __secure_getenv getenv 00075 #endif 00076 00077 #ifdef _LIBC 00078 # include <hp-timing.h> 00079 # if HP_TIMING_AVAIL 00080 # define RANDOM_BITS(Var) \ 00081 if (__builtin_expect (value == UINT64_C (0), 0)) \ 00082 { \ 00083 /* If this is the first time this function is used initialize \ 00084 the variable we accumulate the value in to some somewhat \ 00085 random value. If we'd not do this programs at startup time \ 00086 might have a reduced set of possible names, at least on slow \ 00087 machines. */ \ 00088 struct timeval tv; \ 00089 __gettimeofday (&tv, NULL); \ 00090 value = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; \ 00091 } \ 00092 HP_TIMING_NOW (Var) 00093 # endif 00094 #endif 00095 00096 /* Use the widest available unsigned type if uint64_t is not 00097 available. The algorithm below extracts a number less than 62**6 00098 (approximately 2**35.725) from uint64_t, so ancient hosts where 00099 uintmax_t is only 32 bits lose about 3.725 bits of randomness, 00100 which is better than not having mkstemp at all. */ 00101 #if !defined UINT64_MAX && !defined uint64_t 00102 # define uint64_t uintmax_t 00103 #endif 00104 00105 #if _LIBC 00106 /* Return nonzero if DIR is an existent directory. */ 00107 static int 00108 direxists (const char *dir) 00109 { 00110 struct_stat64 buf; 00111 return __xstat64 (_STAT_VER, dir, &buf) == 0 && S_ISDIR (buf.st_mode); 00112 } 00113 00114 /* Path search algorithm, for tmpnam, tmpfile, etc. If DIR is 00115 non-null and exists, uses it; otherwise uses the first of $TMPDIR, 00116 P_tmpdir, /tmp that exists. Copies into TMPL a template suitable 00117 for use with mk[s]temp. Will fail (-1) if DIR is non-null and 00118 doesn't exist, none of the searched dirs exists, or there's not 00119 enough space in TMPL. */ 00120 int 00121 __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx, 00122 int try_tmpdir) 00123 { 00124 const char *d; 00125 size_t dlen, plen; 00126 00127 if (!pfx || !pfx[0]) 00128 { 00129 pfx = "file"; 00130 plen = 4; 00131 } 00132 else 00133 { 00134 plen = strlen (pfx); 00135 if (plen > 5) 00136 plen = 5; 00137 } 00138 00139 if (try_tmpdir) 00140 { 00141 d = __secure_getenv ("TMPDIR"); 00142 if (d != NULL && direxists (d)) 00143 dir = d; 00144 else if (dir != NULL && direxists (dir)) 00145 /* nothing */ ; 00146 else 00147 dir = NULL; 00148 } 00149 if (dir == NULL) 00150 { 00151 if (direxists (P_tmpdir)) 00152 dir = P_tmpdir; 00153 else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp")) 00154 dir = "/tmp"; 00155 else 00156 { 00157 __set_errno (ENOENT); 00158 return -1; 00159 } 00160 } 00161 00162 dlen = strlen (dir); 00163 while (dlen > 1 && dir[dlen - 1] == '/') 00164 dlen--; /* remove trailing slashes */ 00165 00166 /* check we have room for "${dir}/${pfx}XXXXXX\0" */ 00167 if (tmpl_len < dlen + 1 + plen + 6 + 1) 00168 { 00169 __set_errno (EINVAL); 00170 return -1; 00171 } 00172 00173 sprintf (tmpl, "%.*s/%.*sXXXXXX", (int) dlen, dir, (int) plen, pfx); 00174 return 0; 00175 } 00176 #endif /* _LIBC */ 00177 00178 /* These are the characters used in temporary file names. */ 00179 static const char letters[] = 00180 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 00181 00182 /* Generate a temporary file name based on TMPL. TMPL must match the 00183 rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix). 00184 The name constructed does not exist at the time of the call to 00185 __gen_tempname. TMPL is overwritten with the result. 00186 00187 KIND may be one of: 00188 __GT_NOCREATE: simply verify that the name does not exist 00189 at the time of the call. 00190 __GT_FILE: create the file using open(O_CREAT|O_EXCL) 00191 and return a read-write fd. The file is mode 0600. 00192 __GT_DIR: create a directory, which will be mode 0700. 00193 00194 We use a clever algorithm to get hard-to-predict names. */ 00195 int 00196 __gen_tempname (char *tmpl, int suffixlen, int flags, int kind) 00197 { 00198 int len; 00199 char *XXXXXX; 00200 static uint64_t value; 00201 uint64_t random_time_bits; 00202 unsigned int count; 00203 int fd = -1; 00204 int save_errno = errno; 00205 struct_stat64 st; 00206 00207 /* A lower bound on the number of temporary files to attempt to 00208 generate. The maximum total number of temporary file names that 00209 can exist for a given template is 62**6. It should never be 00210 necessary to try all these combinations. Instead if a reasonable 00211 number of names is tried (we define reasonable as 62**3) fail to 00212 give the system administrator the chance to remove the problems. */ 00213 #define ATTEMPTS_MIN (62 * 62 * 62) 00214 00215 /* The number of times to attempt to generate a temporary file. To 00216 conform to POSIX, this must be no smaller than TMP_MAX. */ 00217 #if ATTEMPTS_MIN < TMP_MAX 00218 unsigned int attempts = TMP_MAX; 00219 #else 00220 unsigned int attempts = ATTEMPTS_MIN; 00221 #endif 00222 00223 len = strlen (tmpl); 00224 if (len < 6 + suffixlen || memcmp (&tmpl[len - 6 - suffixlen], "XXXXXX", 6)) 00225 { 00226 __set_errno (EINVAL); 00227 return -1; 00228 } 00229 00230 /* This is where the Xs start. */ 00231 XXXXXX = &tmpl[len - 6 - suffixlen]; 00232 00233 /* Get some more or less random data. */ 00234 #ifdef RANDOM_BITS 00235 RANDOM_BITS (random_time_bits); 00236 #else 00237 { 00238 struct timeval tv; 00239 __gettimeofday (&tv, NULL); 00240 random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; 00241 } 00242 #endif 00243 value += random_time_bits ^ __getpid (); 00244 00245 for (count = 0; count < attempts; value += 7777, ++count) 00246 { 00247 uint64_t v = value; 00248 00249 /* Fill in the random bits. */ 00250 XXXXXX[0] = letters[v % 62]; 00251 v /= 62; 00252 XXXXXX[1] = letters[v % 62]; 00253 v /= 62; 00254 XXXXXX[2] = letters[v % 62]; 00255 v /= 62; 00256 XXXXXX[3] = letters[v % 62]; 00257 v /= 62; 00258 XXXXXX[4] = letters[v % 62]; 00259 v /= 62; 00260 XXXXXX[5] = letters[v % 62]; 00261 00262 switch (kind) 00263 { 00264 case __GT_FILE: 00265 fd = __open (tmpl, 00266 (flags & ~O_ACCMODE) 00267 | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); 00268 break; 00269 00270 case __GT_DIR: 00271 /* fd = __mkdir (tmpl, PFMOD , S_IRUSR | S_IWUSR | S_IXUSR); */ 00272 /* PFMOD, don't use this feature, and problem on mingw */ 00273 fd = -1; 00274 break; 00275 00276 case __GT_NOCREATE: 00277 /* This case is backward from the other three. __gen_tempname 00278 succeeds if __xstat fails because the name does not exist. 00279 Note the continue to bypass the common logic at the bottom 00280 of the loop. */ 00281 /* PFMOD, don't use this feature, and problem on mingw */ 00282 return -1; 00283 /* 00284 if (__lxstat64 (_STAT_VER, tmpl, &st) < 0) 00285 { 00286 if (errno == ENOENT) 00287 { 00288 __set_errno (save_errno); 00289 return 0; 00290 } 00291 else 00292 return -1; 00293 } 00294 */ 00295 continue; 00296 00297 default: 00298 assert (! "invalid KIND in __gen_tempname"); 00299 abort (); 00300 } 00301 00302 if (fd >= 0) 00303 { 00304 __set_errno (save_errno); 00305 return fd; 00306 } 00307 else if (errno != EEXIST) 00308 return -1; 00309 } 00310 00311 /* We got out of the loop because we ran out of combinations to try. */ 00312 __set_errno (EEXIST); 00313 return -1; 00314 }