File: windows_path.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (62 lines) | stat: -rw-r--r-- 1,440 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//go:build windows
// +build windows

// This implementation only works when compiled for Windows
// as this uses the `path/filepath` which is platform dependent

package path

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

type windowsPath struct {
}

// windowsNamedPipesExp matches a named pipe path (starts with `\\.\pipe\`, possibly with / instead of \)
var windowsNamedPipe = regexp.MustCompile(`(?i)^[/\\]{2}\.[/\\]pipe[/\\][^:*?"<>|\r\n]+$`)

func (p *windowsPath) Join(elem ...string) string {
	return filepath.Join(elem...)
}

func (p *windowsPath) IsAbs(path string) bool {
	if windowsNamedPipe.MatchString(path) {
		return true
	}

	path = filepath.Clean(path)
	return filepath.IsAbs(path)
}

func (p *windowsPath) IsRoot(path string) bool {
	if windowsNamedPipe.MatchString(path) {
		return false
	}

	path = filepath.Clean(path)
	return filepath.IsAbs(path) && filepath.Dir(path) == path
}

func (p *windowsPath) Contains(basePath, targetPath string) bool {
	// we use `filepath.Rel` as this perform OS-specific comparison
	// and this set of functions is compiled using OS-specific golang filepath
	relativePath, err := filepath.Rel(basePath, targetPath)
	if err != nil {
		return false
	}

	// if it starts with `..` it tries to escape the path
	if strings.HasPrefix(relativePath, "..") {
		return false
	}

	return true
}

//revive:disable:unexported-return
func NewWindowsPath() *windowsPath {
	return &windowsPath{}
}