Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Windows path for Docker Desktop and Podman #26415

Merged
merged 1 commit into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ protected List<String> getContainerRuntimeBuildArgs() {
List<String> containerRuntimeArgs = super.getContainerRuntimeBuildArgs();
String volumeOutputPath = outputPath;
if (SystemUtils.IS_OS_WINDOWS) {
volumeOutputPath = FileUtil.translateToVolumePath(volumeOutputPath,
containerRuntime == ContainerRuntimeUtil.ContainerRuntime.PODMAN);
volumeOutputPath = FileUtil.translateToVolumePath(volumeOutputPath);
}

Collections.addAll(containerRuntimeArgs, "-v",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ private boolean runUpxInContainer(NativeImageBuildItem nativeImage, NativeConfig

String volumeOutputPath = nativeImage.getPath().toFile().getParentFile().getAbsolutePath();
if (SystemUtils.IS_OS_WINDOWS) {
volumeOutputPath = FileUtil.translateToVolumePath(volumeOutputPath,
containerRuntime == ContainerRuntimeUtil.ContainerRuntime.PODMAN);
volumeOutputPath = FileUtil.translateToVolumePath(volumeOutputPath);
} else if (SystemUtils.IS_OS_LINUX) {
String uid = getLinuxID("-ur");
String gid = getLinuxID("-gr");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,25 @@ public static byte[] readFileContents(InputStream inputStream) throws IOExceptio
}

/**
* Translates a file path from the Windows Style to a syntax accepted by Docker,
* so that volumes be safely mounted in both Docker for Windows and the legacy
* Docker Toolbox.
* Translates a file path from the Windows Style to a syntax accepted by Docker and Podman,
* so that volumes be safely mounted in both Docker Desktop for Windows and Podman Windows.
* <p>
* <code>docker run -v //c/foo/bar:/somewhere (...)</code>
* <code>docker run -v /c/foo/bar:/somewhere (...)</code>
* <p>
* You should only use this method on Windows-style paths, and not Unix-style
* paths.
*
* @see https://github.com/quarkusio/quarkus/issues/5360
* @param windowsStylePath A path formatted in Windows-style, e.g. "C:\foo\bar".
* @return A translated path accepted by Docker, e.g. "//c/foo/bar".
* @return A translated path accepted by Docker, e.g. "/c/foo/bar".
*/
public static String translateToVolumePath(String windowsStylePath, boolean isPodman) {
public static String translateToVolumePath(String windowsStylePath) {
String translated = windowsStylePath.replace('\\', '/');
Pattern p = Pattern.compile("^(\\w)(?:$|:(/)?(.*))");
Matcher m = p.matcher(translated);
if (m.matches()) {
String slash = Optional.ofNullable(m.group(2)).orElse("/");
String path = Optional.ofNullable(m.group(3)).orElse("");
// Ad `/' and `//' see https://github.com/containers/podman/issues/14414
return (isPodman ? "/" : "//") + m.group(1).toLowerCase() + slash + path;
return "/" + m.group(1).toLowerCase() + slash + path;
}
return translated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ public class FileUtilTest {
@Test
public void testTranslateToVolumePath() {
// Windows-Style paths are formatted.
assertEquals("/c/tmp/code-with-quarkus", translateToVolumePath("C:\\tmp\\code-with-quarkus", true));
assertEquals("//c/", translateToVolumePath("C", false));
assertEquals("//c/", translateToVolumePath("C:", false));
assertEquals("//c/", translateToVolumePath("C:\\", false));
assertEquals("//c/Users", translateToVolumePath("C:\\Users", false));
assertEquals("//c/Users/Quarkus/lambdatest-1.0-SNAPSHOT-native-image-source-jar",
translateToVolumePath("C:\\Users\\Quarkus\\lambdatest-1.0-SNAPSHOT-native-image-source-jar", false));
assertEquals("/c/tmp/code-with-quarkus", translateToVolumePath("C:\\tmp\\code-with-quarkus"));
assertEquals("/c/", translateToVolumePath("C"));
assertEquals("/c/", translateToVolumePath("C:"));
assertEquals("/c/", translateToVolumePath("C:\\"));
assertEquals("/c/Users", translateToVolumePath("C:\\Users"));
assertEquals("/c/Users/Quarkus/lambdatest-1.0-SNAPSHOT-native-image-source-jar",
translateToVolumePath("C:\\Users\\Quarkus\\lambdatest-1.0-SNAPSHOT-native-image-source-jar"));

// Side effect for Unix-style path.
assertEquals("//c/Users/Quarkus", translateToVolumePath("c:/Users/Quarkus", false));
assertEquals("/c/Users/Quarkus", translateToVolumePath("c:/Users/Quarkus"));

// Side effects for fancy inputs - for the sake of documentation.
assertEquals("something/bizarre", translateToVolumePath("something\\bizarre", false));
assertEquals("something.bizarre", translateToVolumePath("something.bizarre", false));
assertEquals("something/bizarre", translateToVolumePath("something\\bizarre"));
assertEquals("something.bizarre", translateToVolumePath("something.bizarre"));
}

}