55
66package org.jetbrains.kotlin.native.executors
77
8- import java.nio.file.FileSystems
9- import java.nio.file.FileVisitResult
8+ import java.io.File
9+ import java.net.JarURLConnection
10+ import java.net.URL
1011import java.nio.file.Path
1112import kotlin.io.path.*
13+ import kotlin.streams.asSequence
14+
15+ private fun URL.copyRecursively (to : Path ) {
16+ // Cannot use ZipFileSystem, because it's impossible to make it read-only w.r.t. SecurityManager.
17+ when (protocol) {
18+ " jar" -> {
19+ val connection = openConnection() as JarURLConnection
20+ val rootPath = Path (connection.entryName)
21+ connection.jarFile.use { jarFile ->
22+ jarFile.stream().asSequence()
23+ .filter { ! it.isDirectory && it.name.startsWith(connection.entryName) }
24+ .forEach { entry ->
25+ val entryRelativePath = Path (entry.name).relativeTo(rootPath)
26+ val targetFile = to.resolve(entryRelativePath)
27+ targetFile.createParentDirectories()
28+ jarFile.getInputStream(entry).use { input ->
29+ targetFile.outputStream().use { output ->
30+ input.copyTo(output)
31+ }
32+ }
33+ }
34+ }
35+ }
36+ else -> {
37+ File (toURI()).copyRecursively(to.toFile())
38+ }
39+ }
40+ }
1241
1342/* *
1443 * Represents an Xcode project.
@@ -24,7 +53,7 @@ internal class XcodeProject(private val workDir: Path) {
2453 /* *
2554 * Path to the project root
2655 */
27- val path: Path = workDir.resolve(PROJECT_RESOURCE_PATH .removePrefix( " / " )).resolve( PROJECT_NAME )
56+ val path: Path = workDir.resolve(PROJECT_NAME )
2857
2958 /* *
3059 * Represents the derived data directory for a Xcode project.
@@ -81,27 +110,7 @@ internal class XcodeProject(private val workDir: Path) {
81110 }
82111 it.createDirectory()
83112 }
84-
85- FileSystems .newFileSystem(url.toURI(), emptyMap<String , Any >()).use {
86- val fsRootPath = it.rootDirectories.singleOrNull()?.root ? : error(" Root of the file system attached to $url not found" )
87-
88- fsRootPath.resolve(PROJECT_RESOURCE_PATH ).visitFileTree {
89- onPreVisitDirectory { directory, _ ->
90- val destination = projectPath.resolveRelativeToRoot(directory)
91- if (! destination.exists()) {
92- destination.createDirectory()
93- }
94-
95- FileVisitResult .CONTINUE
96- }
97- onVisitFile { file, _ ->
98- val destination = projectPath.resolveRelativeToRoot(file)
99- file.copyTo(destination)
100-
101- FileVisitResult .CONTINUE
102- }
103- }
104- }
113+ url.copyRecursively(projectPath)
105114 }
106115 }
107116
@@ -150,10 +159,6 @@ internal class XcodeProject(private val workDir: Path) {
150159 }
151160 }
152161
153- /* *
154- * Resolves a given path relative to the root, which may be different between these two paths.
155- */
156- private fun Path.resolveRelativeToRoot (path : Path ) = this .resolve(path.root.relativize(path).pathString)
157162
158163 /* *
159164 * Performs project code signing.
0 commit comments