Compare commits

...

2 Commits

Author SHA1 Message Date
Ian Lance Taylor 5d2fd3ccab [release-branch.go1.9] godoc: don't try to follow all symlinks
Revert https://golang.org/cl/45096.

Original change description:
    godoc: follow symbolic links to folders in GOROOT

    Directory walking in godoc relies on ReadDir which returns the result
    of os.Lstat.

    Instead make the the OS VFS's ReadDir use os.Stat on symlinks before
    returning.

Updates golang/go#15049
Fixes golang/go#21061

Change-Id: Ieaa7923d85842f3da5696a7f46134d16407dae66
Reviewed-on: https://go-review.googlesource.com/53634
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-on: https://go-review.googlesource.com/57870
Reviewed-by: Russ Cox <rsc@golang.org>
2017-08-24 19:54:20 +00:00
Chris Broadfoot fa615f793b [release-branch.go1.9] all: merge master into release-branch.go1.9
0f5d61c4 imports: print dir of candidates in addition to import path
a237aba5 godoc: fix out-of-bounds panic when serving top-level files
4e70a1b2 godoc: add GoogleCN property to pages
fcc44a63 cmd/getgo: add a user-agent to download requests
3fd990c6 cmd/tip: fix the build
d07a458d cmd/tip: add a cert cache, clean up Kubernetes config, use update-deps
9badcbe4 cmd/getgo: prompt warning if an earlier installation exists
6fdd948b godoc: remove disabled admin code
f2b3bb00 cmd/getgo: fix builds
001b4ec8 cmd/getgo: have consistent messages
29518d98 cmd/getgo: display that -i stands for interactive mode
5724bdc2 x/tools/godoc: fix redirect to Gerrit
ac1e4b19 cmd/getgo: initial commit

Change-Id: Ie58293a2621bbabbadea4f9431c6fe7bc4aa329f
2017-08-07 10:33:19 -07:00
2 changed files with 2 additions and 139 deletions

View File

@ -7,11 +7,9 @@ package vfs
import (
"fmt"
"io/ioutil"
"log"
"os"
pathpkg "path"
"path/filepath"
"strings"
)
// OS returns an implementation of FileSystem reading from the
@ -59,35 +57,9 @@ func (root osFS) Lstat(path string) (os.FileInfo, error) {
}
func (root osFS) Stat(path string) (os.FileInfo, error) {
return stat(root.resolve(path))
return os.Stat(root.resolve(path))
}
var readdir = ioutil.ReadDir // for testing
var stat = os.Stat // for testing
func (root osFS) ReadDir(path string) ([]os.FileInfo, error) {
fis, err := readdir(root.resolve(path))
if err != nil {
return fis, err
}
ret := fis[:0]
// reread the files with os.Stat since they might be symbolic links
for _, fi := range fis {
if fi.Mode()&os.ModeSymlink != 0 {
baseName := fi.Name()
fi, err = root.Stat(pathpkg.Join(path, baseName))
if err != nil {
if os.IsNotExist(err) && strings.HasPrefix(baseName, ".") {
// Ignore editor spam files without log spam.
continue
}
log.Printf("ignoring symlink: %v", err)
continue
}
}
ret = append(ret, fi)
}
return ret, nil // is sorted
return ioutil.ReadDir(root.resolve(path)) // is sorted
}

View File

@ -1,109 +0,0 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vfs
import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"time"
)
type fakeFileInfo struct {
dir bool
link bool
basename string
modtime time.Time
ents []*fakeFileInfo
contents string
err error
}
func (f *fakeFileInfo) Name() string { return f.basename }
func (f *fakeFileInfo) Sys() interface{} { return nil }
func (f *fakeFileInfo) ModTime() time.Time { return f.modtime }
func (f *fakeFileInfo) IsDir() bool { return f.dir }
func (f *fakeFileInfo) Size() int64 { return int64(len(f.contents)) }
func (f *fakeFileInfo) Mode() os.FileMode {
if f.dir {
return 0755 | os.ModeDir
}
if f.link {
return 0644 | os.ModeSymlink
}
return 0644
}
func TestOSReadDirFollowsSymLinks(t *testing.T) {
// Stat is called on ReadDir output by osFS
oldstat := stat
stat = func(path string) (os.FileInfo, error) {
if filepath.ToSlash(path) != "/tmp/subdir/is_link" {
t.Fatalf("stat called on unexpected path %q", path)
}
return &fakeFileInfo{
link: false,
basename: "foo",
}, nil
}
defer func() { stat = oldstat }()
oldreaddir := readdir
readdir = func(path string) ([]os.FileInfo, error) {
return []os.FileInfo{
&fakeFileInfo{
link: true,
basename: "is_link",
},
&fakeFileInfo{
link: false,
basename: "not_link",
},
}, nil
}
defer func() { readdir = oldreaddir }()
fs := OS("/tmp")
result, err := fs.ReadDir("subdir")
if err != nil {
t.Fatal(err)
}
var gotBuf bytes.Buffer
for i, fi := range result {
fmt.Fprintf(&gotBuf, "result[%d] = %v, %v\n", i, fi.Name(), fi.Mode())
}
got := gotBuf.String()
want := `result[0] = foo, -rw-r--r--
result[1] = not_link, -rw-r--r--
`
if got != want {
t.Errorf("ReadDir got:\n%s\n\nwant:\n%s\n", got, want)
}
}
func TestOSReadDirHandlesReadDirErrors(t *testing.T) {
oldreaddir := readdir
readdir = func(path string) ([]os.FileInfo, error) {
return []os.FileInfo{
&fakeFileInfo{
dir: false,
basename: "foo",
},
}, errors.New("some arbitrary filesystem failure")
}
defer func() { readdir = oldreaddir }()
fs := OS("/tmp")
_, err := fs.ReadDir("subdir")
if got, want := fmt.Sprint(err), "some arbitrary filesystem failure"; got != want {
t.Errorf("ReadDir = %v; want %q", got, want)
}
}