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

Fix issue with symlinks in directories for nfpm #24

Merged
merged 4 commits into from Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions glob.go
Expand Up @@ -124,6 +124,14 @@ func Glob(pattern string, opts ...OptFunc) ([]string, error) { // nolint:funlen,
return nil, fmt.Errorf("cannot determine static prefix: %w", err)
}

// Check if the file is valid symlink without following it
// It works only for valid absolut or relative file paths, in other words, will fail for WithFs() option
if patternInfo, err := os.Lstat(pattern); err == nil { // nolint:govet
if patternInfo.Mode()&os.ModeSymlink == os.ModeSymlink {
return cleanFilepaths([]string{pattern}, options.prefix), nil
}
}

prefixInfo, err := fs.Stat(options.fs, prefix)
if errors.Is(err, fs.ErrNotExist) {
if !ContainsMatchers(pattern) {
Expand Down
26 changes: 26 additions & 0 deletions glob_test.go
Expand Up @@ -450,6 +450,32 @@ func TestGlob(t *testing.T) { // nolint:funlen
"a/b/4.txt",
}, matches)
})

t.Run("symlinks", func(t *testing.T) {
t.Parallel()
var fsPath string
if testFS, ok := testFs(t, []string{"./a/file"}, nil).(testfs.FS); ok {
fsPath = testFS.Path()
}

workingSymlink := filepath.Join(fsPath, "b")
brokenSymlink := filepath.Join(fsPath, "c")
err := os.Symlink("a", workingSymlink)
require.NoError(t, err)
err = os.Symlink("non-existent", brokenSymlink)
require.NoError(t, err)

matches, err := Glob(workingSymlink)
require.NoError(t, err)
require.Equal(t, []string{
workingSymlink,
}, matches)
matches, err = Glob(brokenSymlink)
require.NoError(t, err)
require.Equal(t, []string{
brokenSymlink,
}, matches)
})
}

func TestQuoteMeta(t *testing.T) {
Expand Down