]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/bind9/lib/isc/unix/dir.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / bind9 / lib / isc / unix / dir.c
1 /*
2  * Copyright (C) 2004, 2005  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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
18 /* $Id: dir.c,v 1.20.18.3 2005/09/05 00:18:30 marka Exp $ */
19
20 /*! \file
21  * \author  Principal Authors: DCL */
22
23 #include <config.h>
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27
28 #include <ctype.h>
29 #include <errno.h>
30 #include <unistd.h>
31
32 #include <isc/dir.h>
33 #include <isc/magic.h>
34 #include <isc/string.h>
35 #include <isc/util.h>
36
37 #include "errno2result.h"
38
39 #define ISC_DIR_MAGIC           ISC_MAGIC('D', 'I', 'R', '*')
40 #define VALID_DIR(dir)          ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
41
42 void
43 isc_dir_init(isc_dir_t *dir) {
44         REQUIRE(dir != NULL);
45
46         dir->entry.name[0] = '\0';
47         dir->entry.length = 0;
48
49         dir->handle = NULL;
50
51         dir->magic = ISC_DIR_MAGIC;
52 }
53
54 /*!
55  * \brief Allocate workspace and open directory stream. If either one fails,
56  * NULL will be returned.
57  */
58 isc_result_t
59 isc_dir_open(isc_dir_t *dir, const char *dirname) {
60         char *p;
61         isc_result_t result = ISC_R_SUCCESS;
62
63         REQUIRE(VALID_DIR(dir));
64         REQUIRE(dirname != NULL);
65
66         /*
67          * Copy directory name.  Need to have enough space for the name,
68          * a possible path separator, the wildcard, and the final NUL.
69          */
70         if (strlen(dirname) + 3 > sizeof(dir->dirname))
71                 /* XXXDCL ? */
72                 return (ISC_R_NOSPACE);
73         strcpy(dir->dirname, dirname);
74
75         /*
76          * Append path separator, if needed, and "*".
77          */
78         p = dir->dirname + strlen(dir->dirname);
79         if (dir->dirname < p && *(p - 1) != '/')
80                 *p++ = '/';
81         *p++ = '*';
82         *p++ = '\0';
83
84         /*
85          * Open stream.
86          */
87         dir->handle = opendir(dirname);
88
89         if (dir->handle == NULL)
90                 return isc__errno2result(errno);
91
92         return (result);
93 }
94
95 /*!
96  * \brief Return previously retrieved file or get next one.  
97
98  * Unix's dirent has
99  * separate open and read functions, but the Win32 and DOS interfaces open
100  * the dir stream and reads the first file in one operation.
101  */
102 isc_result_t
103 isc_dir_read(isc_dir_t *dir) {
104         struct dirent *entry;
105
106         REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
107
108         /*
109          * Fetch next file in directory.
110          */
111         entry = readdir(dir->handle);
112
113         if (entry == NULL)
114                 return (ISC_R_NOMORE);
115
116         /*
117          * Make sure that the space for the name is long enough.
118          */
119         if (sizeof(dir->entry.name) <= strlen(entry->d_name))
120             return (ISC_R_UNEXPECTED);
121
122         strcpy(dir->entry.name, entry->d_name);
123
124         /*
125          * Some dirents have d_namlen, but it is not portable.
126          */
127         dir->entry.length = strlen(entry->d_name);
128
129         return (ISC_R_SUCCESS);
130 }
131
132 /*!
133  * \brief Close directory stream.
134  */
135 void
136 isc_dir_close(isc_dir_t *dir) {
137        REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
138
139        (void)closedir(dir->handle);
140        dir->handle = NULL;
141 }
142
143 /*!
144  * \brief Reposition directory stream at start.
145  */
146 isc_result_t
147 isc_dir_reset(isc_dir_t *dir) {
148         REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
149
150         rewinddir(dir->handle);
151
152         return (ISC_R_SUCCESS);
153 }
154
155 isc_result_t
156 isc_dir_chdir(const char *dirname) {
157         /*!
158          * \brief Change the current directory to 'dirname'.
159          */
160
161         REQUIRE(dirname != NULL);
162
163         if (chdir(dirname) < 0)
164                 return (isc__errno2result(errno));
165
166         return (ISC_R_SUCCESS);
167 }
168
169 isc_result_t
170 isc_dir_chroot(const char *dirname) {
171
172         REQUIRE(dirname != NULL);
173
174         if (chroot(dirname) < 0)
175                 return (isc__errno2result(errno));
176
177         return (ISC_R_SUCCESS);
178 }
179
180 isc_result_t
181 isc_dir_createunique(char *templet) {
182         isc_result_t result;
183         char *x;
184         char *p;
185         int i;
186         int pid;
187
188         REQUIRE(templet != NULL);
189
190         /*!
191          * \brief mkdtemp is not portable, so this emulates it.
192          */
193
194         pid = getpid();
195
196         /*
197          * Replace trailing Xs with the process-id, zero-filled.
198          */
199         for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
200              x--, pid /= 10)
201                 *x = pid % 10 + '0';
202
203         x++;                    /* Set x to start of ex-Xs. */
204
205         do {
206                 i = mkdir(templet, 0700);
207                 if (i == 0 || errno != EEXIST)
208                         break;
209
210                 /*
211                  * The BSD algorithm.
212                  */
213                 p = x;
214                 while (*p != '\0') {
215                         if (isdigit(*p & 0xff))
216                                 *p = 'a';
217                         else if (*p != 'z')
218                                 ++*p;
219                         else {
220                                 /*
221                                  * Reset character and move to next.
222                                  */
223                                 *p++ = 'a';
224                                 continue;
225                         }
226
227                         break;
228                 }
229
230                 if (*p == '\0') {
231                         /*
232                          * Tried all combinations.  errno should already
233                          * be EEXIST, but ensure it is anyway for
234                          * isc__errno2result().
235                          */
236                         errno = EEXIST;
237                         break;
238                 }
239         } while (1);
240
241         if (i == -1)
242                 result = isc__errno2result(errno);
243         else
244                 result = ISC_R_SUCCESS;
245
246         return (result);
247 }