Debian Codegraph @

mount_helper_windows.go

raw
1 //go:build windows
2
3 /*
4 Copyright 2019 The Kubernetes Authors.
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 */
18
19 package mount
20
21 import (
22 "fmt"
23 "os"
24 "strconv"
25 "strings"
26 "syscall"
27
28 "k8s.io/klog/v2"
29 )
30
31 // following failure codes are from https://docs.microsoft.com/en-us/windows/desktop/debug/system-error-codes--1300-1699-
32 // ERROR_BAD_NETPATH = 53
33 // ERROR_NETWORK_BUSY = 54
34 // ERROR_UNEXP_NET_ERR = 59
35 // ERROR_NETNAME_DELETED = 64
36 // ERROR_NETWORK_ACCESS_DENIED = 65
37 // ERROR_BAD_DEV_TYPE = 66
38 // ERROR_BAD_NET_NAME = 67
39 // ERROR_SESSION_CREDENTIAL_CONFLICT = 1219
40 // ERROR_LOGON_FAILURE = 1326
41 // WSAEHOSTDOWN = 10064
42 var errorNoList = [...]int{53, 54, 59, 64, 65, 66, 67, 1219, 1326, 10064}
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 if _, err := strconv.Atoi(disk); err != nil {
88 return fmt.Errorf("wrong disk number format: %q, err: %v", disk, err)
89 }
90 return nil
91 }
92
93 // isMountPointMatch determines if the mountpoint matches the dir
94 func isMountPointMatch(mp MountPoint, dir string) bool {
95 return mp.Path == dir
96 }
97
98 // PathExists returns true if the specified path exists.
99 // TODO: clean this up to use pkg/util/file/FileExists
100 func PathExists(path string) (bool, error) {
101 _, err := os.Stat(path)
102 if err == nil {
103 return true, nil
104 } else if os.IsNotExist(err) {
105 return false, nil
106 } else if IsCorruptedMnt(err) {
107 return true, err
108 }
109 return false, err
110 }