/contrib/bind9/lib/isc/unix/dir.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 251 lines · 123 code · 52 blank · 76 comment · 42 complexity · 6e7ab7a4f3fbcc1d84c096ea04b9e0c2 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007-2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. /*! \file
  19. * \author Principal Authors: DCL */
  20. #include <config.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <isc/dir.h>
  27. #include <isc/magic.h>
  28. #include <isc/string.h>
  29. #include <isc/util.h>
  30. #include "errno2result.h"
  31. #define ISC_DIR_MAGIC ISC_MAGIC('D', 'I', 'R', '*')
  32. #define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
  33. void
  34. isc_dir_init(isc_dir_t *dir) {
  35. REQUIRE(dir != NULL);
  36. dir->entry.name[0] = '\0';
  37. dir->entry.length = 0;
  38. dir->handle = NULL;
  39. dir->magic = ISC_DIR_MAGIC;
  40. }
  41. /*!
  42. * \brief Allocate workspace and open directory stream. If either one fails,
  43. * NULL will be returned.
  44. */
  45. isc_result_t
  46. isc_dir_open(isc_dir_t *dir, const char *dirname) {
  47. char *p;
  48. isc_result_t result = ISC_R_SUCCESS;
  49. REQUIRE(VALID_DIR(dir));
  50. REQUIRE(dirname != NULL);
  51. /*
  52. * Copy directory name. Need to have enough space for the name,
  53. * a possible path separator, the wildcard, and the final NUL.
  54. */
  55. if (strlen(dirname) + 3 > sizeof(dir->dirname))
  56. /* XXXDCL ? */
  57. return (ISC_R_NOSPACE);
  58. strcpy(dir->dirname, dirname);
  59. /*
  60. * Append path separator, if needed, and "*".
  61. */
  62. p = dir->dirname + strlen(dir->dirname);
  63. if (dir->dirname < p && *(p - 1) != '/')
  64. *p++ = '/';
  65. *p++ = '*';
  66. *p = '\0';
  67. /*
  68. * Open stream.
  69. */
  70. dir->handle = opendir(dirname);
  71. if (dir->handle == NULL)
  72. return isc__errno2result(errno);
  73. return (result);
  74. }
  75. /*!
  76. * \brief Return previously retrieved file or get next one.
  77. * Unix's dirent has
  78. * separate open and read functions, but the Win32 and DOS interfaces open
  79. * the dir stream and reads the first file in one operation.
  80. */
  81. isc_result_t
  82. isc_dir_read(isc_dir_t *dir) {
  83. struct dirent *entry;
  84. REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
  85. /*
  86. * Fetch next file in directory.
  87. */
  88. entry = readdir(dir->handle);
  89. if (entry == NULL)
  90. return (ISC_R_NOMORE);
  91. /*
  92. * Make sure that the space for the name is long enough.
  93. */
  94. if (sizeof(dir->entry.name) <= strlen(entry->d_name))
  95. return (ISC_R_UNEXPECTED);
  96. strcpy(dir->entry.name, entry->d_name);
  97. /*
  98. * Some dirents have d_namlen, but it is not portable.
  99. */
  100. dir->entry.length = strlen(entry->d_name);
  101. return (ISC_R_SUCCESS);
  102. }
  103. /*!
  104. * \brief Close directory stream.
  105. */
  106. void
  107. isc_dir_close(isc_dir_t *dir) {
  108. REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
  109. (void)closedir(dir->handle);
  110. dir->handle = NULL;
  111. }
  112. /*!
  113. * \brief Reposition directory stream at start.
  114. */
  115. isc_result_t
  116. isc_dir_reset(isc_dir_t *dir) {
  117. REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
  118. rewinddir(dir->handle);
  119. return (ISC_R_SUCCESS);
  120. }
  121. isc_result_t
  122. isc_dir_chdir(const char *dirname) {
  123. /*!
  124. * \brief Change the current directory to 'dirname'.
  125. */
  126. REQUIRE(dirname != NULL);
  127. if (chdir(dirname) < 0)
  128. return (isc__errno2result(errno));
  129. return (ISC_R_SUCCESS);
  130. }
  131. isc_result_t
  132. isc_dir_chroot(const char *dirname) {
  133. REQUIRE(dirname != NULL);
  134. #ifdef HAVE_CHROOT
  135. if (chroot(dirname) < 0 || chdir("/") < 0)
  136. return (isc__errno2result(errno));
  137. return (ISC_R_SUCCESS);
  138. #else
  139. return (ISC_R_NOTIMPLEMENTED);
  140. #endif
  141. }
  142. isc_result_t
  143. isc_dir_createunique(char *templet) {
  144. isc_result_t result;
  145. char *x;
  146. char *p;
  147. int i;
  148. int pid;
  149. REQUIRE(templet != NULL);
  150. /*!
  151. * \brief mkdtemp is not portable, so this emulates it.
  152. */
  153. pid = getpid();
  154. /*
  155. * Replace trailing Xs with the process-id, zero-filled.
  156. */
  157. for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
  158. x--, pid /= 10)
  159. *x = pid % 10 + '0';
  160. x++; /* Set x to start of ex-Xs. */
  161. do {
  162. i = mkdir(templet, 0700);
  163. if (i == 0 || errno != EEXIST)
  164. break;
  165. /*
  166. * The BSD algorithm.
  167. */
  168. p = x;
  169. while (*p != '\0') {
  170. if (isdigit(*p & 0xff))
  171. *p = 'a';
  172. else if (*p != 'z')
  173. ++*p;
  174. else {
  175. /*
  176. * Reset character and move to next.
  177. */
  178. *p++ = 'a';
  179. continue;
  180. }
  181. break;
  182. }
  183. if (*p == '\0') {
  184. /*
  185. * Tried all combinations. errno should already
  186. * be EEXIST, but ensure it is anyway for
  187. * isc__errno2result().
  188. */
  189. errno = EEXIST;
  190. break;
  191. }
  192. } while (1);
  193. if (i == -1)
  194. result = isc__errno2result(errno);
  195. else
  196. result = ISC_R_SUCCESS;
  197. return (result);
  198. }