k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/images/agnhost/mounttest/mt_utils_windows.go (about) 1 //go:build windows 2 // +build windows 3 4 /* 5 Copyright 2019 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package mounttest 21 22 import ( 23 "bytes" 24 "fmt" 25 "os" 26 "os/exec" 27 "path/filepath" 28 "strconv" 29 "strings" 30 ) 31 32 func umask(mask int) int { 33 // noop for Windows. 34 return 0 35 } 36 37 func fileOwner(path string) error { 38 // Windows does not have owner UID / GID. However, it has owner SID. 39 // not currently implemented in Kubernetes, so noop. 40 return nil 41 } 42 43 func fileMode(path string) error { 44 if path == "" { 45 return nil 46 } 47 48 permissions, err := getFilePerm(path) 49 if err != nil { 50 return err 51 } 52 53 fmt.Printf("mode of Windows file %q: %v\n", path, permissions) 54 return nil 55 } 56 57 func filePerm(path string) error { 58 if path == "" { 59 return nil 60 } 61 62 permissions, err := getFilePerm(path) 63 if err != nil { 64 return err 65 } 66 67 fmt.Printf("perms of Windows file %q: %v\n", path, permissions) 68 return nil 69 } 70 71 func getFilePerm(path string) (os.FileMode, error) { 72 var ( 73 out bytes.Buffer 74 errOut bytes.Buffer 75 ) 76 77 // NOTE(claudiub): Symlinks have different permissions which might not match the target's. 78 // We want to evaluate the permissions of the target's not the symlink's. 79 info, err := os.Lstat(path) 80 if err == nil && info.Mode()&os.ModeSymlink != 0 { 81 evaluated, err := filepath.EvalSymlinks(path) 82 if err != nil { 83 return 0, err 84 } 85 path = evaluated 86 } 87 88 cmd := exec.Command("powershell.exe", "-NonInteractive", "./filePermissions.ps1", 89 "-FileName", path) 90 cmd.Stdout = &out 91 cmd.Stderr = &errOut 92 err = cmd.Run() 93 94 if err != nil { 95 fmt.Printf("error from PowerShell Script: %v, %v\n", err, errOut.String()) 96 return 0, err 97 } 98 99 output := strings.TrimSpace(out.String()) 100 val, err := strconv.ParseInt(output, 8, 32) 101 if err != nil { 102 fmt.Printf("error parsing string '%s' as int: %v\n", output, err) 103 return 0, err 104 } 105 106 return os.FileMode(val), nil 107 } 108 109 func fsType(path string) error { 110 // only NTFS is supported at the moment. 111 return nil 112 }