2 * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or 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
61 #include <time.h> /* Required for utimes on some platforms. */
62 #include <unistd.h> /* Required for mkstemp on NetBSD. */
72 #include <isc/random.h>
73 #include <isc/string.h>
77 #include "errno2result.h"
78 #include "ntp_stdlib.h" /* NTP change for strlcpy, strlcat */
81 * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
82 * it might be good to provide a mechanism that allows for the results
83 * of a previous stat() to be used again without having to do another stat,
84 * such as perl's mechanism of using "_" in place of a file name to indicate
85 * that the results of the last stat should be used. But then you get into
86 * annoying MP issues. BTW, Win32 has stat().
89 file_stats(const char *file, struct stat *stats) {
90 isc_result_t result = ISC_R_SUCCESS;
92 REQUIRE(file != NULL);
93 REQUIRE(stats != NULL);
95 if (stat(file, stats) != 0)
96 result = isc__errno2result(errno);
102 isc_file_getmodtime(const char *file, isc_time_t *itime) {
106 REQUIRE(file != NULL);
107 REQUIRE(itime != NULL);
109 result = file_stats(file, &stats);
111 if (result == ISC_R_SUCCESS)
113 * XXXDCL some operating systems provide nanoseconds, too,
114 * such as BSD/OS via st_mtimespec.
116 isc_time_set(itime, stats.st_mtime, 0);
122 isc_file_settime(const char *file, isc_time_t *itime) {
123 struct timeval times[2];
125 REQUIRE(file != NULL && itime != NULL);
128 * tv_sec is at least a 32 bit quantity on all platforms we're
129 * dealing with, but it is signed on most (all?) of them,
130 * so we need to make sure the high bit isn't set. This unfortunately
132 * * tv_sec becomes a signed 64 bit integer but long is 32 bits
133 * and isc_time_seconds > LONG_MAX, or
134 * * isc_time_seconds is changed to be > 32 bits but long is 32 bits
135 * and isc_time_seconds has at least 33 significant bits.
137 times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(itime);
140 * Here is the real check for the high bit being set.
142 if ((times[0].tv_sec &
143 (1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
144 return (ISC_R_RANGE);
147 * isc_time_nanoseconds guarantees a value that divided by 1000 will
148 * fit into the minimum possible size tv_usec field. Unfortunately,
149 * we don't know what that type is so can't cast directly ... but
150 * we can at least cast to signed so the IRIX compiler shuts up.
152 times[0].tv_usec = times[1].tv_usec =
153 (isc_int32_t)(isc_time_nanoseconds(itime) / 1000);
155 if (utimes(file, times) < 0)
156 return (isc__errno2result(errno));
158 return (ISC_R_SUCCESS);
162 #define TEMPLATE "tmp-XXXXXXXXXX" /*%< 14 characters. */
165 isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
166 return (isc_file_template(path, TEMPLATE, buf, buflen));
170 isc_file_template(const char *path, const char *templet, char *buf,
174 REQUIRE(path != NULL);
175 REQUIRE(templet != NULL);
176 REQUIRE(buf != NULL);
178 s = strrchr(templet, '/');
182 s = strrchr(path, '/');
185 if ((s - path + 1 + strlen(templet) + 1) > buflen)
186 return (ISC_R_NOSPACE);
188 strlcpy(buf, path, buflen);
189 buf[s - path + 1] = '\0';
190 strlcat(buf, templet, buflen);
192 if ((strlen(templet) + 1) > buflen)
193 return (ISC_R_NOSPACE);
195 strlcpy(buf, templet, buflen);
198 return (ISC_R_SUCCESS);
201 static char alphnum[] =
202 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
205 isc_file_renameunique(const char *file, char *templet) {
210 REQUIRE(file != NULL);
211 REQUIRE(templet != NULL);
217 return (ISC_R_FAILURE);
220 while (cp >= templet && *cp == 'X') {
221 isc_random_get(&which);
222 *cp = alphnum[which % (sizeof(alphnum) - 1)];
225 while (link(file, templet) == -1) {
227 return (isc__errno2result(errno));
231 return (ISC_R_FAILURE);
232 t = strchr(alphnum, *cp);
233 if (t == NULL || *++t == '\0')
241 if (unlink(file) < 0)
243 return (isc__errno2result(errno));
244 return (ISC_R_SUCCESS);
248 isc_file_openunique(char *templet, FILE **fp) {
249 int mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
250 return (isc_file_openuniquemode(templet, mode, fp));
254 isc_file_openuniqueprivate(char *templet, FILE **fp) {
255 int mode = S_IWUSR|S_IRUSR;
256 return (isc_file_openuniquemode(templet, mode, fp));
260 isc_file_openuniquemode(char *templet, int mode, FILE **fp) {
263 isc_result_t result = ISC_R_SUCCESS;
268 REQUIRE(templet != NULL);
269 REQUIRE(fp != NULL && *fp == NULL);
275 return (ISC_R_FAILURE);
278 while (cp >= templet && *cp == 'X') {
279 isc_random_get(&which);
280 *cp = alphnum[which % (sizeof(alphnum) - 1)];
285 while ((fd = open(templet, O_RDWR|O_CREAT|O_EXCL, mode)) == -1) {
287 return (isc__errno2result(errno));
291 return (ISC_R_FAILURE);
292 t = strchr(alphnum, *cp);
293 if (t == NULL || *++t == '\0')
301 f = fdopen(fd, "w+");
303 result = isc__errno2result(errno);
304 if (remove(templet) < 0) {
305 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
306 ISC_LOGMODULE_FILE, ISC_LOG_ERROR,
307 "remove '%s': failed", templet);
317 isc_file_remove(const char *filename) {
320 REQUIRE(filename != NULL);
322 r = unlink(filename);
324 return (ISC_R_SUCCESS);
326 return (isc__errno2result(errno));
330 isc_file_rename(const char *oldname, const char *newname) {
333 REQUIRE(oldname != NULL);
334 REQUIRE(newname != NULL);
336 r = rename(oldname, newname);
338 return (ISC_R_SUCCESS);
340 return (isc__errno2result(errno));
344 isc_file_exists(const char *pathname) {
347 REQUIRE(pathname != NULL);
349 return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
353 isc_file_isplainfile(const char *filename) {
355 * This function returns success if filename is a plain file.
357 struct stat filestat;
358 memset(&filestat,0,sizeof(struct stat));
360 if ((stat(filename, &filestat)) == -1)
361 return(isc__errno2result(errno));
363 if(! S_ISREG(filestat.st_mode))
364 return(ISC_R_INVALIDFILE);
366 return(ISC_R_SUCCESS);
370 isc_file_isabsolute(const char *filename) {
371 REQUIRE(filename != NULL);
372 return (ISC_TF(filename[0] == '/'));
376 isc_file_iscurrentdir(const char *filename) {
377 REQUIRE(filename != NULL);
378 return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
382 isc_file_ischdiridempotent(const char *filename) {
383 REQUIRE(filename != NULL);
384 if (isc_file_isabsolute(filename))
386 if (isc_file_iscurrentdir(filename))
392 isc_file_basename(const char *filename) {
395 REQUIRE(filename != NULL);
397 s = strrchr(filename, '/');
405 isc_file_progname(const char *filename, char *buf, size_t buflen) {
409 REQUIRE(filename != NULL);
410 REQUIRE(buf != NULL);
412 base = isc_file_basename(filename);
413 len = strlen(base) + 1;
416 return (ISC_R_NOSPACE);
417 memcpy(buf, base, len);
419 return (ISC_R_SUCCESS);
423 * Put the absolute name of the current directory into 'dirname', which is
424 * a buffer of at least 'length' characters. End the string with the
425 * appropriate path separator, such that the final product could be
426 * concatenated with a relative pathname to make a valid pathname string.
429 dir_current(char *dirname, size_t length) {
431 isc_result_t result = ISC_R_SUCCESS;
433 REQUIRE(dirname != NULL);
434 REQUIRE(length > 0U);
436 cwd = getcwd(dirname, length);
440 result = ISC_R_NOSPACE;
442 result = isc__errno2result(errno);
444 if (strlen(dirname) + 1 == length)
445 result = ISC_R_NOSPACE;
446 else if (dirname[1] != '\0')
447 strlcat(dirname, "/", length);
454 isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
456 result = dir_current(path, pathlen);
457 if (result != ISC_R_SUCCESS)
459 if (strlen(path) + strlen(filename) + 1 > pathlen)
460 return (ISC_R_NOSPACE);
461 strlcat(path, filename, pathlen);
462 return (ISC_R_SUCCESS);
466 isc_file_truncate(const char *filename, isc_offset_t size) {
467 isc_result_t result = ISC_R_SUCCESS;
469 if (truncate(filename, size) < 0)
470 result = isc__errno2result(errno);
475 isc_file_safecreate(const char *filename, FILE **fp) {
482 REQUIRE(filename != NULL);
483 REQUIRE(fp != NULL && *fp == NULL);
485 result = file_stats(filename, &sb);
486 if (result == ISC_R_SUCCESS) {
487 if ((sb.st_mode & S_IFREG) == 0)
488 return (ISC_R_INVALIDFILE);
489 flags = O_WRONLY | O_TRUNC;
490 } else if (result == ISC_R_FILENOTFOUND) {
491 flags = O_WRONLY | O_CREAT | O_EXCL;
495 fd = open(filename, flags, S_IRUSR | S_IWUSR);
497 return (isc__errno2result(errno));
501 result = isc__errno2result(errno);
507 return (ISC_R_SUCCESS);
511 isc_file_splitpath(isc_mem_t *mctx, char *path, char **dirnam, char **basenam)
513 char *dir, *file, *slash;
515 REQUIRE(path != NULL);
517 slash = strrchr(path, '/');
521 dir = isc_mem_strdup(mctx, "/");
522 } else if (slash != NULL) {
524 dir = isc_mem_allocate(mctx, slash - path);
526 strlcpy(dir, path, slash - path);
529 dir = isc_mem_strdup(mctx, ".");
533 return (ISC_R_NOMEMORY);
536 isc_mem_free(mctx, dir);
537 return (ISC_R_INVALIDFILE);
543 return (ISC_R_SUCCESS);