From 56d1f81949fde78615cd9fec048259d261f99c40 Mon Sep 17 00:00:00 2001 From: Raghavendra Bhat Date: Mon, 8 Oct 2012 11:02:55 +0530 Subject: glusterfs: add gf_mkostemp api and use it instead of mkostemp of libc Change-Id: Icc12b99e2233d22037e2c4bb2b6966e45668b7dd BUG: 764890 Signed-off-by: Raghavendra Bhat Reviewed-on: http://review.gluster.org/4091 Reviewed-by: Anand Avati Tested-by: Anand Avati --- contrib/stdlib/gf_mkostemp.c | 110 +++++++++++++++++++++++++++++++++++++++++++ libglusterfs/src/Makefile.am | 1 + libglusterfs/src/compat.h | 3 ++ 3 files changed, 114 insertions(+) create mode 100644 contrib/stdlib/gf_mkostemp.c diff --git a/contrib/stdlib/gf_mkostemp.c b/contrib/stdlib/gf_mkostemp.c new file mode 100644 index 000000000..988300adb --- /dev/null +++ b/contrib/stdlib/gf_mkostemp.c @@ -0,0 +1,110 @@ +/* Borrowed from glibc-2.16/sysdeps/posix/tempname.c */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Copyright (C) 1991-2001, 2006, 2007, 2009 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +static const char letters[] = +"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + +/* Generate a temporary file name based on TMPL. TMPL must match the + rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix). +*/ + +int +gf_mkostemp (char *tmpl, int suffixlen, int flags) +{ + int len; + char *XXXXXX; + static uint64_t value; + uint64_t random_time_bits; + unsigned int count; + int fd = -1; + + /* A lower bound on the number of temporary files to attempt to + generate. The maximum total number of temporary file names that + can exist for a given template is 62**6. It should never be + necessary to try all these combinations. Instead if a reasonable + number of names is tried (we define reasonable as 62**3) fail to + give the system administrator the chance to remove the problems. */ +#define ATTEMPTS_MIN (62 * 62 * 62) + + /* The number of times to attempt to generate a temporary file. To + conform to POSIX, this must be no smaller than TMP_MAX. */ +#if ATTEMPTS_MIN < TMP_MAX + unsigned int attempts = TMP_MAX; +#else + unsigned int attempts = ATTEMPTS_MIN; +#endif + + len = strlen (tmpl); + if (len < 6 + suffixlen || memcmp (&tmpl[len - 6 - suffixlen], + "XXXXXX", 6)) + return -1; + + /* This is where the Xs start. */ + XXXXXX = &tmpl[len - 6 - suffixlen]; + + /* Get some more or less random data. */ +# if HAVE_GETTIMEOFDAY + struct timeval tv; + gettimeofday (&tv, NULL); + random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; +# else + random_time_bits = time (NULL); +# endif + + value += random_time_bits ^ getpid (); + + for (count = 0; count < attempts; value += 7777, ++count) { + uint64_t v = value; + + /* Fill in the random bits. */ + XXXXXX[0] = letters[v % 62]; + v /= 62; + XXXXXX[1] = letters[v % 62]; + v /= 62; + XXXXXX[2] = letters[v % 62]; + v /= 62; + XXXXXX[3] = letters[v % 62]; + v /= 62; + XXXXXX[4] = letters[v % 62]; + v /= 62; + XXXXXX[5] = letters[v % 62]; + + fd = open (tmpl, (flags & ~O_ACCMODE) + | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); + + if (fd >= 0) + return fd; + else if (errno != EEXIST) + return -1; + } + + /* We got out of the loop because we ran out of combinations to try. */ + return -1; +} diff --git a/libglusterfs/src/Makefile.am b/libglusterfs/src/Makefile.am index 6b55c6452..0503e97f6 100644 --- a/libglusterfs/src/Makefile.am +++ b/libglusterfs/src/Makefile.am @@ -24,6 +24,7 @@ libglusterfs_la_SOURCES = dict.c xlator.c logging.c \ graph-print.c trie.c run.c options.c fd-lk.c circ-buff.c \ event-history.c gidcache.c ctx.c \ $(CONTRIBDIR)/libgen/basename_r.c $(CONTRIBDIR)/libgen/dirname_r.c \ + $(CONTRIBDIR)/stdlib/gf_mkostemp.c \ event-poll.c event-epoll.c diff --git a/libglusterfs/src/compat.h b/libglusterfs/src/compat.h index af890485d..3d0cee1a9 100644 --- a/libglusterfs/src/compat.h +++ b/libglusterfs/src/compat.h @@ -344,6 +344,9 @@ char *dirname_r(char *path); #define dirname(path) dirname_r(path) #endif /* THREAD_UNSAFE_DIRNAME */ +int gf_mkostemp (char *tmpl, int suffixlen, int flags); +#define mkostemp(tmpl, flags) gf_mkostemp(tmpl, 0, flags); + #ifdef HAVE_STRUCT_STAT_ST_ATIM_TV_NSEC /* Linux, Solaris, Cygwin */ #define ST_ATIM_NSEC(stbuf) ((stbuf)->st_atim.tv_nsec) -- cgit