00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
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 
00056 
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 
00068 
00069 
00070         if (strlen(dirname) + 3 > sizeof(dir->dirname))
00071                 
00072                 return (ISC_R_NOSPACE);
00073         strcpy(dir->dirname, dirname);
00074 
00075         
00076 
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 
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 
00097 
00098 
00099 
00100 
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 
00110 
00111         entry = readdir(dir->handle);
00112 
00113         if (entry == NULL)
00114                 return (ISC_R_NOMORE);
00115 
00116         
00117 
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 
00126 
00127         dir->entry.length = strlen(entry->d_name);
00128 
00129         return (ISC_R_SUCCESS);
00130 }
00131 
00132 
00133 
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 
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 
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 
00196 
00197 
00198         pid = getpid();
00199 
00200         
00201 
00202 
00203         for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
00204              x--, pid /= 10)
00205                 *x = pid % 10 + '0';
00206 
00207         x++;                    
00208 
00209         do {
00210                 i = mkdir(templet, 0700);
00211                 if (i == 0 || errno != EEXIST)
00212                         break;
00213 
00214                 
00215 
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 
00226 
00227                                 *p++ = 'a';
00228                                 continue;
00229                         }
00230 
00231                         break;
00232                 }
00233 
00234                 if (*p == '\0') {
00235                         
00236 
00237 
00238 
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 }