00001 /*- 00002 * See the file LICENSE for redistribution information. 00003 * 00004 * Copyright (c) 1997, 1998, 1999, 2000 00005 * Sleepycat Software. All rights reserved. 00006 */ 00007 00008 #include "config.h" 00009 00010 #ifndef lint 00011 static const char revid[] = "$Id: os__rw_8c-source.html,v 1.1 2008年06月08日 10:21:29 sebdiaz Exp $"; 00012 #endif /* not lint */ 00013 00014 #ifndef NO_SYSTEM_INCLUDES 00015 #include <sys/types.h> 00016 00017 #include <string.h> 00018 #include <unistd.h> 00019 #endif 00020 00021 #include "db_int.h" 00022 #include "os_jump.h" 00023 00024 /* 00025 * CDB___os_io -- 00026 * Do an I/O. 00027 * 00028 * PUBLIC: int CDB___os_io __P((DB_ENV *, DB_IO *, int, size_t *)); 00029 */ 00030 int 00031 CDB___os_io(dbenv, db_iop, op, niop) 00032 DB_ENV *dbenv; 00033 DB_IO *db_iop; 00034 int op; 00035 size_t *niop; 00036 { 00037 int ret; 00038 00039 #if defined(HAVE_PREAD) && defined(HAVE_PWRITE) 00040 switch (op) { 00041 case DB_IO_READ: 00042 if (CDB___db_jump.j_read != NULL) 00043 goto slow; 00044 *niop = pread(db_iop->fhp->fd, db_iop->buf, 00045 db_iop->bytes, (off_t)db_iop->pgno * db_iop->pagesize); 00046 break; 00047 case DB_IO_WRITE: 00048 if (CDB___db_jump.j_write != NULL) 00049 goto slow; 00050 *niop = pwrite(db_iop->fhp->fd, db_iop->buf, 00051 db_iop->bytes, (off_t)db_iop->pgno * db_iop->pagesize); 00052 break; 00053 } 00054 if (*niop == (size_t)db_iop->bytes) 00055 return (0); 00056 slow: 00057 #endif 00058 MUTEX_THREAD_LOCK(db_iop->mutexp); 00059 00060 if ((ret = CDB___os_seek(dbenv, db_iop->fhp, 00061 db_iop->pagesize, db_iop->pgno, 0, 0, DB_OS_SEEK_SET)) != 0) 00062 goto err; 00063 switch (op) { 00064 case DB_IO_READ: 00065 ret = CDB___os_read(dbenv, 00066 db_iop->fhp, db_iop->buf, db_iop->bytes, niop); 00067 break; 00068 case DB_IO_WRITE: 00069 ret = CDB___os_write(dbenv, 00070 db_iop->fhp, db_iop->buf, db_iop->bytes, niop); 00071 break; 00072 } 00073 00074 err: MUTEX_THREAD_UNLOCK(db_iop->mutexp); 00075 00076 return (ret); 00077 00078 } 00079 00080 /* 00081 * CDB___os_read -- 00082 * Read from a file handle. 00083 * 00084 * PUBLIC: int CDB___os_read __P((DB_ENV *, DB_FH *, void *, size_t, size_t *)); 00085 */ 00086 int 00087 CDB___os_read(dbenv, fhp, addr, len, nrp) 00088 DB_ENV *dbenv; 00089 DB_FH *fhp; 00090 void *addr; 00091 size_t len; 00092 size_t *nrp; 00093 { 00094 size_t offset; 00095 ssize_t nr; 00096 int ret; 00097 u_int8_t *taddr; 00098 00099 for (taddr = addr, 00100 offset = 0; offset < len; taddr += nr, offset += nr) { 00101 if ((nr = CDB___db_jump.j_read != NULL ? 00102 CDB___db_jump.j_read(fhp->fd, taddr, len - offset) : 00103 read(fhp->fd, taddr, len - offset)) < 0) { 00104 ret = CDB___os_get_errno(); 00105 CDB___db_err(dbenv, "read: 0x%x, %lu: %s", taddr, 00106 (u_long)len-offset, strerror(ret)); 00107 return (ret); 00108 } 00109 if (nr == 0) 00110 break; 00111 } 00112 *nrp = taddr - (u_int8_t *)addr; 00113 return (0); 00114 } 00115 00116 /* 00117 * CDB___os_write -- 00118 * Write to a file handle. 00119 * 00120 * PUBLIC: int CDB___os_write __P((DB_ENV *, DB_FH *, void *, size_t, size_t *)); 00121 */ 00122 int 00123 CDB___os_write(dbenv, fhp, addr, len, nwp) 00124 DB_ENV *dbenv; 00125 DB_FH *fhp; 00126 void *addr; 00127 size_t len; 00128 size_t *nwp; 00129 { 00130 size_t offset; 00131 ssize_t nw; 00132 int ret; 00133 u_int8_t *taddr; 00134 00135 for (taddr = addr, 00136 offset = 0; offset < len; taddr += nw, offset += nw) 00137 if ((nw = CDB___db_jump.j_write != NULL ? 00138 CDB___db_jump.j_write(fhp->fd, taddr, len - offset) : 00139 write(fhp->fd, taddr, len - offset)) < 0){ 00140 ret = CDB___os_get_errno(); 00141 CDB___db_err(dbenv, "write: 0x%x, %lu: %s", taddr, 00142 (u_long)len-offset, strerror(ret)); 00143 return (ret); 00144 } 00145 *nwp = len; 00146 return (0); 00147 }