// 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.package filepathimport ()func ( string) (string, error) { := volumeNameLen() := string(os.PathSeparator)if < len() && os.IsPathSeparator([]) { ++ } := [:] := := 0for , := , ; < len(); = {for < len() && os.IsPathSeparator([]) { ++ } = for < len() && !os.IsPathSeparator([]) { ++ }// On Windows, "." can be a symlink. // We look it up, and use the value if it is absolute. // If not, we just return ".". := runtime.GOOS == "windows" && [volumeNameLen():] == "."// The next path component is in path[start:end].if == {// No more path components.break } elseif [:] == "." && ! {// Ignore path component ".".continue } elseif [:] == ".." {// Back up to previous component if possible. // Note that volLen includes any leading slash.// Set r to the index of the last slash in dest, // after the volume.varintfor = len() - 1; >= ; -- {ifos.IsPathSeparator([]) {break } }if < || [+1:] == ".." {// Either path has no slashes // (it's empty or just "C:") // or it ends in a ".." we had to keep. // Either way, keep this "..".iflen() > { += } += ".." } else {// Discard everything since the last slash. = [:] }continue }// Ordinary path component. Add it to result.iflen() > volumeNameLen() && !os.IsPathSeparator([len()-1]) { += } += [:]// Resolve symlink. , := os.Lstat()if != nil {return"", }if .Mode()&fs.ModeSymlink == 0 {if !.Mode().IsDir() && < len() {return"", syscall.ENOTDIR }continue }// Found symlink. ++if > 255 {return"", errors.New("EvalSymlinks: too many links") } , := os.Readlink()if != nil {return"", }if && !IsAbs() {// On Windows, if "." is a relative symlink, // just return ".".break } = + [:] := volumeNameLen()if > 0 {// Symlink to drive name is an absolute path.if < len() && os.IsPathSeparator([]) { ++ } = [:] = = len() } elseiflen() > 0 && os.IsPathSeparator([0]) {// Symlink to absolute path. = [:1] = 1 = [:1] = 1 } else {// Symlink to relative path; replace last // path component in dest.varintfor = len() - 1; >= ; -- {ifos.IsPathSeparator([]) {break } }if < { = } else { = [:] } = 0 } }returnClean(), nil}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.