file.c 4.74 KB
Newer Older
1
2
/*
 * Copyright (C) 2000  Internet Software Consortium.
3
 *
4
5
6
 * 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.
7
 *
8
9
10
11
12
13
14
15
 * 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.
16
17
 */

18
/* $Id: file.c,v 1.21 2000/09/08 18:37:26 gson Exp $ */
David Lawrence's avatar
David Lawrence committed
19

David Lawrence's avatar
David Lawrence committed
20
21
#include <config.h>

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

#include <sys/stat.h>
28
#include <sys/time.h>
29
30

#include <isc/file.h>
31
#include <isc/string.h>
David Lawrence's avatar
David Lawrence committed
32
#include <isc/time.h>
Bob Halley's avatar
Bob Halley committed
33
#include <isc/util.h>
34

35
#include "errno2result.h"
36

37
38
39
/*
 * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
 * it might be good to provide a mechanism that allows for the results
40
41
 * 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
42
43
44
45
46
47
 * 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;
48

49
	if (stat(file, stats) != 0)
50
		result = isc__errno2result(errno);
51

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	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);
}

74
75
76
77
78
79
isc_result_t
isc_file_settime(const char *file, isc_time_t *time) {
	struct timeval times[2];

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

80
81
82
	/*
	 * tv_sec is at least a 32 bit quantity on all platforms we're
	 * dealing with, but it is signed on most (all?) of them,
83
84
85
86
87
88
	 * so we need to make sure the high bit isn't set.  This unfortunately
	 * loses when either:
	 *   * tv_sec becomes a signed 64 bit integer but long is 32 bits
	 *	and isc_time_seconds > LONG_MAX, or
	 *   * isc_time_seconds is changed to be > 32 bits but long is 32 bits
	 *      and isc_time_seconds has at least 33 significant bits.
89
	 */
90
	times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(time);
91

92
	/*
93
	 * Here is the real check for the high bit being set.
94
95
	 */
	if ((times[0].tv_sec &
96
	     (1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
97
98
99
100
101
102
103
104
105
106
		return (ISC_R_RANGE);

	/*
	 * isc_time_nanoseconds guarantees a value that divided by 1000 will
	 * fit into the minimum possible size tv_usec field.  Unfortunately,
	 * we don't know what that type is so can't cast directly ... but
	 * we can at least cast to signed so the IRIX compiler shuts up.
	 */
	times[0].tv_usec = times[1].tv_usec =
		(isc_int32_t)(isc_time_nanoseconds(time) / 1000);
107
108
109

	if (utimes(file, times) < 0)
		return (isc__errno2result(errno));
110

111
112
113
114
	return (ISC_R_SUCCESS);

}

115
116
117
118
119
120
121
122
123
124
125
126
127
128
#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);
129

130
131
132
133
134
135
		strncpy(buf, path, s - path + 1);
		buf[s - path + 1] = '\0';
		strcat(buf, TEMPLATE);
	} else {
		if (sizeof(TEMPLATE) > buflen)
			return (ISC_R_NOSPACE);
136

137
138
		strcpy(buf, TEMPLATE);
	}
139

140
141
	return (ISC_R_SUCCESS);
}
142

143
isc_result_t
144
isc_file_openunique(char *templet, FILE **fp) {
145
146
147
148
	int fd;
	FILE *f;
	isc_result_t result = ISC_R_SUCCESS;

149
	REQUIRE(templet != NULL);
150
151
152
153
154
	REQUIRE(fp != NULL && *fp == NULL);

	/*
	 * Win32 does not have mkstemp.
	 */
155
	fd = mkstemp(templet);
156
157

	if (fd == -1)
158
		result = isc__errno2result(errno);
159
160
161
	if (result == ISC_R_SUCCESS) {
		f = fdopen(fd, "w+");
		if (f == NULL) {
162
			result = isc__errno2result(errno);
163
			(void)remove(templet);
164
165
166
167
168
169
170
171
			(void)close(fd);

		} else
			*fp = f;
	}

	return (result);
}
172
173
174
175

isc_result_t
isc_file_remove(const char *filename) {
	int r;
176

177
178
179
180
	r = unlink(filename);
	if (r == 0)
		return (ISC_R_SUCCESS);
	else
181
		return (isc__errno2result(errno));
182
}
183
184
185
186
187

isc_boolean_t
isc_file_isabsolute(const char *filename) {
	return (ISC_TF(filename[0] == '/'));
}