|
|
cb4ff2 |
commit 4dddd7e9cbecad4aa03ee5a9b9edb596e3d4e909
|
|
|
cb4ff2 |
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
|
|
|
cb4ff2 |
Date: Tue Sep 29 08:56:07 2020 -0300
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
posix: Sync tempname with gnulib [BZ #26648]
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
It syncs with gnulib commit b1268f22f443e8e4b9e. The try_tempname_len
|
|
|
cb4ff2 |
now uses getrandom on each iteration to get entropy and only uses the
|
|
|
cb4ff2 |
clock plus ASLR as source of entropy if getrandom fails.
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
Checked on x86_64-linux-gnu and i686-linux-gnu.
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
Conflicts:
|
|
|
cb4ff2 |
sysdeps/posix/tempname.c
|
|
|
cb4ff2 |
(Missing tree-wide __gettimeofday to clock_gettime change,
|
|
|
cb4ff2 |
commit 4a39c34c4f85de57fb4e648cfa1e774437d69680 upstream.
|
|
|
cb4ff2 |
File was rebased to the upstream version.)
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
diff --git a/sysdeps/posix/tempname.c b/sysdeps/posix/tempname.c
|
|
|
cb4ff2 |
index a7b404cf4410cb00..f199b25a7a227751 100644
|
|
|
cb4ff2 |
--- a/sysdeps/posix/tempname.c
|
|
|
cb4ff2 |
+++ b/sysdeps/posix/tempname.c
|
|
|
cb4ff2 |
@@ -1,4 +1,4 @@
|
|
|
cb4ff2 |
-/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
|
|
|
cb4ff2 |
+/* Copyright (C) 1991-2021 Free Software Foundation, Inc.
|
|
|
cb4ff2 |
This file is part of the GNU C Library.
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
The GNU C Library is free software; you can redistribute it and/or
|
|
|
cb4ff2 |
@@ -13,10 +13,10 @@
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
You should have received a copy of the GNU Lesser General Public
|
|
|
cb4ff2 |
License along with the GNU C Library; if not, see
|
|
|
cb4ff2 |
- <http://www.gnu.org/licenses/>. */
|
|
|
cb4ff2 |
+ <https://www.gnu.org/licenses/>. */
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
#if !_LIBC
|
|
|
cb4ff2 |
-# include <config.h>
|
|
|
cb4ff2 |
+# include <libc-config.h>
|
|
|
cb4ff2 |
# include "tempname.h"
|
|
|
cb4ff2 |
#endif
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
@@ -24,9 +24,6 @@
|
|
|
cb4ff2 |
#include <assert.h>
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
#include <errno.h>
|
|
|
cb4ff2 |
-#ifndef __set_errno
|
|
|
cb4ff2 |
-# define __set_errno(Val) errno = (Val)
|
|
|
cb4ff2 |
-#endif
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
#include <stdio.h>
|
|
|
cb4ff2 |
#ifndef P_tmpdir
|
|
|
cb4ff2 |
@@ -36,12 +33,12 @@
|
|
|
cb4ff2 |
# define TMP_MAX 238328
|
|
|
cb4ff2 |
#endif
|
|
|
cb4ff2 |
#ifndef __GT_FILE
|
|
|
cb4ff2 |
-# define __GT_FILE 0
|
|
|
cb4ff2 |
-# define __GT_DIR 1
|
|
|
cb4ff2 |
-# define __GT_NOCREATE 2
|
|
|
cb4ff2 |
+# define __GT_FILE 0
|
|
|
cb4ff2 |
+# define __GT_DIR 1
|
|
|
cb4ff2 |
+# define __GT_NOCREATE 2
|
|
|
cb4ff2 |
#endif
|
|
|
cb4ff2 |
-#if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR \
|
|
|
cb4ff2 |
- || GT_NOCREATE != __GT_NOCREATE)
|
|
|
cb4ff2 |
+#if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR \
|
|
|
cb4ff2 |
+ || GT_NOCREATE != __GT_NOCREATE)
|
|
|
cb4ff2 |
# error report this to bug-gnulib@gnu.org
|
|
|
cb4ff2 |
#endif
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
@@ -50,11 +47,11 @@
|
|
|
cb4ff2 |
#include <string.h>
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
#include <fcntl.h>
|
|
|
cb4ff2 |
-#include <sys/time.h>
|
|
|
cb4ff2 |
+#include <stdalign.h>
|
|
|
cb4ff2 |
#include <stdint.h>
|
|
|
cb4ff2 |
-#include <unistd.h>
|
|
|
cb4ff2 |
-
|
|
|
cb4ff2 |
+#include <sys/random.h>
|
|
|
cb4ff2 |
#include <sys/stat.h>
|
|
|
cb4ff2 |
+#include <time.h>
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
#if _LIBC
|
|
|
cb4ff2 |
# define struct_stat64 struct stat64
|
|
|
cb4ff2 |
@@ -62,33 +59,38 @@
|
|
|
cb4ff2 |
#else
|
|
|
cb4ff2 |
# define struct_stat64 struct stat
|
|
|
cb4ff2 |
# define __gen_tempname gen_tempname
|
|
|
cb4ff2 |
-# define __getpid getpid
|
|
|
cb4ff2 |
-# define __gettimeofday gettimeofday
|
|
|
cb4ff2 |
# define __mkdir mkdir
|
|
|
cb4ff2 |
# define __open open
|
|
|
cb4ff2 |
-# define __secure_getenv secure_getenv
|
|
|
cb4ff2 |
+# define __lstat64(file, buf) lstat (file, buf)
|
|
|
cb4ff2 |
+# define __stat64(file, buf) stat (file, buf)
|
|
|
cb4ff2 |
+# define __getrandom getrandom
|
|
|
cb4ff2 |
+# define __clock_gettime64 clock_gettime
|
|
|
cb4ff2 |
+# define __timespec64 timespec
|
|
|
cb4ff2 |
#endif
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
-#ifdef _LIBC
|
|
|
cb4ff2 |
-# include <random-bits.h>
|
|
|
cb4ff2 |
-# define RANDOM_BITS(Var) ((Var) = random_bits ())
|
|
|
cb4ff2 |
-# else
|
|
|
cb4ff2 |
-# define RANDOM_BITS(Var) \
|
|
|
cb4ff2 |
- { \
|
|
|
cb4ff2 |
- struct timeval tv; \
|
|
|
cb4ff2 |
- __gettimeofday (&tv, NULL); \
|
|
|
cb4ff2 |
- (Var) = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; \
|
|
|
cb4ff2 |
- }
|
|
|
cb4ff2 |
-#endif
|
|
|
cb4ff2 |
+/* Use getrandom if it works, falling back on a 64-bit linear
|
|
|
cb4ff2 |
+ congruential generator that starts with Var's value
|
|
|
cb4ff2 |
+ mixed in with a clock's low-order bits if available. */
|
|
|
cb4ff2 |
+typedef uint_fast64_t random_value;
|
|
|
cb4ff2 |
+#define RANDOM_VALUE_MAX UINT_FAST64_MAX
|
|
|
cb4ff2 |
+#define BASE_62_DIGITS 10 /* 62**10 < UINT_FAST64_MAX */
|
|
|
cb4ff2 |
+#define BASE_62_POWER (62LL * 62 * 62 * 62 * 62 * 62 * 62 * 62 * 62 * 62)
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
-/* Use the widest available unsigned type if uint64_t is not
|
|
|
cb4ff2 |
- available. The algorithm below extracts a number less than 62**6
|
|
|
cb4ff2 |
- (approximately 2**35.725) from uint64_t, so ancient hosts where
|
|
|
cb4ff2 |
- uintmax_t is only 32 bits lose about 3.725 bits of randomness,
|
|
|
cb4ff2 |
- which is better than not having mkstemp at all. */
|
|
|
cb4ff2 |
-#if !defined UINT64_MAX && !defined uint64_t
|
|
|
cb4ff2 |
-# define uint64_t uintmax_t
|
|
|
cb4ff2 |
+static random_value
|
|
|
cb4ff2 |
+random_bits (random_value var)
|
|
|
cb4ff2 |
+{
|
|
|
cb4ff2 |
+ random_value r;
|
|
|
cb4ff2 |
+ /* Without GRND_NONBLOCK it can be blocked for minutes on some systems. */
|
|
|
cb4ff2 |
+ if (__getrandom (&r, sizeof r, GRND_NONBLOCK) == sizeof r)
|
|
|
cb4ff2 |
+ return r;
|
|
|
cb4ff2 |
+#if _LIBC || (defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME)
|
|
|
cb4ff2 |
+ /* Add entropy if getrandom did not work. */
|
|
|
cb4ff2 |
+ struct __timespec64 tv;
|
|
|
cb4ff2 |
+ __clock_gettime64 (CLOCK_MONOTONIC, &tv;;
|
|
|
cb4ff2 |
+ var ^= tv.tv_nsec;
|
|
|
cb4ff2 |
#endif
|
|
|
cb4ff2 |
+ return 2862933555777941757 * var + 3037000493;
|
|
|
cb4ff2 |
+}
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
#if _LIBC
|
|
|
cb4ff2 |
/* Return nonzero if DIR is an existent directory. */
|
|
|
cb4ff2 |
@@ -107,7 +109,7 @@ direxists (const char *dir)
|
|
|
cb4ff2 |
enough space in TMPL. */
|
|
|
cb4ff2 |
int
|
|
|
cb4ff2 |
__path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
|
|
|
cb4ff2 |
- int try_tmpdir)
|
|
|
cb4ff2 |
+ int try_tmpdir)
|
|
|
cb4ff2 |
{
|
|
|
cb4ff2 |
const char *d;
|
|
|
cb4ff2 |
size_t dlen, plen;
|
|
|
cb4ff2 |
@@ -121,35 +123,35 @@ __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
|
|
|
cb4ff2 |
{
|
|
|
cb4ff2 |
plen = strlen (pfx);
|
|
|
cb4ff2 |
if (plen > 5)
|
|
|
cb4ff2 |
- plen = 5;
|
|
|
cb4ff2 |
+ plen = 5;
|
|
|
cb4ff2 |
}
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
if (try_tmpdir)
|
|
|
cb4ff2 |
{
|
|
|
cb4ff2 |
d = __secure_getenv ("TMPDIR");
|
|
|
cb4ff2 |
if (d != NULL && direxists (d))
|
|
|
cb4ff2 |
- dir = d;
|
|
|
cb4ff2 |
+ dir = d;
|
|
|
cb4ff2 |
else if (dir != NULL && direxists (dir))
|
|
|
cb4ff2 |
- /* nothing */ ;
|
|
|
cb4ff2 |
+ /* nothing */ ;
|
|
|
cb4ff2 |
else
|
|
|
cb4ff2 |
- dir = NULL;
|
|
|
cb4ff2 |
+ dir = NULL;
|
|
|
cb4ff2 |
}
|
|
|
cb4ff2 |
if (dir == NULL)
|
|
|
cb4ff2 |
{
|
|
|
cb4ff2 |
if (direxists (P_tmpdir))
|
|
|
cb4ff2 |
- dir = P_tmpdir;
|
|
|
cb4ff2 |
+ dir = P_tmpdir;
|
|
|
cb4ff2 |
else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
|
|
|
cb4ff2 |
- dir = "/tmp";
|
|
|
cb4ff2 |
+ dir = "/tmp";
|
|
|
cb4ff2 |
else
|
|
|
cb4ff2 |
- {
|
|
|
cb4ff2 |
- __set_errno (ENOENT);
|
|
|
cb4ff2 |
- return -1;
|
|
|
cb4ff2 |
- }
|
|
|
cb4ff2 |
+ {
|
|
|
cb4ff2 |
+ __set_errno (ENOENT);
|
|
|
cb4ff2 |
+ return -1;
|
|
|
cb4ff2 |
+ }
|
|
|
cb4ff2 |
}
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
dlen = strlen (dir);
|
|
|
cb4ff2 |
while (dlen > 1 && dir[dlen - 1] == '/')
|
|
|
cb4ff2 |
- dlen--; /* remove trailing slashes */
|
|
|
cb4ff2 |
+ dlen--; /* remove trailing slashes */
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
/* check we have room for "${dir}/${pfx}XXXXXX\0" */
|
|
|
cb4ff2 |
if (tmpl_len < dlen + 1 + plen + 6 + 1)
|
|
|
cb4ff2 |
@@ -163,39 +165,91 @@ __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
|
|
|
cb4ff2 |
}
|
|
|
cb4ff2 |
#endif /* _LIBC */
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
+#if _LIBC
|
|
|
cb4ff2 |
+static int try_tempname_len (char *, int, void *, int (*) (char *, void *),
|
|
|
cb4ff2 |
+ size_t);
|
|
|
cb4ff2 |
+#endif
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+static int
|
|
|
cb4ff2 |
+try_file (char *tmpl, void *flags)
|
|
|
cb4ff2 |
+{
|
|
|
cb4ff2 |
+ int *openflags = flags;
|
|
|
cb4ff2 |
+ return __open (tmpl,
|
|
|
cb4ff2 |
+ (*openflags & ~O_ACCMODE)
|
|
|
cb4ff2 |
+ | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
|
|
|
cb4ff2 |
+}
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+static int
|
|
|
cb4ff2 |
+try_dir (char *tmpl, void *flags _GL_UNUSED)
|
|
|
cb4ff2 |
+{
|
|
|
cb4ff2 |
+ return __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
|
|
|
cb4ff2 |
+}
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+static int
|
|
|
cb4ff2 |
+try_nocreate (char *tmpl, void *flags _GL_UNUSED)
|
|
|
cb4ff2 |
+{
|
|
|
cb4ff2 |
+ struct_stat64 st;
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+ if (__lstat64 (tmpl, &st) == 0 || errno == EOVERFLOW)
|
|
|
cb4ff2 |
+ __set_errno (EEXIST);
|
|
|
cb4ff2 |
+ return errno == ENOENT ? 0 : -1;
|
|
|
cb4ff2 |
+}
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
/* These are the characters used in temporary file names. */
|
|
|
cb4ff2 |
static const char letters[] =
|
|
|
cb4ff2 |
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
/* Generate a temporary file name based on TMPL. TMPL must match the
|
|
|
cb4ff2 |
- rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix).
|
|
|
cb4ff2 |
+ rules for mk[s]temp (i.e., end in at least X_SUFFIX_LEN "X"s,
|
|
|
cb4ff2 |
+ possibly with a suffix).
|
|
|
cb4ff2 |
The name constructed does not exist at the time of the call to
|
|
|
cb4ff2 |
- __gen_tempname. TMPL is overwritten with the result.
|
|
|
cb4ff2 |
+ this function. TMPL is overwritten with the result.
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
KIND may be one of:
|
|
|
cb4ff2 |
- __GT_NOCREATE: simply verify that the name does not exist
|
|
|
cb4ff2 |
- at the time of the call.
|
|
|
cb4ff2 |
- __GT_FILE: create the file using open(O_CREAT|O_EXCL)
|
|
|
cb4ff2 |
- and return a read-write fd. The file is mode 0600.
|
|
|
cb4ff2 |
- __GT_DIR: create a directory, which will be mode 0700.
|
|
|
cb4ff2 |
+ __GT_NOCREATE: simply verify that the name does not exist
|
|
|
cb4ff2 |
+ at the time of the call.
|
|
|
cb4ff2 |
+ __GT_FILE: create the file using open(O_CREAT|O_EXCL)
|
|
|
cb4ff2 |
+ and return a read-write fd. The file is mode 0600.
|
|
|
cb4ff2 |
+ __GT_DIR: create a directory, which will be mode 0700.
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
We use a clever algorithm to get hard-to-predict names. */
|
|
|
cb4ff2 |
+#ifdef _LIBC
|
|
|
cb4ff2 |
+static
|
|
|
cb4ff2 |
+#endif
|
|
|
cb4ff2 |
int
|
|
|
cb4ff2 |
-__gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
|
|
|
cb4ff2 |
+gen_tempname_len (char *tmpl, int suffixlen, int flags, int kind,
|
|
|
cb4ff2 |
+ size_t x_suffix_len)
|
|
|
cb4ff2 |
{
|
|
|
cb4ff2 |
- int len;
|
|
|
cb4ff2 |
+ static int (*const tryfunc[]) (char *, void *) =
|
|
|
cb4ff2 |
+ {
|
|
|
cb4ff2 |
+ [__GT_FILE] = try_file,
|
|
|
cb4ff2 |
+ [__GT_DIR] = try_dir,
|
|
|
cb4ff2 |
+ [__GT_NOCREATE] = try_nocreate
|
|
|
cb4ff2 |
+ };
|
|
|
cb4ff2 |
+ return try_tempname_len (tmpl, suffixlen, &flags, tryfunc[kind],
|
|
|
cb4ff2 |
+ x_suffix_len);
|
|
|
cb4ff2 |
+}
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+#ifdef _LIBC
|
|
|
cb4ff2 |
+static
|
|
|
cb4ff2 |
+#endif
|
|
|
cb4ff2 |
+int
|
|
|
cb4ff2 |
+try_tempname_len (char *tmpl, int suffixlen, void *args,
|
|
|
cb4ff2 |
+ int (*tryfunc) (char *, void *), size_t x_suffix_len)
|
|
|
cb4ff2 |
+{
|
|
|
cb4ff2 |
+ size_t len;
|
|
|
cb4ff2 |
char *XXXXXX;
|
|
|
cb4ff2 |
unsigned int count;
|
|
|
cb4ff2 |
int fd = -1;
|
|
|
cb4ff2 |
int save_errno = errno;
|
|
|
cb4ff2 |
- struct_stat64 st;
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
/* A lower bound on the number of temporary files to attempt to
|
|
|
cb4ff2 |
generate. The maximum total number of temporary file names that
|
|
|
cb4ff2 |
can exist for a given template is 62**6. It should never be
|
|
|
cb4ff2 |
necessary to try all of these combinations. Instead if a reasonable
|
|
|
cb4ff2 |
number of names is tried (we define reasonable as 62**3) fail to
|
|
|
cb4ff2 |
- give the system administrator the chance to remove the problems. */
|
|
|
cb4ff2 |
+ give the system administrator the chance to remove the problems.
|
|
|
cb4ff2 |
+ This value requires that X_SUFFIX_LEN be at least 3. */
|
|
|
cb4ff2 |
#define ATTEMPTS_MIN (62 * 62 * 62)
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
/* The number of times to attempt to generate a temporary file. To
|
|
|
cb4ff2 |
@@ -206,82 +260,75 @@ __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
|
|
|
cb4ff2 |
unsigned int attempts = ATTEMPTS_MIN;
|
|
|
cb4ff2 |
#endif
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
+ /* A random variable. The initial value is used only the for fallback path
|
|
|
cb4ff2 |
+ on 'random_bits' on 'getrandom' failure. Its initial value tries to use
|
|
|
cb4ff2 |
+ some entropy from the ASLR and ignore possible bits from the stack
|
|
|
cb4ff2 |
+ alignment. */
|
|
|
cb4ff2 |
+ random_value v = ((uintptr_t) &v) / alignof (max_align_t);
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+ /* How many random base-62 digits can currently be extracted from V. */
|
|
|
cb4ff2 |
+ int vdigits = 0;
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+ /* Least unfair value for V. If V is less than this, V can generate
|
|
|
cb4ff2 |
+ BASE_62_DIGITS digits fairly. Otherwise it might be biased. */
|
|
|
cb4ff2 |
+ random_value const unfair_min
|
|
|
cb4ff2 |
+ = RANDOM_VALUE_MAX - RANDOM_VALUE_MAX % BASE_62_POWER;
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
len = strlen (tmpl);
|
|
|
cb4ff2 |
- if (len < 6 + suffixlen || memcmp (&tmpl[len - 6 - suffixlen], "XXXXXX", 6))
|
|
|
cb4ff2 |
+ if (len < x_suffix_len + suffixlen
|
|
|
cb4ff2 |
+ || strspn (&tmpl[len - x_suffix_len - suffixlen], "X") < x_suffix_len)
|
|
|
cb4ff2 |
{
|
|
|
cb4ff2 |
__set_errno (EINVAL);
|
|
|
cb4ff2 |
return -1;
|
|
|
cb4ff2 |
}
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
/* This is where the Xs start. */
|
|
|
cb4ff2 |
- XXXXXX = &tmpl[len - 6 - suffixlen];
|
|
|
cb4ff2 |
+ XXXXXX = &tmpl[len - x_suffix_len - suffixlen];
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
- uint64_t pid = (uint64_t) __getpid () << 32;
|
|
|
cb4ff2 |
for (count = 0; count < attempts; ++count)
|
|
|
cb4ff2 |
{
|
|
|
cb4ff2 |
- uint64_t v;
|
|
|
cb4ff2 |
- /* Get some more or less random data. */
|
|
|
cb4ff2 |
- RANDOM_BITS (v);
|
|
|
cb4ff2 |
- v ^= pid;
|
|
|
cb4ff2 |
-
|
|
|
cb4ff2 |
- /* Fill in the random bits. */
|
|
|
cb4ff2 |
- XXXXXX[0] = letters[v % 62];
|
|
|
cb4ff2 |
- v /= 62;
|
|
|
cb4ff2 |
- XXXXXX[1] = letters[v % 62];
|
|
|
cb4ff2 |
- v /= 62;
|
|
|
cb4ff2 |
- XXXXXX[2] = letters[v % 62];
|
|
|
cb4ff2 |
- v /= 62;
|
|
|
cb4ff2 |
- XXXXXX[3] = letters[v % 62];
|
|
|
cb4ff2 |
- v /= 62;
|
|
|
cb4ff2 |
- XXXXXX[4] = letters[v % 62];
|
|
|
cb4ff2 |
- v /= 62;
|
|
|
cb4ff2 |
- XXXXXX[5] = letters[v % 62];
|
|
|
cb4ff2 |
-
|
|
|
cb4ff2 |
- switch (kind)
|
|
|
cb4ff2 |
- {
|
|
|
cb4ff2 |
- case __GT_FILE:
|
|
|
cb4ff2 |
- fd = __open (tmpl,
|
|
|
cb4ff2 |
- (flags & ~O_ACCMODE)
|
|
|
cb4ff2 |
- | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
|
|
|
cb4ff2 |
- break;
|
|
|
cb4ff2 |
-
|
|
|
cb4ff2 |
- case __GT_DIR:
|
|
|
cb4ff2 |
- fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
|
|
|
cb4ff2 |
- break;
|
|
|
cb4ff2 |
-
|
|
|
cb4ff2 |
- case __GT_NOCREATE:
|
|
|
cb4ff2 |
- /* This case is backward from the other three. __gen_tempname
|
|
|
cb4ff2 |
- succeeds if lstat fails because the name does not exist.
|
|
|
cb4ff2 |
- Note the continue to bypass the common logic at the bottom
|
|
|
cb4ff2 |
- of the loop. */
|
|
|
cb4ff2 |
- if (__lstat64 (tmpl, &st) < 0)
|
|
|
cb4ff2 |
- {
|
|
|
cb4ff2 |
- if (errno == ENOENT)
|
|
|
cb4ff2 |
- {
|
|
|
cb4ff2 |
- __set_errno (save_errno);
|
|
|
cb4ff2 |
- return 0;
|
|
|
cb4ff2 |
- }
|
|
|
cb4ff2 |
- else
|
|
|
cb4ff2 |
- /* Give up now. */
|
|
|
cb4ff2 |
- return -1;
|
|
|
cb4ff2 |
- }
|
|
|
cb4ff2 |
- continue;
|
|
|
cb4ff2 |
-
|
|
|
cb4ff2 |
- default:
|
|
|
cb4ff2 |
- assert (! "invalid KIND in __gen_tempname");
|
|
|
cb4ff2 |
- abort ();
|
|
|
cb4ff2 |
- }
|
|
|
cb4ff2 |
-
|
|
|
cb4ff2 |
+ for (size_t i = 0; i < x_suffix_len; i++)
|
|
|
cb4ff2 |
+ {
|
|
|
cb4ff2 |
+ if (vdigits == 0)
|
|
|
cb4ff2 |
+ {
|
|
|
cb4ff2 |
+ do
|
|
|
cb4ff2 |
+ v = random_bits (v);
|
|
|
cb4ff2 |
+ while (unfair_min <= v);
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+ vdigits = BASE_62_DIGITS;
|
|
|
cb4ff2 |
+ }
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+ XXXXXX[i] = letters[v % 62];
|
|
|
cb4ff2 |
+ v /= 62;
|
|
|
cb4ff2 |
+ vdigits--;
|
|
|
cb4ff2 |
+ }
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+ fd = tryfunc (tmpl, args);
|
|
|
cb4ff2 |
if (fd >= 0)
|
|
|
cb4ff2 |
- {
|
|
|
cb4ff2 |
- __set_errno (save_errno);
|
|
|
cb4ff2 |
- return fd;
|
|
|
cb4ff2 |
- }
|
|
|
cb4ff2 |
+ {
|
|
|
cb4ff2 |
+ __set_errno (save_errno);
|
|
|
cb4ff2 |
+ return fd;
|
|
|
cb4ff2 |
+ }
|
|
|
cb4ff2 |
else if (errno != EEXIST)
|
|
|
cb4ff2 |
- return -1;
|
|
|
cb4ff2 |
+ return -1;
|
|
|
cb4ff2 |
}
|
|
|
cb4ff2 |
|
|
|
cb4ff2 |
/* We got out of the loop because we ran out of combinations to try. */
|
|
|
cb4ff2 |
__set_errno (EEXIST);
|
|
|
cb4ff2 |
return -1;
|
|
|
cb4ff2 |
}
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+int
|
|
|
cb4ff2 |
+__gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
|
|
|
cb4ff2 |
+{
|
|
|
cb4ff2 |
+ return gen_tempname_len (tmpl, suffixlen, flags, kind, 6);
|
|
|
cb4ff2 |
+}
|
|
|
cb4ff2 |
+
|
|
|
cb4ff2 |
+#if !_LIBC
|
|
|
cb4ff2 |
+int
|
|
|
cb4ff2 |
+try_tempname (char *tmpl, int suffixlen, void *args,
|
|
|
cb4ff2 |
+ int (*tryfunc) (char *, void *))
|
|
|
cb4ff2 |
+{
|
|
|
cb4ff2 |
+ return try_tempname_len (tmpl, suffixlen, args, tryfunc, 6);
|
|
|
cb4ff2 |
+}
|
|
|
cb4ff2 |
+#endif
|