2 * Copyright (C) 2004, 2005, 2007, 2009, 2011-2015 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. Neither the name of the University nor the names of its contributors
31 * may be used to endorse or promote products derived from this software
32 * without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 #include <time.h> /* Required for utimes on some platforms. */
58 #include <unistd.h> /* Required for mkstemp on NetBSD. */
68 #include <isc/random.h>
69 #include <isc/string.h>
73 #include "errno2result.h"
76 * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
77 * it might be good to provide a mechanism that allows for the results
78 * of a previous stat() to be used again without having to do another stat,
79 * such as perl's mechanism of using "_" in place of a file name to indicate
80 * that the results of the last stat should be used. But then you get into
81 * annoying MP issues. BTW, Win32 has stat().
84 file_stats(const char *file, struct stat *stats) {
85 isc_result_t result = ISC_R_SUCCESS;
87 REQUIRE(file != NULL);
88 REQUIRE(stats != NULL);
90 if (stat(file, stats) != 0)
91 result = isc__errno2result(errno);
97 fd_stats(int fd, struct stat *stats) {
98 isc_result_t result = ISC_R_SUCCESS;
100 REQUIRE(stats != NULL);
102 if (fstat(fd, stats) != 0)
103 result = isc__errno2result(errno);
109 isc_file_getsizefd(int fd, off_t *size) {
113 REQUIRE(size != NULL);
115 result = fd_stats(fd, &stats);
117 if (result == ISC_R_SUCCESS)
118 *size = stats.st_size;
124 isc_file_mode(const char *file, mode_t *modep) {
128 REQUIRE(modep != NULL);
130 result = file_stats(file, &stats);
131 if (result == ISC_R_SUCCESS)
132 *modep = (stats.st_mode & 07777);
138 isc_file_getmodtime(const char *file, isc_time_t *modtime) {
142 REQUIRE(file != NULL);
143 REQUIRE(modtime != NULL);
145 result = file_stats(file, &stats);
147 if (result == ISC_R_SUCCESS)
149 * XXXDCL some operating systems provide nanoseconds, too,
150 * such as BSD/OS via st_mtimespec.
152 isc_time_set(modtime, stats.st_mtime, 0);
158 isc_file_settime(const char *file, isc_time_t *when) {
159 struct timeval times[2];
161 REQUIRE(file != NULL && when != NULL);
164 * tv_sec is at least a 32 bit quantity on all platforms we're
165 * dealing with, but it is signed on most (all?) of them,
166 * so we need to make sure the high bit isn't set. This unfortunately
168 * * tv_sec becomes a signed 64 bit integer but long is 32 bits
169 * and isc_time_seconds > LONG_MAX, or
170 * * isc_time_seconds is changed to be > 32 bits but long is 32 bits
171 * and isc_time_seconds has at least 33 significant bits.
173 times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(when);
176 * Here is the real check for the high bit being set.
178 if ((times[0].tv_sec &
179 (1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
180 return (ISC_R_RANGE);
183 * isc_time_nanoseconds guarantees a value that divided by 1000 will
184 * fit into the minimum possible size tv_usec field. Unfortunately,
185 * we don't know what that type is so can't cast directly ... but
186 * we can at least cast to signed so the IRIX compiler shuts up.
188 times[0].tv_usec = times[1].tv_usec =
189 (isc_int32_t)(isc_time_nanoseconds(when) / 1000);
191 if (utimes(file, times) < 0)
192 return (isc__errno2result(errno));
194 return (ISC_R_SUCCESS);
198 #define TEMPLATE "tmp-XXXXXXXXXX" /*%< 14 characters. */
201 isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
202 return (isc_file_template(path, TEMPLATE, buf, buflen));
206 isc_file_template(const char *path, const char *templet, char *buf,
211 REQUIRE(path != NULL);
212 REQUIRE(templet != NULL);
213 REQUIRE(buf != NULL);
215 s = strrchr(templet, '/');
219 s = strrchr(path, '/');
222 if ((s - path + 1 + strlen(templet) + 1) > buflen)
223 return (ISC_R_NOSPACE);
225 strncpy(buf, path, s - path + 1);
226 buf[s - path + 1] = '\0';
227 strcat(buf, templet);
229 if ((strlen(templet) + 1) > buflen)
230 return (ISC_R_NOSPACE);
232 strcpy(buf, templet);
235 return (ISC_R_SUCCESS);
238 static char alphnum[] =
239 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
242 isc_file_renameunique(const char *file, char *templet) {
247 REQUIRE(file != NULL);
248 REQUIRE(templet != NULL);
254 return (ISC_R_FAILURE);
257 while (cp >= templet && *cp == 'X') {
258 isc_random_get(&which);
259 *cp = alphnum[which % (sizeof(alphnum) - 1)];
262 while (link(file, templet) == -1) {
264 return (isc__errno2result(errno));
268 return (ISC_R_FAILURE);
269 t = strchr(alphnum, *cp);
270 if (t == NULL || *++t == '\0')
278 if (unlink(file) < 0)
280 return (isc__errno2result(errno));
281 return (ISC_R_SUCCESS);
285 isc_file_openunique(char *templet, FILE **fp) {
286 int mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
287 return (isc_file_openuniquemode(templet, mode, fp));
291 isc_file_openuniqueprivate(char *templet, FILE **fp) {
292 int mode = S_IWUSR|S_IRUSR;
293 return (isc_file_openuniquemode(templet, mode, fp));
297 isc_file_openuniquemode(char *templet, int mode, FILE **fp) {
300 isc_result_t result = ISC_R_SUCCESS;
305 REQUIRE(templet != NULL);
306 REQUIRE(fp != NULL && *fp == NULL);
312 return (ISC_R_FAILURE);
315 while (cp >= templet && *cp == 'X') {
316 isc_random_get(&which);
317 *cp = alphnum[which % (sizeof(alphnum) - 1)];
322 while ((fd = open(templet, O_RDWR|O_CREAT|O_EXCL, mode)) == -1) {
324 return (isc__errno2result(errno));
328 return (ISC_R_FAILURE);
329 t = strchr(alphnum, *cp);
330 if (t == NULL || *++t == '\0')
338 f = fdopen(fd, "w+");
340 result = isc__errno2result(errno);
341 if (remove(templet) < 0) {
342 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
343 ISC_LOGMODULE_FILE, ISC_LOG_ERROR,
344 "remove '%s': failed", templet);
354 isc_file_bopenunique(char *templet, FILE **fp) {
355 int mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
356 return (isc_file_openuniquemode(templet, mode, fp));
360 isc_file_bopenuniqueprivate(char *templet, FILE **fp) {
361 int mode = S_IWUSR|S_IRUSR;
362 return (isc_file_openuniquemode(templet, mode, fp));
366 isc_file_bopenuniquemode(char *templet, int mode, FILE **fp) {
367 return (isc_file_openuniquemode(templet, mode, fp));
371 isc_file_remove(const char *filename) {
374 REQUIRE(filename != NULL);
376 r = unlink(filename);
378 return (ISC_R_SUCCESS);
380 return (isc__errno2result(errno));
384 isc_file_rename(const char *oldname, const char *newname) {
387 REQUIRE(oldname != NULL);
388 REQUIRE(newname != NULL);
390 r = rename(oldname, newname);
392 return (ISC_R_SUCCESS);
394 return (isc__errno2result(errno));
398 isc_file_exists(const char *pathname) {
401 REQUIRE(pathname != NULL);
403 return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
407 isc_file_isplainfile(const char *filename) {
409 * This function returns success if filename is a plain file.
411 struct stat filestat;
412 memset(&filestat,0,sizeof(struct stat));
414 if ((stat(filename, &filestat)) == -1)
415 return(isc__errno2result(errno));
417 if(! S_ISREG(filestat.st_mode))
418 return(ISC_R_INVALIDFILE);
420 return(ISC_R_SUCCESS);
424 isc_file_isdirectory(const char *filename) {
426 * This function returns success if filename exists and is a
429 struct stat filestat;
430 memset(&filestat,0,sizeof(struct stat));
432 if ((stat(filename, &filestat)) == -1)
433 return(isc__errno2result(errno));
435 if(! S_ISDIR(filestat.st_mode))
436 return(ISC_R_INVALIDFILE);
438 return(ISC_R_SUCCESS);
442 isc_file_isabsolute(const char *filename) {
443 REQUIRE(filename != NULL);
444 return (ISC_TF(filename[0] == '/'));
448 isc_file_iscurrentdir(const char *filename) {
449 REQUIRE(filename != NULL);
450 return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
454 isc_file_ischdiridempotent(const char *filename) {
455 REQUIRE(filename != NULL);
456 if (isc_file_isabsolute(filename))
458 if (isc_file_iscurrentdir(filename))
464 isc_file_basename(const char *filename) {
467 REQUIRE(filename != NULL);
469 s = strrchr(filename, '/');
477 isc_file_progname(const char *filename, char *buf, size_t buflen) {
481 REQUIRE(filename != NULL);
482 REQUIRE(buf != NULL);
484 base = isc_file_basename(filename);
485 len = strlen(base) + 1;
488 return (ISC_R_NOSPACE);
489 memmove(buf, base, len);
491 return (ISC_R_SUCCESS);
495 * Put the absolute name of the current directory into 'dirname', which is
496 * a buffer of at least 'length' characters. End the string with the
497 * appropriate path separator, such that the final product could be
498 * concatenated with a relative pathname to make a valid pathname string.
501 dir_current(char *dirname, size_t length) {
503 isc_result_t result = ISC_R_SUCCESS;
505 REQUIRE(dirname != NULL);
506 REQUIRE(length > 0U);
508 cwd = getcwd(dirname, length);
512 result = ISC_R_NOSPACE;
514 result = isc__errno2result(errno);
516 if (strlen(dirname) + 1 == length)
517 result = ISC_R_NOSPACE;
518 else if (dirname[1] != '\0')
519 strcat(dirname, "/");
526 isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
528 result = dir_current(path, pathlen);
529 if (result != ISC_R_SUCCESS)
531 if (strlen(path) + strlen(filename) + 1 > pathlen)
532 return (ISC_R_NOSPACE);
533 strcat(path, filename);
534 return (ISC_R_SUCCESS);
538 isc_file_truncate(const char *filename, isc_offset_t size) {
539 isc_result_t result = ISC_R_SUCCESS;
541 if (truncate(filename, size) < 0)
542 result = isc__errno2result(errno);
547 isc_file_safecreate(const char *filename, FILE **fp) {
554 REQUIRE(filename != NULL);
555 REQUIRE(fp != NULL && *fp == NULL);
557 result = file_stats(filename, &sb);
558 if (result == ISC_R_SUCCESS) {
559 if ((sb.st_mode & S_IFREG) == 0)
560 return (ISC_R_INVALIDFILE);
561 flags = O_WRONLY | O_TRUNC;
562 } else if (result == ISC_R_FILENOTFOUND) {
563 flags = O_WRONLY | O_CREAT | O_EXCL;
567 fd = open(filename, flags, S_IRUSR | S_IWUSR);
569 return (isc__errno2result(errno));
573 result = isc__errno2result(errno);
579 return (ISC_R_SUCCESS);
583 isc_file_splitpath(isc_mem_t *mctx, const char *path, char **dirname,
584 char const **basename)
587 const char *file, *slash;
590 return (ISC_R_INVALIDFILE);
592 slash = strrchr(path, '/');
596 dir = isc_mem_strdup(mctx, "/");
597 } else if (slash != NULL) {
599 dir = isc_mem_allocate(mctx, slash - path);
601 strlcpy(dir, path, slash - path);
604 dir = isc_mem_strdup(mctx, ".");
608 return (ISC_R_NOMEMORY);
611 isc_mem_free(mctx, dir);
612 return (ISC_R_INVALIDFILE);
618 return (ISC_R_SUCCESS);