aboutsummaryrefslogtreecommitdiff
path: root/src/os_unix.c
diff options
context:
space:
mode:
authordrh <drh@noemail.net>2006-03-23 22:42:20 +0000
committerdrh <drh@noemail.net>2006-03-23 22:42:20 +0000
commitb912b2889c830409071a0b8461d8117ae3f18661 (patch)
tree25c8b79cb20a9a75b4d51af58982e17cb04c4993 /src/os_unix.c
parent4eeb1ffa97276117a323c4a97f653d03b37afaf2 (diff)
downloadsqlite-b912b2889c830409071a0b8461d8117ae3f18661.tar.gz
sqlite-b912b2889c830409071a0b8461d8117ae3f18661.zip
Use the pread()/pwrite() interface on Posix if compiled with -DUSE_PREAD=1.
Note that on Linux this is slower and does not work for large files. (CVS 3147) FossilOrigin-Name: 5a24f61981df4d8b696f03372eba2d37228906d9
Diffstat (limited to 'src/os_unix.c')
-rw-r--r--src/os_unix.c45
1 files changed, 42 insertions, 3 deletions
diff --git a/src/os_unix.c b/src/os_unix.c
index c61efb81e..6dcc43ebe 100644
--- a/src/os_unix.c
+++ b/src/os_unix.c
@@ -80,6 +80,7 @@ struct unixFile {
unsigned char isOpen; /* True if needs to be closed */
unsigned char fullSync; /* Use F_FULLSYNC if available */
int dirfd; /* File descriptor for the directory */
+ i64 offset; /* Seek offset */
#ifdef SQLITE_UNIX_THREADS
pthread_t tid; /* The thread that "owns" this OsFile */
#endif
@@ -905,6 +906,24 @@ int sqlite3UnixIsDirWritable(char *zBuf){
}
/*
+** Seek to the offset in id->offset then read cnt bytes into pBuf.
+** Return the number of bytes actually read. Update the offset.
+*/
+static int seekAndRead(unixFile *id, void *pBuf, int cnt){
+ int got;
+#ifdef USE_PREAD
+ got = pread(id->h, pBuf, cnt, id->offset);
+#else
+ lseek(id->h, id->offset, SEEK_SET);
+ got = read(id->h, pBuf, cnt);
+#endif
+ if( got>0 ){
+ id->offset += got;
+ }
+ return got;
+}
+
+/*
** Read data from a file into a buffer. Return SQLITE_OK if all
** bytes were read successfully and SQLITE_IOERR if anything goes
** wrong.
@@ -914,7 +933,7 @@ static int unixRead(OsFile *id, void *pBuf, int amt){
assert( id );
SimulateIOError(SQLITE_IOERR);
TIMER_START;
- got = read(((unixFile*)id)->h, pBuf, amt);
+ got = seekAndRead((unixFile*)id, pBuf, amt);
TIMER_END;
TRACE5("READ %-3d %5d %7d %d\n", ((unixFile*)id)->h, got,
last_page, TIMER_ELAPSED);
@@ -928,6 +947,25 @@ static int unixRead(OsFile *id, void *pBuf, int amt){
}
/*
+** Seek to the offset in id->offset then read cnt bytes into pBuf.
+** Return the number of bytes actually read. Update the offset.
+*/
+static int seekAndWrite(unixFile *id, const void *pBuf, int cnt){
+ int got;
+#ifdef USE_PREAD
+ got = pwrite(id->h, pBuf, cnt, id->offset);
+#else
+ lseek(id->h, id->offset, SEEK_SET);
+ got = write(id->h, pBuf, cnt);
+#endif
+ if( got>0 ){
+ id->offset += got;
+ }
+ return got;
+}
+
+
+/*
** Write data from a buffer into a file. Return SQLITE_OK on success
** or some other error code on failure.
*/
@@ -938,7 +976,7 @@ static int unixWrite(OsFile *id, const void *pBuf, int amt){
SimulateIOError(SQLITE_IOERR);
SimulateDiskfullError;
TIMER_START;
- while( amt>0 && (wrote = write(((unixFile*)id)->h, pBuf, amt))>0 ){
+ while( amt>0 && (wrote = seekAndWrite((unixFile*)id, pBuf, amt))>0 ){
amt -= wrote;
pBuf = &((char*)pBuf)[wrote];
}
@@ -961,7 +999,7 @@ static int unixSeek(OsFile *id, i64 offset){
#ifdef SQLITE_TEST
if( offset ) SimulateDiskfullError
#endif
- lseek(((unixFile*)id)->h, offset, SEEK_SET);
+ ((unixFile*)id)->offset = offset;
return SQLITE_OK;
}
@@ -1635,6 +1673,7 @@ static int allocateUnixFile(unixFile *pInit, OsFile **pId){
pInit->dirfd = -1;
pInit->fullSync = 0;
pInit->locktype = 0;
+ pInit->offset = 0;
SET_THREADID(pInit);
pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );
if( pNew==0 ){