2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: file.c,v 1.38.2.1 2004/03/09 06:12:09 marka Exp $ */
25 #include <time.h> /* Required for utimes on some platforms. */
26 #include <unistd.h> /* Required for mkstemp on NetBSD. */
33 #include <isc/string.h>
37 #include "errno2result.h"
40 * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
41 * it might be good to provide a mechanism that allows for the results
42 * of a previous stat() to be used again without having to do another stat,
43 * such as perl's mechanism of using "_" in place of a file name to indicate
44 * that the results of the last stat should be used. But then you get into
45 * annoying MP issues. BTW, Win32 has stat().
48 file_stats(const char *file, struct stat *stats) {
49 isc_result_t result = ISC_R_SUCCESS;
51 REQUIRE(file != NULL);
52 REQUIRE(stats != NULL);
54 if (stat(file, stats) != 0)
55 result = isc__errno2result(errno);
61 isc_file_getmodtime(const char *file, isc_time_t *time) {
65 REQUIRE(file != NULL);
66 REQUIRE(time != NULL);
68 result = file_stats(file, &stats);
70 if (result == ISC_R_SUCCESS)
72 * XXXDCL some operating systems provide nanoseconds, too,
73 * such as BSD/OS via st_mtimespec.
75 isc_time_set(time, stats.st_mtime, 0);
81 isc_file_settime(const char *file, isc_time_t *time) {
82 struct timeval times[2];
84 REQUIRE(file != NULL && time != NULL);
87 * tv_sec is at least a 32 bit quantity on all platforms we're
88 * dealing with, but it is signed on most (all?) of them,
89 * so we need to make sure the high bit isn't set. This unfortunately
91 * * tv_sec becomes a signed 64 bit integer but long is 32 bits
92 * and isc_time_seconds > LONG_MAX, or
93 * * isc_time_seconds is changed to be > 32 bits but long is 32 bits
94 * and isc_time_seconds has at least 33 significant bits.
96 times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(time);
99 * Here is the real check for the high bit being set.
101 if ((times[0].tv_sec &
102 (1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
103 return (ISC_R_RANGE);
106 * isc_time_nanoseconds guarantees a value that divided by 1000 will
107 * fit into the minimum possible size tv_usec field. Unfortunately,
108 * we don't know what that type is so can't cast directly ... but
109 * we can at least cast to signed so the IRIX compiler shuts up.
111 times[0].tv_usec = times[1].tv_usec =
112 (isc_int32_t)(isc_time_nanoseconds(time) / 1000);
114 if (utimes(file, times) < 0)
115 return (isc__errno2result(errno));
117 return (ISC_R_SUCCESS);
122 #define TEMPLATE "tmp-XXXXXXXXXX" /* 14 characters. */
125 isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
126 return (isc_file_template(path, TEMPLATE, buf, buflen));
130 isc_file_template(const char *path, const char *templet, char *buf,
134 REQUIRE(path != NULL);
135 REQUIRE(templet != NULL);
136 REQUIRE(buf != NULL);
138 s = strrchr(templet, '/');
142 s = strrchr(path, '/');
145 if ((s - path + 1 + strlen(templet) + 1) > buflen)
146 return (ISC_R_NOSPACE);
148 strncpy(buf, path, s - path + 1);
149 buf[s - path + 1] = '\0';
150 strcat(buf, templet);
152 if ((strlen(templet) + 1) > buflen)
153 return (ISC_R_NOSPACE);
155 strcpy(buf, templet);
158 return (ISC_R_SUCCESS);
162 isc_file_renameunique(const char *file, char *templet) {
165 isc_result_t result = ISC_R_SUCCESS;
167 REQUIRE(file != NULL);
168 REQUIRE(templet != NULL);
170 fd = mkstemp(templet);
172 result = isc__errno2result(errno);
174 if (result == ISC_R_SUCCESS) {
175 res = rename(file, templet);
177 result = isc__errno2result(errno);
178 (void)unlink(templet);
187 isc_file_openunique(char *templet, FILE **fp) {
190 isc_result_t result = ISC_R_SUCCESS;
192 REQUIRE(templet != NULL);
193 REQUIRE(fp != NULL && *fp == NULL);
196 * Win32 does not have mkstemp.
198 fd = mkstemp(templet);
201 result = isc__errno2result(errno);
202 if (result == ISC_R_SUCCESS) {
203 f = fdopen(fd, "w+");
205 result = isc__errno2result(errno);
206 (void)remove(templet);
217 isc_file_remove(const char *filename) {
220 REQUIRE(filename != NULL);
222 r = unlink(filename);
224 return (ISC_R_SUCCESS);
226 return (isc__errno2result(errno));
230 isc_file_rename(const char *oldname, const char *newname) {
233 REQUIRE(oldname != NULL);
234 REQUIRE(newname != NULL);
236 r = rename(oldname, newname);
238 return (ISC_R_SUCCESS);
240 return (isc__errno2result(errno));
244 isc_file_exists(const char *pathname) {
247 REQUIRE(pathname != NULL);
249 return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
253 isc_file_isabsolute(const char *filename) {
254 REQUIRE(filename != NULL);
255 return (ISC_TF(filename[0] == '/'));
259 isc_file_iscurrentdir(const char *filename) {
260 REQUIRE(filename != NULL);
261 return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
265 isc_file_ischdiridempotent(const char *filename) {
266 REQUIRE(filename != NULL);
267 if (isc_file_isabsolute(filename))
269 if (isc_file_iscurrentdir(filename))
275 isc_file_basename(const char *filename) {
278 REQUIRE(filename != NULL);
280 s = strrchr(filename, '/');
288 isc_file_progname(const char *filename, char *buf, size_t buflen) {
292 REQUIRE(filename != NULL);
293 REQUIRE(buf != NULL);
295 base = isc_file_basename(filename);
296 len = strlen(base) + 1;
299 return (ISC_R_NOSPACE);
300 memcpy(buf, base, len);
302 return (ISC_R_SUCCESS);
306 isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
308 result = isc_dir_current(path, pathlen, ISC_TRUE);
309 if (result != ISC_R_SUCCESS)
311 if (strlen(path) + strlen(filename) + 1 > pathlen)
312 return (ISC_R_NOSPACE);
313 strcat(path, filename);
314 return (ISC_R_SUCCESS);