Debian Codegraph @

mount/mount_helper_windows.go

raw
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 mount
21
22 import (
23 "fmt"
24 "os"
25 "strconv"
26 "strings"
27 "syscall"
28
29 "k8s.io/klog/v2"
30 )
31
32 // following failure codes are from https://docs.microsoft.com/en-us/windows/desktop/debug/system-error-codes--1300-1699-
33 // ERROR_BAD_NETPATH = 53
34 // ERROR_NETWORK_BUSY = 54
35 // ERROR_UNEXP_NET_ERR = 59
36 // ERROR_NETNAME_DELETED = 64
37 // ERROR_NETWORK_ACCESS_DENIED = 65
38 // ERROR_BAD_DEV_TYPE = 66
39 // ERROR_BAD_NET_NAME = 67
40 // ERROR_SESSION_CREDENTIAL_CONFLICT = 1219
41 // ERROR_LOGON_FAILURE = 1326
42 var errorNoList = [...]int{53, 54, 59, 64, 65, 66, 67, 1219, 1326}
43
44 // IsCorruptedMnt return true if err is about corrupted mount point
45 func IsCorruptedMnt(err error) bool {
46 if err == nil {
47 return false
48 }
49
50 var underlyingError error
51 switch pe := err.(type) {
52 case nil:
53 return false
54 case *os.PathError:
55 underlyingError = pe.Err
56 case *os.LinkError:
57 underlyingError = pe.Err
58 case *os.SyscallError:
59 underlyingError = pe.Err
60 }
61
62 if ee, ok := underlyingError.(syscall.Errno); ok {
63 for _, errno := range errorNoList {
64 if int(ee) == errno {
65 klog.Warningf("IsCorruptedMnt failed with error: %v, error code: %v", err, errno)
66 return true
67 }
68 }
69 }
70
71 return false
72 }
73
74 // NormalizeWindowsPath makes sure the given path is a valid path on Windows
75 // systems by making sure all instances of `/` are replaced with `\\`, and the
76 // path beings with `c:`
77 func NormalizeWindowsPath(path string) string {
78 normalizedPath := strings.Replace(path, "/", "\\", -1)
79 if strings.HasPrefix(normalizedPath, "\\") {
80 normalizedPath = "c:" + normalizedPath
81 }
82 return normalizedPath
83 }
84
85 // ValidateDiskNumber : disk number should be a number in [0, 99]
86 func ValidateDiskNumber(disk string) error {
87 diskNum, err := strconv.Atoi(disk)
88 if err != nil {
89 return fmt.Errorf("wrong disk number format: %q, err:%v", disk, err)
90 }
91
92 if diskNum < 0 || diskNum > 99 {
93 return fmt.Errorf("disk number out of range: %q", disk)
94 }
95
96 return nil
97 }
98
99 // isMountPointMatch determines if the mountpoint matches the dir
100 func isMountPointMatch(mp MountPoint, dir string) bool {
101 return mp.Path == dir
102 }