File: path_unix.go

package info (click to toggle)
docker-buildx 0.13.1%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: sh: 299; makefile: 87
file content (31 lines) | stat: -rw-r--r-- 826 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//go:build !windows
// +build !windows

package osutil

import (
	"os"
	"path/filepath"
	"regexp"
	"strings"
)

// GetLongPathName is a no-op on non-Windows platforms.
func GetLongPathName(path string) (string, error) {
	return path, nil
}

var windowsPathRegex = regexp.MustCompile(`^[A-Za-z]:[\\/].*$`)

func SanitizePath(path string) string {
	// If we're running in WSL, we need to convert Windows paths to Unix paths.
	// This is because the git binary can be invoked through `git.exe` and
	// therefore returns Windows paths.
	if os.Getenv("WSL_DISTRO_NAME") != "" && windowsPathRegex.MatchString(path) {
		unixPath := strings.ReplaceAll(path, "\\", "/")
		drive := strings.ToLower(string(unixPath[0]))
		rest := filepath.Clean(unixPath[3:])
		return filepath.Join("/mnt", drive, rest)
	}
	return filepath.Clean(path)
}