1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997-2009 Oracle.  All rights reserved.
5 *
6 * $Id$
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13/*
14 * __os_seek --
15 *	Seek to a page/byte offset in the file.
16 *
17 * PUBLIC: int __os_seek __P((ENV *,
18 * PUBLIC:      DB_FH *, db_pgno_t, u_int32_t, u_int32_t));
19 */
20int
21__os_seek(env, fhp, pgno, pgsize, relative)
22	ENV *env;
23	DB_FH *fhp;
24	db_pgno_t pgno;
25	u_int32_t pgsize;
26	u_int32_t relative;
27{
28	DB_ENV *dbenv;
29	off_t offset;
30	int ret;
31
32	dbenv = env == NULL ? NULL : env->dbenv;
33
34	DB_ASSERT(env, F_ISSET(fhp, DB_FH_OPENED) && fhp->fd != -1);
35
36#if defined(HAVE_STATISTICS)
37	++fhp->seek_count;
38#endif
39
40	offset = (off_t)pgsize * pgno + relative;
41
42	if (dbenv != NULL && FLD_ISSET(dbenv->verbose, DB_VERB_FILEOPS_ALL))
43		__db_msg(env,
44		    "fileops: seek %s to %lu", fhp->name, (u_long)offset);
45
46	if (DB_GLOBAL(j_seek) != NULL)
47		ret = DB_GLOBAL(j_seek)(fhp->fd, offset, SEEK_SET);
48	else
49		RETRY_CHK((lseek(
50		    fhp->fd, offset, SEEK_SET) == -1 ? 1 : 0), ret);
51
52	if (ret == 0) {
53		fhp->pgsize = pgsize;
54		fhp->pgno = pgno;
55		fhp->offset = relative;
56	} else {
57		__db_syserr(env, ret,
58		    "seek: %lu: (%lu * %lu) + %lu", (u_long)offset,
59		    (u_long)pgno, (u_long)pgsize, (u_long)relative);
60		ret = __os_posix_err(ret);
61	}
62
63	return (ret);
64}
65