#include <config.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <isc/dir.h>
#include <isc/file.h>
#include <isc/string.h>
#include <isc/time.h>
#include <isc/util.h>
#include "errno2result.h"
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)
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);
times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(time);
if ((times[0].tv_sec &
(1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
return (ISC_R_RANGE);
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"
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);
}
isc_result_t
isc_file_renameunique(const char *file, char *templet) {
int fd = -1;
int res = 0;
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(file != NULL);
REQUIRE(templet != NULL);
fd = mkstemp(templet);
if (fd == -1) {
result = isc__errno2result(errno);
}
if (result == ISC_R_SUCCESS) {
res = rename(file, templet);
if (res != 0) {
result = isc__errno2result(errno);
(void)unlink(templet);
}
}
if (fd != -1)
close(fd);
return (result);
}
isc_result_t
isc_file_openunique(char *templet, FILE **fp) {
int fd;
FILE *f;
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(templet != NULL);
REQUIRE(fp != NULL && *fp == NULL);
fd = mkstemp(templet);
if (fd == -1)
result = isc__errno2result(errno);
if (result == ISC_R_SUCCESS) {
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);
}
isc_result_t
isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
isc_result_t result;
result = isc_dir_current(path, pathlen, ISC_TRUE);
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);
}