Skip to content

Commit 746770f

Browse files
committed
Respond to feedback; make fsys separate from Archive
1 parent 7a3ce0e commit 746770f

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

txtar/archive.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ package txtar
3434
import (
3535
"bytes"
3636
"fmt"
37+
"io/fs"
3738
"io/ioutil"
3839
"strings"
3940
)
@@ -138,3 +139,8 @@ func fixNL(data []byte) []byte {
138139
d[len(data)] = '\n'
139140
return d
140141
}
142+
143+
// FS returns an fs.FS that reads from the Archive.
144+
func (a *Archive) FS() fs.FS {
145+
return fsys{a}
146+
}

txtar/fs.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// Copyright 2021 The Go Authors. All rights reserved.
1+
// Copyright 2022 The Go Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
// +build go1.16
6-
75
package txtar
86

97
import (
@@ -16,10 +14,12 @@ import (
1614
"time"
1715
)
1816

19-
var _ fs.FS = (*Archive)(nil)
17+
type fsys struct {
18+
*Archive
19+
}
2020

2121
// Open implements fs.FS.
22-
func (a *Archive) Open(name string) (fs.File, error) {
22+
func (a fsys) Open(name string) (fs.File, error) {
2323
if !fs.ValidPath(name) {
2424
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
2525
}
@@ -65,10 +65,10 @@ func (a *Archive) Open(name string) (fs.File, error) {
6565
return &openDir{newDirInfo(name), entries, 0}, nil
6666
}
6767

68-
var _ fs.ReadFileFS = (*Archive)(nil)
68+
var _ fs.ReadFileFS = fsys{}
6969

7070
// ReadFile implements fs.ReadFileFS.
71-
func (a *Archive) ReadFile(name string) ([]byte, error) {
71+
func (a fsys) ReadFile(name string) ([]byte, error) {
7272
if !fs.ValidPath(name) {
7373
return nil, &fs.PathError{Op: "open", Path: name, Err: fs.ErrNotExist}
7474
}
@@ -165,8 +165,9 @@ func (d *openDir) ReadDir(count int) ([]fs.DirEntry, error) {
165165
//
166166
// The transformation is lossy.
167167
// For example, because directories are implicit in txtar archives,
168-
// empty directories in fsys will be lost, and txtar does not represent file mode, mtime, or other file metadata.
169-
// From does not guarantee that a.File[i].Data contain no file marker lines.
168+
// empty directories in fsys will be lost,
169+
// and txtar does not represent file mode, mtime, or other file metadata.
170+
// From does not guarantee that a.File[i].Data contains no file marker lines.
170171
// See also warnings on Format.
171172
// In short, it is unwise to use txtar as a generic filesystem serialization mechanism.
172173
func From(fsys fs.FS) (*Archive, error) {

txtar/fs_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,16 @@ two
6868
t.Run(tc.name, func(t *testing.T) {
6969
a := Parse([]byte(tc.input))
7070
files := strings.Fields(tc.files)
71-
if err := fstest.TestFS(a, files...); err != nil {
71+
if err := fstest.TestFS(a.FS(), files...); err != nil {
7272
t.Fatal(err)
7373
}
7474
for _, name := range files {
7575
for _, f := range a.Files {
7676
if f.Name != name {
7777
continue
7878
}
79-
b, err := fs.ReadFile(a, name)
79+
fsys := a.FS()
80+
b, err := fs.ReadFile(fsys, name)
8081
if err != nil {
8182
t.Fatal(err)
8283
}
@@ -85,7 +86,7 @@ two
8586
}
8687
// Be careful with n cases, this open is O(n^3) deep
8788
// Do iotest
88-
fsfile, err := a.Open(name)
89+
fsfile, err := fsys.Open(name)
8990
if err != nil {
9091
t.Fatal(err)
9192
}
@@ -96,7 +97,7 @@ two
9697
t.Fatal(err)
9798
}
9899
// test io.Copy
99-
fsfile, err = a.Open(name)
100+
fsfile, err = fsys.Open(name)
100101
if err != nil {
101102
t.Fatal(err)
102103
}
@@ -116,7 +117,7 @@ two
116117
}
117118
}
118119
}
119-
a2, err := From(a)
120+
a2, err := From(a.FS())
120121
if err != nil {
121122
t.Fatalf("failed to write fsys for %v: %v", tc.name, err)
122123
}

0 commit comments

Comments
 (0)