Skip to content

Commit ad1c100

Browse files
corylanouclaude
andcommitted
refactor(vfs): consolidate temp file tests into vfs_test.go
Move all VFS unit tests from vfs_lock_test.go into vfs_test.go for better organization alongside the integration tests. Inline the localTempFile implementation directly in vfs.go using sync/atomic for lock state tracking, removing the need for vfs_temp_file.go. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 2e4686c commit ad1c100

File tree

3 files changed

+77
-87
lines changed

3 files changed

+77
-87
lines changed

vfs.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path/filepath"
1515
"strings"
1616
"sync"
17+
"sync/atomic"
1718
"time"
1819

1920
"github.com/psanford/sqlite3vfs"
@@ -242,6 +243,82 @@ func openFlagToOSFlag(flag sqlite3vfs.OpenFlag) int {
242243

243244
var errTempFileNotFound = fmt.Errorf("temp file not tracked")
244245

246+
// localTempFile fulfills sqlite3vfs.File solely for SQLite temp & transient files.
247+
// These files stay on the local filesystem and optionally delete themselves
248+
// when SQLite closes them (DeleteOnClose flag).
249+
type localTempFile struct {
250+
f *os.File
251+
deleteOnClose bool
252+
lockType atomic.Int32
253+
onClose func()
254+
}
255+
256+
func newLocalTempFile(f *os.File, deleteOnClose bool, onClose func()) *localTempFile {
257+
return &localTempFile{f: f, deleteOnClose: deleteOnClose, onClose: onClose}
258+
}
259+
260+
func (tf *localTempFile) Close() error {
261+
err := tf.f.Close()
262+
if tf.deleteOnClose {
263+
if removeErr := os.Remove(tf.f.Name()); removeErr != nil && !os.IsNotExist(removeErr) && err == nil {
264+
err = removeErr
265+
}
266+
}
267+
if tf.onClose != nil {
268+
tf.onClose()
269+
}
270+
return err
271+
}
272+
273+
func (tf *localTempFile) ReadAt(p []byte, off int64) (n int, err error) {
274+
return tf.f.ReadAt(p, off)
275+
}
276+
277+
func (tf *localTempFile) WriteAt(b []byte, off int64) (n int, err error) {
278+
return tf.f.WriteAt(b, off)
279+
}
280+
281+
func (tf *localTempFile) Truncate(size int64) error {
282+
return tf.f.Truncate(size)
283+
}
284+
285+
func (tf *localTempFile) Sync(flag sqlite3vfs.SyncType) error {
286+
return tf.f.Sync()
287+
}
288+
289+
func (tf *localTempFile) FileSize() (int64, error) {
290+
info, err := tf.f.Stat()
291+
if err != nil {
292+
return 0, err
293+
}
294+
return info.Size(), nil
295+
}
296+
297+
func (tf *localTempFile) Lock(elock sqlite3vfs.LockType) error {
298+
if elock == sqlite3vfs.LockNone {
299+
return nil
300+
}
301+
tf.lockType.Store(int32(elock))
302+
return nil
303+
}
304+
305+
func (tf *localTempFile) Unlock(elock sqlite3vfs.LockType) error {
306+
tf.lockType.Store(int32(elock))
307+
return nil
308+
}
309+
310+
func (tf *localTempFile) CheckReservedLock() (bool, error) {
311+
return sqlite3vfs.LockType(tf.lockType.Load()) >= sqlite3vfs.LockReserved, nil
312+
}
313+
314+
func (tf *localTempFile) SectorSize() int64 {
315+
return 0
316+
}
317+
318+
func (tf *localTempFile) DeviceCharacteristics() sqlite3vfs.DeviceCharacteristic {
319+
return 0
320+
}
321+
245322
// VFSFile implements the SQLite VFS file interface.
246323
type VFSFile struct {
247324
mu sync.Mutex

vfs_temp_file.go

Lines changed: 0 additions & 87 deletions
This file was deleted.
File renamed without changes.

0 commit comments

Comments
 (0)