sys: compile volume-path regex once, and update GoDoc

Ideally, we would construct this lazily, but adding a function and a
sync.Once felt like a bit "too much".

Also updated the GoDoc for some functions to better describe what they do.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-10-14 16:39:11 +02:00
parent 8ee685662d
commit d422c87e44
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -25,19 +25,22 @@ import (
"golang.org/x/sys/windows" "golang.org/x/sys/windows"
) )
const ( // SddlAdministratorsLocalSystem is local administrators plus NT AUTHORITY\System.
// SddlAdministratorsLocalSystem is local administrators plus NT AUTHORITY\System const SddlAdministratorsLocalSystem = "D:P(A;OICI;GA;;;BA)(A;OICI;GA;;;SY)"
SddlAdministratorsLocalSystem = "D:P(A;OICI;GA;;;BA)(A;OICI;GA;;;SY)"
)
// MkdirAllWithACL is a wrapper for MkdirAll that creates a directory // volumePath is a regular expression to check if a path is a Windows
// ACL'd for Builtin Administrators and Local System. // volume path (e.g., "\\?\Volume{4c1b02c1-d990-11dc-99ae-806e6f6e6963}".
func MkdirAllWithACL(path string, perm os.FileMode) error { var volumePath = regexp.MustCompile(`^\\\\\?\\Volume{[a-z0-9-]+}$`)
// MkdirAllWithACL is a custom version of os.MkdirAll modified for use on Windows
// so that it is both volume path aware, and to create a directory
// an appropriate SDDL defined ACL for Builtin Administrators and Local System.
func MkdirAllWithACL(path string, _ os.FileMode) error {
return mkdirall(path, true) return mkdirall(path, true)
} }
// MkdirAll implementation that is volume path aware for Windows. It can be used // MkdirAll is a custom version of os.MkdirAll that is volume path aware for
// as a drop-in replacement for os.MkdirAll() // Windows. It can be used as a drop-in replacement for os.MkdirAll.
func MkdirAll(path string, _ os.FileMode) error { func MkdirAll(path string, _ os.FileMode) error {
return mkdirall(path, false) return mkdirall(path, false)
} }
@ -46,7 +49,7 @@ func MkdirAll(path string, _ os.FileMode) error {
// so that it is both volume path aware, and can create a directory with // so that it is both volume path aware, and can create a directory with
// a DACL. // a DACL.
func mkdirall(path string, adminAndLocalSystem bool) error { func mkdirall(path string, adminAndLocalSystem bool) error {
if re := regexp.MustCompile(`^\\\\\?\\Volume{[a-z0-9-]+}$`); re.MatchString(path) { if volumePath.MatchString(path) {
return nil return nil
} }