]> Cypherpunks repositories - gostls13.git/commitdiff
path/filepath: ensure Glob does not ignore broken symlinks
authorKelsey Hightower <kelsey.hightower@gmail.com>
Tue, 4 Mar 2014 17:00:45 +0000 (09:00 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Tue, 4 Mar 2014 17:00:45 +0000 (09:00 -0800)
Fixes #6463.

LGTM=bradfitz
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/69870050

src/pkg/path/filepath/match.go
src/pkg/path/filepath/match_test.go

index 3d84145d7f892574c1e132719221ca60f66fa5ff..a9bcc103c5512ffb30d288df10bd557735c58c67 100644 (file)
@@ -230,7 +230,7 @@ func getEsc(chunk string) (r rune, nchunk string, err error) {
 //
 func Glob(pattern string) (matches []string, err error) {
        if !hasMeta(pattern) {
-               if _, err = os.Stat(pattern); err != nil {
+               if _, err = os.Lstat(pattern); err != nil {
                        return nil, nil
                }
                return []string{pattern}, nil
index 13108ce1efcb19a670cb449d1132a8c9b59cacc0..daec81532dd07af4260d78f320045be9c568809d 100644 (file)
@@ -2,9 +2,12 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package filepath
+package filepath_test
 
 import (
+       "io/ioutil"
+       "os"
+       . "path/filepath"
        "runtime"
        "strings"
        "testing"
@@ -153,3 +156,52 @@ func TestGlobError(t *testing.T) {
                t.Error("expected error for bad pattern; got none")
        }
 }
+
+var globSymlinkTests = []struct {
+       path, dest string
+       brokenLink bool
+}{
+       {"test1", "link1", false},
+       {"test2", "link2", true},
+}
+
+func TestGlobSymlink(t *testing.T) {
+       switch runtime.GOOS {
+       case "windows", "plan9":
+               // The tests below are Unix specific so we skip plan9, which does not
+               // support symlinks, and windows.
+               t.Skipf("skipping test on %v", runtime.GOOS)
+       }
+       tmpDir, err := ioutil.TempDir("", "globsymlink")
+       if err != nil {
+               t.Fatal("creating temp dir:", err)
+       }
+       defer os.RemoveAll(tmpDir)
+
+       for _, tt := range globSymlinkTests {
+               path := Join(tmpDir, tt.path)
+               dest := Join(tmpDir, tt.dest)
+               f, err := os.Create(path)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if err := f.Close(); err != nil {
+                       t.Fatal(err)
+               }
+               err = os.Symlink(path, dest)
+               if err != nil {
+                       t.Fatal(err)
+               }
+               if tt.brokenLink {
+                       // Break the symlink.
+                       os.Remove(path)
+               }
+               matches, err := Glob(dest)
+               if err != nil {
+                       t.Errorf("GlobSymlink error for %q: %s", dest, err)
+               }
+               if !contains(matches, dest) {
+                       t.Errorf("Glob(%#q) = %#v want %v", dest, matches, dest)
+               }
+       }
+}