2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 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.
19 * Portions Copyright (c) 1987, 1993
20 * The Regents of the University of California. All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * This product includes software developed by the University of
33 * California, Berkeley and its contributors.
34 * 4. Neither the name of the University nor the names of its contributors
35 * may be used to endorse or promote products derived from this software
36 * without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
39 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
42 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
43 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
44 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
46 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
47 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 /* $Id: file.c,v 1.47.18.2 2005/04/29 00:17:07 marka Exp $ */
61 #include <time.h> /* Required for utimes on some platforms. */
62 #include <unistd.h> /* Required for mkstemp on NetBSD. */
70 #include <isc/random.h>
71 #include <isc/string.h>
75 #include "errno2result.h"
78 * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
79 * it might be good to provide a mechanism that allows for the results
80 * of a previous stat() to be used again without having to do another stat,
81 * such as perl's mechanism of using "_" in place of a file name to indicate
82 * that the results of the last stat should be used. But then you get into
83 * annoying MP issues. BTW, Win32 has stat().
86 file_stats(const char *file, struct stat *stats) {
87 isc_result_t result = ISC_R_SUCCESS;
89 REQUIRE(file != NULL);
90 REQUIRE(stats != NULL);
92 if (stat(file, stats) != 0)
93 result = isc__errno2result(errno);
99 isc_file_getmodtime(const char *file, isc_time_t *time) {
103 REQUIRE(file != NULL);
104 REQUIRE(time != NULL);
106 result = file_stats(file, &stats);
108 if (result == ISC_R_SUCCESS)
110 * XXXDCL some operating systems provide nanoseconds, too,
111 * such as BSD/OS via st_mtimespec.
113 isc_time_set(time, stats.st_mtime, 0);
119 isc_file_settime(const char *file, isc_time_t *time) {
120 struct timeval times[2];
122 REQUIRE(file != NULL && time != NULL);
125 * tv_sec is at least a 32 bit quantity on all platforms we're
126 * dealing with, but it is signed on most (all?) of them,
127 * so we need to make sure the high bit isn't set. This unfortunately
129 * * tv_sec becomes a signed 64 bit integer but long is 32 bits
130 * and isc_time_seconds > LONG_MAX, or
131 * * isc_time_seconds is changed to be > 32 bits but long is 32 bits
132 * and isc_time_seconds has at least 33 significant bits.
134 times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(time);
137 * Here is the real check for the high bit being set.
139 if ((times[0].tv_sec &
140 (1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
141 return (ISC_R_RANGE);
144 * isc_time_nanoseconds guarantees a value that divided by 1000 will
145 * fit into the minimum possible size tv_usec field. Unfortunately,
146 * we don't know what that type is so can't cast directly ... but
147 * we can at least cast to signed so the IRIX compiler shuts up.
149 times[0].tv_usec = times[1].tv_usec =
150 (isc_int32_t)(isc_time_nanoseconds(time) / 1000);
152 if (utimes(file, times) < 0)
153 return (isc__errno2result(errno));
155 return (ISC_R_SUCCESS);
159 #define TEMPLATE "tmp-XXXXXXXXXX" /*%< 14 characters. */
162 isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
163 return (isc_file_template(path, TEMPLATE, buf, buflen));
167 isc_file_template(const char *path, const char *templet, char *buf,
171 REQUIRE(path != NULL);
172 REQUIRE(templet != NULL);
173 REQUIRE(buf != NULL);
175 s = strrchr(templet, '/');
179 s = strrchr(path, '/');
182 if ((s - path + 1 + strlen(templet) + 1) > buflen)
183 return (ISC_R_NOSPACE);
185 strncpy(buf, path, s - path + 1);
186 buf[s - path + 1] = '\0';
187 strcat(buf, templet);
189 if ((strlen(templet) + 1) > buflen)
190 return (ISC_R_NOSPACE);
192 strcpy(buf, templet);
195 return (ISC_R_SUCCESS);
198 static char alphnum[] =
199 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
202 isc_file_renameunique(const char *file, char *templet) {
207 REQUIRE(file != NULL);
208 REQUIRE(templet != NULL);
214 return (ISC_R_FAILURE);
217 while (cp >= templet && *cp == 'X') {
218 isc_random_get(&which);
219 *cp = alphnum[which % (sizeof(alphnum) - 1)];
222 while (link(file, templet) == -1) {
224 return (isc__errno2result(errno));
228 return (ISC_R_FAILURE);
229 t = strchr(alphnum, *cp);
230 if (t == NULL || *++t == '\0')
239 return (ISC_R_SUCCESS);
244 isc_file_openunique(char *templet, FILE **fp) {
247 isc_result_t result = ISC_R_SUCCESS;
253 REQUIRE(templet != NULL);
254 REQUIRE(fp != NULL && *fp == NULL);
260 return (ISC_R_FAILURE);
263 while (cp >= templet && *cp == 'X') {
264 isc_random_get(&which);
265 *cp = alphnum[which % (sizeof(alphnum) - 1)];
269 mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
271 while ((fd = open(templet, O_RDWR|O_CREAT|O_EXCL, mode)) == -1) {
273 return (isc__errno2result(errno));
277 return (ISC_R_FAILURE);
278 t = strchr(alphnum, *cp);
279 if (t == NULL || *++t == '\0')
287 f = fdopen(fd, "w+");
289 result = isc__errno2result(errno);
290 (void)remove(templet);
299 isc_file_remove(const char *filename) {
302 REQUIRE(filename != NULL);
304 r = unlink(filename);
306 return (ISC_R_SUCCESS);
308 return (isc__errno2result(errno));
312 isc_file_rename(const char *oldname, const char *newname) {
315 REQUIRE(oldname != NULL);
316 REQUIRE(newname != NULL);
318 r = rename(oldname, newname);
320 return (ISC_R_SUCCESS);
322 return (isc__errno2result(errno));
326 isc_file_exists(const char *pathname) {
329 REQUIRE(pathname != NULL);
331 return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
335 isc_file_isabsolute(const char *filename) {
336 REQUIRE(filename != NULL);
337 return (ISC_TF(filename[0] == '/'));
341 isc_file_iscurrentdir(const char *filename) {
342 REQUIRE(filename != NULL);
343 return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
347 isc_file_ischdiridempotent(const char *filename) {
348 REQUIRE(filename != NULL);
349 if (isc_file_isabsolute(filename))
351 if (isc_file_iscurrentdir(filename))
357 isc_file_basename(const char *filename) {
360 REQUIRE(filename != NULL);
362 s = strrchr(filename, '/');
370 isc_file_progname(const char *filename, char *buf, size_t buflen) {
374 REQUIRE(filename != NULL);
375 REQUIRE(buf != NULL);
377 base = isc_file_basename(filename);
378 len = strlen(base) + 1;
381 return (ISC_R_NOSPACE);
382 memcpy(buf, base, len);
384 return (ISC_R_SUCCESS);
388 * Put the absolute name of the current directory into 'dirname', which is
389 * a buffer of at least 'length' characters. End the string with the
390 * appropriate path separator, such that the final product could be
391 * concatenated with a relative pathname to make a valid pathname string.
394 dir_current(char *dirname, size_t length) {
396 isc_result_t result = ISC_R_SUCCESS;
398 REQUIRE(dirname != NULL);
399 REQUIRE(length > 0U);
401 cwd = getcwd(dirname, length);
405 result = ISC_R_NOSPACE;
407 result = isc__errno2result(errno);
409 if (strlen(dirname) + 1 == length)
410 result = ISC_R_NOSPACE;
411 else if (dirname[1] != '\0')
412 strcat(dirname, "/");
419 isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
421 result = dir_current(path, pathlen);
422 if (result != ISC_R_SUCCESS)
424 if (strlen(path) + strlen(filename) + 1 > pathlen)
425 return (ISC_R_NOSPACE);
426 strcat(path, filename);
427 return (ISC_R_SUCCESS);
431 isc_file_truncate(const char *filename, isc_offset_t size) {
432 isc_result_t result = ISC_R_SUCCESS;
434 if (truncate(filename, size) < 0)
435 result = isc__errno2result(errno);