2 * Copyright (C) 2004, 2007-2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2001 Internet Software Consortium.
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.
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.
20 /* Principal Authors: DCL */
32 #include <isc/magic.h>
33 #include <isc/assertions.h>
36 #include "errno2result.h"
38 #define ISC_DIR_MAGIC ISC_MAGIC('D', 'I', 'R', '*')
39 #define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
42 start_directory(isc_dir_t *p);
45 isc_dir_init(isc_dir_t *dir) {
48 dir->dirname[0] = '\0';
50 dir->entry.name[0] = '\0';
51 dir->entry.length = 0;
52 memset(&(dir->entry.find_data), 0, sizeof(dir->entry.find_data));
54 dir->entry_filled = ISC_FALSE;
55 dir->search_handle = INVALID_HANDLE_VALUE;
57 dir->magic = ISC_DIR_MAGIC;
61 * Allocate workspace and open directory stream. If either one fails,
62 * NULL will be returned.
65 isc_dir_open(isc_dir_t *dir, const char *dirname) {
69 REQUIRE(dirname != NULL);
70 REQUIRE(VALID_DIR(dir) && dir->search_handle == INVALID_HANDLE_VALUE);
73 * Copy directory name. Need to have enough space for the name,
74 * a possible path separator, the wildcard, and the final NUL.
76 if (strlen(dirname) + 3 > sizeof(dir->dirname))
78 return (ISC_R_NOSPACE);
79 strcpy(dir->dirname, dirname);
82 * Append path separator, if needed, and "*".
84 p = dir->dirname + strlen(dir->dirname);
85 if (dir->dirname < p && *(p - 1) != '\\' && *(p - 1) != ':')
93 result = start_directory(dir);
99 * Return previously retrieved file or get next one. Unix's dirent has
100 * separate open and read functions, but the Win32 and DOS interfaces open
101 * the dir stream and reads the first file in one operation.
104 isc_dir_read(isc_dir_t *dir) {
105 REQUIRE(VALID_DIR(dir) && dir->search_handle != INVALID_HANDLE_VALUE);
107 if (dir->entry_filled)
109 * start_directory() already filled in the first entry.
111 dir->entry_filled = ISC_FALSE;
115 * Fetch next file in directory.
117 if (FindNextFile(dir->search_handle,
118 &dir->entry.find_data) == FALSE)
120 * Either the last file has been processed or
121 * an error has occurred. The former is not
122 * really an error, but the latter is.
124 if (GetLastError() == ERROR_NO_MORE_FILES)
125 return (ISC_R_NOMORE);
127 return (ISC_R_UNEXPECTED);
131 * Make sure that the space for the name is long enough.
133 strcpy(dir->entry.name, dir->entry.find_data.cFileName);
134 dir->entry.length = strlen(dir->entry.name);
136 return (ISC_R_SUCCESS);
140 * Close directory stream.
143 isc_dir_close(isc_dir_t *dir) {
144 REQUIRE(VALID_DIR(dir) && dir->search_handle != INVALID_HANDLE_VALUE);
146 FindClose(dir->search_handle);
147 dir->search_handle = INVALID_HANDLE_VALUE;
151 * Reposition directory stream at start.
154 isc_dir_reset(isc_dir_t *dir) {
157 REQUIRE(VALID_DIR(dir) && dir->search_handle != INVALID_HANDLE_VALUE);
158 REQUIRE(dir->dirname != NULL);
161 * NT cannot reposition the seek pointer to the beginning of the
162 * the directory stream, but rather the directory needs to be
163 * closed and reopened. The latter might fail.
168 result = start_directory(dir);
174 * Initialize isc_dir_t structure with new directory. The function
175 * returns 0 on failure and nonzero on success.
178 * - Be sure to close previous stream before opening new one
181 start_directory(isc_dir_t *dir)
183 REQUIRE(VALID_DIR(dir));
184 REQUIRE(dir->search_handle == INVALID_HANDLE_VALUE);
186 dir->entry_filled = ISC_FALSE;
189 * Open stream and retrieve first file.
191 dir->search_handle = FindFirstFile(dir->dirname,
192 &dir->entry.find_data);
194 if (dir->search_handle == INVALID_HANDLE_VALUE) {
196 * Something went wrong but we don't know what. GetLastError()
197 * could give us more information about the error, but the
198 * MSDN documentation is frustratingly thin about what
199 * possible errors could have resulted. (Score one for
200 * the Unix manual pages.) So there is just this lame error
201 * instead of being able to differentiate ISC_R_NOTFOUND
202 * from ISC_R_UNEXPECTED.
204 return (ISC_R_FAILURE);
208 * Make sure that the space for the name is long enough.
210 INSIST(sizeof(dir->entry.name) >
211 strlen(dir->entry.find_data.cFileName));
214 * Fill in the data for the first entry of the directory.
216 strcpy(dir->entry.name, dir->entry.find_data.cFileName);
217 dir->entry.length = strlen(dir->entry.name);
219 dir->entry_filled = ISC_TRUE;
221 return (ISC_R_SUCCESS);
225 isc_dir_chdir(const char *dirname) {
227 * Change the current directory to 'dirname'.
230 REQUIRE(dirname != NULL);
232 if (chdir(dirname) < 0)
233 return (isc__errno2result(errno));
235 return (ISC_R_SUCCESS);
239 isc_dir_chroot(const char *dirname) {
240 return (ISC_R_NOTIMPLEMENTED);
244 isc_dir_createunique(char *templet) {
251 REQUIRE(templet != NULL);
254 * mkdtemp is not portable, so this emulates it.
260 * Replace trailing Xs with the process-id, zero-filled.
262 for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
266 x++; /* Set x to start of ex-Xs. */
270 i = chmod(templet, 0700);
272 if (i == 0 || errno != EEXIST)
280 if (isdigit(*p & 0xff))
286 * Reset character and move to next.
297 * Tried all combinations. errno should already
298 * be EEXIST, but ensure it is anyway for
299 * isc__errno2result().
307 result = isc__errno2result(errno);
309 result = ISC_R_SUCCESS;