forked from pgpkg/pgpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.go
More file actions
202 lines (166 loc) · 5.04 KB
/
source.go
File metadata and controls
202 lines (166 loc) · 5.04 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package pgpkg
import (
"archive/zip"
"bytes"
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
)
// Source represents the tree of files in a package; it's basically a wrapper
// around fs.FS, but adds context. Source lets us access filesystems in any
// format, which currently includes filesystems (eg, for use with go:embed),
// ZIP files (for packaging), and local directories.
//
// Sources may include a cache, which could be either a read or write
// cache, depending on the type of source.
type Source interface {
// FS is implemented by every Source.
fs.FS
// Sub returns a source representing a subdirectory within the source.
Sub(dir string) (Source, error)
// Location should return the actual path for a source, taking account
// any subpaths that have been extracted from it. This is going to require a different
// format and handling for directories, embeds, ZIPs, and other objects.
Location() string
// Cache returns the cache for this source, if one exists. You should return
// a WriteCache from this function if your source supports writing. FIXME.
Cache() (Cache, error)
}
type FSSource struct {
location string
fs fs.FS
}
func NewFSSource(efs fs.FS, dir string) (*FSSource, error) {
root, err := fs.Sub(efs, dir)
if err != nil {
return nil, fmt.Errorf("unable to create new FS source: %s: %w", dir, err)
}
return &FSSource{fs: root, location: "fs:" + dir}, nil
}
func (f *FSSource) Open(name string) (fs.File, error) {
return f.fs.Open(name)
}
func (f *FSSource) Sub(path string) (Source, error) {
subfs, err := fs.Sub(f.fs, path)
if err != nil {
return nil, err
}
return &FSSource{location: filepath.Join(f.location, path), fs: subfs}, nil
}
func (f *FSSource) Location() string {
return f.location
}
func (f *FSSource) Cache() (Cache, error) {
cache, err := fs.Sub(f.fs, ".pgpkg")
if err != nil {
return nil, fmt.Errorf("unable to open cache .pgpkg: %w", err)
}
return NewReadCache(cache), nil
}
func (f *FSSource) String() string {
return "FSSource(" + f.location + ")"
}
type DirSource struct {
dir string
fs fs.FS
}
func NewDirSource(path string) *DirSource {
return &DirSource{dir: path, fs: os.DirFS(path)}
}
func (ds *DirSource) Sub(path string) (Source, error) {
subfs, err := fs.Sub(ds.fs, path)
if err != nil {
return nil, err
}
return &DirSource{dir: filepath.Join(ds.dir, path), fs: subfs}, nil
}
func (ds *DirSource) Location() string {
return ds.dir
}
func (ds *DirSource) Cache() (Cache, error) {
cacheDir := path.Join(ds.dir, ".pgpkg")
i, err := os.Stat(cacheDir)
if os.IsNotExist(err) {
err = os.Mkdir(cacheDir, 0700)
if err != nil {
return nil, fmt.Errorf("unable to create package cache %s: %w", cacheDir, err)
}
} else if err != nil {
return nil, fmt.Errorf("unable to open cache: %w", err)
} else {
if !i.IsDir() {
return nil, fmt.Errorf("package cache %s: not a directory", cacheDir)
}
}
return NewWriteCache(cacheDir), nil
}
func (ds *DirSource) Open(name string) (fs.File, error) {
return ds.fs.Open(name)
}
// Path returns the path that this DirSource refers to, allowing discovery of the
// underlying filesystem location.
func (ds *DirSource) Path() string {
return ds.dir
}
func (ds *DirSource) String() string {
return "DirSource(" + ds.dir + ")"
}
type ZipByteSource struct {
location string
fs fs.FS
}
// NewZipByteSource creates a new ZIP source from a byte slice.
func NewZipByteSource(zipBytes []byte) (*ZipByteSource, error) {
byteReader := bytes.NewReader(zipBytes)
zipfs, err := zip.NewReader(byteReader, int64(len(zipBytes)))
if err != nil {
return nil, fmt.Errorf("unable to read ZIP data: %w", err)
}
return &ZipByteSource{location: "zip:", fs: zipfs}, nil
}
// NewZipPathSource creates a new ZIP source from a filesystem path.
// This reads the whole ZIP file into memory and returns a ZipByteSource.
func NewZipPathSource(path string) (*ZipByteSource, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return NewZipByteSource(b)
}
// Sub returns a subtree of a ZipByteSource, as a ZipByteSource.
func (zs *ZipByteSource) Sub(dir string) (Source, error) {
newFs, err := fs.Sub(zs.fs, dir)
if err != nil {
return nil, err
}
return &ZipByteSource{fs: newFs, location: filepath.Join(zs.location, dir)}, nil
}
func (zs *ZipByteSource) Cache() (Cache, error) {
cache, err := fs.Sub(zs.fs, ".pgpkg")
if err != nil {
return nil, fmt.Errorf("unable to open zip source: .pgpkg: %w", err)
}
return NewReadCache(cache), nil
}
func (zs *ZipByteSource) Location() string {
return zs.location
}
func (zs *ZipByteSource) Open(name string) (fs.File, error) {
return zs.fs.Open(name)
}
// NewSource returns a Source based on the given filesystem path.
// If the path name ends in ".zip", NewSource will return a ZipByteSource.
// Otherwise, NewSource returns a DirSource.
func NewSource(pkgPath string) (Source, error) {
if strings.HasSuffix(pkgPath, ".zip") {
return NewZipPathSource(pkgPath)
} else {
return NewDirSource(pkgPath), nil
}
}
func (zs *ZipByteSource) String() string {
return "ZipByteSource(" + zs.location + ")"
}