file.c 5.57 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
 */

Brian Wellington's avatar
Brian Wellington committed
18
/* $Id: file.c,v 1.25 2000/10/19 01:10:24 bwelling 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
#undef TEMPLATE
#define TEMPLATE "tmp-XXXXXXXXXX" /* 14 characters. */

isc_result_t
isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
120
121
122
123
124
125
	return (isc_file_template(path, TEMPLATE, buf, buflen));
}

isc_result_t
isc_file_template(const char *path, const char *templat, char *buf,
			size_t buflen) {
126
127
128
129
	char *s;

	REQUIRE(buf != NULL);

130
131
132
133
	s = strrchr(templat, '/');
	if (s != NULL)
		templat = s + 1;

134
135
136
	s = strrchr(path, '/');

	if (s != NULL) {
137
		if ((s - path + 1 + strlen(templat) + 1) > buflen)
138
			return (ISC_R_NOSPACE);
139

140
141
		strncpy(buf, path, s - path + 1);
		buf[s - path + 1] = '\0';
142
		strcat(buf, templat);
143
	} else {
144
		if ((strlen(templat) + 1) > buflen)
145
			return (ISC_R_NOSPACE);
146

147
		strcpy(buf, templat);
148
	}
149

150
151
	return (ISC_R_SUCCESS);
}
152

153
154
155
156
157
158
159
160
161
162
163
164
isc_result_t
isc_file_renameunique(const char *file, char *templet) {
	int fd = -1;
	int res = 0;
	isc_result_t result = ISC_R_SUCCESS;

	fd = mkstemp(templet);
	if (fd == -1) {
		result = isc__errno2result(errno);
	}
	if (result == ISC_R_SUCCESS) {
		res = rename(file, templet);
165
166
		if (res != 0) {
			(void)unlink(templet);
167
			result = isc__errno2result(errno);
168
		}
169
170
171
172
173
174
	}
	if (fd != -1)
		close(fd);
	return (result);
}

175
isc_result_t
176
isc_file_openunique(char *templet, FILE **fp) {
177
178
179
180
	int fd;
	FILE *f;
	isc_result_t result = ISC_R_SUCCESS;

181
	REQUIRE(templet != NULL);
182
183
184
185
186
	REQUIRE(fp != NULL && *fp == NULL);

	/*
	 * Win32 does not have mkstemp.
	 */
187
	fd = mkstemp(templet);
188
189

	if (fd == -1)
190
		result = isc__errno2result(errno);
191
192
193
	if (result == ISC_R_SUCCESS) {
		f = fdopen(fd, "w+");
		if (f == NULL) {
194
			result = isc__errno2result(errno);
195
			(void)remove(templet);
196
197
198
199
200
201
202
203
			(void)close(fd);

		} else
			*fp = f;
	}

	return (result);
}
204
205
206
207

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

209
210
211
212
	r = unlink(filename);
	if (r == 0)
		return (ISC_R_SUCCESS);
	else
213
		return (isc__errno2result(errno));
214
}
215

216
217
218
219
220
221
222
223
224
225
226
isc_result_t
isc_file_rename(const char *oldname, const char *newname) {
	int r;

	r = rename(oldname, newname);
	if (r == 0)
		return (ISC_R_SUCCESS);
	else
		return (isc__errno2result(errno));
}

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