Skip to content

Commit

Permalink
fix: issue with symlinks in directories (#24)
Browse files Browse the repository at this point in the history
* fix: issue with symlinks in directories for nfpm

* test: add NoErr checks for os.Symlink calls

* test: fix assertion linting

* Ignore linter in shadowing error
  • Loading branch information
Felixoid committed Feb 9, 2022
1 parent d57c716 commit 5824e57
Show file tree
Hide file tree
Showing 2 changed files with 34 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 { // 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

0 comments on commit 5824e57

Please sign in to comment.