file.c revision fdacf913e123db672888ff6b24a89caea6b12a50
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence/*
07dcadc6e24f4572f2b7d7cae7d47d0f5256d129Andreas Gustafsson * Copyright (C) 2000, 2001 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence * Permission to use, copy, modify, and distribute this software for any
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence * purpose with or without fee is hereby granted, provided that the above
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
15a44745412679c30a6d022733925af70a38b715David Lawrence * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
15a44745412679c30a6d022733925af70a38b715David Lawrence * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15a44745412679c30a6d022733925af70a38b715David Lawrence * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15a44745412679c30a6d022733925af70a38b715David Lawrence * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence */
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence
fdacf913e123db672888ff6b24a89caea6b12a50Danny Mayer/* $Id: file.c,v 1.26 2001/09/05 04:17:13 mayer Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <config.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#undef rename
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <errno.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <limits.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <stdlib.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <io.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <process.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <sys/stat.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <fcntl.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <sys/utime.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <isc/file.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <isc/result.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <isc/time.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <isc/util.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include <isc/stat.h>
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#include "errno2result.h"
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer/*
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * Emulate UNIX mkstemp, which returns an open FD to the new file
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer *
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerstatic int
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayergettemp(char *path, int *doopen) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer char *start, *trv;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer struct stat sbuf;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer int pid;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer trv = strrchr(path, 'X');
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer trv++;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer pid = getpid();
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /* extra X's get set to 0's */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer while (*--trv == 'X') {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer *trv = (pid % 10) + '0';
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer pid /= 10;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /*
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * check the target directory; if you have six X's and it
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * doesn't exist this runs for a *very* long time.
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer for (start = trv + 1;; --trv) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (trv <= path)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer break;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (*trv == '\\') {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer *trv = '\0';
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (stat(path, &sbuf))
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer return (0);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (!S_ISDIR(sbuf.st_mode)) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer errno = ENOTDIR;
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer return (0);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer *trv = '\\';
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer break;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer for (;;) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (doopen) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if ((*doopen =
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson open(path, O_CREAT|O_EXCL|O_RDWR,
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson _S_IREAD | _S_IWRITE)) >= 0)
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer return (1);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (errno != EEXIST)
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer return (0);
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson } else if (stat(path, &sbuf))
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer return (errno == ENOENT ? 1 : 0);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /* tricky little algorithm for backward compatibility */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer for (trv = start;;) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (!*trv)
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer return (0);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (*trv == 'z')
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer *trv++ = 'a';
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer else {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (isdigit(*trv))
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer *trv = 'a';
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer else
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer ++*trv;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer break;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /*NOTREACHED*/
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
39125379e5c9331f3dafd8d4bcb23fd52e30bfa8Andreas Gustafssonstatic int
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayermkstemp(char *path) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer int fd;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (gettemp(path, &fd) ? fd : -1);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer/*
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * it might be good to provide a mechanism that allows for the results
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * of a previous stat() to be used again without having to do another stat,
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * such as perl's mechanism of using "_" in place of a file name to indicate
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * that the results of the last stat should be used. But then you get into
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * annoying MP issues. BTW, Win32 has stat().
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerstatic isc_result_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerfile_stats(const char *file, struct stat *stats) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer isc_result_t result = ISC_R_SUCCESS;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(file != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(stats != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (stat(file, stats) != 0)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer result = isc__errno2result(errno);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (result);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson/*
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson * isc_file_safemovefile is needed to be defined here to ensure that
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson * any file with the new name is renamed to a backup name and then the
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson * rename is done. If all goes well then the backup can be deleted,
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson * otherwise it gets renamed back.
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerint
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_safemovefile(const char *oldname, const char *newname) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer BOOL filestatus;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer char buf[512];
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer struct stat sbuf;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer BOOL exists = FALSE;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer int tmpfd;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /*
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * Make sure we have something to do
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson if (stat(oldname, &sbuf) != 0) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer errno = ENOENT;
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer return (-1);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /*
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * Rename to a backup the new file if it still exists
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer if (stat(newname, &sbuf) == 0) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer exists = TRUE;
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer strcpy(buf, newname);
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer strcat(buf, ".XXXXX");
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer tmpfd = mkstemp(buf);
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer if (tmpfd > 0)
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer _close(tmpfd);
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer DeleteFile(buf);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer _chmod(newname, _S_IREAD | _S_IWRITE);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer filestatus = MoveFile(newname, buf);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer /* Now rename the file to the new name
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer _chmod(oldname, _S_IREAD | _S_IWRITE);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer filestatus = MoveFile(oldname, newname);
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer if (filestatus == 0) {
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson /*
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson * Try to rename the backup back to the original name
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson * if the backup got created
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson if (exists == TRUE) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer filestatus = MoveFile(buf, newname);
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson if (filestatus == 0) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer errno = EACCES;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson return (-1);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson /*
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson * Delete the backup file if it got created
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson if (exists == TRUE)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer filestatus = DeleteFile(buf);
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson return (0);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_result_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_getmodtime(const char *file, isc_time_t *time) {
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson int fh;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(file != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(time != NULL);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
fdacf913e123db672888ff6b24a89caea6b12a50Danny Mayer if ((fh = open(file, _O_RDONLY | _O_BINARY)) < 0)
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson return (isc__errno2result(errno));
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson if (!GetFileTime((HANDLE) _get_osfhandle(fh),
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson NULL,
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson NULL,
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson &time->absolute))
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson {
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson close(fh);
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson errno = EINVAL;
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson return (isc__errno2result(errno));
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson }
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson close(fh);
bdec94477cc9ecac9ff14d3399f47f3d71cd370aAndreas Gustafsson return (ISC_R_SUCCESS);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_result_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_settime(const char *file, isc_time_t *time) {
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer int fh;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer REQUIRE(file != NULL && time != NULL);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer if ((fh = open(file, _O_RDWR | _O_BINARY)) < 0)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (isc__errno2result(errno));
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
528baec07b1b9a566d47fc638ba5cbbe01475c74Andreas Gustafsson /*
528baec07b1b9a566d47fc638ba5cbbe01475c74Andreas Gustafsson * Set the date via the filedate system call and return. Failing
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer * this call implies the new file times are not supported by the
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer * underlying file system.
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer */
528baec07b1b9a566d47fc638ba5cbbe01475c74Andreas Gustafsson if (!SetFileTime((HANDLE) _get_osfhandle(fh),
528baec07b1b9a566d47fc638ba5cbbe01475c74Andreas Gustafsson NULL,
528baec07b1b9a566d47fc638ba5cbbe01475c74Andreas Gustafsson &time->absolute,
528baec07b1b9a566d47fc638ba5cbbe01475c74Andreas Gustafsson &time->absolute))
528baec07b1b9a566d47fc638ba5cbbe01475c74Andreas Gustafsson {
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer close(fh);
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer errno = EINVAL;
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer return (isc__errno2result(errno));
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer }
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer close(fh);
b602bf9e8b67cb2b1ff9ee8b2a48793bf6c70e16Danny Mayer return (ISC_R_SUCCESS);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence#undef TEMPLATE
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer#define TEMPLATE "XXXXXXXXXX.tmp" /* 14 characters. */
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrenceisc_result_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_mktemplate(const char *path, char *buf, size_t buflen) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (isc_file_template(path, TEMPLATE, buf, buflen));
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_result_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_template(const char *path, const char *templet, char *buf,
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer size_t buflen) {
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence char *s;
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(path != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(templet != NULL);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer REQUIRE(buf != NULL);
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer s = strrchr(templet, '\\');
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (s != NULL)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer templet = s + 1;
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer s = strrchr(path, '\\');
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (s != NULL) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if ((s - path + 1 + strlen(templet) + 1) > buflen)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (ISC_R_NOSPACE);
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer strncpy(buf, path, s - path + 1);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer buf[s - path + 1] = '\0';
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer strcat(buf, templet);
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence } else {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if ((strlen(templet) + 1) > buflen)
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence return (ISC_R_NOSPACE);
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer strcpy(buf, templet);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (ISC_R_SUCCESS);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_result_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_renameunique(const char *file, char *templet) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer int fd = -1;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer int res = 0;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer isc_result_t result = ISC_R_SUCCESS;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(file != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(templet != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer fd = mkstemp(templet);
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson if (fd == -1)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer result = isc__errno2result(errno);
e76d4c91bfadf823f04dcca1c1c5bcc14c67671dAndreas Gustafsson
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (result == ISC_R_SUCCESS) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer res = isc_file_safemovefile(file, templet);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (res != 0) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer result = isc__errno2result(errno);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer (void)unlink(templet);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (fd != -1)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer close(fd);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (result);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_result_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_openunique(char *templet, FILE **fp) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer int fd;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer FILE *f;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer isc_result_t result = ISC_R_SUCCESS;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer REQUIRE(templet != NULL);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer REQUIRE(fp != NULL && *fp == NULL);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /*
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer * Win32 does not have mkstemp. Using emulation above.
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer fd = mkstemp(templet);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (fd == -1)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer result = isc__errno2result(errno);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (result == ISC_R_SUCCESS) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer f = fdopen(fd, "w+");
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (f == NULL) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer result = isc__errno2result(errno);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer (void)remove(templet);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer (void)close(fd);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer } else
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer *fp = f;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (result);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_result_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_remove(const char *filename) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer int r;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(filename != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer r = unlink(filename);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (r == 0)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (ISC_R_SUCCESS);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer else
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (isc__errno2result(errno));
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_result_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_rename(const char *oldname, const char *newname) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer int r;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(oldname != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(newname != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer r = isc_file_safemovefile(oldname, newname);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer if (r == 0)
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (ISC_R_SUCCESS);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer else
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (isc__errno2result(errno));
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_boolean_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_exists(const char *pathname) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer struct stat stats;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(pathname != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_boolean_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_isabsolute(const char *filename) {
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(filename != NULL);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /*
c4958494a98a59ce25e9fecad76a9ab0e36cc59fDanny Mayer * Look for c:\path\... style, c:/path/... or \\computer\shar\path...
ad38e19578815fc064ea0a5f455fa8beffc99e63Danny Mayer * the UNC style file specs
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
ad38e19578815fc064ea0a5f455fa8beffc99e63Danny Mayer if ((filename[0] == '\\') && (filename[1] == '\\'))
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer return (ISC_TRUE);
911533025604975b5548d038ca09cff17c7f534eAndreas Gustafsson if (isalpha(filename[0]) && filename[1] == ':' && filename[2] == '\\')
5d9777319140d5bf62fbb5a43bc791957538c0adAndreas Gustafsson return (ISC_TRUE);
911533025604975b5548d038ca09cff17c7f534eAndreas Gustafsson if (isalpha(filename[0]) && filename[1] == ':' && filename[2] == '/')
c4958494a98a59ce25e9fecad76a9ab0e36cc59fDanny Mayer return (ISC_TRUE);
5d9777319140d5bf62fbb5a43bc791957538c0adAndreas Gustafsson return (ISC_FALSE);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_boolean_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_iscurrentdir(const char *filename) {
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(filename != NULL);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafssonisc_boolean_t
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafssonisc_file_ischdiridempotent(const char *filename) {
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson REQUIRE(filename != NULL);
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson if (isc_file_isabsolute(filename))
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson return (ISC_TRUE);
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson if (filename[0] == '\\')
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson return (ISC_TRUE);
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson if (filename[0] == '/')
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson return (ISC_TRUE);
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson if (isc_file_iscurrentdir(filename))
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson return (ISC_TRUE);
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson return (ISC_FALSE);
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson}
ba35e9924e95c45f8ae536f453b9064740e49841Andreas Gustafsson
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerconst char *
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_basename(const char *filename) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer char *s;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(filename != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer s = strrchr(filename, '\\');
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson if (s == NULL)
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson return (filename);
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson return (s + 1);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer}
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_result_t
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayerisc_file_progname(const char *filename, char *progname, size_t namelen) {
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer const char *s;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer char *p;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer size_t len;
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer REQUIRE(filename != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer REQUIRE(progname != NULL);
bb48ac4f813537a060579615a52f9c7963f5e3c4Danny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /*
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * Strip the path from the name
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer s = isc_file_basename(filename);
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer if (s == NULL) {
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer return (ISC_R_NOSPACE);
68c2ccc953059f389cefc0f8a5ce0f83be7458c9Danny Mayer }
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /*
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * Strip any and all suffixes
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer p = strchr(s, '.');
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson if (p == NULL) {
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson if (namelen <= strlen(s))
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson return (ISC_R_NOSPACE);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer strcpy(progname, s);
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson return (ISC_R_SUCCESS);
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence }
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer /*
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer * Copy the result to the buffer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer */
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer len = p - s;
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson if (len >= namelen)
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson return (ISC_R_NOSPACE);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer strncpy(progname, s, len);
e35c1bb3ecd9a6597360b9160b397c8053af69bfDanny Mayer progname[len] = '\0';
1e71bc82b8e1a95d423d8db1c136080dc034433dAndreas Gustafsson return (ISC_R_SUCCESS);
8b7304a34c751e519ede7d00b77f1f962c0a37e4David Lawrence}
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayerisc_result_t
dee69dd3aafe1339cd62928b858d18fdc0536642Andreas Gustafssonisc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer char *ptrname;
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer DWORD retval;
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer REQUIRE(filename != NULL);
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer REQUIRE(path != NULL);
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer retval = GetFullPathName(filename, pathlen, path, &ptrname);
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer /* Something went wrong in getting the path */
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer if (retval == 0)
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer return (ISC_R_NOTFOUND);
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer /* Caller needs to provide a larger buffer to contain the string */
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer if (retval >= pathlen)
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer return (ISC_R_NOSPACE);
911533025604975b5548d038ca09cff17c7f534eAndreas Gustafsson return (ISC_R_SUCCESS);
3bd4083e486d200d4f5992ac9da2dc7131c7b9aeDanny Mayer}
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayerisc_result_t
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayerisc_file_truncate(const char *filename, isc_offset_t size) {
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer int fh;
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer REQUIRE(filename != NULL && size >= 0);
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer if ((fh = open(filename, _O_RDWR | _O_BINARY)) < 0)
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer return (isc__errno2result(errno));
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer if(_chsize(fh, size) != 0) {
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer close(fh);
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer return (isc__errno2result(errno));
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer }
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer close(fh);
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer return (ISC_R_SUCCESS);
7c7b389ae01b7953982ed8827cfd6010fce6e575Danny Mayer}