1
2
3
4
19
20 package hostutil
21
22 import (
23 "fmt"
24 "io/fs"
25 "os"
26 "path/filepath"
27 "strings"
28 "syscall"
29
30 "golang.org/x/sys/windows"
31 "k8s.io/klog/v2"
32 "k8s.io/kubernetes/pkg/util/filesystem"
33 "k8s.io/mount-utils"
34 utilpath "k8s.io/utils/path"
35 )
36
37
38 type HostUtil struct{}
39
40
41 func NewHostUtil() *HostUtil {
42 return &HostUtil{}
43 }
44
45
46 func (hu *HostUtil) GetDeviceNameFromMount(mounter mount.Interface, mountPath, pluginMountDir string) (string, error) {
47 return getDeviceNameFromMount(mounter, mountPath, pluginMountDir)
48 }
49
50
51
52
53 func getDeviceNameFromMount(mounter mount.Interface, mountPath, pluginMountDir string) (string, error) {
54 refs, err := mounter.GetMountRefs(mountPath)
55 if err != nil {
56 klog.V(4).Infof("GetMountRefs failed for mount path %q: %v", mountPath, err)
57 return "", err
58 }
59 if len(refs) == 0 {
60 return "", fmt.Errorf("directory %s is not mounted", mountPath)
61 }
62 basemountPath := mount.NormalizeWindowsPath(pluginMountDir)
63 for _, ref := range refs {
64 if strings.Contains(ref, basemountPath) {
65 volumeID, err := filepath.Rel(mount.NormalizeWindowsPath(basemountPath), ref)
66 if err != nil {
67 klog.Errorf("Failed to get volume id from mount %s - %v", mountPath, err)
68 return "", err
69 }
70 return volumeID, nil
71 }
72 }
73
74 return filepath.Base(mountPath), nil
75 }
76
77
78 func (hu *HostUtil) DeviceOpened(pathname string) (bool, error) {
79 return false, nil
80 }
81
82
83 func (hu *HostUtil) PathIsDevice(pathname string) (bool, error) {
84 return false, nil
85 }
86
87
88
89 func (hu *HostUtil) MakeRShared(path string) error {
90 return nil
91 }
92
93 func isSystemCannotAccessErr(err error) bool {
94 if fserr, ok := err.(*fs.PathError); ok {
95 errno, ok := fserr.Err.(syscall.Errno)
96 return ok && errno == windows.ERROR_CANT_ACCESS_FILE
97 }
98
99 return false
100 }
101
102
103 func (hu *(HostUtil)) GetFileType(pathname string) (FileType, error) {
104 filetype, err := getFileType(pathname)
105
106
107
108 if err == errUnknownFileType || isSystemCannotAccessErr(err) {
109 if isSocket, errSocket := filesystem.IsUnixDomainSocket(pathname); errSocket == nil && isSocket {
110 return FileTypeSocket, nil
111 }
112 }
113
114 return filetype, err
115 }
116
117
118 func (hu *HostUtil) PathExists(pathname string) (bool, error) {
119 return utilpath.Exists(utilpath.CheckFollowSymlink, pathname)
120 }
121
122
123 func (hu *HostUtil) EvalHostSymlinks(pathname string) (string, error) {
124 return filepath.EvalSymlinks(pathname)
125 }
126
127
128
129
130 func (hu *HostUtil) GetOwner(pathname string) (int64, int64, error) {
131 return -1, -1, nil
132 }
133
134
135
136 func (hu *HostUtil) GetSELinuxSupport(pathname string) (bool, error) {
137 return false, nil
138 }
139
140
141 func (hu *HostUtil) GetMode(pathname string) (os.FileMode, error) {
142 info, err := os.Stat(pathname)
143 if err != nil {
144 return 0, err
145 }
146 return info.Mode(), nil
147 }
148
149
150
151 func (hu *HostUtil) GetSELinuxMountContext(pathname string) (string, error) {
152 return "", nil
153 }
154
View as plain text