github.com/docker/engine@v22.0.0-20211208180946-d456264580cf+incompatible/pkg/system/path_windows.go (about)

     1  package system // import "github.com/docker/docker/pkg/system"
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"golang.org/x/sys/windows"
     9  )
    10  
    11  // GetLongPathName converts Windows short pathnames to full pathnames.
    12  // For example C:\Users\ADMIN~1 --> C:\Users\Administrator.
    13  // It is a no-op on non-Windows platforms
    14  func GetLongPathName(path string) (string, error) {
    15  	// See https://groups.google.com/forum/#!topic/golang-dev/1tufzkruoTg
    16  	p, err := windows.UTF16FromString(path)
    17  	if err != nil {
    18  		return "", err
    19  	}
    20  	b := p // GetLongPathName says we can reuse buffer
    21  	n, err := windows.GetLongPathName(&p[0], &b[0], uint32(len(b)))
    22  	if err != nil {
    23  		return "", err
    24  	}
    25  	if n > uint32(len(b)) {
    26  		b = make([]uint16, n)
    27  		_, err = windows.GetLongPathName(&p[0], &b[0], uint32(len(b)))
    28  		if err != nil {
    29  			return "", err
    30  		}
    31  	}
    32  	return windows.UTF16ToString(b), nil
    33  }
    34  
    35  // checkSystemDriveAndRemoveDriveLetter is the Windows implementation
    36  // of CheckSystemDriveAndRemoveDriveLetter
    37  func checkSystemDriveAndRemoveDriveLetter(path string, driver PathVerifier) (string, error) {
    38  	if len(path) == 2 && string(path[1]) == ":" {
    39  		return "", fmt.Errorf("No relative path specified in %q", path)
    40  	}
    41  	if !driver.IsAbs(path) || len(path) < 2 {
    42  		return filepath.FromSlash(path), nil
    43  	}
    44  	if string(path[1]) == ":" && !strings.EqualFold(string(path[0]), "c") {
    45  		return "", fmt.Errorf("The specified path is not on the system drive (C:)")
    46  	}
    47  	return filepath.FromSlash(path[2:]), nil
    48  }