1N/A/*-
1N/A * See the file LICENSE for redistribution information.
1N/A *
1N/A * Copyright (c) 1997, 1998
1N/A * Sleepycat Software. All rights reserved.
1N/A */
1N/A
1N/A#include "config.h"
1N/A
1N/A#ifndef lint
1N/Astatic const char sccsid[] = "@(#)os_rw.c 10.11 (Sleepycat) 10/12/98";
1N/A#endif /* not lint */
1N/A
1N/A#ifndef NO_SYSTEM_INCLUDES
1N/A#include <sys/types.h>
1N/A
1N/A#include <errno.h>
1N/A#include <unistd.h>
1N/A#endif
1N/A
1N/A#include "db_int.h"
1N/A#include "os_jump.h"
1N/A
1N/A/*
1N/A * __os_io --
1N/A * Do an I/O.
1N/A *
1N/A * PUBLIC: int __os_io __P((DB_IO *, int, ssize_t *));
1N/A */
1N/Aint
1N/A__os_io(db_iop, op, niop)
1N/A DB_IO *db_iop;
1N/A int op;
1N/A ssize_t *niop;
1N/A{
1N/A int ret;
1N/A
1N/A#ifdef HAVE_PREAD
1N/A switch (op) {
1N/A case DB_IO_READ:
1N/A if (__db_jump.j_read != NULL)
1N/A goto slow;
1N/A *niop = pread(db_iop->fd_io, db_iop->buf,
1N/A db_iop->bytes, (off_t)db_iop->pgno * db_iop->pagesize);
1N/A break;
1N/A case DB_IO_WRITE:
1N/A if (__db_jump.j_write != NULL)
1N/A goto slow;
1N/A *niop = pwrite(db_iop->fd_io, db_iop->buf,
1N/A db_iop->bytes, (off_t)db_iop->pgno * db_iop->pagesize);
1N/A break;
1N/A }
1N/A if (*niop == db_iop->bytes)
1N/A return (0);
1N/Aslow:
1N/A#endif
1N/A if (db_iop->mutexp != NULL)
1N/A (void)__db_mutex_lock(db_iop->mutexp, db_iop->fd_lock);
1N/A
1N/A if ((ret = __os_seek(db_iop->fd_io,
1N/A db_iop->pagesize, db_iop->pgno, 0, 0, SEEK_SET)) != 0)
1N/A goto err;
1N/A switch (op) {
1N/A case DB_IO_READ:
1N/A ret =
1N/A __os_read(db_iop->fd_io, db_iop->buf, db_iop->bytes, niop);
1N/A break;
1N/A case DB_IO_WRITE:
1N/A ret =
1N/A __os_write(db_iop->fd_io, db_iop->buf, db_iop->bytes, niop);
1N/A break;
1N/A }
1N/A
1N/Aerr: if (db_iop->mutexp != NULL)
1N/A (void)__db_mutex_unlock(db_iop->mutexp, db_iop->fd_lock);
1N/A
1N/A return (ret);
1N/A
1N/A}
1N/A
1N/A/*
1N/A * __os_read --
1N/A * Read from a file handle.
1N/A *
1N/A * PUBLIC: int __os_read __P((int, void *, size_t, ssize_t *));
1N/A */
1N/Aint
1N/A__os_read(fd, addr, len, nrp)
1N/A int fd;
1N/A void *addr;
1N/A size_t len;
1N/A ssize_t *nrp;
1N/A{
1N/A size_t offset;
1N/A ssize_t nr;
1N/A u_int8_t *taddr;
1N/A
1N/A for (taddr = addr,
1N/A offset = 0; offset < len; taddr += nr, offset += nr) {
1N/A if ((nr = __db_jump.j_read != NULL ?
1N/A __db_jump.j_read(fd, taddr, len - offset) :
1N/A read(fd, taddr, len - offset)) < 0)
1N/A return (errno);
1N/A if (nr == 0)
1N/A break;
1N/A }
1N/A *nrp = taddr - (u_int8_t *)addr;
1N/A return (0);
1N/A}
1N/A
1N/A/*
1N/A * __os_write --
1N/A * Write to a file handle.
1N/A *
1N/A * PUBLIC: int __os_write __P((int, void *, size_t, ssize_t *));
1N/A */
1N/Aint
1N/A__os_write(fd, addr, len, nwp)
1N/A int fd;
1N/A void *addr;
1N/A size_t len;
1N/A ssize_t *nwp;
1N/A{
1N/A size_t offset;
1N/A ssize_t nw;
1N/A u_int8_t *taddr;
1N/A
1N/A for (taddr = addr,
1N/A offset = 0; offset < len; taddr += nw, offset += nw)
1N/A if ((nw = __db_jump.j_write != NULL ?
1N/A __db_jump.j_write(fd, taddr, len - offset) :
1N/A write(fd, taddr, len - offset)) < 0)
1N/A return (errno);
1N/A *nwp = len;
1N/A return (0);
1N/A}