1 /*-------------------------------------------------------------------------
4 * create a mode-0700 temporary directory
6 * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
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 *-------------------------------------------------------------------------
21 #define _DIAGASSERT(x) do {} while (0)
24 /* $NetBSD: gettemp.c,v 1.17 2014/01/21 19:09:48 seanb Exp $ */
27 * Copyright (c) 1987, 1993
28 * The Regents of the University of California. All rights reserved.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
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.
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
55 #if HAVE_NBTOOL_CONFIG_H
56 #include "nbtool_config.h"
59 #if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP || !HAVE_MKDTEMP
62 #include <sys/cdefs.h>
63 #if defined(LIBC_SCCS) && !defined(lint)
65 static char sccsid
[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
67 __RCSID("$NetBSD: gettemp.c,v 1.17 2014/01/21 19:09:48 seanb Exp $");
69 #endif /* LIBC_SCCS and not lint */
72 #include <sys/types.h>
84 #if HAVE_NBTOOL_CONFIG_H
85 #define GETTEMP __nbcompat_gettemp
87 #include "reentrant.h"
89 #define GETTEMP __gettemp
94 GETTEMP(char *path
, int *doopen
, int domkdir
)
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
106 static char xtra
[2] = "aa";
109 _DIAGASSERT(path
!= NULL
);
110 /* doopen may be NULL */
114 /* Move to end of path and count trailing X's. */
115 for (trv
= path
; *trv
; ++trv
)
121 /* Use at least one from xtra. Use 2 if more than 6 X's. */
133 /* Set remaining X's to pid digits with 0's to the left. */
134 for (; xcnt
> 0; xcnt
--)
136 *--trv
= (pid
% 10) + '0';
140 /* update xtra for next call. */
153 * check the target directory; if you have six X's and it doesn't exist
154 * this runs for a *very* long time.
156 for (start
= trv
+ 1;; --trv
)
165 e
= stat(path
, &sbuf
);
168 return doopen
== NULL
&& !domkdir
;
169 if (!S_ISDIR(sbuf
.st_mode
))
172 return doopen
== NULL
&& !domkdir
;
183 open(path
, O_CREAT
| O_EXCL
| O_RDWR
, 0600)) >= 0)
190 if (mkdir(path
, 0700) >= 0)
195 else if (lstat(path
, &sbuf
))
196 return errno
== ENOENT
? 1 : 0;
198 /* tricky little algorithm for backward compatibility */
207 if (isdigit((unsigned char) *trv
))
218 #endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKSTEMP ||
222 /* $NetBSD: mkdtemp.c,v 1.11 2012/03/15 18:22:30 christos Exp $ */
225 * Copyright (c) 1987, 1993
226 * The Regents of the University of California. All rights reserved.
228 * Redistribution and use in source and binary forms, with or without
229 * modification, are permitted provided that the following conditions
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.
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
253 #if HAVE_NBTOOL_CONFIG_H
254 #include "nbtool_config.h"
257 #if !HAVE_NBTOOL_CONFIG_H || !HAVE_MKDTEMP
259 #ifdef NOT_POSTGRESQL
261 #include <sys/cdefs.h>
262 #if defined(LIBC_SCCS) && !defined(lint)
264 static char sccsid
[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
266 __RCSID("$NetBSD: mkdtemp.c,v 1.11 2012/03/15 18:22:30 christos Exp $");
268 #endif /* LIBC_SCCS and not lint */
270 #if HAVE_NBTOOL_CONFIG_H
271 #define GETTEMP __nbcompat_gettemp
278 #include "reentrant.h"
280 #define GETTEMP __gettemp
288 _DIAGASSERT(path
!= NULL
);
290 return GETTEMP(path
, NULL
, 1) ? path
: NULL
;
293 #endif /* !HAVE_NBTOOL_CONFIG_H || !HAVE_MKDTEMP */