[KLIB] Add ZIP file system accessor
The accessor interface is used for reading klib archives in place.
This commit is contained in:
committed by
Space Team
parent
af1f3a102b
commit
e4406638bd
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.konan.file
|
||||
|
||||
import java.nio.file.FileSystem
|
||||
|
||||
interface ZipFileSystemAccessor {
|
||||
fun <T> withZipFileSystem(zipFile: File, action: (FileSystem) -> T): T
|
||||
}
|
||||
|
||||
object ZipFileSystemInPlaceAccessor : ZipFileSystemAccessor {
|
||||
override fun <T> withZipFileSystem(zipFile: File, action: (FileSystem) -> T): T {
|
||||
return zipFile.withZipFileSystem(action)
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.library.impl
|
||||
|
||||
import org.jetbrains.kotlin.konan.file.File
|
||||
import org.jetbrains.kotlin.konan.file.ZipFileSystemAccessor
|
||||
import org.jetbrains.kotlin.konan.properties.Properties
|
||||
import org.jetbrains.kotlin.konan.properties.loadProperties
|
||||
import org.jetbrains.kotlin.library.*
|
||||
@@ -322,11 +323,12 @@ fun createKotlinLibrary(
|
||||
libraryFile: File,
|
||||
component: String,
|
||||
isDefault: Boolean = false,
|
||||
perFile: Boolean = false
|
||||
perFile: Boolean = false,
|
||||
zipAccessor: ZipFileSystemAccessor? = null,
|
||||
): KotlinLibrary {
|
||||
val baseAccess = BaseLibraryAccess<KotlinLibraryLayout>(libraryFile, component)
|
||||
val metadataAccess = MetadataLibraryAccess<MetadataKotlinLibraryLayout>(libraryFile, component)
|
||||
val irAccess = IrLibraryAccess<IrKotlinLibraryLayout>(libraryFile, component)
|
||||
val baseAccess = BaseLibraryAccess<KotlinLibraryLayout>(libraryFile, component, zipAccessor)
|
||||
val metadataAccess = MetadataLibraryAccess<MetadataKotlinLibraryLayout>(libraryFile, component, zipAccessor)
|
||||
val irAccess = IrLibraryAccess<IrKotlinLibraryLayout>(libraryFile, component, zipAccessor)
|
||||
|
||||
val base = BaseKotlinLibraryImpl(baseAccess, isDefault)
|
||||
val metadata = MetadataLibraryImpl(metadataAccess)
|
||||
@@ -337,12 +339,13 @@ fun createKotlinLibrary(
|
||||
|
||||
fun createKotlinLibraryComponents(
|
||||
libraryFile: File,
|
||||
isDefault: Boolean = true
|
||||
) : List<KotlinLibrary> {
|
||||
val baseAccess = BaseLibraryAccess<KotlinLibraryLayout>(libraryFile, null)
|
||||
isDefault: Boolean = true,
|
||||
zipAccessor: ZipFileSystemAccessor? = null,
|
||||
): List<KotlinLibrary> {
|
||||
val baseAccess = BaseLibraryAccess<KotlinLibraryLayout>(libraryFile, null, zipAccessor)
|
||||
val base = BaseKotlinLibraryImpl(baseAccess, isDefault)
|
||||
return base.componentList.map {
|
||||
createKotlinLibrary(libraryFile, it, isDefault)
|
||||
createKotlinLibrary(libraryFile, it, isDefault, zipAccessor = zipAccessor)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
package org.jetbrains.kotlin.library.impl
|
||||
|
||||
import org.jetbrains.kotlin.konan.file.File
|
||||
import org.jetbrains.kotlin.konan.file.file
|
||||
import org.jetbrains.kotlin.konan.file.unzipTo
|
||||
import org.jetbrains.kotlin.konan.file.withZipFileSystem
|
||||
import org.jetbrains.kotlin.konan.file.*
|
||||
import org.jetbrains.kotlin.library.*
|
||||
import org.jetbrains.kotlin.util.removeSuffixIfPresent
|
||||
import java.nio.file.FileSystem
|
||||
@@ -54,9 +51,11 @@ class IrLibraryLayoutImpl(klib: File, component: String) : KotlinLibraryLayoutIm
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
open class BaseLibraryAccess<L : KotlinLibraryLayout>(val klib: File, component: String?) {
|
||||
open class BaseLibraryAccess<L : KotlinLibraryLayout>(val klib: File, component: String?, zipAccessor: ZipFileSystemAccessor? = null) {
|
||||
open val layout = KotlinLibraryLayoutImpl(klib, component)
|
||||
|
||||
private val klibZipAccessor = zipAccessor ?: ZipFileSystemInPlaceAccessor
|
||||
|
||||
fun <T> realFiles(action: (L) -> T): T =
|
||||
if (layout.isZipped)
|
||||
action(layout.extractingToTemp as L)
|
||||
@@ -65,7 +64,7 @@ open class BaseLibraryAccess<L : KotlinLibraryLayout>(val klib: File, component:
|
||||
|
||||
fun <T> inPlace(action: (L) -> T): T =
|
||||
if (layout.isZipped)
|
||||
layout.klib.withZipFileSystem { zipFileSystem ->
|
||||
klibZipAccessor.withZipFileSystem(layout.klib) { zipFileSystem ->
|
||||
action(layout.directlyFromZip(zipFileSystem) as L)
|
||||
}
|
||||
else
|
||||
@@ -73,11 +72,13 @@ open class BaseLibraryAccess<L : KotlinLibraryLayout>(val klib: File, component:
|
||||
}
|
||||
|
||||
|
||||
class MetadataLibraryAccess<L : KotlinLibraryLayout>(klib: File, component: String) : BaseLibraryAccess<L>(klib, component) {
|
||||
class MetadataLibraryAccess<L : KotlinLibraryLayout>(klib: File, component: String, zipAccessor: ZipFileSystemAccessor? = null) :
|
||||
BaseLibraryAccess<L>(klib, component, zipAccessor) {
|
||||
override val layout = MetadataLibraryLayoutImpl(klib, component)
|
||||
}
|
||||
|
||||
class IrLibraryAccess<L : KotlinLibraryLayout>(klib: File, component: String) : BaseLibraryAccess<L>(klib, component) {
|
||||
class IrLibraryAccess<L : KotlinLibraryLayout>(klib: File, component: String, zipAccessor: ZipFileSystemAccessor? = null) :
|
||||
BaseLibraryAccess<L>(klib, component, zipAccessor) {
|
||||
override val layout = IrLibraryLayoutImpl(klib, component)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user