aboutsummaryrefslogtreecommitdiff
path: root/pkg/spec/parse.go
diff options
context:
space:
mode:
authorDaniel J Walsh <dwalsh@redhat.com>2018-08-17 18:17:44 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-08-20 13:42:07 +0000
commit462c503a4762a0f20023d937a5fb05a55d4183a3 (patch)
tree5c82069a9e41364107f0b7b0134cee2c15d8037c /pkg/spec/parse.go
parente40c99a19ecc0edf5d45ea3c861cd48ce2e22448 (diff)
downloadpodman-462c503a4762a0f20023d937a5fb05a55d4183a3.tar.gz
podman-462c503a4762a0f20023d937a5fb05a55d4183a3.tar.bz2
podman-462c503a4762a0f20023d937a5fb05a55d4183a3.zip
Fix handling of devices
Devices are supposed to be able to be passed in via the form of --device /dev/foo --device /dev/foo:/dev/bar --device /dev/foo:rwm --device /dev/foo:/dev/bar:rwm Signed-off-by: Daniel J Walsh <dwalsh@redhat.com> Closes: #1299 Approved by: umohnani8
Diffstat (limited to 'pkg/spec/parse.go')
-rw-r--r--pkg/spec/parse.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/pkg/spec/parse.go b/pkg/spec/parse.go
index d4a655e4f..d34e10760 100644
--- a/pkg/spec/parse.go
+++ b/pkg/spec/parse.go
@@ -126,3 +126,58 @@ func getLoggingPath(opts []string) string {
}
return ""
}
+
+// parseDevice parses device mapping string to a src, dest & permissions string
+func parseDevice(device string) (string, string, string, error) { //nolint
+ src := ""
+ dst := ""
+ permissions := "rwm"
+ arr := strings.Split(device, ":")
+ switch len(arr) {
+ case 3:
+ if !validDeviceMode(arr[2]) {
+ return "", "", "", fmt.Errorf("invalid device mode: %s", arr[2])
+ }
+ permissions = arr[2]
+ fallthrough
+ case 2:
+ if validDeviceMode(arr[1]) {
+ permissions = arr[1]
+ } else {
+ if arr[1][0] != '/' {
+ return "", "", "", fmt.Errorf("invalid device mode: %s", arr[2])
+ }
+ dst = arr[1]
+ }
+ fallthrough
+ case 1:
+ src = arr[0]
+ default:
+ return "", "", "", fmt.Errorf("invalid device specification: %s", device)
+ }
+
+ if dst == "" {
+ dst = src
+ }
+ return src, dst, permissions, nil
+}
+
+// validDeviceMode checks if the mode for device is valid or not.
+// Valid mode is a composition of r (read), w (write), and m (mknod).
+func validDeviceMode(mode string) bool {
+ var legalDeviceMode = map[rune]bool{
+ 'r': true,
+ 'w': true,
+ 'm': true,
+ }
+ if mode == "" {
+ return false
+ }
+ for _, c := range mode {
+ if !legalDeviceMode[c] {
+ return false
+ }
+ legalDeviceMode[c] = false
+ }
+ return true
+}