Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BasePathFs wrongly trims path prefix. #428

Open
movsb opened this issue Jun 22, 2024 · 0 comments
Open

BasePathFs wrongly trims path prefix. #428

movsb opened this issue Jun 22, 2024 · 0 comments

Comments

@movsb
Copy link

movsb commented Jun 22, 2024

It seems that BasePathFile.Name() method returns wrongly trimmed path:

return strings.TrimPrefix(sourcename, filepath.Clean(f.path))

filepath.Clean will have the last / removed, leaving the Name() containing a / prefix.
Next time the BasePathFile trimming a prefix, it trims nothing: strings.TrimPrefix(`/123/afero-test-388191401`, `123`).

package main

import (
	"fmt"

	"github.com/spf13/afero"
)

func must1[A any](a A, e error) A {
	if e != nil {
		panic(e)
	}
	return a
}
func must(e error) {
	if e != nil {
		panic(e)
	}
}

func main() {
	osFs := afero.NewOsFs()
	must(osFs.MkdirAll(`files/123`, 0755))

	fs1 := afero.NewBasePathFs(osFs, `files`)
	fs2 := afero.NewBasePathFs(fs1, `123`)

	tmp := must1(afero.TempFile(fs2, `.`, `afero-test-*`))
	fmt.Println(`Path:`, tmp.Name())

	must(fs2.Chmod(tmp.Name(), 0644))
}

The code above will panic:

$ go run main.go
Path: /123/afero-test-388191401
panic: chmod files/123/123/afero-test-388191401: no such file or directory

goroutine 1 [running]:
main.must(...)
        /Users/.../code/tests/main.go:17
main.main()
        /Users/.../code/tests/main.go:31 +0x1b8
exit status 2

Update:

Chaning fs2 to be fs2 := afero.NewBasePathFs(fs1, `/123`) seems to work, but it's somewhat inconsistent.

movsb added a commit to movsb/taoblog that referenced this issue Jun 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant