File: mkdir_windows.go

package info (click to toggle)
golang-github-tonistiigi-fsutil 0.0~git20240925.a340068-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 644 kB
  • sloc: sh: 21; makefile: 5
file content (103 lines) | stat: -rw-r--r-- 2,509 bytes parent folder | download | duplicates (11)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//go:build windows
// +build windows

package fs

import (
	"fmt"
	"os"
	"syscall"
	"time"

	"github.com/Microsoft/go-winio"
	"github.com/pkg/errors"
	"golang.org/x/sys/windows"
)

const (
	containerAdministratorSidString = "S-1-5-93-2-1"
)

func fixRootDirectory(p string) string {
	if len(p) == len(`\\?\c:`) {
		if os.IsPathSeparator(p[0]) && os.IsPathSeparator(p[1]) && p[2] == '?' && os.IsPathSeparator(p[3]) && p[5] == ':' {
			return p + `\`
		}
	}
	return p
}

func Utimes(p string, tm *time.Time) error {
	info, err := os.Lstat(p)
	if err != nil {
		return errors.Wrap(err, "fetching file info")
	}
	if tm != nil && info.Mode()&os.ModeSymlink == 0 {
		if err := os.Chtimes(p, *tm, *tm); err != nil {
			return errors.Wrap(err, "changing times")
		}
	}
	return nil
}

func Chown(p string, old *User, fn Chowner) error {
	if fn == nil {
		return nil
	}
	user, err := fn(old)
	if err != nil {
		return errors.WithStack(err)
	}
	var userSIDstring string
	if user != nil && user.SID != "" {
		userSIDstring = user.SID
	}
	if userSIDstring == "" {
		userSIDstring = containerAdministratorSidString

	}

	sidPtr, err := syscall.UTF16PtrFromString(userSIDstring)
	if err != nil {
		return errors.Wrap(err, "converting to utf16 ptr")
	}
	var userSID *windows.SID
	if err := windows.ConvertStringSidToSid(sidPtr, &userSID); err != nil {
		return errors.Wrap(err, "converting to windows SID")
	}
	var dacl *windows.ACL
	newEntries := []windows.EXPLICIT_ACCESS{
		{
			AccessPermissions: windows.GENERIC_ALL,
			AccessMode:        windows.GRANT_ACCESS,
			Inheritance:       windows.SUB_CONTAINERS_AND_OBJECTS_INHERIT,
			Trustee: windows.TRUSTEE{
				TrusteeForm:  windows.TRUSTEE_IS_SID,
				TrusteeValue: windows.TrusteeValueFromSID(userSID),
			},
		},
	}
	newAcl, err := windows.ACLFromEntries(newEntries, dacl)
	if err != nil {
		return fmt.Errorf("adding acls: %w", err)
	}

	// Copy file ownership and ACL
	// We need SeRestorePrivilege and SeTakeOwnershipPrivilege in order
	// to restore security info on a file, especially if we're trying to
	// apply security info which includes SIDs not necessarily present on
	// the host.
	privileges := []string{winio.SeRestorePrivilege, seTakeOwnershipPrivilege}
	err = winio.RunWithPrivileges(privileges, func() error {
		if err := windows.SetNamedSecurityInfo(
			p, windows.SE_FILE_OBJECT,
			windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION,
			userSID, nil, newAcl, nil); err != nil {

			return err
		}
		return nil
	})

	return err
}