mktemp_system.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 1987, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "defaults.h"
  34. #include <sys/types.h>
  35. #include <fcntl.h>
  36. #include <errno.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <ctype.h>
  41. #ifdef _win32_
  42. #include "winint.h"
  43. #include <util/folder/dirut.h>
  44. #else
  45. #include <unistd.h>
  46. #endif
  47. #include <util/random/random.h>
  48. #include "sysstat.h"
  49. static const unsigned char padchar[] =
  50. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  51. static int
  52. GetTemp(char* path, int* doopen, int domkdir, int slen)
  53. {
  54. char *start, *trv, *suffp;
  55. char* pad;
  56. #ifndef _win32_
  57. struct stat sbuf;
  58. int rval;
  59. #endif
  60. ui32 rand;
  61. if (doopen != nullptr && domkdir) {
  62. errno = EINVAL;
  63. return (0);
  64. }
  65. for (trv = path; *trv != '\0'; ++trv) {
  66. ;
  67. }
  68. trv -= slen;
  69. suffp = trv;
  70. --trv;
  71. if (trv < path) {
  72. errno = EINVAL;
  73. return (0);
  74. }
  75. /* Fill space with random characters */
  76. while (trv >= path && *trv == 'X') {
  77. rand = (RandomNumber<ui32>()) % (sizeof(padchar) - 1);
  78. *trv-- = padchar[rand];
  79. }
  80. start = trv + 1;
  81. /*
  82. * check the target directory.
  83. */
  84. if (doopen != nullptr || domkdir) {
  85. for (; trv > path; --trv) {
  86. if (*trv == '/') {
  87. *trv = '\0';
  88. #ifdef _win32_
  89. ui32 attr = ::GetFileAttributesA(path);
  90. *trv = '/';
  91. if (attr == 0xFFFFFFFF)
  92. return (0);
  93. if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {
  94. errno = ENOTDIR;
  95. return (0);
  96. }
  97. #else
  98. rval = stat(path, &sbuf);
  99. *trv = '/';
  100. if (rval != 0) {
  101. return (0);
  102. }
  103. if (!S_ISDIR(sbuf.st_mode)) {
  104. errno = ENOTDIR;
  105. return (0);
  106. }
  107. #endif
  108. break;
  109. }
  110. }
  111. }
  112. for (;;) {
  113. if (doopen) {
  114. if ((*doopen =
  115. open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) >= 0) {
  116. return (1);
  117. }
  118. if (errno != EEXIST) {
  119. return (0);
  120. }
  121. } else if (domkdir) {
  122. if (Mkdir(path, S_IRWXU) == 0) {
  123. return (1);
  124. }
  125. if (errno != EEXIST) {
  126. return (0);
  127. }
  128. } else
  129. #ifdef _win32_
  130. if (::GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES)
  131. return (errno == ENOENT);
  132. #else
  133. if (lstat(path, &sbuf)) {
  134. return (errno == ENOENT);
  135. }
  136. #endif
  137. /* If we have a collision, cycle through the space of filenames */
  138. for (trv = start;;) {
  139. if (*trv == '\0' || trv == suffp) {
  140. return (0);
  141. }
  142. pad = strchr((char*)padchar, *trv);
  143. if (pad == nullptr || *++pad == '\0') {
  144. *trv++ = padchar[0];
  145. } else {
  146. *trv++ = *pad;
  147. break;
  148. }
  149. }
  150. }
  151. /*NOTREACHED*/
  152. }
  153. extern "C" int mkstemps(char* path, int slen) {
  154. int fd;
  155. return (GetTemp(path, &fd, 0, slen) ? fd : -1);
  156. }
  157. #if defined(_win_)
  158. char* mkdtemp(char* path) {
  159. return (GetTemp(path, (int*)nullptr, 1, 0) ? path : (char*)nullptr);
  160. }
  161. #endif