file.c 3.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2000  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM 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.
 */

David Lawrence's avatar
David Lawrence committed
18 19
#include <config.h>

20
#include <errno.h>
21
#include <stdlib.h>
22
#include <unistd.h>            /* Required for mkstemp on NetBSD. */
23 24 25 26

#include <sys/stat.h>

#include <isc/file.h>
27
#include <isc/string.h>
David Lawrence's avatar
David Lawrence committed
28
#include <isc/time.h>
Bob Halley's avatar
Bob Halley committed
29
#include <isc/util.h>
30

31
#include "errno2result.h"
32

33 34 35
/*
 * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
 * it might be good to provide a mechanism that allows for the results
36 37
 * of a previous stat() to be used again without having to do another stat,
 * such as perl's mechanism of using "_" in place of a file name to indicate
38 39 40 41 42 43
 * that the results of the last stat should be used.  But then you get into
 * annoying MP issues.   BTW, Win32 has stat().
 */
static isc_result_t
file_stats(const char *file, struct stat *stats) {
	isc_result_t result = ISC_R_SUCCESS;
44 45
	
	if (stat(file, stats) != 0)
46
		result = isc__errno2result(errno);
47
		
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	return (result);
}

isc_result_t
isc_file_getmodtime(const char *file, isc_time_t *time) {
	isc_result_t result;
	struct stat stats;

	REQUIRE(file != NULL && time != NULL);

	result = file_stats(file, &stats);

	if (result == ISC_R_SUCCESS)
		/*
		 * XXXDCL some operating systems provide nanoseconds, too,
		 * such as BSD/OS via st_mtimespec.
		 */
		isc_time_set(time, stats.st_mtime, 0);

	return (result);
}

#undef TEMPLATE
#define TEMPLATE "tmp-XXXXXXXXXX" /* 14 characters. */

isc_result_t
isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
	char *s;

	REQUIRE(buf != NULL);

	s = strrchr(path, '/');

	if (s != NULL) {
		if ((s - path + 1 + sizeof(TEMPLATE)) > buflen)
			return (ISC_R_NOSPACE);
84
		
85 86 87 88 89 90
		strncpy(buf, path, s - path + 1);
		buf[s - path + 1] = '\0';
		strcat(buf, TEMPLATE);
	} else {
		if (sizeof(TEMPLATE) > buflen)
			return (ISC_R_NOSPACE);
91
		
92 93
		strcpy(buf, TEMPLATE);
	}
94
	
95 96
	return (ISC_R_SUCCESS);
}
97
 
98
isc_result_t
99
isc_file_openunique(char *templet, FILE **fp) {
100 101 102 103
	int fd;
	FILE *f;
	isc_result_t result = ISC_R_SUCCESS;

104
	REQUIRE(templet != NULL);
105 106 107 108 109
	REQUIRE(fp != NULL && *fp == NULL);

	/*
	 * Win32 does not have mkstemp.
	 */
110
	fd = mkstemp(templet);
111 112

	if (fd == -1)
113
		result = isc__errno2result(errno);
114 115 116
	if (result == ISC_R_SUCCESS) {
		f = fdopen(fd, "w+");
		if (f == NULL) {
117
			result = isc__errno2result(errno);
118
			(void)remove(templet);
119 120 121 122 123 124 125 126
			(void)close(fd);

		} else
			*fp = f;
	}

	return (result);
}
127 128 129 130 131 132 133 134 135

isc_result_t
isc_file_remove(const char *filename) {
	int r;
	
	r = unlink(filename);
	if (r == 0)
		return (ISC_R_SUCCESS);
	else
136
		return (isc__errno2result(errno));
137
}