Skip to content

Commit 9b769d9

Browse files
committed
BSD WAL fixes.
1 parent 79c83cd commit 9b769d9

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

vfs/shm_bsd.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ import (
2020
// [EXCLUSIVE locking mode]: https://sqlite.org/pragma.html#pragma_locking_mode
2121
const SupportsSharedMemory = true
2222

23-
const (
24-
_SHM_NLOCK = 8
25-
_SHM_BASE = 120
26-
_SHM_DMS = _SHM_BASE + _SHM_NLOCK
27-
)
23+
const _SHM_NLOCK = 8
2824

2925
func (f *vfsFile) SharedMemory() SharedMemory { return f.shm }
3026

@@ -100,22 +96,19 @@ func (s *vfsShm) Close() error {
10096
return err
10197
}
10298

103-
func (s *vfsShm) shmOpen() error {
99+
func (s *vfsShm) shmOpen() (err error) {
104100
if s.vfsShmFile != nil {
105101
return nil
106102
}
107103

108-
var flag int
109-
if s.readOnly {
110-
flag = unix.O_RDONLY
111-
} else {
112-
flag = unix.O_RDWR
113-
}
104+
// Open file read-write, as it will be shared.
114105
f, err := os.OpenFile(s.path,
115-
flag|unix.O_CREAT|unix.O_NOFOLLOW, 0666)
106+
unix.O_RDWR|unix.O_CREAT|unix.O_NOFOLLOW, 0666)
116107
if err != nil {
117108
return _CANTOPEN
118109
}
110+
// Close if file if it's not nil.
111+
defer func() { f.Close() }()
119112

120113
fi, err := f.Stat()
121114
if err != nil {
@@ -125,19 +118,34 @@ func (s *vfsShm) shmOpen() error {
125118
vfsShmFilesMtx.Lock()
126119
defer vfsShmFilesMtx.Unlock()
127120

121+
// Find a shared file, increase the reference count.
128122
for _, g := range vfsShmFiles {
129123
if g != nil && os.SameFile(fi, g.info) {
130-
f.Close()
131124
g.refs++
132125
s.vfsShmFile = g
133126
return nil
134127
}
135128
}
129+
130+
// Lock and truncate the file, if not readonly.
131+
if s.readOnly {
132+
err = _READONLY_CANTINIT
133+
} else {
134+
if rc := osWriteLock(f, 0, 0, 0); rc != _OK {
135+
return rc
136+
}
137+
if err := f.Truncate(0); err != nil {
138+
return _IOERR_SHMOPEN
139+
}
140+
}
141+
142+
// Add the new shared file.
136143
s.vfsShmFile = &vfsShmFile{
137144
File: f,
138145
info: fi,
139146
refs: 1,
140147
}
148+
f = nil
141149
add := true
142150
for i, g := range vfsShmFiles {
143151
if g == nil {
@@ -148,17 +156,7 @@ func (s *vfsShm) shmOpen() error {
148156
if add {
149157
vfsShmFiles = append(vfsShmFiles, s.vfsShmFile)
150158
}
151-
152-
if s.readOnly {
153-
return _READONLY_CANTINIT
154-
}
155-
if rc := osWriteLock(f, _SHM_DMS, 1, 0); rc != _OK {
156-
return rc
157-
}
158-
if err := f.Truncate(0); err != nil {
159-
return _IOERR_SHMOPEN
160-
}
161-
return nil
159+
return err
162160
}
163161

164162
func (s *vfsShm) shmMap(ctx context.Context, mod api.Module, id, size int32, extend bool) (uint32, error) {

0 commit comments

Comments
 (0)