github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/compose/loader/windows_path.go (about)

     1  package loader
     2  
     3  // Copyright 2010 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  // https://github.com/golang/go/blob/master/LICENSE
     7  
     8  // This file contains utilities to check for Windows absolute paths on Linux.
     9  // The code in this file was largely copied from the Golang filepath package
    10  // https://github.com/golang/go/blob/1d0e94b1e13d5e8a323a63cd1cc1ef95290c9c36/src/path/filepath/path_windows.go#L12-L65
    11  
    12  func isSlash(c uint8) bool {
    13  	return c == '\\' || c == '/'
    14  }
    15  
    16  // isAbs reports whether the path is a Windows absolute path.
    17  func isAbs(path string) (b bool) {
    18  	l := volumeNameLen(path)
    19  	if l == 0 {
    20  		return false
    21  	}
    22  	path = path[l:]
    23  	if path == "" {
    24  		return false
    25  	}
    26  	return isSlash(path[0])
    27  }
    28  
    29  // volumeNameLen returns length of the leading volume name on Windows.
    30  // It returns 0 elsewhere.
    31  // nolint: gocyclo
    32  func volumeNameLen(path string) int {
    33  	if len(path) < 2 {
    34  		return 0
    35  	}
    36  	// with drive letter
    37  	c := path[0]
    38  	if path[1] == ':' && ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') {
    39  		return 2
    40  	}
    41  	// is it UNC? https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
    42  	if l := len(path); l >= 5 && isSlash(path[0]) && isSlash(path[1]) &&
    43  		!isSlash(path[2]) && path[2] != '.' {
    44  		// first, leading `\\` and next shouldn't be `\`. its server name.
    45  		for n := 3; n < l-1; n++ {
    46  			// second, next '\' shouldn't be repeated.
    47  			if isSlash(path[n]) {
    48  				n++
    49  				// third, following something characters. its share name.
    50  				if !isSlash(path[n]) {
    51  					if path[n] == '.' {
    52  						break
    53  					}
    54  					for ; n < l; n++ {
    55  						if isSlash(path[n]) {
    56  							break
    57  						}
    58  					}
    59  					return n
    60  				}
    61  				break
    62  			}
    63  		}
    64  	}
    65  	return 0
    66  }