Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

os_seek.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: os_seek.c,v 12.1 2005/06/16 20:23:30 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #include "db_int.h"
00013 
00014 /*
00015  * __os_seek --
00016  *      Seek to a page/byte offset in the file.
00017  */
00018 int
00019 __os_seek(dbenv, fhp, pgsize, pageno, relative, isrewind, db_whence)
00020         DB_ENV *dbenv;
00021         DB_FH *fhp;
00022         u_int32_t pgsize;
00023         db_pgno_t pageno;
00024         u_int32_t relative;
00025         int isrewind;
00026         DB_OS_SEEK db_whence;
00027 {
00028         /* Yes, this really is how Microsoft have designed their API */
00029         union {
00030                 __int64 bigint;
00031                 struct {
00032                         unsigned long low;
00033                         long high;
00034                 };
00035         } offbytes;
00036         off_t offset;
00037         int ret, whence;
00038         DWORD from;
00039 
00040         offset = (off_t)pgsize * pageno + relative;
00041         if (isrewind)
00042                 offset = -offset;
00043 
00044         if (DB_GLOBAL(j_seek) != NULL) {
00045                 switch (db_whence) {
00046                 case DB_OS_SEEK_CUR:
00047                         whence = SEEK_CUR;
00048                         break;
00049                 case DB_OS_SEEK_END:
00050                         whence = SEEK_END;
00051                         break;
00052                 case DB_OS_SEEK_SET:
00053                         whence = SEEK_SET;
00054                         break;
00055                 default:
00056                         return (EINVAL);
00057                 }
00058 
00059                 ret = DB_GLOBAL(j_seek)(fhp->fd, offset, whence);
00060         } else {
00061                 switch (db_whence) {
00062                 case DB_OS_SEEK_CUR:
00063                         from = FILE_CURRENT;
00064                         break;
00065                 case DB_OS_SEEK_END:
00066                         from = FILE_END;
00067                         break;
00068                 case DB_OS_SEEK_SET:
00069                         from = FILE_BEGIN;
00070                         break;
00071                 default:
00072                         return (EINVAL);
00073                 }
00074 
00075                 offbytes.bigint = offset;
00076                 ret = (SetFilePointer(fhp->handle,
00077                     offbytes.low, &offbytes.high, from) == (DWORD) - 1) ?
00078                     __os_get_errno() : 0;
00079         }
00080 
00081         if (ret == 0) {
00082                 fhp->pgsize = pgsize;
00083                 fhp->pgno = pageno;
00084                 fhp->offset = relative;
00085         } else {
00086                 __db_err(dbenv, "seek: %lu %d %d: %s",
00087                     (u_long)pgsize * pageno + relative,
00088                     isrewind, db_whence, strerror(ret));
00089         }
00090 
00091         return (ret);
00092 }

Generated on Sun Dec 25 12:14:42 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2