github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/specgen/winpath.go (about)

     1  package specgen
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"unicode"
     7  
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  func isHostWinPath(path string) bool {
    12  	return shouldResolveWinPaths() && strings.HasPrefix(path, `\\`) || hasWinDriveScheme(path, 0) || winPathExists(path)
    13  }
    14  
    15  func hasWinDriveScheme(path string, start int) bool {
    16  	if len(path) < start+2 || path[start+1] != ':' {
    17  		return false
    18  	}
    19  
    20  	drive := rune(path[start])
    21  	return drive < unicode.MaxASCII && unicode.IsLetter(drive)
    22  }
    23  
    24  // Converts a Windows path to a WSL guest path if local env is a WSL linux guest or this is a Windows client.
    25  func ConvertWinMountPath(path string) (string, error) {
    26  	if !shouldResolveWinPaths() {
    27  		return path, nil
    28  	}
    29  
    30  	if strings.HasPrefix(path, "/") {
    31  		// Handle /[driveletter]/windows/path form (e.g. c:\Users\bar == /c/Users/bar)
    32  		if len(path) > 2 && path[2] == '/' && shouldResolveUnixWinVariant(path) {
    33  			drive := unicode.ToLower(rune(path[1]))
    34  			if unicode.IsLetter(drive) && drive <= unicode.MaxASCII {
    35  				return fmt.Sprintf("/mnt/%c/%s", drive, path[3:]), nil
    36  			}
    37  		}
    38  
    39  		// unix path - pass through
    40  		return path, nil
    41  	}
    42  
    43  	// Convert remote win client relative paths to absolute
    44  	path = resolveRelativeOnWindows(path)
    45  
    46  	// Strip extended marker prefix if present
    47  	path = strings.TrimPrefix(path, `\\?\`)
    48  
    49  	// Drive installed via wsl --mount
    50  	switch {
    51  	case strings.HasPrefix(path, `\\.\`):
    52  		path = "/mnt/wsl/" + path[4:]
    53  	case len(path) > 1 && path[1] == ':':
    54  		path = "/mnt/" + strings.ToLower(path[0:1]) + path[2:]
    55  	default:
    56  		return path, errors.New("unsupported UNC path")
    57  	}
    58  
    59  	return strings.ReplaceAll(path, `\`, "/"), nil
    60  }