-
Notifications
You must be signed in to change notification settings - Fork 3
/
fs.go
277 lines (234 loc) · 6.63 KB
/
fs.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package apk
import (
"archive/tar"
"fmt"
"io"
"log"
"net/http"
"os"
"path"
"strings"
"time"
"github.com/google/go-containerregistry/pkg/logs"
"github.com/google/go-containerregistry/pkg/v1/types"
httpserve "github.com/jonjohnsonjr/dagdotdev/internal/forks/http"
)
// Lots of debugging that we don't want to compile into the binary.
const debug = false
func debugf(s string, i ...interface{}) {
if debug {
log.Printf(s, i...)
}
}
// More than enough for FileServer to Peek at file contents.
const bufferLen = 2 << 16
type tarReader interface {
io.Reader
Next() (*tar.Header, error)
}
// Implements http.FileSystem.
type layerFS struct {
h *handler
prefix string
tr tarReader
headers []*tar.Header
ref string
size int64
kind string
mt types.MediaType
}
func (h *handler) newLayerFS(tr tarReader, size int64, prefix, ref, kind string, mt types.MediaType) *layerFS {
logs.Debug.Printf("size: %d, prefix: %q, ref: %q, kind: %q", size, prefix, ref, kind)
return &layerFS{
h: h,
tr: tr,
size: size,
prefix: prefix,
ref: ref,
kind: kind,
mt: mt,
headers: []*tar.Header{},
}
}
func (fs *layerFS) RenderHeader(w http.ResponseWriter, r *http.Request, fname string, f httpserve.File, ctype string) error {
return fs.h.renderHeader(w, r, fname, strings.Trim(fs.prefix, "/"), fs.ref, fs.kind, fs.mt, fs.size, f, ctype)
}
func (fs *layerFS) Open(original string) (httpserve.File, error) {
logs.Debug.Printf("Open(%q)", original)
logs.Debug.Printf("prefix=%q", fs.prefix)
name := strings.TrimPrefix(original, fs.prefix)
logs.Debug.Printf("name=%q", name)
var found httpserve.File
// Scan through the layer, looking for a matching tar.Header.Name.
for {
header, err := fs.tr.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("Open(%q): %w", original, err)
}
// Cache the headers, so we don't have to re-fetch the blob. This comes
// into play mostly for ReadDir() at the top level, where we already scan
// the entire layer to tell FileServer "/" and "index.html" don't exist.
fs.headers = append(fs.headers, header)
if got := path.Clean("/" + header.Name); got == name {
found = &layerFile{
name: name,
header: header,
fs: fs,
}
// For directories, we need to keep listing everything to populate
// fs.headers. For other files, we can return immediately.
if header.Typeflag != tar.TypeDir {
return found, nil
}
} else {
logs.Debug.Printf("got: %q, want %q", got, name)
}
}
if found != nil {
return found, nil
}
// FileServer is trying to find index.html, but it doesn't exist in the image.
if path.Base(name) == "index.html" {
return nil, fmt.Errorf("nope: %s", name)
}
// We didn't find the entry in the tarball, so we're probably trying to list
// a file or directory that does not exist.
return &layerFile{
name: name,
fs: fs,
}, nil
}
// Implements http.File.
type layerFile struct {
name string
header *tar.Header
fs *layerFS
}
// This used to try to handle Seeking, but it was complicated, so I
// forked net/http instead.
func (f *layerFile) Seek(offset int64, whence int) (int64, error) {
return 0, fmt.Errorf("not implemented")
}
// Allows us to drop Seek impl for http.ServeContent.
func (f *layerFile) Size() int64 {
if f.header == nil {
return 0
}
return f.header.Size
}
// This used to handle content sniffing, but I forked net/http instead.
func (f *layerFile) Read(p []byte) (int, error) {
debugf("Read(%q): len(p) = %d", f.name, len(p))
return f.fs.tr.Read(p)
}
func (f *layerFile) Close() error {
return nil
}
// Scan through the tarball looking for prefixes that match the layerFile's name.
func (f *layerFile) Readdir(count int) ([]os.FileInfo, error) {
debugf("ReadDir(%q)", f.name)
if f.header != nil && f.header.Typeflag == tar.TypeSymlink {
fi := f.header.FileInfo()
return []os.FileInfo{symlink{
FileInfo: fi,
name: ".",
link: f.header.Linkname,
}}, nil
}
prefix := path.Clean("/" + f.name)
if f.Root() {
prefix = "/"
}
fis := []os.FileInfo{}
if !f.Root() {
fis = append(fis, dirInfo{".."})
}
implicitDirs := map[string]struct{}{}
realDirs := map[string]struct{}{}
for _, hdr := range f.fs.headers {
name := path.Clean("/" + hdr.Name)
if prefix != "/" && name != prefix && !strings.HasPrefix(name, prefix+"/") {
continue
}
fdir := path.Dir(strings.TrimPrefix(name, prefix))
if !(fdir == "/" || (fdir == "." && prefix == "/")) {
if fdir != "" && fdir != "." {
if fdir[0] == '/' {
fdir = fdir[1:]
}
implicit := strings.Split(fdir, "/")[0]
if implicit != "" {
implicitDirs[implicit] = struct{}{}
}
}
continue
}
if hdr.Typeflag == tar.TypeDir {
dirname := strings.TrimPrefix(name, prefix)
if dirname != "" && dirname != "." {
if dirname[0] == '/' {
dirname = dirname[1:]
}
realDirs[dirname] = struct{}{}
}
}
fis = append(fis, hdr.FileInfo())
}
for dir := range implicitDirs {
if _, ok := realDirs[dir]; !ok {
logs.Debug.Printf("Adding implicit dir: %s", dir)
fis = append(fis, dirInfo{dir})
}
}
return fis, nil
}
func (f *layerFile) Stat() (os.FileInfo, error) {
debugf("Stat(%q)", f.name)
// This is a non-existent entry in the tarball, we need to synthesize one.
if f.header == nil || f.Root() {
return dirInfo{f.name}, nil
}
// If you try to load a symlink directly, we will render it as a directory.
if f.header.Typeflag == tar.TypeSymlink {
hdr := *f.header
hdr.Typeflag = tar.TypeDir
return hdr.FileInfo(), nil
}
return f.header.FileInfo(), nil
}
func (f *layerFile) Root() bool {
return f.name == "" || f.name == "/" || f.name == "/index.html"
}
// Implements os.FileInfo for empty directory.
type dirInfo struct {
name string
}
func (f dirInfo) ModTime() time.Time { return time.Unix(0, 0) }
func (f dirInfo) Name() string { return f.name }
func (f dirInfo) Size() int64 { return 0 }
func (f dirInfo) Mode() os.FileMode { return os.ModeDir }
func (f dirInfo) IsDir() bool { return true }
func (f dirInfo) Sys() interface{} { return nil }
type symlink struct {
os.FileInfo
name string
link string
typeflag byte
}
func (s symlink) Name() string {
return s.name
}
// Implements os.FileInfo for a file that is too large.
type bigFifo struct {
name string
content []byte
}
func (b bigFifo) Name() string { return b.name }
func (b bigFifo) Size() int64 { return int64(len(b.content)) }
func (b bigFifo) ModTime() time.Time { return time.Now() }
func (b bigFifo) Mode() os.FileMode { return 0 }
func (b bigFifo) IsDir() bool { return false }
func (b bigFifo) Sys() interface{} { return nil }