xref: /illumos-gate/usr/src/cmd/sendmail/db/os/os_rw.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 1997, 1998
5  *	Sleepycat Software.  All rights reserved.
6  */
7 
8 #include "config.h"
9 
10 #ifndef lint
11 static const char sccsid[] = "@(#)os_rw.c	10.11 (Sleepycat) 10/12/98";
12 #endif /* not lint */
13 
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
16 
17 #include <errno.h>
18 #include <unistd.h>
19 #endif
20 
21 #include "db_int.h"
22 #include "os_jump.h"
23 
24 /*
25  * __os_io --
26  *	Do an I/O.
27  *
28  * PUBLIC: int __os_io __P((DB_IO *, int, ssize_t *));
29  */
30 int
31 __os_io(db_iop, op, niop)
32 	DB_IO *db_iop;
33 	int op;
34 	ssize_t *niop;
35 {
36 	int ret;
37 
38 #ifdef HAVE_PREAD
39 	switch (op) {
40 	case DB_IO_READ:
41 		if (__db_jump.j_read != NULL)
42 			goto slow;
43 		*niop = pread(db_iop->fd_io, db_iop->buf,
44 		    db_iop->bytes, (off_t)db_iop->pgno * db_iop->pagesize);
45 		break;
46 	case DB_IO_WRITE:
47 		if (__db_jump.j_write != NULL)
48 			goto slow;
49 		*niop = pwrite(db_iop->fd_io, db_iop->buf,
50 		    db_iop->bytes, (off_t)db_iop->pgno * db_iop->pagesize);
51 		break;
52 	}
53 	if (*niop == db_iop->bytes)
54 		return (0);
55 slow:
56 #endif
57 	if (db_iop->mutexp != NULL)
58 		(void)__db_mutex_lock(db_iop->mutexp, db_iop->fd_lock);
59 
60 	if ((ret = __os_seek(db_iop->fd_io,
61 	    db_iop->pagesize, db_iop->pgno, 0, 0, SEEK_SET)) != 0)
62 		goto err;
63 	switch (op) {
64 	case DB_IO_READ:
65 		ret =
66 		    __os_read(db_iop->fd_io, db_iop->buf, db_iop->bytes, niop);
67 		break;
68 	case DB_IO_WRITE:
69 		ret =
70 		    __os_write(db_iop->fd_io, db_iop->buf, db_iop->bytes, niop);
71 		break;
72 	}
73 
74 err:	if (db_iop->mutexp != NULL)
75 		(void)__db_mutex_unlock(db_iop->mutexp, db_iop->fd_lock);
76 
77 	return (ret);
78 
79 }
80 
81 /*
82  * __os_read --
83  *	Read from a file handle.
84  *
85  * PUBLIC: int __os_read __P((int, void *, size_t, ssize_t *));
86  */
87 int
88 __os_read(fd, addr, len, nrp)
89 	int fd;
90 	void *addr;
91 	size_t len;
92 	ssize_t *nrp;
93 {
94 	size_t offset;
95 	ssize_t nr;
96 	u_int8_t *taddr;
97 
98 	for (taddr = addr,
99 	    offset = 0; offset < len; taddr += nr, offset += nr) {
100 		if ((nr = __db_jump.j_read != NULL ?
101 		    __db_jump.j_read(fd, taddr, len - offset) :
102 		    read(fd, taddr, len - offset)) < 0)
103 			return (errno);
104 		if (nr == 0)
105 			break;
106 	}
107 	*nrp = taddr - (u_int8_t *)addr;
108 	return (0);
109 }
110 
111 /*
112  * __os_write --
113  *	Write to a file handle.
114  *
115  * PUBLIC: int __os_write __P((int, void *, size_t, ssize_t *));
116  */
117 int
118 __os_write(fd, addr, len, nwp)
119 	int fd;
120 	void *addr;
121 	size_t len;
122 	ssize_t *nwp;
123 {
124 	size_t offset;
125 	ssize_t nw;
126 	u_int8_t *taddr;
127 
128 	for (taddr = addr,
129 	    offset = 0; offset < len; taddr += nw, offset += nw)
130 		if ((nw = __db_jump.j_write != NULL ?
131 		    __db_jump.j_write(fd, taddr, len - offset) :
132 		    write(fd, taddr, len - offset)) < 0)
133 			return (errno);
134 	*nwp = len;
135 	return (0);
136 }
137