github.com/yukk001/go1.10.8@v0.0.0-20190813125351-6df2d3982e20/src/cmd/go/internal/load/path.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package load
     6  
     7  import (
     8  	"path/filepath"
     9  	"strings"
    10  )
    11  
    12  // hasSubdir reports whether dir is a subdirectory of
    13  // (possibly multiple levels below) root.
    14  // If so, it sets rel to the path fragment that must be
    15  // appended to root to reach dir.
    16  func hasSubdir(root, dir string) (rel string, ok bool) {
    17  	if p, err := filepath.EvalSymlinks(root); err == nil {
    18  		root = p
    19  	}
    20  	if p, err := filepath.EvalSymlinks(dir); err == nil {
    21  		dir = p
    22  	}
    23  	const sep = string(filepath.Separator)
    24  	root = filepath.Clean(root)
    25  	if !strings.HasSuffix(root, sep) {
    26  		root += sep
    27  	}
    28  	dir = filepath.Clean(dir)
    29  	if !strings.HasPrefix(dir, root) {
    30  		return "", false
    31  	}
    32  	return filepath.ToSlash(dir[len(root):]), true
    33  }
    34  
    35  // hasPathPrefix reports whether the path s begins with the
    36  // elements in prefix.
    37  func hasPathPrefix(s, prefix string) bool {
    38  	switch {
    39  	default:
    40  		return false
    41  	case len(s) == len(prefix):
    42  		return s == prefix
    43  	case len(s) > len(prefix):
    44  		if prefix != "" && prefix[len(prefix)-1] == '/' {
    45  			return strings.HasPrefix(s, prefix)
    46  		}
    47  		return s[len(prefix)] == '/' && s[:len(prefix)] == prefix
    48  	}
    49  }
    50  
    51  // expandPath returns the symlink-expanded form of path.
    52  func expandPath(p string) string {
    53  	x, err := filepath.EvalSymlinks(p)
    54  	if err == nil {
    55  		return x
    56  	}
    57  	return p
    58  }