os_stat.c 2 KB
Newer Older
unknown's avatar
unknown committed
1 2 3
/*-
 * See the file LICENSE for redistribution information.
 *
unknown's avatar
unknown committed
4
 * Copyright (c) 1997-2005
unknown's avatar
unknown committed
5
 *	Sleepycat Software.  All rights reserved.
unknown's avatar
unknown committed
6
 *
unknown's avatar
unknown committed
7
 * $Id: os_stat.c,v 12.1 2005/06/16 20:23:31 bostic Exp $
unknown's avatar
unknown committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 */

#include "db_config.h"

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <sys/stat.h>

#include <string.h>
#endif

#include "db_int.h"

/*
 * __os_exists --
 *	Return if the file exists.
 */
int
__os_exists(path, isdirp)
	const char *path;
	int *isdirp;
{
	int ret;
	DWORD attrs;
unknown's avatar
unknown committed
32
	_TCHAR *tpath;
unknown's avatar
unknown committed
33 34 35 36

	if (DB_GLOBAL(j_exists) != NULL)
		return (DB_GLOBAL(j_exists)(path, isdirp));

unknown's avatar
unknown committed
37
	TO_TSTRING(NULL, path, tpath, ret);
unknown's avatar
unknown committed
38 39 40
	if (ret != 0)
		return (ret);

unknown's avatar
unknown committed
41 42 43 44
	RETRY_CHK(
	    ((attrs = GetFileAttributes(tpath)) == (DWORD)-1 ? 1 : 0), ret);

	if (ret == 0 && isdirp != NULL)
unknown's avatar
unknown committed
45 46
		*isdirp = (attrs & FILE_ATTRIBUTE_DIRECTORY);

unknown's avatar
unknown committed
47 48
	FREE_STRING(NULL, tpath);
	return (ret);
unknown's avatar
unknown committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
}

/*
 * __os_ioinfo --
 *	Return file size and I/O size; abstracted to make it easier
 *	to replace.
 */
int
__os_ioinfo(dbenv, path, fhp, mbytesp, bytesp, iosizep)
	DB_ENV *dbenv;
	const char *path;
	DB_FH *fhp;
	u_int32_t *mbytesp, *bytesp, *iosizep;
{
	int ret;
	BY_HANDLE_FILE_INFORMATION bhfi;
	unsigned __int64 filesize;

	if (DB_GLOBAL(j_ioinfo) != NULL)
		return (DB_GLOBAL(j_ioinfo)(path,
		    fhp->fd, mbytesp, bytesp, iosizep));

unknown's avatar
unknown committed
71 72
	RETRY_CHK((!GetFileInformationByHandle(fhp->handle, &bhfi)), ret);
	if (ret != 0) {
unknown's avatar
unknown committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86
		__db_err(dbenv,
		    "GetFileInformationByHandle: %s", strerror(ret));
		return (ret);
	}

	filesize = ((unsigned __int64)bhfi.nFileSizeHigh << 32) +
	    bhfi.nFileSizeLow;

	/* Return the size of the file. */
	if (mbytesp != NULL)
		*mbytesp = (u_int32_t)(filesize / MEGABYTE);
	if (bytesp != NULL)
		*bytesp = (u_int32_t)(filesize % MEGABYTE);

unknown's avatar
unknown committed
87 88 89 90 91 92
	/*
	 * The filesystem blocksize is not easily available.  In particular,
	 * the values returned by GetDiskFreeSpace() are not very helpful
	 * (NTFS volumes often report 512B clusters, which are too small to
	 * be a useful default).
	 */
unknown's avatar
unknown committed
93 94 95 96
	if (iosizep != NULL)
		*iosizep = DB_DEF_IOSIZE;
	return (0);
}