2 * Copyright (C) 2004, 2005, 2007, 2009, 2011-2014 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 *time) {
142 REQUIRE(file != NULL);
143 REQUIRE(time != 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(time, stats.st_mtime, 0);
158 isc_file_settime(const char *file, isc_time_t *time) {
159 struct timeval times[2];
161 REQUIRE(file != NULL && time != 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(time);
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(time) / 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,
210 REQUIRE(path != NULL);
211 REQUIRE(templet != NULL);
212 REQUIRE(buf != NULL);
214 s = strrchr(templet, '/');
218 s = strrchr(path, '/');
221 if ((s - path + 1 + strlen(templet) + 1) > buflen)
222 return (ISC_R_NOSPACE);
224 strncpy(buf, path, s - path + 1);
225 buf[s - path + 1] = '\0';
226 strcat(buf, templet);
228 if ((strlen(templet) + 1) > buflen)
229 return (ISC_R_NOSPACE);
231 strcpy(buf, templet);
234 return (ISC_R_SUCCESS);
237 static char alphnum[] =
238 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
241 isc_file_renameunique(const char *file, char *templet) {
246 REQUIRE(file != NULL);
247 REQUIRE(templet != NULL);
253 return (ISC_R_FAILURE);
256 while (cp >= templet && *cp == 'X') {
257 isc_random_get(&which);
258 *cp = alphnum[which % (sizeof(alphnum) - 1)];
261 while (link(file, templet) == -1) {
263 return (isc__errno2result(errno));
267 return (ISC_R_FAILURE);
268 t = strchr(alphnum, *cp);
269 if (t == NULL || *++t == '\0')
277 if (unlink(file) < 0)
279 return (isc__errno2result(errno));
280 return (ISC_R_SUCCESS);
284 isc_file_openunique(char *templet, FILE **fp) {
285 int mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
286 return (isc_file_openuniquemode(templet, mode, fp));
290 isc_file_openuniqueprivate(char *templet, FILE **fp) {
291 int mode = S_IWUSR|S_IRUSR;
292 return (isc_file_openuniquemode(templet, mode, fp));
296 isc_file_openuniquemode(char *templet, int mode, FILE **fp) {
299 isc_result_t result = ISC_R_SUCCESS;
304 REQUIRE(templet != NULL);
305 REQUIRE(fp != NULL && *fp == NULL);
311 return (ISC_R_FAILURE);
314 while (cp >= templet && *cp == 'X') {
315 isc_random_get(&which);
316 *cp = alphnum[which % (sizeof(alphnum) - 1)];
321 while ((fd = open(templet, O_RDWR|O_CREAT|O_EXCL, mode)) == -1) {
323 return (isc__errno2result(errno));
327 return (ISC_R_FAILURE);
328 t = strchr(alphnum, *cp);
329 if (t == NULL || *++t == '\0')
337 f = fdopen(fd, "w+");
339 result = isc__errno2result(errno);
340 if (remove(templet) < 0) {
341 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
342 ISC_LOGMODULE_FILE, ISC_LOG_ERROR,
343 "remove '%s': failed", templet);
353 isc_file_bopenunique(char *templet, FILE **fp) {
354 int mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
355 return (isc_file_openuniquemode(templet, mode, fp));
359 isc_file_bopenuniqueprivate(char *templet, FILE **fp) {
360 int mode = S_IWUSR|S_IRUSR;
361 return (isc_file_openuniquemode(templet, mode, fp));
365 isc_file_bopenuniquemode(char *templet, int mode, FILE **fp) {
366 return (isc_file_openuniquemode(templet, mode, fp));
370 isc_file_remove(const char *filename) {
373 REQUIRE(filename != NULL);
375 r = unlink(filename);
377 return (ISC_R_SUCCESS);
379 return (isc__errno2result(errno));
383 isc_file_rename(const char *oldname, const char *newname) {
386 REQUIRE(oldname != NULL);
387 REQUIRE(newname != NULL);
389 r = rename(oldname, newname);
391 return (ISC_R_SUCCESS);
393 return (isc__errno2result(errno));
397 isc_file_exists(const char *pathname) {
400 REQUIRE(pathname != NULL);
402 return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
406 isc_file_isplainfile(const char *filename) {
408 * This function returns success if filename is a plain file.
410 struct stat filestat;
411 memset(&filestat,0,sizeof(struct stat));
413 if ((stat(filename, &filestat)) == -1)
414 return(isc__errno2result(errno));
416 if(! S_ISREG(filestat.st_mode))
417 return(ISC_R_INVALIDFILE);
419 return(ISC_R_SUCCESS);
423 isc_file_isdirectory(const char *filename) {
425 * This function returns success if filename exists and is a
428 struct stat filestat;
429 memset(&filestat,0,sizeof(struct stat));
431 if ((stat(filename, &filestat)) == -1)
432 return(isc__errno2result(errno));
434 if(! S_ISDIR(filestat.st_mode))
435 return(ISC_R_INVALIDFILE);
437 return(ISC_R_SUCCESS);
441 isc_file_isabsolute(const char *filename) {
442 REQUIRE(filename != NULL);
443 return (ISC_TF(filename[0] == '/'));
447 isc_file_iscurrentdir(const char *filename) {
448 REQUIRE(filename != NULL);
449 return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
453 isc_file_ischdiridempotent(const char *filename) {
454 REQUIRE(filename != NULL);
455 if (isc_file_isabsolute(filename))
457 if (isc_file_iscurrentdir(filename))
463 isc_file_basename(const char *filename) {
466 REQUIRE(filename != NULL);
468 s = strrchr(filename, '/');
476 isc_file_progname(const char *filename, char *buf, size_t buflen) {
480 REQUIRE(filename != NULL);
481 REQUIRE(buf != NULL);
483 base = isc_file_basename(filename);
484 len = strlen(base) + 1;
487 return (ISC_R_NOSPACE);
488 memmove(buf, base, len);
490 return (ISC_R_SUCCESS);
494 * Put the absolute name of the current directory into 'dirname', which is
495 * a buffer of at least 'length' characters. End the string with the
496 * appropriate path separator, such that the final product could be
497 * concatenated with a relative pathname to make a valid pathname string.
500 dir_current(char *dirname, size_t length) {
502 isc_result_t result = ISC_R_SUCCESS;
504 REQUIRE(dirname != NULL);
505 REQUIRE(length > 0U);
507 cwd = getcwd(dirname, length);
511 result = ISC_R_NOSPACE;
513 result = isc__errno2result(errno);
515 if (strlen(dirname) + 1 == length)
516 result = ISC_R_NOSPACE;
517 else if (dirname[1] != '\0')
518 strcat(dirname, "/");
525 isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
527 result = dir_current(path, pathlen);
528 if (result != ISC_R_SUCCESS)
530 if (strlen(path) + strlen(filename) + 1 > pathlen)
531 return (ISC_R_NOSPACE);
532 strcat(path, filename);
533 return (ISC_R_SUCCESS);
537 isc_file_truncate(const char *filename, isc_offset_t size) {
538 isc_result_t result = ISC_R_SUCCESS;
540 if (truncate(filename, size) < 0)
541 result = isc__errno2result(errno);
546 isc_file_safecreate(const char *filename, FILE **fp) {
553 REQUIRE(filename != NULL);
554 REQUIRE(fp != NULL && *fp == NULL);
556 result = file_stats(filename, &sb);
557 if (result == ISC_R_SUCCESS) {
558 if ((sb.st_mode & S_IFREG) == 0)
559 return (ISC_R_INVALIDFILE);
560 flags = O_WRONLY | O_TRUNC;
561 } else if (result == ISC_R_FILENOTFOUND) {
562 flags = O_WRONLY | O_CREAT | O_EXCL;
566 fd = open(filename, flags, S_IRUSR | S_IWUSR);
568 return (isc__errno2result(errno));
572 result = isc__errno2result(errno);
578 return (ISC_R_SUCCESS);
582 isc_file_splitpath(isc_mem_t *mctx, char *path, char **dirname, char **basename)
584 char *dir, *file, *slash;
587 return (ISC_R_INVALIDFILE);
589 slash = strrchr(path, '/');
593 dir = isc_mem_strdup(mctx, "/");
594 } else if (slash != NULL) {
596 dir = isc_mem_allocate(mctx, slash - path);
598 strlcpy(dir, path, slash - path);
601 dir = isc_mem_strdup(mctx, ".");
605 return (ISC_R_NOMEMORY);
608 isc_mem_free(mctx, dir);
609 return (ISC_R_INVALIDFILE);
615 return (ISC_R_SUCCESS);