|
| 1 | +// Copyright 2024 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package txtar |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + "io/fs" |
| 12 | + "path" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +// FS returns the file system form of an Archive. |
| 17 | +// It returns an error if any of the file names in the archive |
| 18 | +// are not valid file system names. |
| 19 | +// The archive must not be modified while the FS is in use. |
| 20 | +// |
| 21 | +// If the file system detects that it has been modified, calls to the |
| 22 | +// file system return an ErrModified error. |
| 23 | +func FS(a *Archive) (fs.FS, error) { |
| 24 | + // Create a filesystem with a root directory. |
| 25 | + root := &node{fileinfo: fileinfo{path: ".", mode: readOnlyDir}} |
| 26 | + fsys := &filesystem{a, map[string]*node{root.path: root}} |
| 27 | + |
| 28 | + if err := initFiles(fsys); err != nil { |
| 29 | + return nil, fmt.Errorf("cannot create fs.FS from txtar.Archive: %s", err) |
| 30 | + } |
| 31 | + return fsys, nil |
| 32 | +} |
| 33 | + |
| 34 | +const ( |
| 35 | + readOnly fs.FileMode = 0o444 // read only mode |
| 36 | + readOnlyDir = readOnly | fs.ModeDir |
| 37 | +) |
| 38 | + |
| 39 | +// ErrModified indicates that file system returned by FS |
| 40 | +// noticed that the underlying archive has been modified |
| 41 | +// since the call to FS. Detection of modification is best effort, |
| 42 | +// to help diagnose misuse of the API, and is not guaranteed. |
| 43 | +var ErrModified error = errors.New("txtar.Archive has been modified during txtar.FS") |
| 44 | + |
| 45 | +// A filesystem is a simple in-memory file system for txtar archives, |
| 46 | +// represented as a map from valid path names to information about the |
| 47 | +// files or directories they represent. |
| 48 | +// |
| 49 | +// File system operations are read only. Modifications to the underlying |
| 50 | +// *Archive may race. To help prevent this, the filesystem tries |
| 51 | +// to detect modification during Open and return ErrModified if it |
| 52 | +// is able to detect a modification. |
| 53 | +type filesystem struct { |
| 54 | + ar *Archive |
| 55 | + nodes map[string]*node |
| 56 | +} |
| 57 | + |
| 58 | +// node is a file or directory in the tree of a filesystem. |
| 59 | +type node struct { |
| 60 | + fileinfo // fs.FileInfo and fs.DirEntry implementation |
| 61 | + idx int // index into ar.Files (for files) |
| 62 | + entries []fs.DirEntry // subdirectories and files (for directories) |
| 63 | +} |
| 64 | + |
| 65 | +var _ fs.FS = (*filesystem)(nil) |
| 66 | +var _ fs.DirEntry = (*node)(nil) |
| 67 | + |
| 68 | +// initFiles initializes fsys from fsys.ar.Files. Returns an error if there are any |
| 69 | +// invalid file names or collisions between file or directories. |
| 70 | +func initFiles(fsys *filesystem) error { |
| 71 | + for idx, file := range fsys.ar.Files { |
| 72 | + name := file.Name |
| 73 | + if !fs.ValidPath(name) { |
| 74 | + return fmt.Errorf("file %q is an invalid path", name) |
| 75 | + } |
| 76 | + |
| 77 | + n := &node{idx: idx, fileinfo: fileinfo{path: name, size: len(file.Data), mode: readOnly}} |
| 78 | + if err := insert(fsys, n); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + } |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +// insert adds node n as an entry to its parent directory within the filesystem. |
| 86 | +func insert(fsys *filesystem, n *node) error { |
| 87 | + if m := fsys.nodes[n.path]; m != nil { |
| 88 | + return fmt.Errorf("duplicate path %q", n.path) |
| 89 | + } |
| 90 | + fsys.nodes[n.path] = n |
| 91 | + |
| 92 | + // fsys.nodes contains "." to prevent infinite loops. |
| 93 | + parent, err := directory(fsys, path.Dir(n.path)) |
| 94 | + if err != nil { |
| 95 | + return err |
| 96 | + } |
| 97 | + parent.entries = append(parent.entries, n) |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +// directory returns the directory node with the path dir and lazily-creates it |
| 102 | +// if it does not exist. |
| 103 | +func directory(fsys *filesystem, dir string) (*node, error) { |
| 104 | + if m := fsys.nodes[dir]; m != nil && m.IsDir() { |
| 105 | + return m, nil // pre-existing directory |
| 106 | + } |
| 107 | + |
| 108 | + n := &node{fileinfo: fileinfo{path: dir, mode: readOnlyDir}} |
| 109 | + if err := insert(fsys, n); err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + return n, nil |
| 113 | +} |
| 114 | + |
| 115 | +// dataOf returns the data associated with the file t. |
| 116 | +// May return ErrModified if fsys.ar has been modified. |
| 117 | +func dataOf(fsys *filesystem, n *node) ([]byte, error) { |
| 118 | + if n.idx >= len(fsys.ar.Files) { |
| 119 | + return nil, ErrModified |
| 120 | + } |
| 121 | + |
| 122 | + f := fsys.ar.Files[n.idx] |
| 123 | + if f.Name != n.path || len(f.Data) != n.size { |
| 124 | + return nil, ErrModified |
| 125 | + } |
| 126 | + return f.Data, nil |
| 127 | +} |
| 128 | + |
| 129 | +func (fsys *filesystem) Open(name string) (fs.File, error) { |
| 130 | + if !fs.ValidPath(name) { |
| 131 | + return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrInvalid} |
| 132 | + } |
| 133 | + |
| 134 | + n := fsys.nodes[name] |
| 135 | + switch { |
| 136 | + case n == nil: |
| 137 | + return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist} |
| 138 | + case n.IsDir(): |
| 139 | + return &openDir{fileinfo: n.fileinfo, entries: n.entries}, nil |
| 140 | + default: |
| 141 | + data, err := dataOf(fsys, n) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + return &openFile{fileinfo: n.fileinfo, data: data}, nil |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func (fsys *filesystem) ReadFile(name string) ([]byte, error) { |
| 150 | + file, err := fsys.Open(name) |
| 151 | + if err != nil { |
| 152 | + return nil, err |
| 153 | + } |
| 154 | + if file, ok := file.(*openFile); ok { |
| 155 | + // TODO: use slices.Clone once x/tools has 1.21 available. |
| 156 | + cp := make([]byte, file.size) |
| 157 | + copy(cp, file.data) |
| 158 | + return cp, err |
| 159 | + } |
| 160 | + return nil, &fs.PathError{Op: "read", Path: name, Err: fs.ErrInvalid} |
| 161 | +} |
| 162 | + |
| 163 | +// A fileinfo implements fs.FileInfo and fs.DirEntry for a given archive file. |
| 164 | +type fileinfo struct { |
| 165 | + path string // unique path to the file or directory within a filesystem |
| 166 | + size int |
| 167 | + mode fs.FileMode |
| 168 | +} |
| 169 | + |
| 170 | +var _ fs.FileInfo = (*fileinfo)(nil) |
| 171 | +var _ fs.DirEntry = (*fileinfo)(nil) |
| 172 | + |
| 173 | +func (i *fileinfo) Name() string { return path.Base(i.path) } |
| 174 | +func (i *fileinfo) Size() int64 { return int64(i.size) } |
| 175 | +func (i *fileinfo) Mode() fs.FileMode { return i.mode } |
| 176 | +func (i *fileinfo) Type() fs.FileMode { return i.mode.Type() } |
| 177 | +func (i *fileinfo) ModTime() time.Time { return time.Time{} } |
| 178 | +func (i *fileinfo) IsDir() bool { return i.mode&fs.ModeDir != 0 } |
| 179 | +func (i *fileinfo) Sys() any { return nil } |
| 180 | +func (i *fileinfo) Info() (fs.FileInfo, error) { return i, nil } |
| 181 | + |
| 182 | +// An openFile is a regular (non-directory) fs.File open for reading. |
| 183 | +type openFile struct { |
| 184 | + fileinfo |
| 185 | + data []byte |
| 186 | + offset int64 |
| 187 | +} |
| 188 | + |
| 189 | +var _ fs.File = (*openFile)(nil) |
| 190 | + |
| 191 | +func (f *openFile) Stat() (fs.FileInfo, error) { return &f.fileinfo, nil } |
| 192 | +func (f *openFile) Close() error { return nil } |
| 193 | +func (f *openFile) Read(b []byte) (int, error) { |
| 194 | + if f.offset >= int64(len(f.data)) { |
| 195 | + return 0, io.EOF |
| 196 | + } |
| 197 | + if f.offset < 0 { |
| 198 | + return 0, &fs.PathError{Op: "read", Path: f.path, Err: fs.ErrInvalid} |
| 199 | + } |
| 200 | + n := copy(b, f.data[f.offset:]) |
| 201 | + f.offset += int64(n) |
| 202 | + return n, nil |
| 203 | +} |
| 204 | + |
| 205 | +func (f *openFile) Seek(offset int64, whence int) (int64, error) { |
| 206 | + switch whence { |
| 207 | + case 0: |
| 208 | + // offset += 0 |
| 209 | + case 1: |
| 210 | + offset += f.offset |
| 211 | + case 2: |
| 212 | + offset += int64(len(f.data)) |
| 213 | + } |
| 214 | + if offset < 0 || offset > int64(len(f.data)) { |
| 215 | + return 0, &fs.PathError{Op: "seek", Path: f.path, Err: fs.ErrInvalid} |
| 216 | + } |
| 217 | + f.offset = offset |
| 218 | + return offset, nil |
| 219 | +} |
| 220 | + |
| 221 | +func (f *openFile) ReadAt(b []byte, offset int64) (int, error) { |
| 222 | + if offset < 0 || offset > int64(len(f.data)) { |
| 223 | + return 0, &fs.PathError{Op: "read", Path: f.path, Err: fs.ErrInvalid} |
| 224 | + } |
| 225 | + n := copy(b, f.data[offset:]) |
| 226 | + if n < len(b) { |
| 227 | + return n, io.EOF |
| 228 | + } |
| 229 | + return n, nil |
| 230 | +} |
| 231 | + |
| 232 | +// A openDir is a directory fs.File (so also an fs.ReadDirFile) open for reading. |
| 233 | +type openDir struct { |
| 234 | + fileinfo |
| 235 | + entries []fs.DirEntry |
| 236 | + offset int |
| 237 | +} |
| 238 | + |
| 239 | +var _ fs.ReadDirFile = (*openDir)(nil) |
| 240 | + |
| 241 | +func (d *openDir) Stat() (fs.FileInfo, error) { return &d.fileinfo, nil } |
| 242 | +func (d *openDir) Close() error { return nil } |
| 243 | +func (d *openDir) Read(b []byte) (int, error) { |
| 244 | + return 0, &fs.PathError{Op: "read", Path: d.path, Err: fs.ErrInvalid} |
| 245 | +} |
| 246 | + |
| 247 | +func (d *openDir) ReadDir(count int) ([]fs.DirEntry, error) { |
| 248 | + n := len(d.entries) - d.offset |
| 249 | + if n == 0 && count > 0 { |
| 250 | + return nil, io.EOF |
| 251 | + } |
| 252 | + if count > 0 && n > count { |
| 253 | + n = count |
| 254 | + } |
| 255 | + list := make([]fs.DirEntry, n) |
| 256 | + copy(list, d.entries[d.offset:d.offset+n]) |
| 257 | + d.offset += n |
| 258 | + return list, nil |
| 259 | +} |
0 commit comments