file.c 15.8 KB
Newer Older
1
/*
Tinderbox User's avatar
Tinderbox User committed
2
 * Copyright (C) 2004, 2005, 2007, 2009, 2011-2014  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 2000-2002  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
6 7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
16 17
 */

18 19 20 21 22 23 24 25 26 27 28 29
/*
 * Portions Copyright (c) 1987, 1993
 *      The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
30
 * 3. Neither the name of the University nor the names of its contributors
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

Mark Andrews's avatar
Mark Andrews committed
47
/* $Id$ */
48 49

/*! \file */
David Lawrence's avatar
David Lawrence committed
50

David Lawrence's avatar
David Lawrence committed
51 52
#include <config.h>

53
#include <errno.h>
54
#include <fcntl.h>
55
#include <limits.h>
56
#include <stdlib.h>
57 58
#include <time.h>		/* Required for utimes on some platforms. */
#include <unistd.h>		/* Required for mkstemp on NetBSD. */
59

60

61
#include <sys/stat.h>
62
#include <sys/time.h>
63

Evan Hunt's avatar
Evan Hunt committed
64 65 66 67
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif

68
#include <isc/dir.h>
69
#include <isc/file.h>
Mark Andrews's avatar
Mark Andrews committed
70
#include <isc/log.h>
71
#include <isc/mem.h>
72
#include <isc/random.h>
73
#include <isc/string.h>
David Lawrence's avatar
David Lawrence committed
74
#include <isc/time.h>
Bob Halley's avatar
Bob Halley committed
75
#include <isc/util.h>
76

77
#include "errno2result.h"
78

79 80 81
/*
 * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
 * it might be good to provide a mechanism that allows for the results
82 83
 * of a previous stat() to be used again without having to do another stat,
 * such as perl's mechanism of using "_" in place of a file name to indicate
84 85 86 87 88 89
 * that the results of the last stat should be used.  But then you get into
 * annoying MP issues.   BTW, Win32 has stat().
 */
static isc_result_t
file_stats(const char *file, struct stat *stats) {
	isc_result_t result = ISC_R_SUCCESS;
90

91 92 93
	REQUIRE(file != NULL);
	REQUIRE(stats != NULL);

94
	if (stat(file, stats) != 0)
95
		result = isc__errno2result(errno);
96

97 98 99
	return (result);
}

100 101 102
static isc_result_t
fd_stats(int fd, struct stat *stats) {
	isc_result_t result = ISC_R_SUCCESS;
Tinderbox User's avatar
Tinderbox User committed
103

104
	REQUIRE(stats != NULL);
Tinderbox User's avatar
Tinderbox User committed
105

106 107
	if (fstat(fd, stats) != 0)
		result = isc__errno2result(errno);
Tinderbox User's avatar
Tinderbox User committed
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	return (result);
}

isc_result_t
isc_file_getsizefd(int fd, off_t *size) {
	isc_result_t result;
	struct stat stats;

	REQUIRE(size != NULL);

	result = fd_stats(fd, &stats);

	if (result == ISC_R_SUCCESS)
		*size = stats.st_size;

	return (result);
}

Curtis Blackburn's avatar
Curtis Blackburn committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140
isc_result_t
isc_file_mode(const char *file, mode_t *modep) {
	isc_result_t result;
	struct stat stats;

	REQUIRE(modep != NULL);

	result = file_stats(file, &stats);
	if (result == ISC_R_SUCCESS)
		*modep = (stats.st_mode & 07777);

	return (result);
}

141 142 143 144 145
isc_result_t
isc_file_getmodtime(const char *file, isc_time_t *time) {
	isc_result_t result;
	struct stat stats;

146 147
	REQUIRE(file != NULL);
	REQUIRE(time != NULL);
148 149 150 151 152 153 154 155 156 157 158 159 160

	result = file_stats(file, &stats);

	if (result == ISC_R_SUCCESS)
		/*
		 * XXXDCL some operating systems provide nanoseconds, too,
		 * such as BSD/OS via st_mtimespec.
		 */
		isc_time_set(time, stats.st_mtime, 0);

	return (result);
}

161 162 163 164 165 166
isc_result_t
isc_file_getsize(const char *file, off_t *size) {
	isc_result_t result;
	struct stat stats;

	REQUIRE(file != NULL);
167
	REQUIRE(size != NULL);
168 169 170 171 172 173 174 175 176

	result = file_stats(file, &stats);

	if (result == ISC_R_SUCCESS)
		*size = stats.st_size;

	return (result);
}

177 178 179 180 181 182
isc_result_t
isc_file_settime(const char *file, isc_time_t *time) {
	struct timeval times[2];

	REQUIRE(file != NULL && time != NULL);

183 184 185
	/*
	 * tv_sec is at least a 32 bit quantity on all platforms we're
	 * dealing with, but it is signed on most (all?) of them,
186 187 188 189 190 191
	 * so we need to make sure the high bit isn't set.  This unfortunately
	 * loses when either:
	 *   * tv_sec becomes a signed 64 bit integer but long is 32 bits
	 *	and isc_time_seconds > LONG_MAX, or
	 *   * isc_time_seconds is changed to be > 32 bits but long is 32 bits
	 *      and isc_time_seconds has at least 33 significant bits.
192
	 */
193
	times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(time);
194

195
	/*
196
	 * Here is the real check for the high bit being set.
197 198
	 */
	if ((times[0].tv_sec &
199
	     (1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
200 201 202 203 204 205 206 207 208 209
		return (ISC_R_RANGE);

	/*
	 * isc_time_nanoseconds guarantees a value that divided by 1000 will
	 * fit into the minimum possible size tv_usec field.  Unfortunately,
	 * we don't know what that type is so can't cast directly ... but
	 * we can at least cast to signed so the IRIX compiler shuts up.
	 */
	times[0].tv_usec = times[1].tv_usec =
		(isc_int32_t)(isc_time_nanoseconds(time) / 1000);
210 211 212

	if (utimes(file, times) < 0)
		return (isc__errno2result(errno));
213

214 215 216
	return (ISC_R_SUCCESS);
}

217
#undef TEMPLATE
218
#define TEMPLATE "tmp-XXXXXXXXXX" /*%< 14 characters. */
219 220 221

isc_result_t
isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
222 223 224 225
	return (isc_file_template(path, TEMPLATE, buf, buflen));
}

isc_result_t
226
isc_file_template(const char *path, const char *templet, char *buf,
227
			size_t buflen) {
228 229
	char *s;

230 231
	REQUIRE(path != NULL);
	REQUIRE(templet != NULL);
232 233
	REQUIRE(buf != NULL);

234
	s = strrchr(templet, '/');
235
	if (s != NULL)
236
		templet = s + 1;
237

238 239 240
	s = strrchr(path, '/');

	if (s != NULL) {
241
		if ((s - path + 1 + strlen(templet) + 1) > buflen)
242
			return (ISC_R_NOSPACE);
243

244 245
		strncpy(buf, path, s - path + 1);
		buf[s - path + 1] = '\0';
246
		strcat(buf, templet);
247
	} else {
248
		if ((strlen(templet) + 1) > buflen)
249
			return (ISC_R_NOSPACE);
250

251
		strcpy(buf, templet);
252
	}
253

254 255
	return (ISC_R_SUCCESS);
}
256

257 258 259
static char alphnum[] =
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

260 261
isc_result_t
isc_file_renameunique(const char *file, char *templet) {
262 263 264
	char *x;
	char *cp;
	isc_uint32_t which;
265

266 267 268
	REQUIRE(file != NULL);
	REQUIRE(templet != NULL);

269 270 271 272 273 274 275
	cp = templet;
	while (*cp != '\0')
		cp++;
	if (cp == templet)
		return (ISC_R_FAILURE);

	x = cp--;
276
	while (cp >= templet && *cp == 'X') {
277 278 279
		isc_random_get(&which);
		*cp = alphnum[which % (sizeof(alphnum) - 1)];
		x = cp--;
280
	}
281 282 283 284 285 286 287 288 289 290 291 292 293 294
	while (link(file, templet) == -1) {
		if (errno != EEXIST)
			return (isc__errno2result(errno));
		for (cp = x;;) {
			char *t;
			if (*cp == '\0')
				return (ISC_R_FAILURE);
			t = strchr(alphnum, *cp);
			if (t == NULL || *++t == '\0')
				*cp++ = alphnum[0];
			else {
				*cp = *t;
				break;
			}
295
		}
296
	}
Mark Andrews's avatar
Mark Andrews committed
297 298 299
	if (unlink(file) < 0)
		if (errno != ENOENT)
			return (isc__errno2result(errno));
300
	return (ISC_R_SUCCESS);
301 302
}

303
isc_result_t
304
isc_file_openunique(char *templet, FILE **fp) {
305 306 307 308 309 310 311 312 313 314 315 316
	int mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
	return (isc_file_openuniquemode(templet, mode, fp));
}

isc_result_t
isc_file_openuniqueprivate(char *templet, FILE **fp) {
	int mode = S_IWUSR|S_IRUSR;
	return (isc_file_openuniquemode(templet, mode, fp));
}

isc_result_t
isc_file_openuniquemode(char *templet, int mode, FILE **fp) {
317 318 319
	int fd;
	FILE *f;
	isc_result_t result = ISC_R_SUCCESS;
320 321 322
	char *x;
	char *cp;
	isc_uint32_t which;
323

324
	REQUIRE(templet != NULL);
325 326
	REQUIRE(fp != NULL && *fp == NULL);

327 328 329 330 331
	cp = templet;
	while (*cp != '\0')
		cp++;
	if (cp == templet)
		return (ISC_R_FAILURE);
332

333
	x = cp--;
334
	while (cp >= templet && *cp == 'X') {
335 336 337 338
		isc_random_get(&which);
		*cp = alphnum[which % (sizeof(alphnum) - 1)];
		x = cp--;
	}
339

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

	while ((fd = open(templet, O_RDWR|O_CREAT|O_EXCL, mode)) == -1) {
		if (errno != EEXIST)
			return (isc__errno2result(errno));
		for (cp = x;;) {
			char *t;
			if (*cp == '\0')
				return (ISC_R_FAILURE);
			t = strchr(alphnum, *cp);
			if (t == NULL || *++t == '\0')
				*cp++ = alphnum[0];
			else {
				*cp = *t;
				break;
			}
		}
356
	}
357 358 359
	f = fdopen(fd, "w+");
	if (f == NULL) {
		result = isc__errno2result(errno);
Mark Andrews's avatar
Mark Andrews committed
360 361 362 363 364
		if (remove(templet) < 0) {
			isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
				      ISC_LOGMODULE_FILE, ISC_LOG_ERROR,
				      "remove '%s': failed", templet);
		}
365 366 367
		(void)close(fd);
	} else
		*fp = f;
368 369 370

	return (result);
}
371

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
isc_result_t
isc_file_bopenunique(char *templet, FILE **fp) {
	int mode = S_IWUSR|S_IRUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
	return (isc_file_openuniquemode(templet, mode, fp));
}

isc_result_t
isc_file_bopenuniqueprivate(char *templet, FILE **fp) {
	int mode = S_IWUSR|S_IRUSR;
	return (isc_file_openuniquemode(templet, mode, fp));
}

isc_result_t
isc_file_bopenuniquemode(char *templet, int mode, FILE **fp) {
	return (isc_file_openuniquemode(templet, mode, fp));
}

389 390 391
isc_result_t
isc_file_remove(const char *filename) {
	int r;
392

393 394
	REQUIRE(filename != NULL);

395 396 397 398
	r = unlink(filename);
	if (r == 0)
		return (ISC_R_SUCCESS);
	else
399
		return (isc__errno2result(errno));
400
}
401

402 403 404 405
isc_result_t
isc_file_rename(const char *oldname, const char *newname) {
	int r;

406 407 408
	REQUIRE(oldname != NULL);
	REQUIRE(newname != NULL);

409 410 411 412 413 414 415
	r = rename(oldname, newname);
	if (r == 0)
		return (ISC_R_SUCCESS);
	else
		return (isc__errno2result(errno));
}

416
isc_boolean_t
417
isc_file_exists(const char *pathname) {
418 419
	struct stat stats;

420 421
	REQUIRE(pathname != NULL);

422
	return (ISC_TF(file_stats(pathname, &stats) == ISC_R_SUCCESS));
423 424
}

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
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);
}

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
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);
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
isc_result_t
isc_file_isdirectory(const char *filename) {
	/*
	 * This function returns success if filename exists and 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);
}


478 479
isc_boolean_t
isc_file_isabsolute(const char *filename) {
480
	REQUIRE(filename != NULL);
481 482
	return (ISC_TF(filename[0] == '/'));
}
483 484 485

isc_boolean_t
isc_file_iscurrentdir(const char *filename) {
486
	REQUIRE(filename != NULL);
487 488
	return (ISC_TF(filename[0] == '.' && filename[1] == '\0'));
}
489

490 491 492 493 494 495 496 497 498 499
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);
}

500
const char *
501 502 503
isc_file_basename(const char *filename) {
	char *s;

504 505
	REQUIRE(filename != NULL);

506 507 508
	s = strrchr(filename, '/');
	if (s == NULL)
		return (filename);
509

510 511
	return (s + 1);
}
512 513 514

isc_result_t
isc_file_progname(const char *filename, char *buf, size_t buflen) {
515 516 517 518 519 520 521 522
	const char *base;
	size_t len;

	REQUIRE(filename != NULL);
	REQUIRE(buf != NULL);

	base = isc_file_basename(filename);
	len = strlen(base) + 1;
523

524 525
	if (len > buflen)
		return (ISC_R_NOSPACE);
526
	memmove(buf, base, len);
527

528 529
	return (ISC_R_SUCCESS);
}
530

531 532
/*
 * Put the absolute name of the current directory into 'dirname', which is
Automatic Updater's avatar
Automatic Updater committed
533
 * a buffer of at least 'length' characters.  End the string with the
534 535 536 537 538 539 540 541 542
 * appropriate path separator, such that the final product could be
 * concatenated with a relative pathname to make a valid pathname string.
 */
static isc_result_t
dir_current(char *dirname, size_t length) {
	char *cwd;
	isc_result_t result = ISC_R_SUCCESS;

	REQUIRE(dirname != NULL);
Mark Andrews's avatar
Mark Andrews committed
543
	REQUIRE(length > 0U);
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

	cwd = getcwd(dirname, length);

	if (cwd == NULL) {
		if (errno == ERANGE)
			result = ISC_R_NOSPACE;
		else
			result = isc__errno2result(errno);
	} else {
		if (strlen(dirname) + 1 == length)
			result = ISC_R_NOSPACE;
		else if (dirname[1] != '\0')
			strcat(dirname, "/");
	}

	return (result);
}

562 563 564
isc_result_t
isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
	isc_result_t result;
565
	result = dir_current(path, pathlen);
566 567 568 569 570 571 572
	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);
}
Mark Andrews's avatar
Mark Andrews committed
573 574 575 576 577

isc_result_t
isc_file_truncate(const char *filename, isc_offset_t size) {
	isc_result_t result = ISC_R_SUCCESS;

Automatic Updater's avatar
Automatic Updater committed
578
	if (truncate(filename, size) < 0)
Mark Andrews's avatar
Mark Andrews committed
579 580 581
		result = isc__errno2result(errno);
	return (result);
}
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

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);
}
618 619 620 621 622 623

isc_result_t
isc_file_splitpath(isc_mem_t *mctx, char *path, char **dirname, char **basename)
{
	char *dir, *file, *slash;

Evan Hunt's avatar
Evan Hunt committed
624 625 626
	if (path == NULL)
		return (ISC_R_INVALIDFILE);

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
	slash = strrchr(path, '/');

	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);
}
Evan Hunt's avatar
Evan Hunt committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669

void *
isc_file_mmap(void *addr, size_t len, int prot,
	      int flags, int fd, off_t offset)
{
#ifdef HAVE_MMAP
	return (mmap(addr, len, prot, flags, fd, offset));
#else
	void *buf;
	ssize_t ret;
	off_t end;

	UNUSED(addr);
	UNUSED(prot);
	UNUSED(flags);
Tinderbox User's avatar
Tinderbox User committed
670

Evan Hunt's avatar
Evan Hunt committed
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
	end = lseek(fd, 0, SEEK_END);
	lseek(fd, offset, SEEK_SET);
	if (end - offset < (off_t) len)
		len = end - offset;

	buf = malloc(len);
	ret = read(fd, buf, len);
	if (ret != (ssize_t) len) {
		free(buf);
		buf = NULL;
	}

	return (buf);
#endif
}

int
isc_file_munmap(void *addr, size_t len) {
#ifdef HAVE_MMAP
	return (munmap(addr, len));
#else
	UNUSED(len);

	free(addr);
	return (0);
#endif
}