Skip to content

Commit

Permalink
fix: issue with symlinks in directories for nfpm
Browse files Browse the repository at this point in the history
  • Loading branch information
Felixoid committed Feb 9, 2022
1 parent 80e86ff commit ef73bef
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
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 {
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
24 changes: 24 additions & 0 deletions glob_test.go
Expand Up @@ -450,6 +450,30 @@ func TestGlob(t *testing.T) { // nolint:funlen
"a/b/4.txt",
}, matches)
})

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

fsPath := testFS.Path()
workingSymlink := filepath.Join(fsPath, "b")
brokenSymlink := filepath.Join(fsPath, "c")
os.Symlink("a", workingSymlink)
os.Symlink("non-existent", brokenSymlink)

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

0 comments on commit ef73bef

Please sign in to comment.