Read only files.

This commit is contained in:
Nuno Cruces
2023-03-25 11:46:13 +00:00
parent 89f4327b2b
commit 80039385d3
5 changed files with 41 additions and 10 deletions

16
mem.go
View File

@@ -88,6 +88,22 @@ func (m memory) writeUint64(ptr uint32, v uint64) {
}
}
func (m memory) readBool8(ptr uint32) bool {
v := m.readUint8(ptr)
if v != 0 {
return true
}
return false
}
func (m memory) writeBool8(ptr uint32, v bool) {
var b uint8
if v {
b = 1
}
m.writeUint8(ptr, b)
}
func (m memory) readFloat64(ptr uint32) float64 {
return math.Float64frombits(m.readUint64(ptr))
}

View File

@@ -21,6 +21,7 @@ struct os_file {
char lock;
char psow;
char syncDir;
char readOnly;
int lockTimeout;
};
@@ -28,6 +29,7 @@ static_assert(offsetof(struct os_file, id) == 4, "Unexpected offset");
static_assert(offsetof(struct os_file, lock) == 8, "Unexpected offset");
static_assert(offsetof(struct os_file, psow) == 9, "Unexpected offset");
static_assert(offsetof(struct os_file, syncDir) == 10, "Unexpected offset");
static_assert(offsetof(struct os_file, readOnly) == 11, "Unexpected offset");
static_assert(offsetof(struct os_file, lockTimeout) == 12, "Unexpected offset");
int os_close(sqlite3_file *);

3
vfs.go
View File

@@ -236,6 +236,9 @@ func vfsOpen(ctx context.Context, mod api.Module, pVfs, zName, pFile uint32, fla
vfsFile.Open(ctx, mod, pFile, file)
if flags&OPEN_READONLY != 0 {
vfsFile.SetReadOnly(ctx, mod, pFile, true)
}
if runtime.GOOS != "windows" &&
flags&(OPEN_CREATE) != 0 &&
flags&(OPEN_MAIN_JOURNAL|OPEN_SUPER_JOURNAL|OPEN_WAL) != 0 {

View File

@@ -13,6 +13,7 @@ const (
vfsFileIDOffset = 4
vfsFileLockOffset = 8
vfsFileSyncDirOffset = 10
vfsFileReadOnlyOffset = 11
vfsFileLockTimeoutOffset = 12
)
@@ -71,17 +72,20 @@ func (vfsFileMethods) GetLockTimeout(ctx context.Context, mod api.Module, pFile
func (vfsFileMethods) GetSyncDir(ctx context.Context, mod api.Module, pFile uint32) bool {
mem := memory{mod}
if b := mem.readUint8(pFile + vfsFileSyncDirOffset); b != 0 {
return true
}
return false
return mem.readBool8(pFile + vfsFileSyncDirOffset)
}
func (vfsFileMethods) SetSyncDir(ctx context.Context, mod api.Module, pFile uint32, state bool) {
func (vfsFileMethods) SetSyncDir(ctx context.Context, mod api.Module, pFile uint32, val bool) {
mem := memory{mod}
var b uint8
if state {
b = 1
}
mem.writeUint8(pFile+vfsFileSyncDirOffset, b)
mem.writeBool8(pFile+vfsFileSyncDirOffset, val)
}
func (vfsFileMethods) GetReadOnly(ctx context.Context, mod api.Module, pFile uint32) bool {
mem := memory{mod}
return mem.readBool8(pFile + vfsFileReadOnlyOffset)
}
func (vfsFileMethods) SetReadOnly(ctx context.Context, mod api.Module, pFile uint32, val bool) {
mem := memory{mod}
mem.writeBool8(pFile+vfsFileReadOnlyOffset, val)
}

View File

@@ -65,6 +65,7 @@ func vfsLock(ctx context.Context, mod api.Module, pFile uint32, eLock vfsLockSta
file := vfsFile.GetOS(ctx, mod, pFile)
cLock := vfsFile.GetLock(ctx, mod, pFile)
timeout := vfsFile.GetLockTimeout(ctx, mod, pFile)
readOnly := vfsFile.GetReadOnly(ctx, mod, pFile)
switch {
case cLock < _NO_LOCK || cLock > _EXCLUSIVE_LOCK:
@@ -83,6 +84,11 @@ func vfsLock(ctx context.Context, mod api.Module, pFile uint32, eLock vfsLockSta
return _OK
}
// Do not allow any kind of write-lock on a read-only database.
if readOnly && eLock > _RESERVED_LOCK {
return uint32(IOERR_LOCK)
}
switch eLock {
case _SHARED_LOCK:
// Must be unlocked to get SHARED.