Files
sqlite3/vfs_file.go

70 lines
1.8 KiB
Go
Raw Permalink Normal View History

2023-01-22 17:33:21 +00:00
package sqlite3
import (
2023-03-07 03:51:07 +00:00
"context"
2023-01-22 17:33:21 +00:00
"os"
2023-03-18 01:13:31 +00:00
"time"
2023-01-22 17:33:21 +00:00
"github.com/tetratelabs/wazero/api"
)
const (
// These need to match the offsets asserted in os.c
vfsFileIDOffset = 4
vfsFileLockOffset = 8
vfsFileLockTimeoutOffset = 12
)
2023-03-07 10:45:11 +00:00
func (vfsFileMethods) NewID(ctx context.Context, file *os.File) uint32 {
2023-03-07 03:51:07 +00:00
vfs := ctx.Value(vfsKey{}).(*vfsState)
2023-01-22 17:33:21 +00:00
2023-03-07 03:51:07 +00:00
// Find an empty slot.
for id, ptr := range vfs.files {
if ptr == nil {
vfs.files[id] = file
return uint32(id)
}
}
2023-01-22 17:33:21 +00:00
2023-03-07 03:51:07 +00:00
// Add a new slot.
vfs.files = append(vfs.files, file)
return uint32(len(vfs.files) - 1)
2023-01-23 14:00:37 +00:00
}
2023-03-07 10:45:11 +00:00
func (vfsFileMethods) Open(ctx context.Context, mod api.Module, pFile uint32, file *os.File) {
2023-03-07 03:51:07 +00:00
mem := memory{mod}
2023-03-07 10:45:11 +00:00
id := vfsFile.NewID(ctx, file)
mem.writeUint32(pFile+vfsFileIDOffset, id)
2023-02-07 03:11:59 +00:00
}
2023-03-07 10:45:11 +00:00
func (vfsFileMethods) Close(ctx context.Context, mod api.Module, pFile uint32) error {
2023-03-07 03:51:07 +00:00
mem := memory{mod}
id := mem.readUint32(pFile + vfsFileIDOffset)
2023-03-07 03:51:07 +00:00
vfs := ctx.Value(vfsKey{}).(*vfsState)
file := vfs.files[id]
vfs.files[id] = nil
return file.Close()
2023-01-23 14:00:37 +00:00
}
2023-03-07 10:45:11 +00:00
func (vfsFileMethods) GetOS(ctx context.Context, mod api.Module, pFile uint32) *os.File {
2023-03-07 03:51:07 +00:00
mem := memory{mod}
id := mem.readUint32(pFile + vfsFileIDOffset)
2023-03-07 03:51:07 +00:00
vfs := ctx.Value(vfsKey{}).(*vfsState)
return vfs.files[id]
2023-01-22 17:33:21 +00:00
}
2023-03-07 10:45:11 +00:00
func (vfsFileMethods) GetLock(ctx context.Context, mod api.Module, pFile uint32) vfsLockState {
2023-03-07 03:51:07 +00:00
mem := memory{mod}
return vfsLockState(mem.readUint8(pFile + vfsFileLockOffset))
2023-01-23 14:00:37 +00:00
}
2023-03-07 10:45:11 +00:00
func (vfsFileMethods) SetLock(ctx context.Context, mod api.Module, pFile uint32, lock vfsLockState) {
2023-03-07 03:51:07 +00:00
mem := memory{mod}
mem.writeUint8(pFile+vfsFileLockOffset, uint8(lock))
2023-01-22 17:33:21 +00:00
}
2023-03-18 01:13:31 +00:00
func (vfsFileMethods) GetLockTimeout(ctx context.Context, mod api.Module, pFile uint32) time.Duration {
mem := memory{mod}
return time.Duration(mem.readUint32(pFile+vfsFileLockTimeoutOffset)) * time.Millisecond
}