file.c revision dafcb997e390efa4423883dafd100c975c4095d6
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews/*
206a2ec1714a36057227ad8ebafd23cd4972f21fMark Andrews * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Copyright (C) 2000-2002 Internet Software Consortium.
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews *
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * Permission to use, copy, modify, and distribute this software for any
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * purpose with or without fee is hereby granted, provided that the above
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * copyright notice and this permission notice appear in all copies.
7d8b12117d49b4ebe24841c857f2b87b2056ae88Mark Andrews *
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
0d88065af34e10d7695680ca48f0d2ab2aa0aa32Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
0d88065af34e10d7695680ca48f0d2ab2aa0aa32Mark Andrews */
a846a36bdec99f266170cf7d4dac87081a3055baMark Andrews
a846a36bdec99f266170cf7d4dac87081a3055baMark Andrews/*
0d88065af34e10d7695680ca48f0d2ab2aa0aa32Mark Andrews * Portions Copyright (c) 1987, 1993
a846a36bdec99f266170cf7d4dac87081a3055baMark Andrews * The Regents of the University of California. All rights reserved.
a846a36bdec99f266170cf7d4dac87081a3055baMark Andrews *
a846a36bdec99f266170cf7d4dac87081a3055baMark Andrews * Redistribution and use in source and binary forms, with or without
0d88065af34e10d7695680ca48f0d2ab2aa0aa32Mark Andrews * modification, are permitted provided that the following conditions
f89ccd99a4c1f3fa8e0d610463fb9afad4174c5cMark Andrews * are met:
f89ccd99a4c1f3fa8e0d610463fb9afad4174c5cMark Andrews * 1. Redistributions of source code must retain the above copyright
a846a36bdec99f266170cf7d4dac87081a3055baMark Andrews * notice, this list of conditions and the following disclaimer.
a846a36bdec99f266170cf7d4dac87081a3055baMark Andrews * 2. Redistributions in binary form must reproduce the above copyright
a846a36bdec99f266170cf7d4dac87081a3055baMark Andrews * notice, this list of conditions and the following disclaimer in the
f89ccd99a4c1f3fa8e0d610463fb9afad4174c5cMark Andrews * documentation and/or other materials provided with the distribution.
f89ccd99a4c1f3fa8e0d610463fb9afad4174c5cMark Andrews * 3. All advertising materials mentioning features or use of this software
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews * must display the following acknowledgement:
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews * This product includes software developed by the University of
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews * California, Berkeley and its contributors.
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews * 4. Neither the name of the University nor the names of its contributors
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews * may be used to endorse or promote products derived from this software
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews * without specific prior written permission.
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews *
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
a963386d02b0b21d26a50fce1ef442602c81377bMark Andrews * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews * SUCH DAMAGE.
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews */
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews
9a2a12db5a179ff7b9711a0c035036223f6fae79Mark Andrews/* $Id: file.c,v 1.46 2004/03/05 05:11:44 marka Exp $ */
0d88065af34e10d7695680ca48f0d2ab2aa0aa32Mark Andrews
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews#include <config.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews
9213ea3cec20ddb6a305f4d877b75eaa8100c02dMark Andrews#include <errno.h>
9213ea3cec20ddb6a305f4d877b75eaa8100c02dMark Andrews#include <fcntl.h>
9213ea3cec20ddb6a305f4d877b75eaa8100c02dMark Andrews#include <limits.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <stdlib.h>
206a2ec1714a36057227ad8ebafd23cd4972f21fMark Andrews#include <time.h> /* Required for utimes on some platforms. */
9213ea3cec20ddb6a305f4d877b75eaa8100c02dMark Andrews#include <unistd.h> /* Required for mkstemp on NetBSD. */
206a2ec1714a36057227ad8ebafd23cd4972f21fMark Andrews
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews
88a44b0542f5544bc2c29ed64540e4ddfe6eed87Mark Andrews#include <sys/stat.h>
206a2ec1714a36057227ad8ebafd23cd4972f21fMark Andrews#include <sys/time.h>
206a2ec1714a36057227ad8ebafd23cd4972f21fMark Andrews
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/dir.h>
393fd55d91306e6def3a3d06748eee031f57bd17Mark Andrews#include <isc/file.h>
206a2ec1714a36057227ad8ebafd23cd4972f21fMark Andrews#include <isc/random.h>
#include <isc/string.h>
#include <isc/time.h>
#include <isc/util.h>
#include "errno2result.h"
/*
* XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
* it might be good to provide a mechanism that allows for the results
* of a previous stat() to be used again without having to do another stat,
* such as perl's mechanism of using "_" in place of a file name to indicate
* that the results of the last stat should be used. But then you get into
* annoying MP issues. BTW, Win32 has stat().
*/
static isc_result_t
file_stats(const char *file, struct stat *stats) {
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(file != NULL);
REQUIRE(stats != NULL);
if (stat(file, stats) != 0)
result = isc__errno2result(errno);
return (result);
}
isc_result_t
isc_file_getmodtime(const char *file, isc_time_t *time) {
isc_result_t result;
struct stat stats;
REQUIRE(file != NULL);
REQUIRE(time != NULL);
result = file_stats(file, &stats);
if (result == ISC_R_SUCCESS)
/*
* XXXDCL some operating systems provide nanoseconds, too,
* such as BSD/OS via st_mtimespec.
*/
isc_time_set(time, stats.st_mtime, 0);
return (result);
}
isc_result_t
isc_file_settime(const char *file, isc_time_t *time) {
struct timeval times[2];
REQUIRE(file != NULL && time != NULL);
/*
* tv_sec is at least a 32 bit quantity on all platforms we're
* dealing with, but it is signed on most (all?) of them,
* so we need to make sure the high bit isn't set. This unfortunately
* loses when either:
* * tv_sec becomes a signed 64 bit integer but long is 32 bits
* and isc_time_seconds > LONG_MAX, or
* * isc_time_seconds is changed to be > 32 bits but long is 32 bits
* and isc_time_seconds has at least 33 significant bits.
*/
times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(time);
/*
* Here is the real check for the high bit being set.
*/
if ((times[0].tv_sec &
(1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
return (ISC_R_RANGE);
/*
* isc_time_nanoseconds guarantees a value that divided by 1000 will
* fit into the minimum possible size tv_usec field. Unfortunately,
* we don't know what that type is so can't cast directly ... but
* we can at least cast to signed so the IRIX compiler shuts up.
*/
times[0].tv_usec = times[1].tv_usec =
(isc_int32_t)(isc_time_nanoseconds(time) / 1000);
if (utimes(file, times) < 0)
return (isc__errno2result(errno));
return (ISC_R_SUCCESS);
}
#undef TEMPLATE
#define TEMPLATE "tmp-XXXXXXXXXX" /* 14 characters. */
isc_result_t
isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
return (isc_file_template(path, TEMPLATE, buf, buflen));
}
isc_result_t
isc_file_template(const char *path, const char *templet, char *buf,
size_t buflen) {
char *s;
REQUIRE(path != NULL);
REQUIRE(templet != NULL);
REQUIRE(buf != NULL);
s = strrchr(templet, '/');
if (s != NULL)
templet = s + 1;
s = strrchr(path, '/');
if (s != NULL) {
if ((s - path + 1 + strlen(templet) + 1) > buflen)
return (ISC_R_NOSPACE);
strncpy(buf, path, s - path + 1);
buf[s - path + 1] = '\0';
strcat(buf, templet);
} else {
if ((strlen(templet) + 1) > buflen)
return (ISC_R_NOSPACE);
strcpy(buf, templet);
}
return (ISC_R_SUCCESS);
}
static char alphnum[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
isc_result_t
isc_file_renameunique(const char *file, char *templet) {
char *x;
char *cp;
isc_uint32_t which;
REQUIRE(file != NULL);
REQUIRE(templet != NULL);
cp = templet;
while (*cp != '\0')
cp++;
if (cp == templet)
return (ISC_R_FAILURE);
x = cp--;
while (cp >= templet && *cp == 'X') {
isc_random_get(&which);
*cp = alphnum[which % (sizeof(alphnum) - 1)];
x = cp--;
}
while (link(file, templet) == -1) {
if (errno != EEXIST)
return (isc__errno2result(errno));
for (cp = x;;) {
char *t;
if (*cp == '\0')
return (ISC_R_FAILURE);
t = strchr(alphnum, *cp);
if (t == NULL || *++t == '\0')
*cp++ = alphnum[0];
else {
*cp = *t;
break;
}
}
}
(void)unlink(file);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_file_openunique(char *templet, FILE **fp) {
int fd;
FILE *f;
isc_result_t result = ISC_R_SUCCESS;
char *x;
char *cp;
isc_uint32_t which;
int mode;
REQUIRE(templet != NULL);
REQUIRE(fp != NULL && *fp == NULL);
cp = templet;
while (*cp != '\0')
cp++;
if (cp == templet)
return (ISC_R_FAILURE);
x = cp--;
while (cp >= templet && *cp == 'X') {
isc_random_get(&which);
*cp = alphnum[which % (sizeof(alphnum) - 1)];
x = cp--;
}
mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
while ((fd = open(templet, O_RDWR|O_CREAT|O_EXCL, mode)) == -1) {
if (errno != EEXIST)
return (isc__errno2result(errno));
for (cp = x;;) {
char *t;
if (*cp == '\0')
return (ISC_R_FAILURE);
t = strchr(alphnum, *cp);
if (t == NULL || *++t == '\0')
*cp++ = alphnum[0];
else {
*cp = *t;
break;
}
}
}
f = fdopen(fd, "w+");
if (f == NULL) {
result = isc__errno2result(errno);
(void)remove(templet);
(void)close(fd);
} else
*fp = f;
return (result);
}
isc_result_t
isc_file_remove(const char *filename) {
int r;
REQUIRE(filename != NULL);
r = unlink(filename);
if (r == 0)
return (ISC_R_SUCCESS);
else
return (isc__errno2result(errno));
}
isc_result_t
isc_file_rename(const char *oldname, const char *newname) {
int r;
REQUIRE(oldname != NULL);
REQUIRE(newname != NULL);
r = rename(oldname, newname);
if (r == 0)
return (ISC_R_SUCCESS);
else
return (isc__errno2result(errno));
}
isc_boolean_t
isc_file_exists(const char *pathname) {
struct stat stats;
REQUIRE(pathname != NULL);
return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
}
isc_boolean_t
isc_file_isabsolute(const char *filename) {
REQUIRE(filename != NULL);
return (ISC_TF(filename[0] == '/'));
}
isc_boolean_t
isc_file_iscurrentdir(const char *filename) {
REQUIRE(filename != NULL);
return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
}
isc_boolean_t
isc_file_ischdiridempotent(const char *filename) {
REQUIRE(filename != NULL);
if (isc_file_isabsolute(filename))
return (ISC_TRUE);
if (isc_file_iscurrentdir(filename))
return (ISC_TRUE);
return (ISC_FALSE);
}
const char *
isc_file_basename(const char *filename) {
char *s;
REQUIRE(filename != NULL);
s = strrchr(filename, '/');
if (s == NULL)
return (filename);
return (s + 1);
}
isc_result_t
isc_file_progname(const char *filename, char *buf, size_t buflen) {
const char *base;
size_t len;
REQUIRE(filename != NULL);
REQUIRE(buf != NULL);
base = isc_file_basename(filename);
len = strlen(base) + 1;
if (len > buflen)
return (ISC_R_NOSPACE);
memcpy(buf, base, len);
return (ISC_R_SUCCESS);
}
/*
* Put the absolute name of the current directory into 'dirname', which is
* a buffer of at least 'length' characters. End the string with the
* appropriate path separator, such that the final product could be
* concatenated with a relative pathname to make a valid pathname string.
*/
static isc_result_t
dir_current(char *dirname, size_t length) {
char *cwd;
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(dirname != NULL);
REQUIRE(length > 0);
cwd = getcwd(dirname, length);
if (cwd == NULL) {
if (errno == ERANGE)
result = ISC_R_NOSPACE;
else
result = isc__errno2result(errno);
} else {
if (strlen(dirname) + 1 == length)
result = ISC_R_NOSPACE;
else if (dirname[1] != '\0')
strcat(dirname, "/");
}
return (result);
}
isc_result_t
isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
isc_result_t result;
result = dir_current(path, pathlen);
if (result != ISC_R_SUCCESS)
return (result);
if (strlen(path) + strlen(filename) + 1 > pathlen)
return (ISC_R_NOSPACE);
strcat(path, filename);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_file_truncate(const char *filename, isc_offset_t size) {
isc_result_t result = ISC_R_SUCCESS;
if (truncate(filename, size) < 0)
result = isc__errno2result(errno);
return (result);
}