file.c revision 4dd41c7d5976ff6889e4fbf306036f4256af409a
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff/*
ca41b452ede6feaa9d8739ec3cae19389a7b0d03Bob Halley * Copyright (C) 2004, 2007, 2009, 2011-2015 Internet Systems Consortium, Inc. ("ISC")
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * Copyright (C) 2000-2002 Internet Software Consortium.
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff *
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * Permission to use, copy, modify, and/or distribute this software for any
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * purpose with or without fee is hereby granted, provided that the above
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * copyright notice and this permission notice appear in all copies.
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff *
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff * PERFORMANCE OF THIS SOFTWARE.
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff */
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff/* $Id$ */
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff#include <config.h>
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff#undef rename
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff#include <errno.h>
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff#include <limits.h>
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff#include <stdlib.h>
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff#include <io.h>
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff#include <process.h>
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff#include <sys/stat.h>
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff#include <fcntl.h>
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff#include <sys/utime.h>
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff#include <isc/file.h>
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff#include <isc/mem.h>
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff#include <isc/print.h>
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff#include <isc/random.h>
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff#include <isc/result.h>
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff#include <isc/sha2.h>
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff#include <isc/stat.h>
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff#include <isc/string.h>
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff#include <isc/time.h>
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff#include <isc/util.h>
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff#include "errno2result.h"
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graffstatic const char alphnum[] =
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff/*
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff * Emulate UNIX mkstemp, which returns an open FD to the new file
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff *
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff */
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graffstatic int
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graffgettemp(char *path, isc_boolean_t binary, int *doopen) {
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff char *start, *trv;
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff struct stat sbuf;
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff int flags = O_CREAT|O_EXCL|O_RDWR;
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff if (binary)
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff flags |= _O_BINARY;
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff trv = strrchr(path, 'X');
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff trv++;
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff /* extra X's get set to 0's */
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff while (*--trv == 'X') {
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff isc_uint32_t which;
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff isc_random_get(&which);
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff *trv = alphnum[which % (sizeof(alphnum) - 1)];
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff }
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff /*
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff * check the target directory; if you have six X's and it
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff * doesn't exist this runs for a *very* long time.
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff */
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff for (start = trv + 1;; --trv) {
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff if (trv <= path)
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff break;
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff if (*trv == '\\') {
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff *trv = '\0';
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff if (stat(path, &sbuf))
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff return (0);
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff if (!S_ISDIR(sbuf.st_mode)) {
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff errno = ENOTDIR;
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff return (0);
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff }
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff *trv = '\\';
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff break;
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff }
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff }
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff for (;;) {
62e4837585f2d48ce9ef3ad6880d8fb75a578e58Andreas Gustafsson if (doopen) {
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff if ((*doopen =
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff open(path, flags, _S_IREAD | _S_IWRITE)) >= 0)
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff return (1);
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff if (errno != EEXIST)
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff return (0);
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff } else if (stat(path, &sbuf))
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff return (errno == ENOENT ? 1 : 0);
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff /* tricky little algorithm for backward compatibility */
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff for (trv = start;;) {
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff if (!*trv)
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff return (0);
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff if (*trv == 'z')
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff *trv++ = 'a';
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff else {
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff if (isdigit(*trv))
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff *trv = 'a';
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff else
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff ++*trv;
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff break;
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff }
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff }
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff }
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff /*NOTREACHED*/
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff}
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graffstatic int
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graffmkstemp(char *path, isc_boolean_t binary) {
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff int fd;
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff return (gettemp(path, binary, &fd) ? fd : -1);
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff}
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff/*
62e4837585f2d48ce9ef3ad6880d8fb75a578e58Andreas Gustafsson * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff * it might be good to provide a mechanism that allows for the results
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff * of a previous stat() to be used again without having to do another stat,
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff * such as perl's mechanism of using "_" in place of a file name to indicate
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff * that the results of the last stat should be used. But then you get into
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff * annoying MP issues. BTW, Win32 has stat().
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff */
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graffstatic isc_result_t
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Grafffile_stats(const char *file, struct stat *stats) {
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff isc_result_t result = ISC_R_SUCCESS;
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff REQUIRE(file != NULL);
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff REQUIRE(stats != NULL);
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff if (stat(file, stats) != 0)
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff result = isc__errno2result(errno);
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff return (result);
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff}
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graffstatic isc_result_t
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Grafffd_stats(int fd, struct stat *stats) {
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff isc_result_t result = ISC_R_SUCCESS;
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff REQUIRE(stats != NULL);
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff if (fstat(fd, stats) != 0)
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff result = isc__errno2result(errno);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff return (result);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff}
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffisc_result_t
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffisc_file_getsizefd(int fd, off_t *size) {
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff isc_result_t result;
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff struct stat stats;
e24f605ad64182532640dc6721070456b13112d5Michael Graff
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff REQUIRE(size != NULL);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff result = fd_stats(fd, &stats);
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff if (result == ISC_R_SUCCESS)
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff *size = stats.st_size;
4e675038a097065ff13944232cd7c89ac5961984Michael Graff return (result);
4e675038a097065ff13944232cd7c89ac5961984Michael Graff}
4e675038a097065ff13944232cd7c89ac5961984Michael Graff
4e675038a097065ff13944232cd7c89ac5961984Michael Graffisc_result_t
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graffisc_file_mode(const char *file, mode_t *modep) {
4e675038a097065ff13944232cd7c89ac5961984Michael Graff isc_result_t result;
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff struct stat stats;
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff REQUIRE(modep != NULL);
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff result = file_stats(file, &stats);
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff if (result == ISC_R_SUCCESS)
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff *modep = (stats.st_mode & 07777);
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff return (result);
e24f605ad64182532640dc6721070456b13112d5Michael Graff}
e24f605ad64182532640dc6721070456b13112d5Michael Graff
e24f605ad64182532640dc6721070456b13112d5Michael Graff/*
e24f605ad64182532640dc6721070456b13112d5Michael Graff * isc_file_safemovefile is needed to be defined here to ensure that
e24f605ad64182532640dc6721070456b13112d5Michael Graff * any file with the new name is renamed to a backup name and then the
e24f605ad64182532640dc6721070456b13112d5Michael Graff * rename is done. If all goes well then the backup can be deleted,
e24f605ad64182532640dc6721070456b13112d5Michael Graff * otherwise it gets renamed back.
e24f605ad64182532640dc6721070456b13112d5Michael Graff */
291b0d910d115e41a4b69d0603c3376aebf0c630Michael Graff
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffint
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffisc_file_safemovefile(const char *oldname, const char *newname) {
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff BOOL filestatus;
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff char buf[512];
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff struct stat sbuf;
e24f605ad64182532640dc6721070456b13112d5Michael Graff BOOL exists = FALSE;
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff int tmpfd;
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff /*
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff * Make sure we have something to do
e24f605ad64182532640dc6721070456b13112d5Michael Graff */
e24f605ad64182532640dc6721070456b13112d5Michael Graff if (stat(oldname, &sbuf) != 0) {
e24f605ad64182532640dc6721070456b13112d5Michael Graff errno = ENOENT;
e24f605ad64182532640dc6721070456b13112d5Michael Graff return (-1);
e24f605ad64182532640dc6721070456b13112d5Michael Graff }
e24f605ad64182532640dc6721070456b13112d5Michael Graff
e24f605ad64182532640dc6721070456b13112d5Michael Graff /*
e24f605ad64182532640dc6721070456b13112d5Michael Graff * Rename to a backup the new file if it still exists
e24f605ad64182532640dc6721070456b13112d5Michael Graff */
e24f605ad64182532640dc6721070456b13112d5Michael Graff if (stat(newname, &sbuf) == 0) {
e24f605ad64182532640dc6721070456b13112d5Michael Graff exists = TRUE;
e24f605ad64182532640dc6721070456b13112d5Michael Graff strcpy(buf, newname);
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff strcat(buf, ".XXXXX");
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff tmpfd = mkstemp(buf, ISC_TRUE);
e24f605ad64182532640dc6721070456b13112d5Michael Graff if (tmpfd > 0)
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff _close(tmpfd);
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff (void)DeleteFile(buf);
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff _chmod(newname, _S_IREAD | _S_IWRITE);
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff filestatus = MoveFile(newname, buf);
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff }
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff /* Now rename the file to the new name
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff */
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff _chmod(oldname, _S_IREAD | _S_IWRITE);
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff filestatus = MoveFile(oldname, newname);
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff if (filestatus == 0) {
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff /*
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff * Try to rename the backup back to the original name
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff * if the backup got created
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff */
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff if (exists == TRUE) {
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff filestatus = MoveFile(buf, newname);
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff if (filestatus == 0)
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff errno = EACCES;
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff }
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff return (-1);
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff }
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff /*
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff * Delete the backup file if it got created
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff */
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff if (exists == TRUE)
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff (void)DeleteFile(buf);
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff return (0);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff}
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffisc_result_t
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffisc_file_getmodtime(const char *file, isc_time_t *time) {
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff int fh;
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff REQUIRE(file != NULL);
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff REQUIRE(time != NULL);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff if ((fh = open(file, _O_RDONLY | _O_BINARY)) < 0)
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff return (isc__errno2result(errno));
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff if (!GetFileTime((HANDLE) _get_osfhandle(fh),
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff NULL,
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff NULL,
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff &time->absolute))
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff {
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff close(fh);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff errno = EINVAL;
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff return (isc__errno2result(errno));
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff }
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff close(fh);
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff return (ISC_R_SUCCESS);
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff}
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffisc_result_t
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffisc_file_getsize(const char *file, off_t *size) {
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff isc_result_t result;
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff struct stat stats;
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff REQUIRE(file != NULL);
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff REQUIRE(size != NULL);
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff result = file_stats(file, &stats);
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff if (result == ISC_R_SUCCESS)
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff *size = stats.st_size;
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff return (result);
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff}
1a0e33bc2044e1902493111db14cbf793083ac47Michael Graff
e24f605ad64182532640dc6721070456b13112d5Michael Graffisc_result_t
4e675038a097065ff13944232cd7c89ac5961984Michael Graffisc_file_settime(const char *file, isc_time_t *time) {
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff int fh;
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff REQUIRE(file != NULL && time != NULL);
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff if ((fh = open(file, _O_RDWR | _O_BINARY)) < 0)
e24f605ad64182532640dc6721070456b13112d5Michael Graff return (isc__errno2result(errno));
e24f605ad64182532640dc6721070456b13112d5Michael Graff
e24f605ad64182532640dc6721070456b13112d5Michael Graff /*
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff * Set the date via the filedate system call and return. Failing
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff * this call implies the new file times are not supported by the
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff * underlying file system.
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff */
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff if (!SetFileTime((HANDLE) _get_osfhandle(fh),
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff NULL,
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff &time->absolute,
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff &time->absolute))
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff {
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff close(fh);
a3ab70dae26d009bf78b0594b2ab5eb9208f4b91Michael Graff errno = EINVAL;
return (isc__errno2result(errno));
}
close(fh);
return (ISC_R_SUCCESS);
}
#undef TEMPLATE
#define TEMPLATE "XXXXXXXXXX.tmp" /* 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) > (ssize_t)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;
int res = 0;
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(file != NULL);
REQUIRE(templet != NULL);
fd = mkstemp(templet, ISC_TRUE);
if (fd == -1)
result = isc__errno2result(errno);
else
close(fd);
if (result == ISC_R_SUCCESS) {
res = isc_file_safemovefile(file, templet);
if (res != 0) {
result = isc__errno2result(errno);
(void)unlink(templet);
}
}
return (result);
}
static isc_result_t
openuniquemode(char *templet, int mode, isc_boolean_t binary, FILE **fp) {
int fd;
FILE *f;
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(templet != NULL);
REQUIRE(fp != NULL && *fp == NULL);
/*
* Win32 does not have mkstemp. Using emulation above.
*/
fd = mkstemp(templet, binary);
if (fd == -1)
result = isc__errno2result(errno);
if (result == ISC_R_SUCCESS) {
#if 1
UNUSED(mode);
#else
(void)fchmod(fd, mode);
#endif
f = fdopen(fd, binary ? "wb+" : "w+");
if (f == NULL) {
result = isc__errno2result(errno);
(void)remove(templet);
(void)close(fd);
} else
*fp = f;
}
return (result);
}
isc_result_t
isc_file_openuniqueprivate(char *templet, FILE **fp) {
int mode = _S_IREAD | _S_IWRITE;
return (openuniquemode(templet, mode, ISC_FALSE, fp));
}
isc_result_t
isc_file_openunique(char *templet, FILE **fp) {
int mode = _S_IREAD | _S_IWRITE;
return (openuniquemode(templet, mode, ISC_FALSE, fp));
}
isc_result_t
isc_file_openuniquemode(char *templet, int mode, FILE **fp) {
return (openuniquemode(templet, mode, ISC_FALSE, fp));
}
isc_result_t
isc_file_bopenuniqueprivate(char *templet, FILE **fp) {
int mode = _S_IREAD | _S_IWRITE;
return (openuniquemode(templet, mode, ISC_TRUE, fp));
}
isc_result_t
isc_file_bopenunique(char *templet, FILE **fp) {
int mode = _S_IREAD | _S_IWRITE;
return (openuniquemode(templet, mode, ISC_TRUE, fp));
}
isc_result_t
isc_file_bopenuniquemode(char *templet, int mode, FILE **fp) {
return (openuniquemode(templet, mode, ISC_TRUE, fp));
}
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 = isc_file_safemovefile(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_result_t
isc_file_isplainfile(const char *filename) {
/*
* This function returns success if filename is a plain file.
*/
struct stat filestat;
memset(&filestat,0,sizeof(struct stat));
if ((stat(filename, &filestat)) == -1)
return(isc__errno2result(errno));
if(! S_ISREG(filestat.st_mode))
return(ISC_R_INVALIDFILE);
return(ISC_R_SUCCESS);
}
isc_result_t
isc_file_isplainfilefd(int fd) {
/*
* This function returns success if filename is a plain file.
*/
struct stat filestat;
memset(&filestat,0,sizeof(struct stat));
if ((fstat(fd, &filestat)) == -1)
return(isc__errno2result(errno));
if(! S_ISREG(filestat.st_mode))
return(ISC_R_INVALIDFILE);
return(ISC_R_SUCCESS);
}
isc_result_t
isc_file_isdirectory(const char *filename) {
/*
* This function returns success if filename is a directory.
*/
struct stat filestat;
memset(&filestat,0,sizeof(struct stat));
if ((stat(filename, &filestat)) == -1)
return(isc__errno2result(errno));
if(! S_ISDIR(filestat.st_mode))
return(ISC_R_INVALIDFILE);
return(ISC_R_SUCCESS);
}
isc_boolean_t
isc_file_isabsolute(const char *filename) {
REQUIRE(filename != NULL);
/*
* Look for c:\path\... style, c:/path/... or \\computer\shar\path...
* the UNC style file specs
*/
if ((filename[0] == '\\') && (filename[1] == '\\'))
return (ISC_TRUE);
if (isalpha(filename[0]) && filename[1] == ':' && filename[2] == '\\')
return (ISC_TRUE);
if (isalpha(filename[0]) && filename[1] == ':' && filename[2] == '/')
return (ISC_TRUE);
return (ISC_FALSE);
}
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 (filename[0] == '\\')
return (ISC_TRUE);
if (filename[0] == '/')
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 *progname, size_t namelen) {
const char *s;
char *p;
size_t len;
REQUIRE(filename != NULL);
REQUIRE(progname != NULL);
/*
* Strip the path from the name
*/
s = isc_file_basename(filename);
if (s == NULL) {
return (ISC_R_NOSPACE);
}
/*
* Strip any and all suffixes
*/
p = strchr(s, '.');
if (p == NULL) {
if (namelen <= strlen(s))
return (ISC_R_NOSPACE);
strcpy(progname, s);
return (ISC_R_SUCCESS);
}
/*
* Copy the result to the buffer
*/
len = p - s;
if (len >= namelen)
return (ISC_R_NOSPACE);
strncpy(progname, s, len);
progname[len] = '\0';
return (ISC_R_SUCCESS);
}
isc_result_t
isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
char *ptrname;
DWORD retval;
REQUIRE(filename != NULL);
REQUIRE(path != NULL);
retval = GetFullPathName(filename, (DWORD) pathlen, path, &ptrname);
/* Something went wrong in getting the path */
if (retval == 0)
return (ISC_R_NOTFOUND);
/* Caller needs to provide a larger buffer to contain the string */
if (retval >= pathlen)
return (ISC_R_NOSPACE);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_file_truncate(const char *filename, isc_offset_t size) {
int fh;
REQUIRE(filename != NULL && size >= 0);
if ((fh = open(filename, _O_RDWR | _O_BINARY)) < 0)
return (isc__errno2result(errno));
if(_chsize(fh, size) != 0) {
close(fh);
return (isc__errno2result(errno));
}
close(fh);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_file_safecreate(const char *filename, FILE **fp) {
isc_result_t result;
int flags;
struct stat sb;
FILE *f;
int fd;
REQUIRE(filename != NULL);
REQUIRE(fp != NULL && *fp == NULL);
result = file_stats(filename, &sb);
if (result == ISC_R_SUCCESS) {
if ((sb.st_mode & S_IFREG) == 0)
return (ISC_R_INVALIDFILE);
flags = O_WRONLY | O_TRUNC;
} else if (result == ISC_R_FILENOTFOUND) {
flags = O_WRONLY | O_CREAT | O_EXCL;
} else
return (result);
fd = open(filename, flags, S_IRUSR | S_IWUSR);
if (fd == -1)
return (isc__errno2result(errno));
f = fdopen(fd, "w");
if (f == NULL) {
result = isc__errno2result(errno);
close(fd);
return (result);
}
*fp = f;
return (ISC_R_SUCCESS);
}
isc_result_t
isc_file_splitpath(isc_mem_t *mctx, char *path, char **dirname, char **basename)
{
char *dir, *file, *slash;
char *backslash;
slash = strrchr(path, '/');
backslash = strrchr(path, '\\');
if ((slash != NULL && backslash != NULL && backslash > slash) ||
(slash == NULL && backslash != NULL))
slash = backslash;
if (slash == path) {
file = ++slash;
dir = isc_mem_strdup(mctx, "/");
} else if (slash != NULL) {
file = ++slash;
dir = isc_mem_allocate(mctx, slash - path);
if (dir != NULL)
strlcpy(dir, path, slash - path);
} else {
file = path;
dir = isc_mem_strdup(mctx, ".");
}
if (dir == NULL)
return (ISC_R_NOMEMORY);
if (*file == '\0') {
isc_mem_free(mctx, dir);
return (ISC_R_INVALIDFILE);
}
*dirname = dir;
*basename = file;
return (ISC_R_SUCCESS);
}
void *
isc_file_mmap(void *addr, size_t len, int prot,
int flags, int fd, off_t offset)
{
void *buf;
ssize_t ret;
off_t end;
UNUSED(addr);
UNUSED(prot);
UNUSED(flags);
end = lseek(fd, 0, SEEK_END);
lseek(fd, offset, SEEK_SET);
if (end - offset < (off_t) len)
len = end - offset;
buf = malloc(len);
if (buf == NULL)
return (NULL);
ret = read(fd, buf, (unsigned int) len);
if (ret != (ssize_t) len) {
free(buf);
buf = NULL;
}
return (buf);
}
int
isc_file_munmap(void *addr, size_t len) {
UNUSED(len);
free(addr);
return (0);
}
#define DISALLOW "\\/:ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
isc_result_t
isc_file_sanitize(const char *dir, const char *base, const char *ext,
char *path, size_t length)
{
char buf[PATH_MAX], hash[PATH_MAX];
size_t l = 0;
REQUIRE(base != NULL);
REQUIRE(path != NULL);
l = strlen(base) + 1;
/*
* allow room for a full sha256 hash (64 chars
* plus null terminator)
*/
if (l < 65)
l = 65;
if (dir != NULL)
l += strlen(dir) + 1;
if (ext != NULL)
l += strlen(ext) + 1;
if (l > length || l > PATH_MAX)
return (ISC_R_NOSPACE);
/* Check whether the full-length SHA256 hash filename exists */
isc_sha256_data((const void *) base, strlen(base), hash);
snprintf(buf, sizeof(buf), "%s%s%s%s%s",
dir != NULL ? dir : "", dir != NULL ? "/" : "",
hash, ext != NULL ? "." : "", ext != NULL ? ext : "");
if (isc_file_exists(buf)) {
strlcpy(path, buf, length);
return (ISC_R_SUCCESS);
}
/* Check for a truncated SHA256 hash filename */
hash[16] = '\0';
snprintf(buf, sizeof(buf), "%s%s%s%s%s",
dir != NULL ? dir : "", dir != NULL ? "/" : "",
hash, ext != NULL ? "." : "", ext != NULL ? ext : "");
if (isc_file_exists(buf)) {
strlcpy(path, buf, length);
return (ISC_R_SUCCESS);
}
/*
* If neither hash filename already exists, then we'll use
* the original base name if it has no disallowed characters,
* or the truncated hash name if it does.
*/
if (strpbrk(base, DISALLOW) != NULL) {
strlcpy(path, buf, length);
return (ISC_R_SUCCESS);
}
snprintf(buf, sizeof(buf), "%s%s%s%s%s",
dir != NULL ? dir : "", dir != NULL ? "/" : "",
base, ext != NULL ? "." : "", ext != NULL ? ext : "");
strlcpy(path, buf, length);
return (ISC_R_SUCCESS);
}