Skip to content

Commit

Permalink
Merge pull request kubernetes#81739 from codenrhoden/clarify-mkdir-mk…
Browse files Browse the repository at this point in the history
…file-behavior

Move MakeFile/Dir from HostUtil to host_path vol
  • Loading branch information
k8s-ci-robot authored Aug 27, 2019
2 parents bccf68b + 107039a commit bc46e8f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 62 deletions.
12 changes: 0 additions & 12 deletions pkg/util/mount/fake_hostutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions pkg/util/mount/hostutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 0 additions & 21 deletions pkg/util/mount/hostutil_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
23 changes: 0 additions & 23 deletions pkg/util/mount/hostutil_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 28 additions & 2 deletions pkg/volume/hostpath/host_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit bc46e8f

Please sign in to comment.