-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move chwrap common functionality to internal package (#1723)
* move chwrap common functionality to osutils and make example call from nodeprep * moved chwrap common functions to internal/chwrap * fix root path * renamed and added constant * fix linter issue * gofumpt it * review comments inject only findBinary and remove unnecessary interface * chwrap not windows * change string type to alias * add windows stub
- Loading branch information
1 parent
6d55b38
commit 397c153
Showing
6 changed files
with
150 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package chwrap | ||
|
||
import "golang.org/x/sys/unix" | ||
|
||
const ( | ||
hostRootPath = "/host" | ||
containerRootPath = "/" | ||
) | ||
|
||
func validBinary(path string) bool { | ||
var stat unix.Stat_t | ||
if err := unix.Stat(path, &stat); nil != err { | ||
// Can't stat file | ||
return false | ||
} | ||
if (stat.Mode&unix.S_IFMT) != unix.S_IFREG && (stat.Mode&unix.S_IFMT) != unix.S_IFLNK { | ||
// Not a regular file or symlink | ||
return false | ||
} | ||
if 0 == stat.Mode&unix.S_IRUSR || 0 == stat.Mode&unix.S_IXUSR { | ||
// Not readable or not executable | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func findBinary(prefix, binary string) string { | ||
for _, part1 := range []string{"usr/local/", "usr/", ""} { | ||
for _, part2 := range []string{"sbin", "bin"} { | ||
path := "/" + part1 + part2 + "/" + binary | ||
if validBinary(prefix + path) { | ||
return path | ||
} | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func FindBinary(binary string) (rootPath, fullPath string) { | ||
if fullPath = findBinary(hostRootPath, binary); fullPath != "" { | ||
return hostRootPath, fullPath | ||
} | ||
return containerRootPath, findBinary("", binary) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package chwrap | ||
|
||
func FindBinary(binary string) (rootPath, fullPath string) { | ||
return "", "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters