-
Notifications
You must be signed in to change notification settings - Fork 9
/
tarfs.go
180 lines (139 loc) · 3.99 KB
/
tarfs.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package tarfs
import (
"archive/tar"
"context"
"io"
"io/fs"
"path/filepath"
"namespacelabs.dev/foundation/internal/bytestream"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/internal/fnfs/memfs"
)
type TarStreamFunc func() (io.ReadCloser, error)
type FS struct {
TarStream TarStreamFunc
}
var _ fs.ReadDirFS = FS{}
var _ fnfs.VisitFS = FS{}
func (l FS) Open(path string) (fs.File, error) {
if !fs.ValidPath(path) {
return nil, &fs.PathError{Op: "open", Path: path, Err: fnerrors.New("invalid path")}
}
var inmem memfs.FS
readFile, err := ReadFilesInto(&inmem, l.TarStream, checkPath(path))
if err != nil {
return nil, &fs.PathError{Op: "open", Path: path, Err: err}
}
if len(readFile) == 0 {
return nil, &fs.PathError{Op: "open", Path: path, Err: fs.ErrNotExist}
}
return inmem.Open(path)
}
type checkPath string
func (c checkPath) Match(p string) (string, bool) { return p, p == string(c) }
func (l FS) ReadDir(dir string) ([]fs.DirEntry, error) {
if !fs.ValidPath(dir) {
return nil, &fs.PathError{Op: "readdir", Path: dir, Err: fnerrors.New("invalid path")}
}
f, err := l.TarStream()
if err != nil {
return nil, err
}
defer f.Close()
dir = filepath.Clean(dir)
var dirents []fs.DirEntry
tr := tar.NewReader(f)
for {
h, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
return nil, &fs.PathError{Op: "readdir", Path: dir, Err: fnerrors.BadInputError("unexpected error: %v", err)}
}
if filepath.Dir(h.Name) != dir {
continue
}
switch h.Typeflag {
case tar.TypeReg:
dirents = append(dirents, tarDirent{h.FileInfo()})
case tar.TypeDir:
if h.Name != "." {
dirents = append(dirents, tarDirent{h.FileInfo()})
}
}
}
return dirents, nil
}
type tarDirent struct{ info fs.FileInfo }
func (de tarDirent) Name() string { return de.info.Name() }
func (de tarDirent) IsDir() bool { return de.info.IsDir() }
func (de tarDirent) Type() fs.FileMode { return de.info.Mode().Type() }
func (de tarDirent) Info() (fs.FileInfo, error) { return de.info, nil }
func (l FS) VisitFiles(ctx context.Context, visitor func(string, bytestream.ByteStream, fs.DirEntry) error) error {
f, err := l.TarStream()
if err != nil {
return err
}
defer f.Close()
tr := tar.NewReader(f)
for {
if err := ctx.Err(); err != nil {
return err
}
h, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
return err
}
if h.Typeflag != tar.TypeReg {
continue
}
b, err := io.ReadAll(tr)
if err != nil {
return fnerrors.BadInputError("failed to read contents of %q: %v", h.Name, err)
}
// XXX could optimize this by deferring the read to the first Reader() that is requested.
if err := visitor(h.Name, bytestream.Static{Contents: b}, tarDirent{h.FileInfo()}); err != nil {
return err
}
}
return nil
}
type HasHasString interface {
Match(string) (string, bool)
}
func ReadFilesInto(fsys fnfs.WriteFS, tarStream TarStreamFunc, include HasHasString) ([]string, error) {
f, err := tarStream()
if err != nil {
return nil, err
}
defer f.Close()
var readFiles []string
tr := tar.NewReader(f)
for {
h, err := tr.Next()
if err == io.EOF {
break
} else if err != nil {
return nil, fnerrors.BadInputError("unexpected error: %v", err)
}
if clean, ok := include.Match(filepath.Clean(h.Name)); ok {
w, err := fsys.OpenWrite(clean, h.FileInfo().Mode())
if err != nil {
return nil, fnerrors.TransientError("failed to open %q for writing (was %q)", clean, h.Name)
}
_, err = io.Copy(w, tr)
w.Close() // Close
if err != nil {
return nil, fnerrors.TransientError("failed to copy %q (was %q): %v", clean, h.Name, err)
}
readFiles = append(readFiles, clean)
}
}
return readFiles, nil
}