about summary refs log tree commit diff
path: root/db2/os/os_stat.c
blob: 73600b633668decc78f2d168a7cb8c66c89eb2d1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997
 *	Sleepycat Software.  All rights reserved.
 */

#include "config.h"

#ifndef lint
static const char sccsid[] = "@(#)os_stat.c	10.11 (Sleepycat) 1/8/98";
#endif /* not lint */

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

#include <errno.h>
#include <string.h>
#endif

#include "db_int.h"
#include "common_ext.h"

/*
 * __os_exists --
 *	Return if the file exists.
 *
 * PUBLIC: int __os_exists __P((const char *, int *));
 */
int
__os_exists(path, isdirp)
	const char *path;
	int *isdirp;
{
	struct stat sb;

	if (stat(path, &sb) != 0)
		return (errno);
	if (isdirp != NULL)
		*isdirp = S_ISDIR(sb.st_mode);
	return (0);
}

/*
 * __os_ioinfo --
 *	Return file size and I/O size; abstracted to make it easier
 *	to replace.
 *
 * PUBLIC: int __os_ioinfo
 * PUBLIC:    __P((const char *, int, u_int32_t *, u_int32_t *, u_int32_t *));
 */
int
__os_ioinfo(path, fd, mbytesp, bytesp, iosizep)
	const char *path;
	int fd;
	u_int32_t *mbytesp, *bytesp, *iosizep;
{
	struct stat sb;

	COMPQUIET(path, NULL);

	if (fstat(fd, &sb) == -1)
		return (errno);

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

	/* Return the underlying filesystem blocksize, if available. */
#ifdef HAVE_ST_BLKSIZE
	if (iosizep != NULL)
		*iosizep = sb.st_blksize;
#else
	if (iosizep != NULL)
		*iosizep = DB_DEF_IOSIZE;
#endif
	return (0);
}