8000 tarfs.FS uses shared file position for each file handler · Issue #486 · spf13/afero · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
tarfs.FS uses shared file position for each file handler #486
Open
@Emyrk

Description

@Emyrk

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 *bytes.Reader, so all file handlers for a filepath share the same reader:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0