Description
Playground: https://go.dev/play/p/kNiuE6X1qtk
When using tarfs.FS
, if you open a file, read it, close it. The index for the file seeker is at the end of the file. So if you open it again later, any read
will return an empty slice.
This is because the file handler returned is the same for both callers. All files are stored in a map, and returned as a struct copy to each caller:
Cached files:
https://github.com/spf13/afero/blob/master/tarfs/fs.go#L17
File copy:
https://github.com/spf13/afero/blob/master/tarfs/fs.go#L83-L97
This struct copy of the File
has its data in a
https://github.com/spf13/afero/blob/master/tarfs/file.go#L16
And bytes.Reader
has an index for the current position:
type Reader struct {
s []byte
i int64 // current reading index
prevRune int // index of previous rune; or < 0
}
File handles to the tarfs.File
should all have their own unique file seek location. This was fixed for MemMapFS
files a long time ago: https://github.com/spf13/afero/pull/55/files
I think tarfs.File
needs a similar fix. Or just make it use mem.File