diff --git a/pkg/util/mount/fake_hostutil.go b/pkg/util/mount/fake_hostutil.go index c85ff70081f..f00eb402c1e 100644 --- a/pkg/util/mount/fake_hostutil.go +++ b/pkg/util/mount/fake_hostutil.go @@ -71,18 +71,6 @@ func (hu *FakeHostUtil) GetFileType(pathname string) (FileType, error) { return FileType("Directory"), nil } -// MakeDir creates a new directory. -// No-op for testing -func (hu *FakeHostUtil) MakeDir(pathname string) error { - return nil -} - -// MakeFile creates a new file. -// No-op for testing -func (hu *FakeHostUtil) MakeFile(pathname string) error { - return nil -} - // PathExists checks if pathname exists. func (hu *FakeHostUtil) PathExists(pathname string) (bool, error) { if _, ok := hu.Filesystem[pathname]; ok { diff --git a/pkg/util/mount/hostutil.go b/pkg/util/mount/hostutil.go index 25d8c17ce2a..0e748065377 100644 --- a/pkg/util/mount/hostutil.go +++ b/pkg/util/mount/hostutil.go @@ -57,10 +57,6 @@ type HostUtils interface { MakeRShared(path string) error // GetFileType checks for file/directory/socket/block/character devices. GetFileType(pathname string) (FileType, error) - // MakeFile creates an empty file. - MakeFile(pathname string) error - // MakeDir creates a new directory. - MakeDir(pathname string) error // PathExists tests if the given path already exists // Error is returned on any other error than "file not found". PathExists(pathname string) (bool, error) diff --git a/pkg/util/mount/hostutil_linux.go b/pkg/util/mount/hostutil_linux.go index e995a5c8715..e60c4b22289 100644 --- a/pkg/util/mount/hostutil_linux.go +++ b/pkg/util/mount/hostutil_linux.go @@ -140,27 +140,6 @@ func (hu *hostUtil) GetFileType(pathname string) (FileType, error) { return getFileType(pathname) } -func (hu *hostUtil) MakeDir(pathname string) error { - err := os.MkdirAll(pathname, os.FileMode(0755)) - if err != nil { - if !os.IsExist(err) { - return err - } - } - return nil -} - -func (hu *hostUtil) MakeFile(pathname string) error { - f, err := os.OpenFile(pathname, os.O_CREATE, os.FileMode(0644)) - defer f.Close() - if err != nil { - if !os.IsExist(err) { - return err - } - } - return nil -} - func (hu *hostUtil) PathExists(pathname string) (bool, error) { return utilpath.Exists(utilpath.CheckFollowSymlink, pathname) } diff --git a/pkg/util/mount/hostutil_windows.go b/pkg/util/mount/hostutil_windows.go index 20f340a9cde..87e9fc2e6c9 100644 --- a/pkg/util/mount/hostutil_windows.go +++ b/pkg/util/mount/hostutil_windows.go @@ -91,29 +91,6 @@ func (hu *(hostUtil)) GetFileType(pathname string) (FileType, error) { return getFileType(pathname) } -// MakeFile creates a new directory -func (hu *hostUtil) MakeDir(pathname string) error { - err := os.MkdirAll(pathname, os.FileMode(0755)) - if err != nil { - if !os.IsExist(err) { - return err - } - } - return nil -} - -// MakeFile creates an empty file -func (hu *hostUtil) MakeFile(pathname string) error { - f, err := os.OpenFile(pathname, os.O_CREATE, os.FileMode(0644)) - defer f.Close() - if err != nil { - if !os.IsExist(err) { - return err - } - } - return nil -} - // PathExists checks whether the path exists func (hu *hostUtil) PathExists(pathname string) (bool, error) { return utilpath.Exists(utilpath.CheckFollowSymlink, pathname) diff --git a/pkg/volume/hostpath/host_path.go b/pkg/volume/hostpath/host_path.go index 99ed22749ed..7541ec9dd28 100644 --- a/pkg/volume/hostpath/host_path.go +++ b/pkg/volume/hostpath/host_path.go @@ -377,7 +377,7 @@ func (ftc *fileTypeChecker) IsFile() bool { } func (ftc *fileTypeChecker) MakeFile() error { - return ftc.hu.MakeFile(ftc.path) + return makeFile(ftc.path) } func (ftc *fileTypeChecker) IsDir() bool { @@ -392,7 +392,7 @@ func (ftc *fileTypeChecker) IsDir() bool { } func (ftc *fileTypeChecker) MakeDir() error { - return ftc.hu.MakeDir(ftc.path) + return makeDir(ftc.path) } func (ftc *fileTypeChecker) IsBlock() bool { @@ -470,3 +470,29 @@ func checkTypeInternal(ftc hostPathTypeChecker, pathType *v1.HostPathType) error return nil } + +// makeDir creates a new directory. +// If pathname already exists as a directory, no error is returned. +// If pathname already exists as a file, an error is returned. +func makeDir(pathname string) error { + err := os.MkdirAll(pathname, os.FileMode(0755)) + if err != nil { + if !os.IsExist(err) { + return err + } + } + return nil +} + +// makeFile creates an empty file. +// If pathname already exists, whether a file or directory, no error is returned. +func makeFile(pathname string) error { + f, err := os.OpenFile(pathname, os.O_CREATE, os.FileMode(0644)) + defer f.Close() + if err != nil { + if !os.IsExist(err) { + return err + } + } + return nil +}