Skip to content

Commit

Permalink
Replace Files static calls with kotlin extension functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Jan 13, 2024
1 parent e33628a commit 3a5c70f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import org.gradle.api.file.DirectoryProperty
import org.gradle.api.tasks.OutputDirectory
import java.io.File
import java.nio.file.FileAlreadyExistsException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardOpenOption
import kotlin.io.path.*

/**
* @author Silvio Giebl
Expand All @@ -24,11 +24,11 @@ abstract class DistributionRegistryDataTask : OciImagesInputTask() {
digestToLayer: Map<OciDigest, File>,
) {
val registryDataDirectory = registryDataDirectory.get().asFile.toPath().ensureEmptyDirectory()
val blobsDirectory: Path = Files.createDirectory(registryDataDirectory.resolve("blobs"))
val repositoriesDirectory: Path = Files.createDirectory(registryDataDirectory.resolve("repositories"))
val blobsDirectory = registryDataDirectory.resolve("blobs").createDirectory()
val repositoriesDirectory = registryDataDirectory.resolve("repositories").createDirectory()

for ((digest, layer) in digestToLayer) {
Files.createLink(blobsDirectory.resolveDigestDataFile(digest), layer.toPath())
blobsDirectory.resolveDigestDataFile(digest).createLinkPointingTo(layer.toPath())
}
for ((resolvedComponent, imageReferences) in resolvedComponentToImageReferences) {
writeImage(resolvedComponent, imageReferences, blobsDirectory, repositoriesDirectory)
Expand Down Expand Up @@ -64,56 +64,56 @@ abstract class DistributionRegistryDataTask : OciImagesInputTask() {
val indexDigest = index.digest

for (imageReference in imageReferences) {
val repositoryDirectory: Path = Files.createDirectories(repositoriesDirectory.resolve(imageReference.name))
val layersDirectory: Path = Files.createDirectories(repositoryDirectory.resolve("_layers"))
val repositoryDirectory = repositoriesDirectory.resolve(imageReference.name).createDirectories()
val layersDirectory = repositoryDirectory.resolve("_layers").createDirectories()
for (blobDigest in blobDigests) {
layersDirectory.writeDigestLink(blobDigest)
}
val manifestsDirectory: Path = Files.createDirectories(repositoryDirectory.resolve("_manifests"))
val manifestRevisionsDirectory: Path = Files.createDirectories(manifestsDirectory.resolve("revisions"))
val manifestsDirectory = repositoryDirectory.resolve("_manifests").createDirectories()
val manifestRevisionsDirectory = manifestsDirectory.resolve("revisions").createDirectories()
for ((_, manifestDescriptor) in manifests) {
manifestRevisionsDirectory.writeDigestLink(manifestDescriptor.digest)
}
manifestRevisionsDirectory.writeDigestLink(indexDigest)
val tagDirectory: Path =
Files.createDirectories(manifestsDirectory.resolve("tags").resolve(imageReference.tag))
val tagDirectory = manifestsDirectory.resolve("tags").resolve(imageReference.tag).createDirectories()
tagDirectory.writeTagLink(indexDigest)
Files.createDirectories(tagDirectory.resolve("index")).writeDigestLink(indexDigest)
tagDirectory.resolve("index").createDirectories().writeDigestLink(indexDigest)
}
}

private fun Path.resolveDigestDataFile(digest: OciDigest): Path {
val encodedHash = digest.encodedHash
return Files.createDirectories(
resolve(digest.algorithm.ociPrefix).resolve(encodedHash.substring(0, 2)).resolve(encodedHash)
).resolve("data")
return resolve(digest.algorithm.ociPrefix).resolve(encodedHash.substring(0, 2))
.resolve(encodedHash)
.createDirectories()
.resolve("data")
}

private fun Path.writeDigestData(dataDescriptor: OciDataDescriptor) {
val digestDataFile = resolveDigestDataFile(dataDescriptor.digest)
try {
Files.write(digestDataFile, dataDescriptor.data, StandardOpenOption.CREATE_NEW)
digestDataFile.writeBytes(dataDescriptor.data, StandardOpenOption.CREATE_NEW)
} catch (e: FileAlreadyExistsException) {
if (!dataDescriptor.data.contentEquals(Files.readAllBytes(digestDataFile))) {
if (!dataDescriptor.data.contentEquals(digestDataFile.readBytes())) {
throw IllegalStateException("hash collision for digest ${dataDescriptor.digest}: expected file content of $digestDataFile to be the same as ${dataDescriptor.data.contentToString()}")
}
}
}

private fun Path.writeDigestLink(digest: OciDigest) {
Files.write(
Files.createDirectories(resolve(digest.algorithm.ociPrefix).resolve(digest.encodedHash)).resolve("link"),
digest.toString().toByteArray(),
)
resolve(digest.algorithm.ociPrefix).resolve(digest.encodedHash)
.createDirectories()
.resolve("link")
.writeBytes(digest.toString().toByteArray())
}

private fun Path.writeTagLink(digest: OciDigest) {
val tagLinkFile: Path = Files.createDirectories(resolve("current")).resolve("link")
val tagLinkFile: Path = resolve("current").createDirectories().resolve("link")
val digestBytes = digest.toString().toByteArray()
try {
Files.write(tagLinkFile, digestBytes, StandardOpenOption.CREATE_NEW)
tagLinkFile.writeBytes(digestBytes, StandardOpenOption.CREATE_NEW)
} catch (e: FileAlreadyExistsException) {
if (!digestBytes.contentEquals(Files.readAllBytes(tagLinkFile))) {
if (!digestBytes.contentEquals(tagLinkFile.readBytes())) {
throw IllegalStateException("tried to link the same image name/tag to different images")
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciPushTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ import reactor.kotlin.core.publisher.toMono
import reactor.netty.NettyOutbound
import java.io.File
import java.net.URI
import java.nio.file.Files
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicInteger
import javax.inject.Inject
import kotlin.io.path.fileSize

/**
* @author Silvio Giebl
Expand Down Expand Up @@ -110,7 +110,7 @@ abstract class OciPushTask @Inject constructor(
val sourceBlob = blobs[digest]
blobFutures += if (sourceBlob == null) {
val file = digestToLayer[digest]!!.toPath()
val size = Files.size(file)
val size = file.fileSize()
val sender: NettyOutbound.() -> Publisher<Void> = { sendFileChunked(file, 0, size) }
val sourceImageName = resolvedBundle.component.imageReference.name
val future = CompletableFuture<Unit>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import org.gradle.api.tasks.OutputDirectory
import java.io.File
import java.io.IOException
import java.nio.file.FileAlreadyExistsException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardOpenOption
import kotlin.io.path.*

/**
* @author Silvio Giebl
Expand All @@ -25,11 +25,11 @@ abstract class OciRegistryDataTask : OciImagesInputTask() {
digestToLayer: Map<OciDigest, File>,
) {
val registryDataDirectory = registryDataDirectory.get().asFile.toPath().ensureEmptyDirectory()
val blobsDirectory: Path = Files.createDirectory(registryDataDirectory.resolve("blobs"))
val repositoriesDirectory: Path = Files.createDirectory(registryDataDirectory.resolve("repositories"))
val blobsDirectory = registryDataDirectory.resolve("blobs").createDirectory()
val repositoriesDirectory = registryDataDirectory.resolve("repositories").createDirectory()

for ((digest, layer) in digestToLayer) {
Files.createLink(blobsDirectory.resolveDigestFile(digest), layer.toPath())
blobsDirectory.resolveDigestFile(digest).createLinkPointingTo(layer.toPath())
}
for ((resolvedComponent, imageReferences) in resolvedComponentToImageReferences) {
writeImage(resolvedComponent, imageReferences, blobsDirectory, repositoriesDirectory)
Expand Down Expand Up @@ -65,12 +65,12 @@ abstract class OciRegistryDataTask : OciImagesInputTask() {
val indexDigest = index.digest

for (imageReference in imageReferences) {
val repositoryDirectory: Path = Files.createDirectories(repositoriesDirectory.resolve(imageReference.name))
val repositoryBlobsDirectory: Path = Files.createDirectories(repositoryDirectory.resolve("blobs"))
val repositoryDirectory = repositoriesDirectory.resolve(imageReference.name).createDirectories()
val repositoryBlobsDirectory = repositoryDirectory.resolve("blobs").createDirectories()
for (blobDigest in blobDigests) {
repositoryBlobsDirectory.writeDigestLink(blobDigest)
}
val manifestsDirectory: Path = Files.createDirectories(repositoryDirectory.resolve("manifests"))
val manifestsDirectory = repositoryDirectory.resolve("manifests").createDirectories()
for ((_, manifestDescriptor) in manifests) {
manifestsDirectory.writeDigestLink(manifestDescriptor.digest)
}
Expand All @@ -79,31 +79,30 @@ abstract class OciRegistryDataTask : OciImagesInputTask() {
}
}

private fun Path.resolveDigestFile(digest: OciDigest): Path {
return Files.createDirectories(resolve(digest.algorithm.ociPrefix)).resolve(digest.encodedHash)
}
private fun Path.resolveDigestFile(digest: OciDigest): Path =
resolve(digest.algorithm.ociPrefix).createDirectories().resolve(digest.encodedHash)

private fun Path.writeDigestData(dataDescriptor: OciDataDescriptor) {
val digestDataFile = resolveDigestFile(dataDescriptor.digest)
try {
Files.write(digestDataFile, dataDescriptor.data, StandardOpenOption.CREATE_NEW)
digestDataFile.writeBytes(dataDescriptor.data, StandardOpenOption.CREATE_NEW)
} catch (e: FileAlreadyExistsException) {
if (!dataDescriptor.data.contentEquals(Files.readAllBytes(digestDataFile))) {
if (!dataDescriptor.data.contentEquals(digestDataFile.readBytes())) {
throw IllegalStateException("hash collision for digest ${dataDescriptor.digest}: expected file content of $digestDataFile to be the same as ${dataDescriptor.data.contentToString()}")
}
}
}

private fun Path.writeDigestLink(digest: OciDigest) {
Files.write(resolveDigestFile(digest), digest.toString().toByteArray())
resolveDigestFile(digest).writeBytes(digest.toString().toByteArray())
}

private fun Path.writeTagLink(digest: OciDigest) {
val digestBytes = digest.toString().toByteArray()
try {
Files.write(this, digestBytes, StandardOpenOption.CREATE_NEW)
writeBytes(digestBytes, StandardOpenOption.CREATE_NEW)
} catch (e: FileAlreadyExistsException) {
if (!digestBytes.contentEquals(Files.readAllBytes(this))) {
if (!digestBytes.contentEquals(readBytes())) {
throw IllegalStateException("tried to link the same image name/tag to different images")
}
}
Expand All @@ -114,5 +113,5 @@ internal fun Path.ensureEmptyDirectory(): Path {
if (!toFile().deleteRecursively()) {
throw IOException("$this could not be deleted")
}
return Files.createDirectories(this)
return createDirectories()
}

0 comments on commit 3a5c70f

Please sign in to comment.