dir.c revision 04260c5c48d234734863f0222e207b6564cd41a8
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson/*
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * Copyright (C) 1999-2001 Internet Software Consortium.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson *
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * Permission to use, copy, modify, and distribute this software for any
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * copyright notice and this permission notice appear in all copies.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson *
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson/* $Id: dir.c,v 1.18 2001/06/08 23:50:31 tale Exp $ */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson/* Principal Authors: DCL */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <config.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <sys/types.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <sys/stat.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <ctype.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <errno.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <unistd.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <isc/dir.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <isc/magic.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <isc/string.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include <isc/util.h>
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#include "errno2result.h"
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#define ISC_DIR_MAGIC ISC_MAGIC('D', 'I', 'R', '*')
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson#define VALID_DIR(dir) ISC_MAGIC_VALID(dir, ISC_DIR_MAGIC)
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonvoid
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonisc_dir_init(isc_dir_t *dir) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson REQUIRE(dir != NULL);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson dir->entry.name[0] = '\0';
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson dir->entry.length = 0;
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson dir->handle = NULL;
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson dir->magic = ISC_DIR_MAGIC;
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson/*
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * Allocate workspace and open directory stream. If either one fails,
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * NULL will be returned.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonisc_result_t
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonisc_dir_open(isc_dir_t *dir, const char *dirname) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson isc_result_t result = ISC_R_SUCCESS;
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson REQUIRE(VALID_DIR(dir));
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson REQUIRE(dirname != NULL);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson /*
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * Open stream.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson dir->handle = opendir(dirname);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson if (dir->handle == NULL)
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return isc__errno2result(errno);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return (result);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson/*
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * Return previously retrieved file or get next one. Unix's dirent has
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * separate open and read functions, but the Win32 and DOS interfaces open
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * the dir stream and reads the first file in one operation.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonisc_result_t
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafssonisc_dir_read(isc_dir_t *dir) {
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson struct dirent *entry;
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson /*
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * Fetch next file in directory.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson entry = readdir(dir->handle);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson if (entry == NULL)
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return (ISC_R_NOMORE);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson /*
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * Make sure that the space for the name is long enough.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson if (sizeof(dir->entry.name) <= strlen(entry->d_name))
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return (ISC_R_UNEXPECTED);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson strcpy(dir->entry.name, entry->d_name);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson /*
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson * Some dirents have d_namlen, but it is not portable.
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson */
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson dir->entry.length = strlen(entry->d_name);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson return (ISC_R_SUCCESS);
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson}
e7a4f58d55042cbc981a70b5071aaea46b9ebf7fAndreas Gustafsson
/*
* Close directory stream.
*/
void
isc_dir_close(isc_dir_t *dir) {
REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
(void)closedir(dir->handle);
dir->handle = NULL;
}
/*
* Reposition directory stream at start.
*/
isc_result_t
isc_dir_reset(isc_dir_t *dir) {
REQUIRE(VALID_DIR(dir) && dir->handle != NULL);
rewinddir(dir->handle);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_dir_chdir(const char *dirname) {
/*
* Change the current directory to 'dirname'.
*/
REQUIRE(dirname != NULL);
if (chdir(dirname) < 0)
return (isc__errno2result(errno));
return (ISC_R_SUCCESS);
}
isc_result_t
isc_dir_chroot(const char *dirname) {
REQUIRE(dirname != NULL);
if (chroot(dirname) < 0)
return (isc__errno2result(errno));
return (ISC_R_SUCCESS);
}
isc_result_t
isc_dir_current(char *dirname, size_t length, isc_boolean_t end_sep) {
char *cwd;
isc_result_t result = ISC_R_SUCCESS;
/*
* XXXDCL Could automatically allocate memory if dirname == NULL.
*/
REQUIRE(dirname != NULL);
REQUIRE(length > 0);
cwd = getcwd(dirname, length);
if (cwd == NULL) {
if (errno == ERANGE)
result = ISC_R_NOSPACE;
else
result = isc__errno2result(errno);
} else if (end_sep) {
if (strlen(dirname) + 1 == length)
result = ISC_R_NOSPACE;
else if (dirname[1] != '\0')
strcat(dirname, "/");
}
return (result);
}
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, 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);
}