file.c revision 4dd41c7d5976ff6889e4fbf306036f4256af409a
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 * 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 * 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.
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graffstatic const char alphnum[] =
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff * Emulate UNIX mkstemp, which returns an open FD to the new file
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graffgettemp(char *path, isc_boolean_t binary, int *doopen) {
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff /* extra X's get set to 0's */
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff *trv = alphnum[which % (sizeof(alphnum) - 1)];
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 open(path, flags, _S_IREAD | _S_IWRITE)) >= 0)
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff /* tricky little algorithm for backward compatibility */
e4f074a2c2340ea80099beebecc3b89aa234fa8fMichael Graff /*NOTREACHED*/
c05e003dce672b2f8555a3e56857f29ce89c1677Michael Graff return (gettemp(path, binary, &fd) ? fd : -1);
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 Grafffile_stats(const char *file, struct stat *stats) {
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graffisc_file_mode(const char *file, mode_t *modep) {
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.
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffisc_file_safemovefile(const char *oldname, const char *newname) {
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff * Make sure we have something to do
e24f605ad64182532640dc6721070456b13112d5Michael Graff * Rename to a backup the new file if it still exists
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff /* Now rename the file to the new name
74889a341cac183d477e15cfead391a8f7bdba95Michael Graff * Try to rename the backup back to the original name
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff * if the backup got created
ae7d0a4375abaecfd5c5b0816616d9882831e69bMichael Graff * Delete the backup file if it got created
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffisc_file_getmodtime(const char *file, isc_time_t *time) {
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graff if ((fh = open(file, _O_RDONLY | _O_BINARY)) < 0)
7dbf5a0b64237aa3052f04f4c8f7d56be8ec5d79Michael Graffisc_file_getsize(const char *file, off_t *size) {
4e675038a097065ff13944232cd7c89ac5961984Michael Graffisc_file_settime(const char *file, isc_time_t *time) {
2311073ce0ef26c0250e91e4a083d7cc94fa7d33Michael Graff if ((fh = open(file, _O_RDWR | _O_BINARY)) < 0)
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.
return (ISC_R_SUCCESS);
if (s != NULL)
if (s != NULL) {
return (ISC_R_NOSPACE);
return (ISC_R_NOSPACE);
return (ISC_R_SUCCESS);
int fd;
int res = 0;
if (res != 0) {
return (result);
static isc_result_t
int fd;
FILE *f;
if (f == NULL) {
*fp = f;
return (result);
return (ISC_R_SUCCESS);
return (ISC_R_SUCCESS);
return(ISC_R_INVALIDFILE);
return(ISC_R_SUCCESS);
return(ISC_R_INVALIDFILE);
return(ISC_R_SUCCESS);
return(ISC_R_INVALIDFILE);
return(ISC_R_SUCCESS);
return (ISC_TRUE);
return (ISC_TRUE);
return (ISC_TRUE);
return (ISC_FALSE);
return (ISC_TRUE);
return (ISC_TRUE);
return (ISC_TRUE);
return (ISC_TRUE);
return (ISC_FALSE);
if (s == NULL)
return (filename);
if (s == NULL) {
return (ISC_R_NOSPACE);
if (p == NULL) {
return (ISC_R_NOSPACE);
return (ISC_R_SUCCESS);
len = p - s;
return (ISC_R_NOSPACE);
return (ISC_R_SUCCESS);
char *ptrname;
if (retval == 0)
return (ISC_R_NOTFOUND);
return (ISC_R_NOSPACE);
return (ISC_R_SUCCESS);
int fh;
return (ISC_R_SUCCESS);
int flags;
FILE *f;
int fd;
return (ISC_R_INVALIDFILE);
return (result);
if (f == NULL) {
return (result);
*fp = f;
return (ISC_R_SUCCESS);
char *backslash;
return (ISC_R_NOMEMORY);
return (ISC_R_INVALIDFILE);
return (ISC_R_SUCCESS);
void *buf;
return (NULL);
return (buf);
#ifndef PATH_MAX
size_t l = 0;
return (ISC_R_NOSPACE);
return (ISC_R_SUCCESS);
return (ISC_R_SUCCESS);
return (ISC_R_SUCCESS);
return (ISC_R_SUCCESS);