file.c revision a2734fa74aecefc958622b01467398985041cec1
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 2000, 2001 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * purpose with or without fee is hereby granted, provided that the above
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Portions Copyright (c) 1987, 1993
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * The Regents of the University of California. All rights reserved.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * Redistribution and use in source and binary forms, with or without
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * modification, are permitted provided that the following conditions
6b7257f756eb0530cdf54df9a7fab8d51a5001c3David Lawrence * 1. Redistributions of source code must retain the above copyright
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * notice, this list of conditions and the following disclaimer.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * 2. Redistributions in binary form must reproduce the above copyright
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * notice, this list of conditions and the following disclaimer in the
ce2be9b7211ab5bacaa10fe74ef35def3a3f6089David Lawrence * documentation and/or other materials provided with the distribution.
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * 3. All advertising materials mentioning features or use of this software
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * must display the following acknowledgement:
8f66dad9393ae0724f758c4a51e06ff55c2d1219Brian Wellington * This product includes software developed by the University of
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * California, Berkeley and its contributors.
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * 4. Neither the name of the University nor the names of its contributors
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence * may be used to endorse or promote products derived from this software
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * without specific prior written permission.
f96b41064bcd427d8125a096fd646c1f068d8ed7David Lawrence * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
e19501436a92cd48eba2ff47d90fa49c661ec8d8Brian Wellington * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
669e9657c731176df235832367f61435f7b83ddfAndreas Gustafsson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3db78e0855a8dfc162180880cd70d9c1a03d9301David Lawrence * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * SUCH DAMAGE.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington/* $Id: file.c,v 1.44 2002/05/22 05:57:17 marka Exp $ */
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington#include <time.h> /* Required for utimes on some platforms. */
4cd765650776027d05fe7fca248478918e02e63bDavid Lawrence#include <unistd.h> /* Required for mkstemp on NetBSD. */
71ca6e64b4d208a090d255eb64c24f945e615ea0Brian Wellington * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * it might be good to provide a mechanism that allows for the results
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * of a previous stat() to be used again without having to do another stat,
bfafdac0616107ff32389532e7040567cd84b8aaBrian Wellington * such as perl's mechanism of using "_" in place of a file name to indicate
2ba574f329c14376d26d7c0f22c89d7a978a2625Mark Andrews * that the results of the last stat should be used. But then you get into
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews * annoying MP issues. BTW, Win32 has stat().
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonfile_stats(const char *file, struct stat *stats) {
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafssonisc_file_getmodtime(const char *file, isc_time_t *time) {
f7c21e46c4b5fdae516b91374c24a87671f83ea3Andreas Gustafsson * XXXDCL some operating systems provide nanoseconds, too,
cf300e03de3df3ff422db922520bf07c686c86daMark Andrews * such as BSD/OS via st_mtimespec.
5455f30a7532738d750252c00e649890c694ee30Brian Wellingtonisc_file_settime(const char *file, isc_time_t *time) {
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence * tv_sec is at least a 32 bit quantity on all platforms we're
94b166ffa58ef0ff263563c0550d0b30eb9f7772David Lawrence * dealing with, but it is signed on most (all?) of them,
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence * so we need to make sure the high bit isn't set. This unfortunately
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence * loses when either:
87983da955bf63128de85d180359bdc418516c3cDavid Lawrence * * tv_sec becomes a signed 64 bit integer but long is 32 bits
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * and isc_time_seconds > LONG_MAX, or
73a691c373488e4f70387a62462cd8ce0d991705David Lawrence * * isc_time_seconds is changed to be > 32 bits but long is 32 bits
20bd7b4bbf2437ef2f9109edca168ab0ce8445b3David Lawrence * and isc_time_seconds has at least 33 significant bits.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(time);
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews * Here is the real check for the high bit being set.
7389e8330d62a059b8923fb8ca6f933caeb559d9Mark Andrews (1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * isc_time_nanoseconds guarantees a value that divided by 1000 will
b493dfe8bce94b05efc0f161238d32f1234c5670Brian Wellington * fit into the minimum possible size tv_usec field. Unfortunately,
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellington * we don't know what that type is so can't cast directly ... but
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * we can at least cast to signed so the IRIX compiler shuts up.
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington (isc_int32_t)(isc_time_nanoseconds(time) / 1000);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define TEMPLATE "tmp-XXXXXXXXXX" /* 14 characters. */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_file_mktemplate(const char *path, char *buf, size_t buflen) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (isc_file_template(path, TEMPLATE, buf, buflen));
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellingtonisc_file_template(const char *path, const char *templet, char *buf,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if ((s - path + 1 + strlen(templet) + 1) > buflen)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic char alphnum[] =
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellingtonisc_file_renameunique(const char *file, char *templet) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews for (cp = x;;) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews while ((fd = open(templet, O_RDWR|O_CREAT|O_EXCL, mode)) == -1) {
72ddc4cef9c6a6de53aae530dea1ddbb90631131Mark Andrews for (cp = x;;) {
420e5e1022ff5ca4697ed5286462eeaf03614e53Brian Wellingtonisc_file_rename(const char *oldname, const char *newname) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_file_ischdiridempotent(const char *filename) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (s + 1);
b6b9d8b8434e4eaab74b69cd14fcacf448055ca5Brian Wellingtonisc_file_progname(const char *filename, char *buf, size_t buflen) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * Put the absolute name of the current directory into 'dirname', which is
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * a buffer of at least 'length' characters. End the string with the
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * appropriate path separator, such that the final product could be
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews * concatenated with a relative pathname to make a valid pathname string.
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrewsisc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
326bcfa0e2a6b924cb829a0bcc3bf9590ce21ad6Mark Andrews if (strlen(path) + strlen(filename) + 1 > pathlen)