From 8875ffeff041c573c878a52914271de571faa347 Mon Sep 17 00:00:00 2001 From: Daniel Erat Date: Mon, 18 Jul 2011 00:49:54 +0000 Subject: [PATCH] sqlite: add unused file size impl Change-Id: Ia0985ad51c716ac7a06c479655a60cc641977854 --- misc/sqlite/sqlite3_os_go.c | 26 +++++++++++++++++++++++--- misc/sqlite/vfs.go | 23 ++++++++++++++++++++++- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/misc/sqlite/sqlite3_os_go.c b/misc/sqlite/sqlite3_os_go.c index e2cbd83a1..3b3059dc8 100644 --- a/misc/sqlite/sqlite3_os_go.c +++ b/misc/sqlite/sqlite3_os_go.c @@ -32,6 +32,25 @@ static int go_file_write(sqlite3_file* file, const void* src, int iAmt, sqlite3_ return 0; } +static int go_file_truncate(sqlite3_file* file, sqlite3_int64 size) { + // TODO: implement + return SQLITE_OK; +} + +static int go_file_sync(sqlite3_file* file, int flags) { + // TODO: implement + return SQLITE_OK; +} + +static int go_file_file_size(sqlite3_file* file, sqlite3_int64* pSize) { + struct GoFileFileSize_return result = GoFileFileSize(((GoFile*) file)->fd); + if (result.r0 != 0) + return SQLITE_ERROR; + + *pSize = result.r1; + return SQLITE_OK; +} + /* VFS methods */ static int go_vfs_open(sqlite3_vfs* vfs, @@ -70,6 +89,7 @@ static int go_vfs_full_pathname(sqlite3_vfs* vfs, const char* zName, int nOut, char* zOut) { + // TODO: Actually implement this. strncpy(zOut, zName, nOut); zOut[nOut - 1] = '\0'; return SQLITE_OK; @@ -157,10 +177,10 @@ int sqlite3_os_init(void) { g_file_methods.xClose = go_file_close; g_file_methods.xRead = go_file_read; g_file_methods.xWrite = go_file_write; + g_file_methods.xTruncate = go_file_truncate; + g_file_methods.xSync = go_file_sync; + g_file_methods.xFileSize = go_file_file_size; #if 0 - int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); - int (*xSync)(sqlite3_file*, int flags); - int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); int (*xLock)(sqlite3_file*, int); int (*xUnlock)(sqlite3_file*, int); int (*xCheckReservedLock)(sqlite3_file*, int *pResOut); diff --git a/misc/sqlite/vfs.go b/misc/sqlite/vfs.go index 2b10b4b74..b383659b5 100644 --- a/misc/sqlite/vfs.go +++ b/misc/sqlite/vfs.go @@ -35,7 +35,7 @@ func GoFileClose(fd C.int) (int) { //export GoFileRead // Returns 0 on success and -1 on error. -func GoFileRead(fd C.int, dst *C.char, n C.int, offset C.int) (rv int) { +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() { println("read returning", rv); @@ -68,6 +68,27 @@ func GoFileRead(fd C.int, dst *C.char, n C.int, offset C.int) (rv int) { return 0 } +//export GoFileFileSize +// rv is 0 on success and -1 on error. +// TODO: size should be C.long, but cgo fails +func GoFileFileSize(fd C.int) (rv int, size C.int) { + println("getting file size for fd", fd); + defer func() { + println("returning", rv, "with size", size); + }() + + file := GetFile(int(fd)) + if file == nil { + return -1, 0 + } + + info, err := file.Stat() + if err != nil { + return -1, 0 + } + return 0, C.int(info.Size) +} + //export GoVFSOpen // fd is -1 on error. func GoVFSOpen(filename *C.char, flags C.int) (fd int) {