From 29f40c0d4065a0f30bbd1d4c4c45e742d42cffb0 Mon Sep 17 00:00:00 2001 From: ian Date: Fri, 30 Mar 2012 21:27:11 +0000 Subject: libgo: Update to weekly.2012-03-13. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@186023 138bc75d-0d04-0410-961f-82ee72b054a4 --- libgo/go/path/filepath/symlink.go | 67 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 libgo/go/path/filepath/symlink.go (limited to 'libgo/go/path/filepath/symlink.go') diff --git a/libgo/go/path/filepath/symlink.go b/libgo/go/path/filepath/symlink.go new file mode 100644 index 00000000000..307dd0f8fee --- /dev/null +++ b/libgo/go/path/filepath/symlink.go @@ -0,0 +1,67 @@ +// Copyright 2012 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. + +// +build !windows + +package filepath + +import ( + "bytes" + "errors" + "os" + "strings" +) + +func evalSymlinks(path string) (string, error) { + const maxIter = 255 + originalPath := path + // consume path by taking each frontmost path element, + // expanding it if it's a symlink, and appending it to b + var b bytes.Buffer + for n := 0; path != ""; n++ { + if n > maxIter { + return "", errors.New("EvalSymlinks: too many links in " + originalPath) + } + + // find next path component, p + i := strings.IndexRune(path, Separator) + var p string + if i == -1 { + p, path = path, "" + } else { + p, path = path[:i], path[i+1:] + } + + if p == "" { + if b.Len() == 0 { + // must be absolute path + b.WriteRune(Separator) + } + continue + } + + fi, err := os.Lstat(b.String() + p) + if err != nil { + return "", err + } + if fi.Mode()&os.ModeSymlink == 0 { + b.WriteString(p) + if path != "" { + b.WriteRune(Separator) + } + continue + } + + // it's a symlink, put it at the front of path + dest, err := os.Readlink(b.String() + p) + if err != nil { + return "", err + } + if IsAbs(dest) { + b.Reset() + } + path = dest + string(Separator) + path + } + return Clean(b.String()), nil +} -- cgit v1.2.1