k8s.io/kubernetes@v1.29.3/pkg/kubelet/container/os.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 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 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package container 18 19 import ( 20 "os" 21 "path/filepath" 22 "time" 23 ) 24 25 // OSInterface collects system level operations that need to be mocked out 26 // during tests. 27 type OSInterface interface { 28 MkdirAll(path string, perm os.FileMode) error 29 Symlink(oldname string, newname string) error 30 Stat(path string) (os.FileInfo, error) 31 Remove(path string) error 32 RemoveAll(path string) error 33 Create(path string) (*os.File, error) 34 Chmod(path string, perm os.FileMode) error 35 Hostname() (name string, err error) 36 Chtimes(path string, atime time.Time, mtime time.Time) error 37 Pipe() (r *os.File, w *os.File, err error) 38 ReadDir(dirname string) ([]os.DirEntry, error) 39 Glob(pattern string) ([]string, error) 40 Open(name string) (*os.File, error) 41 OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) 42 Rename(oldpath, newpath string) error 43 } 44 45 // RealOS is used to dispatch the real system level operations. 46 type RealOS struct{} 47 48 // MkdirAll will call os.MkdirAll to create a directory. 49 func (RealOS) MkdirAll(path string, perm os.FileMode) error { 50 return os.MkdirAll(path, perm) 51 } 52 53 // Symlink will call os.Symlink to create a symbolic link. 54 func (RealOS) Symlink(oldname string, newname string) error { 55 return os.Symlink(oldname, newname) 56 } 57 58 // Stat will call os.Stat to get the FileInfo for a given path 59 func (RealOS) Stat(path string) (os.FileInfo, error) { 60 return os.Stat(path) 61 } 62 63 // Remove will call os.Remove to remove the path. 64 func (RealOS) Remove(path string) error { 65 return os.Remove(path) 66 } 67 68 // RemoveAll will call os.RemoveAll to remove the path and its children. 69 func (RealOS) RemoveAll(path string) error { 70 return os.RemoveAll(path) 71 } 72 73 // Create will call os.Create to create and return a file 74 // at path. 75 func (RealOS) Create(path string) (*os.File, error) { 76 return os.Create(path) 77 } 78 79 // Chmod will change the permissions on the specified path or return 80 // an error. 81 func (RealOS) Chmod(path string, perm os.FileMode) error { 82 return os.Chmod(path, perm) 83 } 84 85 // Hostname will call os.Hostname to return the hostname. 86 func (RealOS) Hostname() (name string, err error) { 87 return os.Hostname() 88 } 89 90 // Chtimes will call os.Chtimes to change the atime and mtime of the path 91 func (RealOS) Chtimes(path string, atime time.Time, mtime time.Time) error { 92 return os.Chtimes(path, atime, mtime) 93 } 94 95 // Pipe will call os.Pipe to return a connected pair of pipe. 96 func (RealOS) Pipe() (r *os.File, w *os.File, err error) { 97 return os.Pipe() 98 } 99 100 // ReadDir will call os.ReadDir to return the files under the directory. 101 func (RealOS) ReadDir(dirname string) ([]os.DirEntry, error) { 102 return os.ReadDir(dirname) 103 } 104 105 // Glob will call filepath.Glob to return the names of all files matching 106 // pattern. 107 func (RealOS) Glob(pattern string) ([]string, error) { 108 return filepath.Glob(pattern) 109 } 110 111 // Open will call os.Open to return the file. 112 func (RealOS) Open(name string) (*os.File, error) { 113 return os.Open(name) 114 } 115 116 // OpenFile will call os.OpenFile to return the file. 117 func (RealOS) OpenFile(name string, flag int, perm os.FileMode) (*os.File, error) { 118 return os.OpenFile(name, flag, perm) 119 } 120 121 // Rename will call os.Rename to rename a file. 122 func (RealOS) Rename(oldpath, newpath string) error { 123 return os.Rename(oldpath, newpath) 124 }