Skip to content

Commit 9e6b1bf

Browse files
committed
Add mkdtemp() to libpgport.
This function is pervasive on free software operating systems; import NetBSD's implementation. Back-patch to 8.4, like the commit that will harness it.
1 parent 0ef0b67 commit 9e6b1bf

File tree

7 files changed

+317
-2
lines changed

7 files changed

+317
-2
lines changed

configure

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11650,6 +11650,19 @@ esac
1165011650

1165111651
fi
1165211652

11653+
ac_fn_c_check_func "$LINENO" "mkdtemp" "ac_cv_func_mkdtemp"
11654+
if test "x$ac_cv_func_mkdtemp" = xyes; then :
11655+
$as_echo "#define HAVE_MKDTEMP 1" >>confdefs.h
11656+
11657+
else
11658+
case " $LIBOBJS " in
11659+
*" mkdtemp.$ac_objext "* ) ;;
11660+
*) LIBOBJS="$LIBOBJS mkdtemp.$ac_objext"
11661+
;;
11662+
esac
11663+
11664+
fi
11665+
1165311666
ac_fn_c_check_func "$LINENO" "random" "ac_cv_func_random"
1165411667
if test "x$ac_cv_func_random" = xyes; then :
1165511668
$as_echo "#define HAVE_RANDOM 1" >>confdefs.h

configure.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1357,7 +1357,7 @@ else
13571357
AC_CHECK_FUNCS([fpclass fp_class fp_class_d class], [break])
13581358
fi
13591359

1360-
AC_REPLACE_FUNCS([crypt fls getopt getrusage inet_aton random rint srandom strerror strlcat strlcpy])
1360+
AC_REPLACE_FUNCS([crypt fls getopt getrusage inet_aton mkdtemp random rint srandom strerror strlcat strlcpy])
13611361

13621362
case $host_os in
13631363

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@
330330
/* Define to 1 if the system has the type `MINIDUMP_TYPE'. */
331331
#undef HAVE_MINIDUMP_TYPE
332332

333+
/* Define to 1 if you have the `mkdtemp' function. */
334+
#undef HAVE_MKDTEMP
335+
333336
/* Define to 1 if you have the <netinet/in.h> header file. */
334337
#undef HAVE_NETINET_IN_H
335338

src/include/pg_config.h.win32

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@
249249
/* Define to 1 if the system has the type `MINIDUMP_TYPE'. */
250250
#define HAVE_MINIDUMP_TYPE 1
251251

252+
/* Define to 1 if you have the `mkdtemp' function. */
253+
/* #undef HAVE_MKDTEMP */
254+
252255
/* Define to 1 if you have the <netinet/in.h> header file. */
253256
#define HAVE_NETINET_IN_H 1
254257

src/include/port.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ extern int pg_check_dir(const char *dir);
462462
/* port/pgmkdirp.c */
463463
extern int pg_mkdir_p(char *path, int omode);
464464

465+
/* port/mkdtemp.c */
466+
extern char *mkdtemp(char *path);
467+
465468
/* port/pqsignal.c */
466469
typedef void (*pqsigfunc) (int signo);
467470
extern pqsigfunc pqsignal(int signo, pqsigfunc func);

src/port/mkdtemp.c

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* mkdtemp.c
4+
* create a mode-0700 temporary directory
5+
*
6+
* Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
7+
*
8+
*
9+
* IDENTIFICATION
10+
* src/port/mkdtemp.c
11+
*
12+
* This code was taken from NetBSD to provide an implementation for platforms
13+
* that lack it. (Among compatibly-licensed implementations, the OpenBSD
14+
* version better resists denial-of-service attacks. However, it has a
15+
* cryptographic dependency.) The NetBSD copyright terms follow.
16+
*-------------------------------------------------------------------------
17+
*/
18+
19+
#include "c.h"
20+
21+
#define _DIAGASSERT(x) do {} while (0)
22+
23+
24+
/* $NetBSD: gettemp.c,v 1.17 2014/01/21 19:09:48 seanb Exp $ */
25+
26+
/*
27+
* Copyright (c) 1987, 1993
28+
* The Regents of the University of California. All rights reserved.
29+
*
30+
* Redistribution and use in source and binary forms, with or without
31+
* modification, are permitted provided that the following conditions
32+
* are met:
33+
* 1. Redistributions of source code must retain the above copyright
34+
* notice, this list of conditions and the following disclaimer.
35+
* 2. Redistributions in binary form must reproduce the above copyright
36+
* notice, this list of conditions and the following disclaimer in the
37+
* documentation and/or other materials provided with the distribution.
38+
* 3. Neither the name of the University nor the names of its contributors
39+
* may be used to endorse or promote products derived from this software
40+
* without specific prior written permission.
41+
*
42+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52+
* SUCH DAMAGE.
53+
*/
54+
55+
#if HAVE_NBTOOL_CONFIG_H
56+
#include "nbtool_config.h"
57+
#endif
58+
59+
#if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP
60+
61+
#ifdef NOT_POSTGRESQL
62+
#include <sys/cdefs.h>
63+
#if defined(LIBC_SCCS) && !defined(lint)
64+
#if 0
65+
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
66+
#else
67+
__RCSID("$NetBSD: gettemp.c,v 1.17 2014/01/21 19:09:48 seanb Exp $");
68+
#endif
69+
#endif /* LIBC_SCCS and not lint */
70+
#endif
71+
72+
#include <sys/types.h>
73+
#include <sys/stat.h>
74+
75+
#include <assert.h>
76+
#include <ctype.h>
77+
#include <errno.h>
78+
#include <fcntl.h>
79+
#include <stdio.h>
80+
#include <stdlib.h>
81+
#include <unistd.h>
82+
83+
#ifdef NOT_POSTGRESQL
84+
#if HAVE_NBTOOL_CONFIG_H
85+
#define GETTEMP __nbcompat_gettemp
86+
#else
87+
#include "reentrant.h"
88+
#include "local.h"
89+
#define GETTEMP __gettemp
90+
#endif
91+
#endif
92+
93+
static int
94+
GETTEMP(char *path, int *doopen, int domkdir)
95+
{
96+
char *start,
97+
*trv;
98+
struct stat sbuf;
99+
u_int pid;
100+
101+
/*
102+
* To guarantee multiple calls generate unique names even if the file is
103+
* not created. 676 different possibilities with 7 or more X's, 26 with 6
104+
* or less.
105+
*/
106+
static char xtra[2] = "aa";
107+
int xcnt = 0;
108+
109+
_DIAGASSERT(path != NULL);
110+
/* doopen may be NULL */
111+
112+
pid = getpid();
113+
114+
/* Move to end of path and count trailing X's. */
115+
for (trv = path; *trv; ++trv)
116+
if (*trv == 'X')
117+
xcnt++;
118+
else
119+
xcnt = 0;
120+
121+
/* Use at least one from xtra. Use 2 if more than 6 X's. */
122+
if (xcnt > 0)
123+
{
124+
*--trv = xtra[0];
125+
xcnt--;
126+
}
127+
if (xcnt > 5)
128+
{
129+
*--trv = xtra[1];
130+
xcnt--;
131+
}
132+
133+
/* Set remaining X's to pid digits with 0's to the left. */
134+
for (; xcnt > 0; xcnt--)
135+
{
136+
*--trv = (pid % 10) + '0';
137+
pid /= 10;
138+
}
139+
140+
/* update xtra for next call. */
141+
if (xtra[0] != 'z')
142+
xtra[0]++;
143+
else
144+
{
145+
xtra[0] = 'a';
146+
if (xtra[1] != 'z')
147+
xtra[1]++;
148+
else
149+
xtra[1] = 'a';
150+
}
151+
152+
/*
153+
* check the target directory; if you have six X's and it doesn't exist
154+
* this runs for a *very* long time.
155+
*/
156+
for (start = trv + 1;; --trv)
157+
{
158+
if (trv <= path)
159+
break;
160+
if (*trv == '/')
161+
{
162+
int e;
163+
164+
*trv = '\0';
165+
e = stat(path, &sbuf);
166+
*trv = '/';
167+
if (e == -1)
168+
return doopen == NULL && !domkdir;
169+
if (!S_ISDIR(sbuf.st_mode))
170+
{
171+
errno = ENOTDIR;
172+
return doopen == NULL && !domkdir;
173+
}
174+
break;
175+
}
176+
}
177+
178+
for (;;)
179+
{
180+
if (doopen)
181+
{
182+
if ((*doopen =
183+
open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) >= 0)
184+
return 1;
185+
if (errno != EEXIST)
186+
return 0;
187+
}
188+
else if (domkdir)
189+
{
190+
if (mkdir(path, 0700) >= 0)
191+
return 1;
192+
if (errno != EEXIST)
193+
return 0;
194+
}
195+
else if (lstat(path, &sbuf))
196+
return errno == ENOENT ? 1 : 0;
197+
198+
/* tricky little algorithm for backward compatibility */
199+
for (trv = start;;)
200+
{
201+
if (!*trv)
202+
return 0;
203+
if (*trv == 'z')
204+
*trv++ = 'a';
205+
else
206+
{
207+
if (isdigit((unsigned char) *trv))
208+
*trv = 'a';
209+
else
210+
++* trv;
211+
break;
212+
}
213+
}
214+
}
215+
/* NOTREACHED */
216+
}
217+
218+
#endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP ||
219+
* !HAVE_MKDTEMP */
220+
221+
222+
/* $NetBSD: mkdtemp.c,v 1.11 2012/03/15 18:22:30 christos Exp $ */
223+
224+
/*
225+
* Copyright (c) 1987, 1993
226+
* The Regents of the University of California. All rights reserved.
227+
*
228+
* Redistribution and use in source and binary forms, with or without
229+
* modification, are permitted provided that the following conditions
230+
* are met:
231+
* 1. Redistributions of source code must retain the above copyright
232+
* notice, this list of conditions and the following disclaimer.
233+
* 2. Redistributions in binary form must reproduce the above copyright
234+
* notice, this list of conditions and the following disclaimer in the
235+
* documentation and/or other materials provided with the distribution.
236+
* 3. Neither the name of the University nor the names of its contributors
237+
* may be used to endorse or promote products derived from this software
238+
* without specific prior written permission.
239+
*
240+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
241+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
243+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
244+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
246+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
247+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
249+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
250+
* SUCH DAMAGE.
251+
*/
252+
253+
#if HAVE_NBTOOL_CONFIG_H
254+
#include "nbtool_config.h"
255+
#endif
256+
257+
#if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKDTEMP
258+
259+
#ifdef NOT_POSTGRESQL
260+
261+
#include <sys/cdefs.h>
262+
#if defined(LIBC_SCCS) && !defined(lint)
263+
#if 0
264+
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
265+
#else
266+
__RCSID("$NetBSD: mkdtemp.c,v 1.11 2012/03/15 18:22:30 christos Exp $");
267+
#endif
268+
#endif /* LIBC_SCCS and not lint */
269+
270+
#if HAVE_NBTOOL_CONFIG_H
271+
#define GETTEMP __nbcompat_gettemp
272+
#else
273+
#include <assert.h>
274+
#include <errno.h>
275+
#include <stdio.h>
276+
#include <stdlib.h>
277+
#include <unistd.h>
278+
#include "reentrant.h"
279+
#include "local.h"
280+
#define GETTEMP __gettemp
281+
#endif
282+
283+
#endif
284+
285+
char *
286+
mkdtemp(char *path)
287+
{
288+
_DIAGASSERT(path != NULL);
289+
290+
return GETTEMP(path, NULL, 1) ? path : NULL;
291+
}
292+
293+
#endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKDTEMP */

src/tools/msvc/Mkvcbuild.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ sub mkvcbuild
6969
srandom.c getaddrinfo.c gettimeofday.c inet_net_ntop.c kill.c open.c
7070
erand48.c snprintf.c strlcat.c strlcpy.c dirmod.c noblock.c path.c
7171
pgcheckdir.c pg_crc.c pgmkdirp.c pgsleep.c pgstrcasecmp.c pqsignal.c
72-
qsort.c qsort_arg.c quotes.c system.c
72+
mkdtemp.c qsort.c qsort_arg.c quotes.c system.c
7373
sprompt.c tar.c thread.c getopt.c getopt_long.c dirent.c
7474
win32env.c win32error.c win32setlocale.c);
7575

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy