Blame contrib/dlz/modules/filesystem/dir.c

Packit Service ae04f2
/*
Packit Service ae04f2
 * Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
Packit Service ae04f2
 *
Packit Service ae04f2
 * Permission to use, copy, modify, and/or distribute this software for any
Packit Service ae04f2
 * purpose with or without fee is hereby granted, provided that the above
Packit Service ae04f2
 * copyright notice and this permission notice appear in all copies.
Packit Service ae04f2
 *
Packit Service ae04f2
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
Packit Service ae04f2
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
Packit Service ae04f2
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
Packit Service ae04f2
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
Packit Service ae04f2
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
Packit Service ae04f2
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
Packit Service ae04f2
 * PERFORMANCE OF THIS SOFTWARE.
Packit Service ae04f2
 */
Packit Service ae04f2
Packit Service ae04f2
#include <sys/types.h>
Packit Service ae04f2
#include <sys/stat.h>
Packit Service ae04f2
Packit Service ae04f2
#include <ctype.h>
Packit Service ae04f2
#include <errno.h>
Packit Service ae04f2
#include <unistd.h>
Packit Service ae04f2
#include <string.h>
Packit Service ae04f2
Packit Service ae04f2
#include "dlz_minimal.h"
Packit Service ae04f2
#include "dir.h"
Packit Service ae04f2
Packit Service ae04f2
void
Packit Service ae04f2
dir_init(dir_t *dir) {
Packit Service ae04f2
	dir->entry.name[0] = '\0';
Packit Service ae04f2
	dir->entry.length = 0;
Packit Service ae04f2
Packit Service ae04f2
	dir->handle = NULL;
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
dir_open(dir_t *dir, const char *dirname) {
Packit Service ae04f2
	char *p;
Packit Service ae04f2
	isc_result_t result = ISC_R_SUCCESS;
Packit Service ae04f2
Packit Service ae04f2
	if (strlen(dirname) + 3 > sizeof(dir->dirname))
Packit Service ae04f2
		return (ISC_R_NOSPACE);
Packit Service ae04f2
	strcpy(dir->dirname, dirname);
Packit Service ae04f2
Packit Service ae04f2
	p = dir->dirname + strlen(dir->dirname);
Packit Service ae04f2
	if (dir->dirname < p && *(p - 1) != '/')
Packit Service ae04f2
		*p++ = '/';
Packit Service ae04f2
	*p++ = '*';
Packit Service ae04f2
	*p = '\0';
Packit Service ae04f2
Packit Service ae04f2
	dir->handle = opendir(dirname);
Packit Service ae04f2
	if (dir->handle == NULL) {
Packit Service ae04f2
		switch (errno) {
Packit Service ae04f2
		case ENOTDIR:
Packit Service ae04f2
		case ELOOP:
Packit Service ae04f2
		case EINVAL:
Packit Service ae04f2
		case ENAMETOOLONG:
Packit Service ae04f2
		case EBADF:
Packit Service ae04f2
			result = ISC_R_INVALIDFILE;
Packit Service ae04f2
			break;
Packit Service ae04f2
		case ENOENT:
Packit Service ae04f2
			result = ISC_R_FILENOTFOUND;
Packit Service ae04f2
			break;
Packit Service ae04f2
		case EACCES:
Packit Service ae04f2
		case EPERM:
Packit Service ae04f2
			result = ISC_R_NOPERM;
Packit Service ae04f2
			break;
Packit Service ae04f2
		case ENOMEM:
Packit Service ae04f2
			result = ISC_R_NOMEMORY;
Packit Service ae04f2
			break;
Packit Service ae04f2
		default:
Packit Service ae04f2
			result = ISC_R_UNEXPECTED;
Packit Service ae04f2
			break;
Packit Service ae04f2
		}
Packit Service ae04f2
	}
Packit Service ae04f2
Packit Service ae04f2
	return (result);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
/*!
Packit Service ae04f2
 * \brief Return previously retrieved file or get next one.
Packit Service ae04f2
Packit Service ae04f2
 * Unix's dirent has
Packit Service ae04f2
 * separate open and read functions, but the Win32 and DOS interfaces open
Packit Service ae04f2
 * the dir stream and reads the first file in one operation.
Packit Service ae04f2
 */
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
dir_read(dir_t *dir) {
Packit Service ae04f2
	struct dirent *entry;
Packit Service ae04f2
Packit Service ae04f2
	entry = readdir(dir->handle);
Packit Service ae04f2
	if (entry == NULL)
Packit Service ae04f2
		return (ISC_R_NOMORE);
Packit Service ae04f2
Packit Service ae04f2
	if (sizeof(dir->entry.name) <= strlen(entry->d_name))
Packit Service ae04f2
	    return (ISC_R_UNEXPECTED);
Packit Service ae04f2
Packit Service ae04f2
	strcpy(dir->entry.name, entry->d_name);
Packit Service ae04f2
Packit Service ae04f2
	dir->entry.length = strlen(entry->d_name);
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
/*!
Packit Service ae04f2
 * \brief Close directory stream.
Packit Service ae04f2
 */
Packit Service ae04f2
void
Packit Service ae04f2
dir_close(dir_t *dir) {
Packit Service ae04f2
       (void)closedir(dir->handle);
Packit Service ae04f2
       dir->handle = NULL;
Packit Service ae04f2
}
Packit Service ae04f2
Packit Service ae04f2
/*!
Packit Service ae04f2
 * \brief Reposition directory stream at start.
Packit Service ae04f2
 */
Packit Service ae04f2
isc_result_t
Packit Service ae04f2
dir_reset(dir_t *dir) {
Packit Service ae04f2
	rewinddir(dir->handle);
Packit Service ae04f2
Packit Service ae04f2
	return (ISC_R_SUCCESS);
Packit Service ae04f2
}