podman
114 строк · 3.0 Кб
1// Copyright 2009 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
5package objabi6
7import (8"os"9"path/filepath"10"strings"11)
12
13// WorkingDir returns the current working directory
14// (or "/???" if the directory cannot be identified),
15// with "/" as separator.
16func WorkingDir() string {17var path string18path, _ = os.Getwd()19if path == "" {20path = "/???"21}22return filepath.ToSlash(path)23}
24
25// AbsFile returns the absolute filename for file in the given directory,
26// as rewritten by the rewrites argument.
27// For unrewritten paths, AbsFile rewrites a leading $GOROOT prefix to the literal "$GOROOT".
28// If the resulting path is the empty string, the result is "??".
29//
30// The rewrites argument is a ;-separated list of rewrites.
31// Each rewrite is of the form "prefix" or "prefix=>replace",
32// where prefix must match a leading sequence of path elements
33// and is either removed entirely or replaced by the replacement.
34func AbsFile(dir, file, rewrites string) string {35abs := file36if dir != "" && !filepath.IsAbs(file) {37abs = filepath.Join(dir, file)38}39
40start := 041for i := 0; i <= len(rewrites); i++ {42if i == len(rewrites) || rewrites[i] == ';' {43if new, ok := applyRewrite(abs, rewrites[start:i]); ok {44abs = new45goto Rewritten46}47start = i + 148}49}50if hasPathPrefix(abs, GOROOT) {51abs = "$GOROOT" + abs[len(GOROOT):]52}53
54Rewritten:55if abs == "" {56abs = "??"57}58return abs59}
60
61// applyRewrite applies the rewrite to the path,
62// returning the rewritten path and a boolean
63// indicating whether the rewrite applied at all.
64func applyRewrite(path, rewrite string) (string, bool) {65prefix, replace := rewrite, ""66if j := strings.LastIndex(rewrite, "=>"); j >= 0 {67prefix, replace = rewrite[:j], rewrite[j+len("=>"):]68}69
70if prefix == "" || !hasPathPrefix(path, prefix) {71return path, false72}73if len(path) == len(prefix) {74return replace, true75}76if replace == "" {77return path[len(prefix)+1:], true78}79return replace + path[len(prefix):], true80}
81
82// Does s have t as a path prefix?
83// That is, does s == t or does s begin with t followed by a slash?
84// For portability, we allow ASCII case folding, so that hasPathPrefix("a/b/c", "A/B") is true.
85// Similarly, we allow slash folding, so that hasPathPrefix("a/b/c", "a\\b") is true.
86// We do not allow full Unicode case folding, for fear of causing more confusion
87// or harm than good. (For an example of the kinds of things that can go wrong,
88// see http://article.gmane.org/gmane.linux.kernel/1853266.)
89func hasPathPrefix(s string, t string) bool {90if len(t) > len(s) {91return false92}93var i int94for i = 0; i < len(t); i++ {95cs := int(s[i])96ct := int(t[i])97if 'A' <= cs && cs <= 'Z' {98cs += 'a' - 'A'99}100if 'A' <= ct && ct <= 'Z' {101ct += 'a' - 'A'102}103if cs == '\\' {104cs = '/'105}106if ct == '\\' {107ct = '/'108}109if cs != ct {110return false111}112}113return i >= len(s) || s[i] == '/' || s[i] == '\\'114}
115