summaryrefslogtreecommitdiff
path: root/db/os_win32/os_seek.c
blob: 1837530da3e99889c0417dd5b27b80771ed18ea4 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997-2003
 *	Sleepycat Software.  All rights reserved.
 */

#include "db_config.h"

#ifndef lint
static const char revid[] = "$Id: os_seek.c,v 11.19 2003/01/08 05:34:57 bostic Exp $";
#endif /* not lint */

#include "db_int.h"

/*
 * __os_seek --
 *	Seek to a page/byte offset in the file.
 */
int
__os_seek(dbenv, fhp, pgsize, pageno, relative, isrewind, db_whence)
	DB_ENV *dbenv;
	DB_FH *fhp;
	size_t pgsize;
	db_pgno_t pageno;
	u_int32_t relative;
	int isrewind;
	DB_OS_SEEK db_whence;
{
	/* Yes, this really is how Microsoft have designed their API */
	union {
		__int64 bigint;
		struct {
			unsigned long low;
			long high;
		};
	} offset;
	int ret, whence;
	DWORD from;

	if (DB_GLOBAL(j_seek) != NULL) {
		switch (db_whence) {
		case DB_OS_SEEK_CUR:
			whence = SEEK_CUR;
			break;
		case DB_OS_SEEK_END:
			whence = SEEK_END;
			break;
		case DB_OS_SEEK_SET:
			whence = SEEK_SET;
			break;
		default:
			return (EINVAL);
		}

		ret = DB_GLOBAL(j_seek)(fhp->fd, pgsize, pageno,
		    relative, isrewind, whence);
	} else {
		switch (db_whence) {
		case DB_OS_SEEK_CUR:
			from = FILE_CURRENT;
			break;
		case DB_OS_SEEK_END:
			from = FILE_END;
			break;
		case DB_OS_SEEK_SET:
			from = FILE_BEGIN;
			break;
		default:
			return (EINVAL);
		}

		offset.bigint = (__int64)pgsize * pageno + relative;
		if (isrewind)
			offset.bigint = -offset.bigint;

		ret = (SetFilePointer(fhp->handle,
		    offset.low, &offset.high, from) == (DWORD) - 1) ?
		    __os_win32_errno() : 0;
	}

	if (ret == 0) {
		fhp->pgsize = pgsize;
		fhp->pgno = pageno;
		fhp->offset = relative;
	} else {
		__db_err(dbenv, "seek: %lu %d %d: %s",
		    (u_long)pgsize * pageno + relative,
		    isrewind, db_whence, strerror(ret));
	}

	return (ret);
}