| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,8 @@ | |||
| 1 | 1 | package com.github.dockerjava.api.model; | |
| 2 | 2 | ||
| 3 | 3 | import static com.google.common.base.Preconditions.checkNotNull; | |
| 4 | + import static org.apache.commons.lang.BooleanUtils.isNotTrue; | ||
| 5 | + import static org.apache.commons.lang.StringUtils.isEmpty; | ||
| 4 | 6 | ||
| 5 | 7 | import org.apache.commons.lang.builder.EqualsBuilder; | |
| 6 | 8 | import org.apache.commons.lang.builder.HashCodeBuilder; | |
@@ -9,6 +11,10 @@ | |||
| 9 | 11 | import com.fasterxml.jackson.annotation.JsonInclude.Include; | |
| 10 | 12 | import com.fasterxml.jackson.annotation.JsonProperty; | |
| 11 | 13 | ||
| 14 | + import javax.annotation.Nonnull; | ||
| 15 | + import java.util.HashMap; | ||
| 16 | + import java.util.Map; | ||
| 17 | + | ||
| 12 | 18 | @JsonInclude(Include.NON_NULL) | |
| 13 | 19 | public class Device { | |
| 14 | 20 | ||
@@ -45,6 +51,66 @@ public String getPathOnHost() { | |||
| 45 | 51 | return pathOnHost; | |
| 46 | 52 | } | |
| 47 | 53 | ||
| 54 | + @Nonnull | ||
| 55 | + public static Device parse(@Nonnull String deviceStr) { | ||
| 56 | + String src = ""; | ||
| 57 | + String dst = ""; | ||
| 58 | + String permissions = "rwm"; | ||
| 59 | + final String[] arr = deviceStr.split(":"); | ||
| 60 | + switch (arr.length) { | ||
| 61 | + case 3: { | ||
| 62 | + permissions = arr[2]; | ||
| 63 | + } | ||
| 64 | + case 2: { | ||
| 65 | + if (validDeviceMode(arr[1])) { | ||
| 66 | + permissions = arr[1]; | ||
| 67 | + } else { | ||
| 68 | + dst = arr[1]; | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + case 1: { | ||
| 72 | + src = arr[0]; | ||
| 73 | + break; | ||
| 74 | + } | ||
| 75 | + default: { | ||
| 76 | + throw new IllegalArgumentException("invalid device specification: " + deviceStr); | ||
| 77 | + } | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + if (isEmpty(dst)) { | ||
| 81 | + dst = src; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + return new Device(permissions, dst, src); | ||
| 85 | + } | ||
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * ValidDeviceMode checks if the mode for device is valid or not. | ||
| 89 | + * Valid mode is a composition of r (read), w (write), and m (mknod). | ||
| 90 | + * | ||
| 91 | + * @link https://github.com/docker/docker/blob/6b4a46f28266031ce1a1315f17fb69113a06efe1/runconfig/opts/parse.go#L796 | ||
| 92 | + */ | ||
| 93 | + private static boolean validDeviceMode(String deviceMode) { | ||
| 94 | + Map<String, Boolean> validModes = new HashMap<>(3); | ||
| 95 | + validModes.put("r", true); | ||
| 96 | + validModes.put("w", true); | ||
| 97 | + validModes.put("m", true); | ||
| 98 | + | ||
| 99 | + if (isEmpty(deviceMode)) { | ||
| 100 | + return false; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + for (char ch : deviceMode.toCharArray()) { | ||
| 104 | + final String mode = String.valueOf(ch); | ||
| 105 | + if (isNotTrue(validModes.get(mode))) { | ||
| 106 | + return false; // wrong mode | ||
| 107 | + } | ||
| 108 | + validModes.put(mode, false); | ||
| 109 | + } | ||
| 110 | + | ||
| 111 | + return true; | ||
| 112 | + } | ||
| 113 | + | ||
| 48 | 114 | @Override | |
| 49 | 115 | public boolean equals(Object obj) { | |
| 50 | 116 | if (obj instanceof Device) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,27 @@ | |||
| 1 | + package com.github.dockerjava.api.model; | ||
| 2 | + | ||
| 3 | + import org.testng.annotations.Test; | ||
| 4 | + | ||
| 5 | + import static org.hamcrest.MatcherAssert.assertThat; | ||
| 6 | + import static org.hamcrest.Matchers.equalTo; | ||
| 7 | + | ||
| 8 | + /** | ||
| 9 | + * @author Kanstantsin Shautsou | ||
| 10 | + */ | ||
| 11 | + public class DeviceTest { | ||
| 12 | + @Test | ||
| 13 | + public void testParse() throws Exception { | ||
| 14 | + assertThat(Device.parse("/dev/sda:/dev/xvdc:r"), | ||
| 15 | + equalTo(new Device("r", "/dev/xvdc", "/dev/sda"))); | ||
| 16 | + | ||
| 17 | + assertThat(Device.parse("/dev/snd:rw"), | ||
| 18 | + equalTo(new Device("rw", "/dev/snd", "/dev/snd"))); | ||
| 19 | + | ||
| 20 | + assertThat(Device.parse("/dev/snd:/something"), | ||
| 21 | + equalTo(new Device("rwm", "/something", "/dev/snd"))); | ||
| 22 | + | ||
| 23 | + assertThat(Device.parse("/dev/snd:/something:rw"), | ||
| 24 | + equalTo(new Device("rw", "/something", "/dev/snd"))); | ||
| 25 | + | ||
| 26 | + } | ||
| 27 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments