Skip to content

Commit 3428aad

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 3cca58c commit 3428aad

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
lru "github.com/hashicorp/golang-lru/v2"
@@ -297,6 +298,82 @@ func openFlagToOSFlag(flag sqlite3vfs.OpenFlag) int {
297298

298299
var errTempFileNotFound = fmt.Errorf("temp file not tracked")
299300

301+
// localTempFile fulfills sqlite3vfs.File solely for SQLite temp & transient files.
302+
// These files stay on the local filesystem and optionally delete themselves
303+
// when SQLite closes them (DeleteOnClose flag).
304+
type localTempFile struct {
305+
f *os.File
306+
deleteOnClose bool
307+
lockType atomic.Int32
308+
onClose func()
309+
}
310+
311+
func newLocalTempFile(f *os.File, deleteOnClose bool, onClose func()) *localTempFile {
312+
return &localTempFile{f: f, deleteOnClose: deleteOnClose, onClose: onClose}
313+
}
314+
315+
func (tf *localTempFile) Close() error {
316+
err := tf.f.Close()
317+
if tf.deleteOnClose {
318+
if removeErr := os.Remove(tf.f.Name()); removeErr != nil && !os.IsNotExist(removeErr) && err == nil {
319+
err = removeErr
320+
}
321+
}
322+
if tf.onClose != nil {
323+
tf.onClose()
324+
}
325+
return err
326+
}
327+
328+
func (tf *localTempFile) ReadAt(p []byte, off int64) (n int, err error) {
329+
return tf.f.ReadAt(p, off)
330+
}
331+
332+
func (tf *localTempFile) WriteAt(b []byte, off int64) (n int, err error) {
333+
return tf.f.WriteAt(b, off)
334+
}
335+
336+
func (tf *localTempFile) Truncate(size int64) error {
337+
return tf.f.Truncate(size)
338+
}
339+
340+
func (tf *localTempFile) Sync(flag sqlite3vfs.SyncType) error {
341+
return tf.f.Sync()
342+
}
343+
344+
func (tf *localTempFile) FileSize() (int64, error) {
345+
info, err := tf.f.Stat()
346+
if err != nil {
347+
return 0, err
348+
}
349+
return info.Size(), nil
350+
}
351+
352+
func (tf *localTempFile) Lock(elock sqlite3vfs.LockType) error {
353+
if elock == sqlite3vfs.LockNone {
354+
return nil
355+
}
356+
tf.lockType.Store(int32(elock))
357+
return nil
358+
}
359+
360+
func (tf *localTempFile) Unlock(elock sqlite3vfs.LockType) error {
361+
tf.lockType.Store(int32(elock))
362+
return nil
363+
}
364+
365+
func (tf *localTempFile) CheckReservedLock() (bool, error) {
366+
return sqlite3vfs.LockType(tf.lockType.Load()) >= sqlite3vfs.LockReserved, nil
367+
}
368+
369+
func (tf *localTempFile) SectorSize() int64 {
370+
return 0
371+
}
372+
373+
func (tf *localTempFile) DeviceCharacteristics() sqlite3vfs.DeviceCharacteristic {
374+
return 0
375+
}
376+
300377
// VFSFile implements the SQLite VFS file interface.
301378
type VFSFile struct {
302379
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)