mktemp_system.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 <fcntl.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. #ifdef _win32_
  38. #include "winint.h"
  39. #include <util/folder/dirut.h>
  40. #endif
  41. #include <util/random/random.h>
  42. #include "sysstat.h"
  43. static const unsigned char padchar[] =
  44. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  45. static int
  46. GetTemp(char* path, int* doopen, int domkdir, int slen)
  47. {
  48. char *start, *trv, *suffp;
  49. char* pad;
  50. #ifndef _win32_
  51. struct stat sbuf;
  52. int rval;
  53. #endif
  54. ui32 rand;
  55. if (doopen != nullptr && domkdir) {
  56. errno = EINVAL;
  57. return (0);
  58. }
  59. trv = path;
  60. while (*trv != 0) {
  61. ++trv;
  62. }
  63. trv -= slen;
  64. suffp = trv;
  65. --trv;
  66. if (trv < path) {
  67. errno = EINVAL;
  68. return (0);
  69. }
  70. /* Fill space with random characters */
  71. while (trv >= path && *trv == 'X') {
  72. rand = (RandomNumber<ui32>()) % (sizeof(padchar) - 1);
  73. *trv-- = padchar[rand];
  74. }
  75. start = trv + 1;
  76. /*
  77. * check the target directory.
  78. */
  79. if (doopen != nullptr || domkdir) {
  80. for (; trv > path; --trv) {
  81. if (*trv == '/') {
  82. *trv = '\0';
  83. #ifdef _win32_
  84. ui32 attr = ::GetFileAttributesA(path);
  85. *trv = '/';
  86. if (attr == 0xFFFFFFFF)
  87. return (0);
  88. if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {
  89. errno = ENOTDIR;
  90. return (0);
  91. }
  92. #else
  93. rval = stat(path, &sbuf);
  94. *trv = '/';
  95. if (rval != 0) {
  96. return (0);
  97. }
  98. if (!S_ISDIR(sbuf.st_mode)) {
  99. errno = ENOTDIR;
  100. return (0);
  101. }
  102. #endif
  103. break;
  104. }
  105. }
  106. }
  107. for (;;) {
  108. if (doopen) {
  109. if ((*doopen =
  110. open(path, O_CREAT | O_EXCL | O_RDWR, 0600)) >= 0) {
  111. return (1);
  112. }
  113. if (errno != EEXIST) {
  114. return (0);
  115. }
  116. } else if (domkdir) {
  117. if (Mkdir(path, S_IRWXU) == 0) {
  118. return (1);
  119. }
  120. if (errno != EEXIST) {
  121. return (0);
  122. }
  123. } else
  124. #ifdef _win32_
  125. if (::GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES)
  126. return (errno == ENOENT);
  127. #else
  128. if (lstat(path, &sbuf)) {
  129. return (errno == ENOENT);
  130. }
  131. #endif
  132. /* If we have a collision, cycle through the space of filenames */
  133. for (trv = start;;) {
  134. if (*trv == '\0' || trv == suffp) {
  135. return (0);
  136. }
  137. pad = strchr((char*)padchar, *trv);
  138. if (pad == nullptr || *++pad == '\0') {
  139. *trv++ = padchar[0];
  140. } else {
  141. *trv++ = *pad;
  142. break;
  143. }
  144. }
  145. }
  146. /*NOTREACHED*/
  147. }
  148. extern "C" int mkstemps(char* path, int slen) {
  149. int fd;
  150. return (GetTemp(path, &fd, 0, slen) ? fd : -1);
  151. }
  152. #if defined(_win_)
  153. char* mkdtemp(char* path) {
  154. return (GetTemp(path, (int*)nullptr, 1, 0) ? path : (char*)nullptr);
  155. }
  156. #endif