dir.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007-2009, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1999-2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id$ */
00019 
00020 /*! \file
00021  * \author  Principal Authors: DCL */
00022 
00023 #include <config.h>
00024 
00025 #include <sys/types.h>
00026 #include <sys/stat.h>
00027 
00028 #include <ctype.h>
00029 #include <errno.h>
00030 #include <unistd.h>
00031 
00032 #include <isc/dir.h>
00033 #include <isc/magic.h>
00034 #include <isc/string.h>
00035 #include <isc/util.h>
00036 
00037 #include "errno2result.h"
00038 
00039 #define ISC_DIR_MAGIC           ISC_MAGIC('D', 'I', 'R', '*')
00040 #define VALID_DIR(dir)          ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
00041 
00042 void
00043 isc_dir_init(isc_dir_t *dir) {
00044         REQUIRE(dir != NULL);
00045 
00046         dir->entry.name[0] = '\0';
00047         dir->entry.length = 0;
00048 
00049         dir->handle = NULL;
00050 
00051         dir->magic = ISC_DIR_MAGIC;
00052 }
00053 
00054 /*!
00055  * \brief Allocate workspace and open directory stream. If either one fails,
00056  * NULL will be returned.
00057  */
00058 isc_result_t
00059 isc_dir_open(isc_dir_t *dir, const char *dirname) {
00060         char *p;
00061         isc_result_t result = ISC_R_SUCCESS;
00062 
00063         REQUIRE(VALID_DIR(dir));
00064         REQUIRE(dirname != NULL);
00065 
00066         /*
00067          * Copy directory name.  Need to have enough space for the name,
00068          * a possible path separator, the wildcard, and the final NUL.
00069          */
00070         if (strlen(dirname) + 3 > sizeof(dir->dirname))
00071                 /* XXXDCL ? */
00072                 return (ISC_R_NOSPACE);
00073         strcpy(dir->dirname, dirname);
00074 
00075         /*
00076          * Append path separator, if needed, and "*".
00077          */
00078         p = dir->dirname + strlen(dir->dirname);
00079         if (dir->dirname < p && *(p - 1) != '/')
00080                 *p++ = '/';
00081         *p++ = '*';
00082         *p = '\0';
00083 
00084         /*
00085          * Open stream.
00086          */
00087         dir->handle = opendir(dirname);
00088 
00089         if (dir->handle == NULL)
00090                 return isc__errno2result(errno);
00091 
00092         return (result);
00093 }
00094 
00095 /*!
00096  * \brief Return previously retrieved file or get next one.
00097 
00098  * Unix's dirent has
00099  * separate open and read functions, but the Win32 and DOS interfaces open
00100  * the dir stream and reads the first file in one operation.
00101  */
00102 isc_result_t
00103 isc_dir_read(isc_dir_t *dir) {
00104         struct dirent *entry;
00105 
00106         REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
00107 
00108         /*
00109          * Fetch next file in directory.
00110          */
00111         entry = readdir(dir->handle);
00112 
00113         if (entry == NULL)
00114                 return (ISC_R_NOMORE);
00115 
00116         /*
00117          * Make sure that the space for the name is long enough.
00118          */
00119         if (sizeof(dir->entry.name) <= strlen(entry->d_name))
00120             return (ISC_R_UNEXPECTED);
00121 
00122         strcpy(dir->entry.name, entry->d_name);
00123 
00124         /*
00125          * Some dirents have d_namlen, but it is not portable.
00126          */
00127         dir->entry.length = strlen(entry->d_name);
00128 
00129         return (ISC_R_SUCCESS);
00130 }
00131 
00132 /*!
00133  * \brief Close directory stream.
00134  */
00135 void
00136 isc_dir_close(isc_dir_t *dir) {
00137        REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
00138 
00139        (void)closedir(dir->handle);
00140        dir->handle = NULL;
00141 }
00142 
00143 /*!
00144  * \brief Reposition directory stream at start.
00145  */
00146 isc_result_t
00147 isc_dir_reset(isc_dir_t *dir) {
00148         REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
00149 
00150         rewinddir(dir->handle);
00151 
00152         return (ISC_R_SUCCESS);
00153 }
00154 
00155 isc_result_t
00156 isc_dir_chdir(const char *dirname) {
00157         /*!
00158          * \brief Change the current directory to 'dirname'.
00159          */
00160 
00161         REQUIRE(dirname != NULL);
00162 
00163         if (chdir(dirname) < 0)
00164                 return (isc__errno2result(errno));
00165 
00166         return (ISC_R_SUCCESS);
00167 }
00168 
00169 isc_result_t
00170 isc_dir_chroot(const char *dirname) {
00171 
00172         REQUIRE(dirname != NULL);
00173 
00174 #ifdef HAVE_CHROOT
00175         if (chroot(dirname) < 0 || chdir("/") < 0)
00176                 return (isc__errno2result(errno));
00177 
00178         return (ISC_R_SUCCESS);
00179 #else
00180         return (ISC_R_NOTIMPLEMENTED);
00181 #endif
00182 }
00183 
00184 isc_result_t
00185 isc_dir_createunique(char *templet) {
00186         isc_result_t result;
00187         char *x;
00188         char *p;
00189         int i;
00190         int pid;
00191 
00192         REQUIRE(templet != NULL);
00193 
00194         /*!
00195          * \brief mkdtemp is not portable, so this emulates it.
00196          */
00197 
00198         pid = getpid();
00199 
00200         /*
00201          * Replace trailing Xs with the process-id, zero-filled.
00202          */
00203         for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
00204              x--, pid /= 10)
00205                 *x = pid % 10 + '0';
00206 
00207         x++;                    /* Set x to start of ex-Xs. */
00208 
00209         do {
00210                 i = mkdir(templet, 0700);
00211                 if (i == 0 || errno != EEXIST)
00212                         break;
00213 
00214                 /*
00215                  * The BSD algorithm.
00216                  */
00217                 p = x;
00218                 while (*p != '\0') {
00219                         if (isdigit(*p & 0xff))
00220                                 *p = 'a';
00221                         else if (*p != 'z')
00222                                 ++*p;
00223                         else {
00224                                 /*
00225                                  * Reset character and move to next.
00226                                  */
00227                                 *p++ = 'a';
00228                                 continue;
00229                         }
00230 
00231                         break;
00232                 }
00233 
00234                 if (*p == '\0') {
00235                         /*
00236                          * Tried all combinations.  errno should already
00237                          * be EEXIST, but ensure it is anyway for
00238                          * isc__errno2result().
00239                          */
00240                         errno = EEXIST;
00241                         break;
00242                 }
00243         } while (1);
00244 
00245         if (i == -1)
00246                 result = isc__errno2result(errno);
00247         else
00248                 result = ISC_R_SUCCESS;
00249 
00250         return (result);
00251 }

Generated on Tue Apr 28 17:41:05 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha