2000-04-24 21:36:10 +00:00
|
|
|
/*
|
2001-01-09 22:01:04 +00:00
|
|
|
* Copyright (C) 2000, 2001 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-04-24 21:36:10 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-07-27 09:55:03 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
|
|
|
|
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
|
|
|
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
|
|
|
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2000-04-24 21:36:10 +00:00
|
|
|
*/
|
|
|
|
|
2001-05-08 19:47:55 +00:00
|
|
|
/* $Id: file.c,v 1.33 2001/05/08 19:47:55 gson Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2000-04-28 02:08:20 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
2000-04-24 21:36:10 +00:00
|
|
|
#include <errno.h>
|
2000-05-24 21:36:01 +00:00
|
|
|
#include <limits.h>
|
2000-05-08 20:05:27 +00:00
|
|
|
#include <stdlib.h>
|
2001-04-12 19:46:39 +00:00
|
|
|
#include <time.h> /* Required for utimes on some platforms. */
|
|
|
|
#include <unistd.h> /* Required for mkstemp on NetBSD. */
|
2000-04-24 21:36:10 +00:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2000-05-16 21:05:03 +00:00
|
|
|
#include <sys/time.h>
|
2000-04-24 21:36:10 +00:00
|
|
|
|
|
|
|
#include <isc/file.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-04-25 22:04:26 +00:00
|
|
|
#include <isc/time.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
2000-04-24 21:36:10 +00:00
|
|
|
|
2000-05-11 15:09:30 +00:00
|
|
|
#include "errno2result.h"
|
2000-05-09 23:19:32 +00:00
|
|
|
|
2000-04-24 21:36:10 +00:00
|
|
|
/*
|
|
|
|
* XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
|
|
|
|
* it might be good to provide a mechanism that allows for the results
|
2000-05-08 14:38:29 +00:00
|
|
|
* 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
|
2000-04-24 21:36:10 +00:00
|
|
|
* 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;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-05-09 23:19:32 +00:00
|
|
|
if (stat(file, stats) != 0)
|
2000-05-11 15:09:30 +00:00
|
|
|
result = isc__errno2result(errno);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-04-24 21:36:10 +00:00
|
|
|
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 && 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);
|
|
|
|
}
|
|
|
|
|
2000-05-16 02:13:08 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_file_settime(const char *file, isc_time_t *time) {
|
|
|
|
struct timeval times[2];
|
|
|
|
|
|
|
|
REQUIRE(file != NULL && time != NULL);
|
|
|
|
|
2000-05-18 17:12:14 +00:00
|
|
|
/*
|
|
|
|
* 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,
|
2000-05-24 02:36:49 +00:00
|
|
|
* 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.
|
2000-05-18 17:12:14 +00:00
|
|
|
*/
|
2000-05-24 21:36:01 +00:00
|
|
|
times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(time);
|
2000-05-24 02:36:49 +00:00
|
|
|
|
2000-05-24 21:36:01 +00:00
|
|
|
/*
|
2000-06-26 21:33:57 +00:00
|
|
|
* Here is the real check for the high bit being set.
|
2000-05-24 21:36:01 +00:00
|
|
|
*/
|
|
|
|
if ((times[0].tv_sec &
|
2000-06-26 21:33:57 +00:00
|
|
|
(1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
|
2000-05-18 17:12:14 +00:00
|
|
|
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);
|
2000-05-16 02:13:08 +00:00
|
|
|
|
|
|
|
if (utimes(file, times) < 0)
|
|
|
|
return (isc__errno2result(errno));
|
2000-05-18 17:12:14 +00:00
|
|
|
|
2000-05-16 02:13:08 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-04-24 21:36:10 +00:00
|
|
|
#undef TEMPLATE
|
|
|
|
#define TEMPLATE "tmp-XXXXXXXXXX" /* 14 characters. */
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
|
2000-10-03 05:45:39 +00:00
|
|
|
return (isc_file_template(path, TEMPLATE, buf, buflen));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2000-10-20 22:09:01 +00:00
|
|
|
isc_file_template(const char *path, const char *templet, char *buf,
|
2000-10-03 05:45:39 +00:00
|
|
|
size_t buflen) {
|
2000-04-24 21:36:10 +00:00
|
|
|
char *s;
|
|
|
|
|
|
|
|
REQUIRE(buf != NULL);
|
|
|
|
|
2000-10-20 22:09:01 +00:00
|
|
|
s = strrchr(templet, '/');
|
2000-10-03 05:45:39 +00:00
|
|
|
if (s != NULL)
|
2000-10-20 22:09:01 +00:00
|
|
|
templet = s + 1;
|
2000-10-03 05:45:39 +00:00
|
|
|
|
2000-04-24 21:36:10 +00:00
|
|
|
s = strrchr(path, '/');
|
|
|
|
|
|
|
|
if (s != NULL) {
|
2000-10-20 22:09:01 +00:00
|
|
|
if ((s - path + 1 + strlen(templet) + 1) > buflen)
|
2000-04-24 21:36:10 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-04-24 21:36:10 +00:00
|
|
|
strncpy(buf, path, s - path + 1);
|
|
|
|
buf[s - path + 1] = '\0';
|
2000-10-20 22:09:01 +00:00
|
|
|
strcat(buf, templet);
|
2000-04-24 21:36:10 +00:00
|
|
|
} else {
|
2000-10-20 22:09:01 +00:00
|
|
|
if ((strlen(templet) + 1) > buflen)
|
2000-04-24 21:36:10 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-10-20 22:09:01 +00:00
|
|
|
strcpy(buf, templet);
|
2000-04-24 21:36:10 +00:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-04-24 21:36:10 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-10-03 05:45:39 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_file_renameunique(const char *file, char *templet) {
|
|
|
|
int fd = -1;
|
|
|
|
int res = 0;
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
fd = mkstemp(templet);
|
|
|
|
if (fd == -1) {
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
}
|
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
res = rename(file, templet);
|
2000-10-07 06:55:01 +00:00
|
|
|
if (res != 0) {
|
2000-10-03 05:45:39 +00:00
|
|
|
result = isc__errno2result(errno);
|
2000-12-27 17:21:53 +00:00
|
|
|
(void)unlink(templet);
|
2000-10-07 06:55:01 +00:00
|
|
|
}
|
2000-10-03 05:45:39 +00:00
|
|
|
}
|
|
|
|
if (fd != -1)
|
|
|
|
close(fd);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2000-04-24 21:36:10 +00:00
|
|
|
isc_result_t
|
2000-04-26 00:45:24 +00:00
|
|
|
isc_file_openunique(char *templet, FILE **fp) {
|
2000-04-24 21:36:10 +00:00
|
|
|
int fd;
|
|
|
|
FILE *f;
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
2000-04-26 00:45:24 +00:00
|
|
|
REQUIRE(templet != NULL);
|
2000-04-24 21:36:10 +00:00
|
|
|
REQUIRE(fp != NULL && *fp == NULL);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Win32 does not have mkstemp.
|
|
|
|
*/
|
2000-04-26 00:45:24 +00:00
|
|
|
fd = mkstemp(templet);
|
2000-04-24 21:36:10 +00:00
|
|
|
|
|
|
|
if (fd == -1)
|
2000-05-11 15:09:30 +00:00
|
|
|
result = isc__errno2result(errno);
|
2000-04-24 21:36:10 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
f = fdopen(fd, "w+");
|
|
|
|
if (f == NULL) {
|
2000-05-11 15:09:30 +00:00
|
|
|
result = isc__errno2result(errno);
|
2000-04-26 00:45:24 +00:00
|
|
|
(void)remove(templet);
|
2000-04-24 21:36:10 +00:00
|
|
|
(void)close(fd);
|
|
|
|
|
|
|
|
} else
|
|
|
|
*fp = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
2000-05-09 23:19:32 +00:00
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_remove(const char *filename) {
|
|
|
|
int r;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2000-05-09 23:19:32 +00:00
|
|
|
r = unlink(filename);
|
|
|
|
if (r == 0)
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
else
|
2000-05-11 15:09:30 +00:00
|
|
|
return (isc__errno2result(errno));
|
2000-05-09 23:19:32 +00:00
|
|
|
}
|
2000-09-08 18:37:28 +00:00
|
|
|
|
2000-09-08 21:47:03 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_file_rename(const char *oldname, const char *newname) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
r = rename(oldname, newname);
|
|
|
|
if (r == 0)
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
else
|
|
|
|
return (isc__errno2result(errno));
|
|
|
|
}
|
|
|
|
|
2000-09-08 18:37:28 +00:00
|
|
|
isc_boolean_t
|
|
|
|
isc_file_isabsolute(const char *filename) {
|
|
|
|
return (ISC_TF(filename[0] == '/'));
|
|
|
|
}
|
2001-03-29 02:33:48 +00:00
|
|
|
|
|
|
|
isc_boolean_t
|
|
|
|
isc_file_iscurrentdir(const char *filename) {
|
|
|
|
return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
|
|
|
|
}
|
2001-05-03 18:59:30 +00:00
|
|
|
|
2001-05-05 02:47:21 +00:00
|
|
|
const char *
|
2001-05-03 18:59:30 +00:00
|
|
|
isc_file_basename(const char *filename) {
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
s = strrchr(filename, '/');
|
|
|
|
if (s == NULL)
|
|
|
|
return (filename);
|
|
|
|
return (s + 1);
|
|
|
|
}
|
2001-05-08 19:47:55 +00:00
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_file_progname(const char *filename, char *buf, size_t buflen) {
|
|
|
|
const char *base = isc_file_basename(filename);
|
|
|
|
size_t len = strlen(base) + 1;
|
|
|
|
if (len > buflen)
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
memcpy(buf, base, len);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|