mktemp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * Copyright (c) 1987, 1993
  18. * The Regents of the University of California. All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. * 1. Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. * 2. Redistributions in binary form must reproduce the above copyright
  26. * notice, this list of conditions and the following disclaimer in the
  27. * documentation and/or other materials provided with the distribution.
  28. * 3. All advertising materials mentioning features or use of this software
  29. * must display the following acknowledgement:
  30. * This product includes software developed by the University of
  31. * California, Berkeley and its contributors.
  32. * 4. Neither the name of the University nor the names of its contributors
  33. * may be used to endorse or promote products derived from this software
  34. * without specific prior written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  37. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  40. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  41. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  42. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  44. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  45. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. */
  48. #include "fspr_private.h"
  49. #include "fspr_file_io.h" /* prototype of fspr_mkstemp() */
  50. #include "fspr_strings.h" /* prototype of fspr_mkstemp() */
  51. #include "fspr_arch_file_io.h" /* prototype of fspr_mkstemp() */
  52. #include "fspr_portable.h" /* for fspr_os_file_put() */
  53. #ifndef HAVE_MKSTEMP
  54. #if defined(SVR4) || defined(WIN32) || defined(NETWARE)
  55. #ifdef SVR4
  56. #if HAVE_INTTYPES_H
  57. #include <inttypes.h>
  58. #endif
  59. #endif
  60. #define arc4random() rand()
  61. #define seedrandom(a) srand(a)
  62. #else
  63. #if APR_HAVE_STDINT_H
  64. #include <stdint.h>
  65. #endif
  66. #define arc4random() random()
  67. #define seedrandom(a) srandom(a)
  68. #endif
  69. #if APR_HAVE_SYS_TYPES_H
  70. #include <sys/types.h>
  71. #endif
  72. #if APR_HAVE_SYS_STAT_H
  73. #include <sys/stat.h>
  74. #endif
  75. #if APR_HAVE_FCNTL_H
  76. #include <fcntl.h>
  77. #endif
  78. #include <stdio.h>
  79. #include <stdlib.h>
  80. #include <string.h>
  81. #include <ctype.h>
  82. #ifdef HAVE_TIME_H
  83. #include <time.h>
  84. #endif
  85. static const unsigned char padchar[] =
  86. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  87. static fspr_uint32_t randseed=0;
  88. static int gettemp(char *path, fspr_file_t **doopen, fspr_int32_t flags, fspr_pool_t *p)
  89. {
  90. register char *start, *trv, *suffp;
  91. char *pad;
  92. fspr_finfo_t sbuf;
  93. fspr_status_t rv;
  94. fspr_uint32_t randnum;
  95. if (randseed==0) {
  96. randseed = (int)fspr_time_now();
  97. seedrandom(randseed);
  98. }
  99. for (trv = path; *trv; ++trv)
  100. ;
  101. suffp = trv;
  102. --trv;
  103. if (trv < path) {
  104. return APR_EINVAL;
  105. }
  106. /* Fill space with random characters */
  107. while (*trv == 'X') {
  108. randnum = arc4random() % (sizeof(padchar) - 1);
  109. *trv-- = padchar[randnum];
  110. }
  111. start = trv + 1;
  112. /*
  113. * check the target directory.
  114. */
  115. for (;; --trv) {
  116. if (trv <= path)
  117. break;
  118. if (*trv == '/') {
  119. *trv = '\0';
  120. rv = fspr_stat(&sbuf, path, APR_FINFO_TYPE, p);
  121. *trv = '/';
  122. if (rv != APR_SUCCESS)
  123. return rv;
  124. if (sbuf.filetype != APR_DIR) {
  125. return APR_ENOTDIR;
  126. }
  127. break;
  128. }
  129. }
  130. for (;;) {
  131. if ((rv = fspr_file_open(doopen, path, flags,
  132. APR_UREAD | APR_UWRITE, p)) == APR_SUCCESS)
  133. return APR_SUCCESS;
  134. if (!APR_STATUS_IS_EEXIST(rv))
  135. return rv;
  136. /* If we have a collision, cycle through the space of filenames */
  137. for (trv = start;;) {
  138. if (*trv == '\0' || trv == suffp)
  139. return APR_EINVAL; /* XXX: is this the correct return code? */
  140. pad = strchr((char *)padchar, *trv);
  141. if (pad == NULL || !*++pad) {
  142. *trv++ = padchar[0];
  143. }
  144. else {
  145. *trv++ = *pad;
  146. break;
  147. }
  148. }
  149. }
  150. /*NOTREACHED*/
  151. }
  152. #else
  153. #if APR_HAVE_STDLIB_H
  154. #include <stdlib.h> /* for mkstemp() - Single Unix */
  155. #endif
  156. #if APR_HAVE_UNISTD_H
  157. #include <unistd.h> /* for mkstemp() - FreeBSD */
  158. #endif
  159. #endif /* !defined(HAVE_MKSTEMP) */
  160. APR_DECLARE(fspr_status_t) fspr_file_mktemp(fspr_file_t **fp, char *template, fspr_int32_t flags, fspr_pool_t *p)
  161. {
  162. #ifdef HAVE_MKSTEMP
  163. int fd;
  164. #endif
  165. flags = (!flags) ? APR_CREATE | APR_READ | APR_WRITE | APR_EXCL |
  166. APR_DELONCLOSE : flags;
  167. #ifndef HAVE_MKSTEMP
  168. return gettemp(template, fp, flags, p);
  169. #else
  170. #ifdef HAVE_MKSTEMP64
  171. fd = mkstemp64(template);
  172. #else
  173. fd = mkstemp(template);
  174. #endif
  175. if (fd == -1) {
  176. return errno;
  177. }
  178. /* XXX: We must reset several flags values as passed-in, since
  179. * mkstemp didn't subscribe to our preference flags.
  180. *
  181. * We either have to unset the flags, or fix up the fd and other
  182. * xthread and inherit bits appropriately. Since gettemp() above
  183. * calls fspr_file_open, our flags are respected in that code path.
  184. */
  185. fspr_os_file_put(fp, &fd, flags, p);
  186. (*fp)->fname = fspr_pstrdup(p, template);
  187. fspr_pool_cleanup_register((*fp)->pool, (void *)(*fp),
  188. fspr_unix_file_cleanup, fspr_unix_file_cleanup);
  189. #endif
  190. return APR_SUCCESS;
  191. }