file.c revision bbb3705e4cbdacf7cf6da7e448d79d97fdab2411
3759f10fc543747668b1ca4b4671f35b0dea8445Francis Dupont * Copyright (C) 2004, 2007, 2009, 2011, 2012 Internet Systems Consortium, Inc. ("ISC")
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Copyright (C) 2000-2002 Internet Software Consortium.
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * Permission to use, copy, modify, and/or distribute this software for any
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * Emulate UNIX mkstemp, which returns an open FD to the new file
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafssongettemp(char *path, isc_boolean_t binary, int *doopen) {
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson /* extra X's get set to 0's */
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * check the target directory; if you have six X's and it
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * doesn't exist this runs for a *very* long time.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence open(path, flags, _S_IREAD | _S_IWRITE)) >= 0)
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence /* tricky little algorithm for backward compatibility */
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson /*NOTREACHED*/
9cd6d409b78a6f833b681c13a68fbdc7c024fe66David Lawrence return (gettemp(path, binary, &fd) ? fd : -1);
4b87939256ede703385e9cab92d3c58d03c31098Mark Andrews * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * it might be good to provide a mechanism that allows for the results
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * of a previous stat() to be used again without having to do another stat,
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * such as perl's mechanism of using "_" in place of a file name to indicate
79c08618e9f27da052ad6bbc0b232f23ff2e568eAndreas Gustafsson * that the results of the last stat should be used. But then you get into
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * annoying MP issues. BTW, Win32 has stat().
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafssonfile_stats(const char *file, struct stat *stats) {
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafssonisc_file_mode(const char *file, mode_t *modep) {
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * isc_file_safemovefile is needed to be defined here to ensure that
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * any file with the new name is renamed to a backup name and then the
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * rename is done. If all goes well then the backup can be deleted,
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * otherwise it gets renamed back.
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafssonisc_file_safemovefile(const char *oldname, const char *newname) {
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * Make sure we have something to do
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * Rename to a backup the new file if it still exists
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson /* Now rename the file to the new name
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * Try to rename the backup back to the original name
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * if the backup got created
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * Delete the backup file if it got created
461a00bbdee50a46ba7fcfb3f9f2847d3bda5ad3Andreas Gustafssonisc_file_getmodtime(const char *file, isc_time_t *time) {
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson if ((fh = open(file, _O_RDONLY | _O_BINARY)) < 0)
461a00bbdee50a46ba7fcfb3f9f2847d3bda5ad3Andreas Gustafssonisc_file_settime(const char *file, isc_time_t *time) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if ((fh = open(file, _O_RDWR | _O_BINARY)) < 0)
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * Set the date via the filedate system call and return. Failing
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * this call implies the new file times are not supported by the
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson * underlying file system.
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson if (!SetFileTime((HANDLE) _get_osfhandle(fh),
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#define TEMPLATE "XXXXXXXXXX.tmp" /* 14 characters. */
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafssonisc_file_mktemplate(const char *path, char *buf, size_t buflen) {
ca485ab26fffa241a3eac1899b2d2012dd1fdb73Andreas Gustafsson return (isc_file_template(path, TEMPLATE, buf, buflen));
21ba6c18e4ccc73933af5cf28701a5cc3b7963ecMark Andrewsisc_file_template(const char *path, const char *templet, char *buf,
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 (buf);