dir.c 6.92 KB
Newer Older
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2004, 2007-2009  Internet Systems Consortium, Inc. ("ISC")
Brian Wellington's avatar
Brian Wellington committed
3
 * Copyright (C) 1999-2001  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
6 7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
16 17
 */

Automatic Updater's avatar
Automatic Updater committed
18
/* $Id: dir.c,v 1.18 2009/01/18 23:48:14 tbox Exp $ */
19 20 21

/* Principal Authors: DCL */

Danny Mayer's avatar
Danny Mayer committed
22 23
#include <config.h>

24
#include <string.h>
Danny Mayer's avatar
Danny Mayer committed
25 26 27 28 29
#include <direct.h>
#include <process.h>
#include <io.h>

#include <sys/stat.h>
30 31

#include <isc/dir.h>
Danny Mayer's avatar
Danny Mayer committed
32
#include <isc/magic.h>
33
#include <isc/assertions.h>
Danny Mayer's avatar
Danny Mayer committed
34 35 36
#include <isc/util.h>

#include "errno2result.h"
37

38 39
#define ISC_DIR_MAGIC		ISC_MAGIC('D', 'I', 'R', '*')
#define VALID_DIR(dir)		ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

static isc_result_t
start_directory(isc_dir_t *p);

void
isc_dir_init(isc_dir_t *dir) {
	REQUIRE(dir != NULL);

	dir->dirname[0] = '\0';

	dir->entry.name[0] = '\0';
	dir->entry.length = 0;
	memset(&(dir->entry.find_data), 0, sizeof(dir->entry.find_data));

	dir->entry_filled = ISC_FALSE;
	dir->search_handle = INVALID_HANDLE_VALUE;

	dir->magic = ISC_DIR_MAGIC;
}

/*
61
 * Allocate workspace and open directory stream. If either one fails,
62 63 64
 * NULL will be returned.
 */
isc_result_t
65
isc_dir_open(isc_dir_t *dir, const char *dirname) {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	char *p;
	isc_result_t result;

	REQUIRE(dirname != NULL);
	REQUIRE(VALID_DIR(dir) && dir->search_handle == INVALID_HANDLE_VALUE);

	/*
	 * Copy directory name.  Need to have enough space for the name,
	 * a possible path separator, the wildcard, and the final NUL.
	 */
	if (strlen(dirname) + 3 > sizeof(dir->dirname))
		/* XXXDCL ? */
		return (ISC_R_NOSPACE);
	strcpy(dir->dirname, dirname);

	/*
	 * Append path separator, if needed, and "*".
	 */
84 85 86 87 88
	p = dir->dirname + strlen(dir->dirname);
	if (dir->dirname < p && *(p - 1) != '\\' && *(p - 1) != ':')
		*p++ = '\\';
	*p++ = '*';
	*p++ = '\0';
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

	/*
	 * Open stream.
	 */
	result = start_directory(dir);

	return (result);
}

/*
 * Return previously retrieved file or get next one.  Unix's dirent has
 * separate open and read functions, but the Win32 and DOS interfaces open
 * the dir stream and reads the first file in one operation.
 */
isc_result_t
isc_dir_read(isc_dir_t *dir) {
	REQUIRE(VALID_DIR(dir) && dir->search_handle != INVALID_HANDLE_VALUE);

	if (dir->entry_filled)
		/*
		 * start_directory() already filled in the first entry.
		 */
		dir->entry_filled = ISC_FALSE;

	else {
		/*
		 * Fetch next file in directory.
		 */
		if (FindNextFile(dir->search_handle,
				 &dir->entry.find_data) == FALSE)
			/*
			 * Either the last file has been processed or
Francis Dupont's avatar
Francis Dupont committed
121
			 * an error has occurred.  The former is not
122 123 124 125 126 127 128 129 130
			 * really an error, but the latter is.
			 */
			if (GetLastError() == ERROR_NO_MORE_FILES)
				return (ISC_R_NOMORE);
			else
				return (ISC_R_UNEXPECTED);
	}

	/*
131
	 * Make sure that the space for the name is long enough.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
	 */
	strcpy(dir->entry.name, dir->entry.find_data.cFileName);
	dir->entry.length = strlen(dir->entry.name);

	return (ISC_R_SUCCESS);
}

/*
 * Close directory stream.
 */
void
isc_dir_close(isc_dir_t *dir) {
       REQUIRE(VALID_DIR(dir) && dir->search_handle != INVALID_HANDLE_VALUE);

       FindClose(dir->search_handle);
       dir->search_handle = INVALID_HANDLE_VALUE;
}

/*
 * Reposition directory stream at start.
 */
isc_result_t
isc_dir_reset(isc_dir_t *dir) {
	isc_result_t result;

	REQUIRE(VALID_DIR(dir) && dir->search_handle != INVALID_HANDLE_VALUE);
	REQUIRE(dir->dirname != NULL);

	/*
	 * NT cannot reposition the seek pointer to the beginning of the
	 * the directory stream, but rather the directory needs to be
	 * closed and reopened.  The latter might fail.
	 */

	isc_dir_close(dir);

	result = start_directory(dir);

	return (result);
}

/*
 * Initialize isc_dir_t structure with new directory. The function
 * returns 0 on failure and nonzero on success.
 *
 * Note:
 * - Be sure to close previous stream before opening new one
 */
static isc_result_t
start_directory(isc_dir_t *dir)
182
{
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	REQUIRE(VALID_DIR(dir));
	REQUIRE(dir->search_handle == INVALID_HANDLE_VALUE);

	dir->entry_filled = ISC_FALSE;

	/*
	 * Open stream and retrieve first file.
	 */
	dir->search_handle = FindFirstFile(dir->dirname,
					    &dir->entry.find_data);

	if (dir->search_handle == INVALID_HANDLE_VALUE) {
		/*
		 * Something went wrong but we don't know what. GetLastError()
		 * could give us more information about the error, but the
		 * MSDN documentation is frustratingly thin about what
		 * possible errors could have resulted.  (Score one for
		 * the Unix manual pages.)  So there is just this lame error
		 * instead of being able to differentiate ISC_R_NOTFOUND
		 * from ISC_R_UNEXPECTED.
		 */
		return (ISC_R_FAILURE);
	}

	/*
208
	 * Make sure that the space for the name is long enough.
209 210 211 212 213 214 215 216 217 218 219 220 221 222
	 */
	INSIST(sizeof(dir->entry.name) >
	       strlen(dir->entry.find_data.cFileName));

	/*
	 * Fill in the data for the first entry of the directory.
	 */
	strcpy(dir->entry.name, dir->entry.find_data.cFileName);
	dir->entry.length = strlen(dir->entry.name);

	dir->entry_filled = ISC_TRUE;

	return (ISC_R_SUCCESS);
}
223 224 225 226 227 228 229 230 231

isc_result_t
isc_dir_chdir(const char *dirname) {
	/*
	 * Change the current directory to 'dirname'.
	 */

	REQUIRE(dirname != NULL);

Danny Mayer's avatar
Danny Mayer committed
232 233 234 235 236 237 238 239
	if (chdir(dirname) < 0)
		return (isc__errno2result(errno));

	return (ISC_R_SUCCESS);
}

isc_result_t
isc_dir_chroot(const char *dirname) {
Francis Dupont's avatar
Francis Dupont committed
240
	return (ISC_R_NOTIMPLEMENTED);
Danny Mayer's avatar
Danny Mayer committed
241
}
Andreas Gustafsson's avatar
spacing  
Andreas Gustafsson committed
242

Danny Mayer's avatar
Danny Mayer committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
isc_result_t
isc_dir_createunique(char *templet) {
	isc_result_t result;
	char *x;
	char *p;
	int i;
	int pid;

	REQUIRE(templet != NULL);

	/*
	 * mkdtemp is not portable, so this emulates it.
	 */

	pid = getpid();

	/*
	 * Replace trailing Xs with the process-id, zero-filled.
	 */
	for (x = templet + strlen(templet) - 1; *x == 'X' && x >= templet;
	     x--, pid /= 10)
		*x = pid % 10 + '0';

	x++;			/* Set x to start of ex-Xs. */

	do {
		i = mkdir(templet);
		i = chmod(templet, 0700);

		if (i == 0 || errno != EEXIST)
			break;

		/*
		 * The BSD algorithm.
		 */
		p = x;
		while (*p != '\0') {
			if (isdigit(*p & 0xff))
				*p = 'a';
			else if (*p != 'z')
				++*p;
			else {
				/*
				 * Reset character and move to next.
				 */
				*p++ = 'a';
				continue;
			}

			break;
		}

		if (*p == '\0') {
			/*
			 * Tried all combinations.  errno should already
			 * be EEXIST, but ensure it is anyway for
			 * isc__errno2result().
			 */
			errno = EEXIST;
			break;
		}
	} while (1);

	if (i == -1)
		result = isc__errno2result(errno);
	else
		result = ISC_R_SUCCESS;

	return (result);
312
}