From aaa66176662ab9d45e16702e282274c4b8c2a075 Mon Sep 17 00:00:00 2001 From: Daniel Erat Date: Tue, 19 Jul 2011 22:24:58 +0000 Subject: [PATCH] sqlite: make read match behavior of unix vfs Change-Id: Ibcae7ee7f989ad535b00df87a2fa03316f3dad01 --- misc/sqlite/sqlite3_os_go.c | 4 +--- misc/sqlite/vfs.go | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/misc/sqlite/sqlite3_os_go.c b/misc/sqlite/sqlite3_os_go.c index 5bb16b528..ddd0710cd 100644 --- a/misc/sqlite/sqlite3_os_go.c +++ b/misc/sqlite/sqlite3_os_go.c @@ -24,9 +24,7 @@ static int go_file_close(sqlite3_file* file) { } static int go_file_read(sqlite3_file* file, void* dest, int iAmt, sqlite3_int64 iOfst) { - return GoFileRead(((GoFile*) file)->fd, dest, iAmt, iOfst) == 0 ? - SQLITE_OK : - SQLITE_ERROR; + return GoFileRead(((GoFile*) file)->fd, dest, iAmt, iOfst); } static int go_file_write(sqlite3_file* file, const void* src, int iAmt, sqlite3_int64 iOfst) { diff --git a/misc/sqlite/vfs.go b/misc/sqlite/vfs.go index c1334870d..c1b7c0979 100644 --- a/misc/sqlite/vfs.go +++ b/misc/sqlite/vfs.go @@ -34,7 +34,10 @@ func GoFileClose(fd C.int) (int) { } //export GoFileRead -// Returns 0 on success and -1 on error. +// Returns SQLite error codes to be returned by xRead: +// SQLITE_OK: read n bytes +// SQLITE_IOERR_READ: got error while reading +// SQLITE_IOERR_SHORT_READ: read fewer than n bytes; rest will be zeroed func GoFileRead(fd C.int, dst *C.char, n C.int, offset C.long) (rv int) { println("reading", n, "bytes at offset", offset, "from fd", fd); defer func() { @@ -43,7 +46,7 @@ func GoFileRead(fd C.int, dst *C.char, n C.int, offset C.long) (rv int) { file := GetFile(int(fd)) if file == nil { - return -1 + return C.SQLITE_IOERR_READ } buf := make([]byte, n) @@ -56,16 +59,16 @@ func GoFileRead(fd C.int, dst *C.char, n C.int, offset C.long) (rv int) { break } if err != nil { - return -1 + return C.SQLITE_IOERR_READ } } - if n != 0 { - return -1 - } - C.memcpy(unsafe.Pointer(dst), unsafe.Pointer(&buf[0]), C.size_t(len(buf))) - return 0 + + if n != 0 { + return C.SQLITE_IOERR_SHORT_READ + } + return C.SQLITE_OK } //export GoFileFileSize