mount_helper_windows.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // +build windows
  2. /*
  3. Copyright 2019 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package mount
  15. import (
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "syscall"
  21. "k8s.io/klog"
  22. )
  23. // following failure codes are from https://docs.microsoft.com/en-us/windows/desktop/debug/system-error-codes--1300-1699-
  24. // ERROR_BAD_NETPATH = 53
  25. // ERROR_NETWORK_BUSY = 54
  26. // ERROR_UNEXP_NET_ERR = 59
  27. // ERROR_NETNAME_DELETED = 64
  28. // ERROR_NETWORK_ACCESS_DENIED = 65
  29. // ERROR_BAD_DEV_TYPE = 66
  30. // ERROR_BAD_NET_NAME = 67
  31. // ERROR_SESSION_CREDENTIAL_CONFLICT = 1219
  32. // ERROR_LOGON_FAILURE = 1326
  33. var errorNoList = [...]int{53, 54, 59, 64, 65, 66, 67, 1219, 1326}
  34. // IsCorruptedMnt return true if err is about corrupted mount point
  35. func IsCorruptedMnt(err error) bool {
  36. if err == nil {
  37. return false
  38. }
  39. var underlyingError error
  40. switch pe := err.(type) {
  41. case nil:
  42. return false
  43. case *os.PathError:
  44. underlyingError = pe.Err
  45. case *os.LinkError:
  46. underlyingError = pe.Err
  47. case *os.SyscallError:
  48. underlyingError = pe.Err
  49. }
  50. if ee, ok := underlyingError.(syscall.Errno); ok {
  51. for _, errno := range errorNoList {
  52. if int(ee) == errno {
  53. klog.Warningf("IsCorruptedMnt failed with error: %v, error code: %v", err, errno)
  54. return true
  55. }
  56. }
  57. }
  58. return false
  59. }
  60. // NormalizeWindowsPath makes sure the given path is a valid path on Windows
  61. // systems by making sure all instances of `/` are replaced with `\\`, and the
  62. // path beings with `c:`
  63. func NormalizeWindowsPath(path string) string {
  64. normalizedPath := strings.Replace(path, "/", "\\", -1)
  65. if strings.HasPrefix(normalizedPath, "\\") {
  66. normalizedPath = "c:" + normalizedPath
  67. }
  68. return normalizedPath
  69. }
  70. // ValidateDiskNumber : disk number should be a number in [0, 99]
  71. func ValidateDiskNumber(disk string) error {
  72. diskNum, err := strconv.Atoi(disk)
  73. if err != nil {
  74. return fmt.Errorf("wrong disk number format: %q, err:%v", disk, err)
  75. }
  76. if diskNum < 0 || diskNum > 99 {
  77. return fmt.Errorf("disk number out of range: %q", disk)
  78. }
  79. return nil
  80. }
  81. // isMountPointMatch determines if the mountpoint matches the dir
  82. func isMountPointMatch(mp MountPoint, dir string) bool {
  83. return mp.Path == dir
  84. }