From 8eb3f17fc7fd54d63cb8f92cee955b398ce63599 Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Mon, 2 Nov 2009 13:19:27 -0700 Subject: [PATCH] tempname: resync from glibc * lib/tempname.c (__gen_tempname): Add suffixlen argument. Use same values for __GT_FILE as glibc. Abort even when assertions are disabled. * lib/tempname.h (GT_FILE): Use glibc __GT_FILE, if available, and match its value otherwise. Allow idempotent inclusion. * lib/mkdtemp.c (mkdtemp): Adjust caller. * lib/mkostemp.c (mkostemp): Likewise. * lib/mkstemp.c (mkstemp): Likewise. * lib/tmpfile.c (tmpfile): Likewise. * NEWS: Document this. Signed-off-by: Eric Blake --- ChangeLog | 12 ++++++++++++ NEWS | 3 +++ lib/mkdtemp.c | 2 +- lib/mkostemp.c | 9 ++++++--- lib/mkstemp.c | 9 ++++++--- lib/tempname.c | 30 ++++++++++++++++++------------ lib/tempname.h | 27 ++++++++++++++++++++------- lib/tmpfile.c | 2 +- 8 files changed, 67 insertions(+), 27 deletions(-) diff --git a/ChangeLog b/ChangeLog index 57a7fdb32..e57ce6999 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,17 @@ 2009-11-04 Eric Blake + tempname: resync from glibc + * lib/tempname.c (__gen_tempname): Add suffixlen argument. Use + same values for __GT_FILE as glibc. Abort even when assertions + are disabled. + * lib/tempname.h (GT_FILE): Use glibc __GT_FILE, if available, and + match its value otherwise. Allow idempotent inclusion. + * lib/mkdtemp.c (mkdtemp): Adjust caller. + * lib/mkostemp.c (mkostemp): Likewise. + * lib/mkstemp.c (mkstemp): Likewise. + * lib/tmpfile.c (tmpfile): Likewise. + * NEWS: Document this. + utimens: fix use of futimens on older Linux * lib/utimens.c (fdutimens): Use updated, rather than original, timespec to avoid bug in older Linux kernel. diff --git a/NEWS b/NEWS index fcf8fe550..a6351504e 100644 --- a/NEWS +++ b/NEWS @@ -6,6 +6,9 @@ User visible incompatible changes Date Modules Changes +2009-11-04 tempname The gen_tempname function takes an additional + 'suffixlen' argument. You can safely pass 0. + 2009-11-04 nproc The num_processors function now takes an argument. 2009-11-02 inet_pton The use of this module now requires linking with diff --git a/lib/mkdtemp.c b/lib/mkdtemp.c index b5181d155..7f07ee4fd 100644 --- a/lib/mkdtemp.c +++ b/lib/mkdtemp.c @@ -31,7 +31,7 @@ char * mkdtemp (char *template) { - if (gen_tempname (template, 0, GT_DIR)) + if (gen_tempname (template, 0, 0, GT_DIR)) return NULL; else return template; diff --git a/lib/mkostemp.c b/lib/mkostemp.c index 6b94d409e..02a97e662 100644 --- a/lib/mkostemp.c +++ b/lib/mkostemp.c @@ -1,4 +1,5 @@ -/* Copyright (C) 1998, 1999, 2001, 2005, 2006, 2007, 2009 Free Software Foundation, Inc. +/* Copyright (C) 1998, 1999, 2001, 2005, 2006, 2007, 2009 Free + Software Foundation, Inc. This file is derived from the one in the GNU C Library. This program is free software: you can redistribute it and/or modify @@ -23,7 +24,9 @@ #if !_LIBC # include "tempname.h" # define __gen_tempname gen_tempname -# define __GT_FILE GT_FILE +# ifndef __GTFILE +# define __GT_FILE GT_FILE +# endif #endif #include @@ -41,5 +44,5 @@ mkostemp (template, flags) char *template; int flags; { - return __gen_tempname (template, flags, __GT_FILE); + return __gen_tempname (template, 0, flags, __GT_FILE); } diff --git a/lib/mkstemp.c b/lib/mkstemp.c index ccf7a7bee..684c98331 100644 --- a/lib/mkstemp.c +++ b/lib/mkstemp.c @@ -1,4 +1,5 @@ -/* Copyright (C) 1998, 1999, 2001, 2005, 2006, 2007, 2009 Free Software Foundation, Inc. +/* Copyright (C) 1998, 1999, 2001, 2005, 2006, 2007, 2009 Free + Software Foundation, Inc. This file is derived from the one in the GNU C Library. This program is free software: you can redistribute it and/or modify @@ -23,7 +24,9 @@ #if !_LIBC # include "tempname.h" # define __gen_tempname gen_tempname -# define __GT_FILE GT_FILE +# ifndef __GT_FILE +# define __GT_FILE GT_FILE +# endif #endif #include @@ -40,5 +43,5 @@ int mkstemp (template) char *template; { - return __gen_tempname (template, 0, __GT_FILE); + return __gen_tempname (template, 0, 0, __GT_FILE); } diff --git a/lib/tempname.c b/lib/tempname.c index 410213447..2da5afeec 100644 --- a/lib/tempname.c +++ b/lib/tempname.c @@ -40,9 +40,13 @@ # define TMP_MAX 238328 #endif #ifndef __GT_FILE -# define __GT_FILE 1 -# define __GT_DIR 2 -# define __GT_NOCREATE 3 +# define __GT_FILE 0 +# define __GT_DIR 1 +# define __GT_NOCREATE 2 +#endif +#if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR \ + || GT_NOCREATE != __GT_NOCREATE) +# error report this to bug-gnulib@gnu.org #endif #include @@ -60,11 +64,12 @@ # define struct_stat64 struct stat64 #else # define struct_stat64 struct stat -# define __open open # define __gen_tempname gen_tempname # define __getpid getpid # define __gettimeofday gettimeofday # define __mkdir mkdir +# define __open open +# define __open64 open # define __lxstat64(version, file, buf) lstat (file, buf) # define __xstat64(version, file, buf) stat (file, buf) #endif @@ -179,9 +184,9 @@ 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"). The name constructed - does not exist at the time of the call to __gen_tempname. TMPL is - overwritten with the result. + rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix). + The name constructed does not exist at the time of the call to + __gen_tempname. TMPL is overwritten with the result. KIND may be one of: __GT_NOCREATE: simply verify that the name does not exist @@ -192,7 +197,7 @@ static const char letters[] = We use a clever algorithm to get hard-to-predict names. */ int -__gen_tempname (char *tmpl, int flags, int kind) +__gen_tempname (char *tmpl, int suffixlen, int flags, int kind) { int len; char *XXXXXX; @@ -220,14 +225,14 @@ __gen_tempname (char *tmpl, int flags, int kind) #endif len = strlen (tmpl); - if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX")) + if (len < 6 + suffixlen || memcmp (&tmpl[len - 6 - suffixlen], "XXXXXX", 6)) { __set_errno (EINVAL); return -1; } /* This is where the Xs start. */ - XXXXXX = &tmpl[len - 6]; + XXXXXX = &tmpl[len - 6 - suffixlen]; /* Get some more or less random data. */ #ifdef RANDOM_BITS @@ -262,8 +267,8 @@ __gen_tempname (char *tmpl, int flags, int kind) { case __GT_FILE: fd = __open (tmpl, - (flags & ~0777) | O_RDWR | O_CREAT | O_EXCL, - S_IRUSR | S_IWUSR); + (flags & ~O_ACCMODE) + | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); break; case __GT_DIR: @@ -290,6 +295,7 @@ __gen_tempname (char *tmpl, int flags, int kind) default: assert (! "invalid KIND in __gen_tempname"); + abort (); } if (fd >= 0) diff --git a/lib/tempname.h b/lib/tempname.h index edf707431..cd69e7d75 100644 --- a/lib/tempname.h +++ b/lib/tempname.h @@ -17,14 +17,25 @@ /* header written by Eric Blake */ -#define GT_FILE 1 -#define GT_DIR 2 -#define GT_NOCREATE 3 +#ifndef GL_TEMPNAME_H +# define GL_TEMPNAME_H + +# include + +# ifdef __GT_FILE +# define GT_FILE __GT_FILE +# define GT_DIR __GT_DIR +# define GT_NOCREATE __GT_NOCREATE +# else +# define GT_FILE 0 +# define GT_DIR 1 +# define GT_NOCREATE 2 +# endif /* Generate a temporary file name based on TMPL. TMPL must match the - rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed - does not exist at the time of the call to gen_tempname. TMPL is - overwritten with the result. + rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix). + The name constructed does not exist at the time of the call to + gen_tempname. TMPL is overwritten with the result. KIND may be one of: GT_NOCREATE: simply verify that the name does not exist @@ -34,4 +45,6 @@ GT_DIR: create a directory, which will be mode 0700. We use a clever algorithm to get hard-to-predict names. */ -extern int gen_tempname (char *tmpl, int flags, int kind); +extern int gen_tempname (char *tmpl, int suffixlen, int flags, int kind); + +#endif /* GL_TEMPNAME_H */ diff --git a/lib/tmpfile.c b/lib/tmpfile.c index 19c8a5e24..83df9fe29 100644 --- a/lib/tmpfile.c +++ b/lib/tmpfile.c @@ -83,7 +83,7 @@ tmpfile (void) do { memcpy (&xtemplate[len - 6], "XXXXXX", 6); - if (gen_tempname (xtemplate, 0, GT_NOCREATE) < 0) + if (gen_tempname (xtemplate, 0, 0, GT_NOCREATE) < 0) { fd = -1; break; -- 2.11.0