|
| 1 | +// Borrowed from https://github.com/golang/tools/pull/289. |
| 2 | +// TODO(mvdan): replace by x/tools/txtar once the proposal at |
| 3 | +// https://github.com/golang/go/issues/44158 is accepted and implemented. |
| 4 | + |
| 5 | +// Copyright 2022 The Go Authors. All rights reserved. |
| 6 | +// Use of this source code is governed by a BSD-style |
| 7 | +// license that can be found in the LICENSE file. |
| 8 | + |
| 9 | +package registrytest |
| 10 | + |
| 11 | +import ( |
| 12 | + "bytes" |
| 13 | + "errors" |
| 14 | + "io" |
| 15 | + "io/fs" |
| 16 | + "path" |
| 17 | + "strings" |
| 18 | + "time" |
| 19 | + |
| 20 | + "golang.org/x/tools/txtar" |
| 21 | +) |
| 22 | + |
| 23 | +// TxtarFS returns an fs.FS which reads from a txtar.Archive. |
| 24 | +func TxtarFS(ar *txtar.Archive) fs.FS { |
| 25 | + return archiveFS{ar} |
| 26 | +} |
| 27 | + |
| 28 | +type archiveFS struct { |
| 29 | + a *txtar.Archive |
| 30 | +} |
| 31 | + |
| 32 | +// Open implements fs.FS. |
| 33 | +func (fsys archiveFS) Open(name string) (fs.File, error) { |
| 34 | + if !fs.ValidPath(name) { |
| 35 | + return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist} |
| 36 | + } |
| 37 | + |
| 38 | + for _, f := range fsys.a.Files { |
| 39 | + // In case the txtar has weird filenames |
| 40 | + cleanName := path.Clean(f.Name) |
| 41 | + if name == cleanName { |
| 42 | + return newOpenFile(f), nil |
| 43 | + } |
| 44 | + } |
| 45 | + var entries []fileInfo |
| 46 | + dirs := make(map[string]bool) |
| 47 | + prefix := name + "/" |
| 48 | + if name == "." { |
| 49 | + prefix = "" |
| 50 | + } |
| 51 | + |
| 52 | + for _, f := range fsys.a.Files { |
| 53 | + cleanName := path.Clean(f.Name) |
| 54 | + if !strings.HasPrefix(cleanName, prefix) { |
| 55 | + continue |
| 56 | + } |
| 57 | + felem := cleanName[len(prefix):] |
| 58 | + i := strings.Index(felem, "/") |
| 59 | + if i < 0 { |
| 60 | + entries = append(entries, newFileInfo(f)) |
| 61 | + } else { |
| 62 | + dirs[felem[:i]] = true |
| 63 | + } |
| 64 | + } |
| 65 | + // If there are no children of the name, |
| 66 | + // then the directory is treated as not existing |
| 67 | + // unless the directory is "." |
| 68 | + if len(entries) == 0 && len(dirs) == 0 && name != "." { |
| 69 | + return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist} |
| 70 | + } |
| 71 | + |
| 72 | + for name := range dirs { |
| 73 | + entries = append(entries, newDirInfo(name)) |
| 74 | + } |
| 75 | + |
| 76 | + return &openDir{newDirInfo(name), entries, 0}, nil |
| 77 | +} |
| 78 | + |
| 79 | +var _ fs.ReadFileFS = archiveFS{} |
| 80 | + |
| 81 | +// ReadFile implements fs.ReadFileFS. |
| 82 | +func (fsys archiveFS) ReadFile(name string) ([]byte, error) { |
| 83 | + if !fs.ValidPath(name) { |
| 84 | + return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist} |
| 85 | + } |
| 86 | + if name == "." { |
| 87 | + return nil, &fs.PathError{Op: "read", Path: name, Err: errors.New("is a directory")} |
| 88 | + } |
| 89 | + prefix := name + "/" |
| 90 | + for _, f := range fsys.a.Files { |
| 91 | + if cleanName := path.Clean(f.Name); name == cleanName { |
| 92 | + return append(([]byte)(nil), f.Data...), nil |
| 93 | + } |
| 94 | + // It's a directory |
| 95 | + if strings.HasPrefix(f.Name, prefix) { |
| 96 | + return nil, &fs.PathError{Op: "read", Path: name, Err: errors.New("is a directory")} |
| 97 | + } |
| 98 | + } |
| 99 | + return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist} |
| 100 | +} |
| 101 | + |
| 102 | +var ( |
| 103 | + _ fs.File = (*openFile)(nil) |
| 104 | + _ io.ReadSeekCloser = (*openFile)(nil) |
| 105 | + _ io.ReaderAt = (*openFile)(nil) |
| 106 | + _ io.WriterTo = (*openFile)(nil) |
| 107 | +) |
| 108 | + |
| 109 | +type openFile struct { |
| 110 | + bytes.Reader |
| 111 | + fi fileInfo |
| 112 | +} |
| 113 | + |
| 114 | +func newOpenFile(f txtar.File) *openFile { |
| 115 | + var o openFile |
| 116 | + o.Reader.Reset(f.Data) |
| 117 | + o.fi = fileInfo{f, 0444} |
| 118 | + return &o |
| 119 | +} |
| 120 | + |
| 121 | +func (o *openFile) Stat() (fs.FileInfo, error) { return o.fi, nil } |
| 122 | + |
| 123 | +func (o *openFile) Close() error { return nil } |
| 124 | + |
| 125 | +var _ fs.FileInfo = fileInfo{} |
| 126 | + |
| 127 | +type fileInfo struct { |
| 128 | + f txtar.File |
| 129 | + m fs.FileMode |
| 130 | +} |
| 131 | + |
| 132 | +func newFileInfo(f txtar.File) fileInfo { |
| 133 | + return fileInfo{f, 0444} |
| 134 | +} |
| 135 | + |
| 136 | +func newDirInfo(name string) fileInfo { |
| 137 | + return fileInfo{txtar.File{Name: name}, fs.ModeDir | 0555} |
| 138 | +} |
| 139 | + |
| 140 | +func (f fileInfo) Name() string { return path.Base(f.f.Name) } |
| 141 | +func (f fileInfo) Size() int64 { return int64(len(f.f.Data)) } |
| 142 | +func (f fileInfo) Mode() fs.FileMode { return f.m } |
| 143 | +func (f fileInfo) Type() fs.FileMode { return f.m.Type() } |
| 144 | +func (f fileInfo) ModTime() time.Time { return time.Time{} } |
| 145 | +func (f fileInfo) IsDir() bool { return f.m.IsDir() } |
| 146 | +func (f fileInfo) Sys() interface{} { return f.f } |
| 147 | +func (f fileInfo) Info() (fs.FileInfo, error) { return f, nil } |
| 148 | + |
| 149 | +type openDir struct { |
| 150 | + dirInfo fileInfo |
| 151 | + entries []fileInfo |
| 152 | + offset int |
| 153 | +} |
| 154 | + |
| 155 | +func (d *openDir) Stat() (fs.FileInfo, error) { return &d.dirInfo, nil } |
| 156 | +func (d *openDir) Close() error { return nil } |
| 157 | +func (d *openDir) Read(b []byte) (int, error) { |
| 158 | + return 0, &fs.PathError{Op: "read", Path: d.dirInfo.f.Name, Err: errors.New("is a directory")} |
| 159 | +} |
| 160 | + |
| 161 | +func (d *openDir) ReadDir(count int) ([]fs.DirEntry, error) { |
| 162 | + n := len(d.entries) - d.offset |
| 163 | + if count > 0 && n > count { |
| 164 | + n = count |
| 165 | + } |
| 166 | + if n == 0 && count > 0 { |
| 167 | + return nil, io.EOF |
| 168 | + } |
| 169 | + entries := make([]fs.DirEntry, n) |
| 170 | + for i := range entries { |
| 171 | + entries[i] = &d.entries[d.offset+i] |
| 172 | + } |
| 173 | + d.offset += n |
| 174 | + return entries, nil |
| 175 | +} |
| 176 | + |
| 177 | +// From constructs an Archive with the contents of fsys and an empty Comment. |
| 178 | +// Subsequent changes to fsys are not reflected in the returned archive. |
| 179 | +// |
| 180 | +// The transformation is lossy. |
| 181 | +// For example, because directories are implicit in txtar archives, |
| 182 | +// empty directories in fsys will be lost, |
| 183 | +// and txtar does not represent file mode, mtime, or other file metadata. |
| 184 | +// From does not guarantee that a.File[i].Data contains no file marker lines. |
| 185 | +// See also warnings on Format. |
| 186 | +// In short, it is unwise to use txtar as a generic filesystem serialization mechanism. |
| 187 | +func From(fsys fs.FS) (*txtar.Archive, error) { |
| 188 | + ar := new(txtar.Archive) |
| 189 | + walkfn := func(path string, d fs.DirEntry, err error) error { |
| 190 | + if err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + if d.IsDir() { |
| 194 | + // Directories in txtar are implicit. |
| 195 | + return nil |
| 196 | + } |
| 197 | + data, err := fs.ReadFile(fsys, path) |
| 198 | + if err != nil { |
| 199 | + return err |
| 200 | + } |
| 201 | + ar.Files = append(ar.Files, txtar.File{Name: path, Data: data}) |
| 202 | + return nil |
| 203 | + } |
| 204 | + |
| 205 | + if err := fs.WalkDir(fsys, ".", walkfn); err != nil { |
| 206 | + return nil, err |
| 207 | + } |
| 208 | + return ar, nil |
| 209 | +} |
0 commit comments