File: system_windows.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.0~git20190125.d66bd3c%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports
  • size: 8,912 kB
  • sloc: asm: 1,394; yacc: 155; makefile: 109; sh: 108; ansic: 17; xml: 11
file content (86 lines) | stat: -rw-r--r-- 1,815 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package main

import (
	"context"
	"log"
	"os"
	"syscall"
	"unsafe"
)

const (
	envSeparator = ";"
	homeKey      = "USERPROFILE"
	lineEnding   = "/r/n"
	pathVar      = "$env:Path"
)

var installPath = `c:\go`

func isWindowsXP() bool {
	v, err := syscall.GetVersion()
	if err != nil {
		log.Fatalf("GetVersion failed: %v", err)
	}
	major := byte(v)
	return major < 6
}

func whichGo(ctx context.Context) (string, error) {
	return findGo(ctx, "where")
}

// currentShell reports the current shell.
// It might be "powershell.exe", "cmd.exe" or any of the *nix shells.
//
// Returns empty string if the shell is unknown.
func currentShell() string {
	shell := os.Getenv("SHELL")
	if shell != "" {
		return shell
	}

	pid := os.Getppid()
	pe, err := getProcessEntry(pid)
	if err != nil {
		verbosef("getting shell from process entry failed: %v", err)
		return ""
	}

	return syscall.UTF16ToString(pe.ExeFile[:])
}

func getProcessEntry(pid int) (*syscall.ProcessEntry32, error) {
	// From https://go.googlesource.com/go/+/go1.8.3/src/syscall/syscall_windows.go#941
	snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0)
	if err != nil {
		return nil, err
	}
	defer syscall.CloseHandle(snapshot)

	var procEntry syscall.ProcessEntry32
	procEntry.Size = uint32(unsafe.Sizeof(procEntry))
	if err = syscall.Process32First(snapshot, &procEntry); err != nil {
		return nil, err
	}

	for {
		if procEntry.ProcessID == uint32(pid) {
			return &procEntry, nil
		}

		if err := syscall.Process32Next(snapshot, &procEntry); err != nil {
			return nil, err
		}
	}
}

func persistEnvChangesForSession() error {
	return nil
}