-
Notifications
You must be signed in to change notification settings - Fork 1
/
vfs.go
90 lines (73 loc) · 1.82 KB
/
vfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package sqlitezstd
import (
"fmt"
"io"
"net/url"
"os"
"strings"
"sync"
seekable "github.com/SaveTheRbtz/zstd-seekable-format-go/pkg"
"github.com/klauspost/compress/zstd"
_ "github.com/mattn/go-sqlite3"
"github.com/psanford/sqlite3vfs"
"howett.net/ranger"
)
type ZstdVFS struct{}
var _ sqlite3vfs.VFS = &ZstdVFS{}
func (z *ZstdVFS) Access(name string, flags sqlite3vfs.AccessFlag) (bool, error) {
if strings.HasSuffix(name, "-wal") || strings.HasSuffix(name, "-journal") {
return false, nil
}
return true, nil
}
func (z *ZstdVFS) Delete(name string, dirSync bool) error {
return sqlite3vfs.ReadOnlyError
}
func (z *ZstdVFS) FullPathname(name string) string {
return name
}
func (z *ZstdVFS) Open(name string, flags sqlite3vfs.OpenFlag) (sqlite3vfs.File, sqlite3vfs.OpenFlag, error) {
var (
err error
reader io.ReadSeeker
)
if strings.HasPrefix(name, "http://") || strings.HasPrefix(name, "https://") {
uri, err := url.Parse(name)
if err != nil {
return nil, 0, sqlite3vfs.CantOpenError
}
reader, err = ranger.NewReader(&ranger.HTTPRanger{URL: uri})
if err != nil {
return nil, 0, sqlite3vfs.CantOpenError
}
} else {
reader, err = os.Open(name)
if err != nil {
return nil, 0, sqlite3vfs.CantOpenError
}
}
decoder, err := zstd.NewReader(nil)
if err != nil {
return nil, 0, sqlite3vfs.CantOpenError
}
seekable, err := seekable.NewReader(reader, decoder)
if err != nil {
return nil, 0, sqlite3vfs.CantOpenError
}
return &ZstdFile{
decoder: decoder,
reader: reader,
seekable: seekable,
}, flags | sqlite3vfs.OpenReadOnly, nil
}
// nolint: gochecknoglobals
var once = sync.OnceValue(func() error {
err := sqlite3vfs.RegisterVFS("zstd", &ZstdVFS{})
if err != nil {
return fmt.Errorf("could not register vfs: %w", err)
}
return nil
})
func Init() error {
return once()
}