|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftContainerPlugin open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the SwiftContainerPlugin project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftContainerPlugin project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import struct Foundation.Data |
| 16 | +import class Foundation.OutputStream |
| 17 | +import class Foundation.JSONDecoder |
| 18 | +import class Foundation.JSONEncoder |
| 19 | +import Tar |
| 20 | + |
| 21 | +public class TarImageDestination { |
| 22 | + public var decoder: JSONDecoder |
| 23 | + var encoder: JSONEncoder |
| 24 | + |
| 25 | + var archive: Archive |
| 26 | + |
| 27 | + public init(toStream stream: OutputStream) throws { |
| 28 | + self.archive = Archive(toStream: stream) |
| 29 | + self.decoder = JSONDecoder() |
| 30 | + self.encoder = containerJSONEncoder() |
| 31 | + |
| 32 | + try archive.appendFile(name: "oci-layout", data: [UInt8](encoder.encode(ImageLayoutHeader()))) |
| 33 | + try archive.appendDirectory(name: "blobs") |
| 34 | + try archive.appendDirectory(name: "blobs/sha256") |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +extension TarImageDestination: ImageDestination { |
| 39 | + /// Saves a blob of unstructured data to the destination. |
| 40 | + /// - Parameters: |
| 41 | + /// - repository: Name of the destination repository. |
| 42 | + /// - mediaType: mediaType field for returned ContentDescriptor. |
| 43 | + /// On the wire, all blob uploads are `application/octet-stream'. |
| 44 | + /// - data: Object to be uploaded. |
| 45 | + /// - Returns: An ContentDescriptor object representing the |
| 46 | + /// saved blob. |
| 47 | + /// - Throws: If the blob cannot be encoded or the save fails. |
| 48 | + public func putBlob( |
| 49 | + repository: ImageReference.Repository, |
| 50 | + mediaType: String, |
| 51 | + data: Data |
| 52 | + ) async throws -> ContentDescriptor { |
| 53 | + let digest = ImageReference.Digest(of: Data(data)) |
| 54 | + try archive.appendFile(name: "\(digest.value)", prefix: "blobs/\(digest.algorithm)", data: [UInt8](data)) |
| 55 | + return .init(mediaType: mediaType, digest: "\(digest)", size: Int64(data.count)) |
| 56 | + } |
| 57 | + |
| 58 | + /// Saves a JSON object to the destination, serialized as an unstructured blob. |
| 59 | + /// - Parameters: |
| 60 | + /// - repository: Name of the destination repository. |
| 61 | + /// - mediaType: mediaType field for returned ContentDescriptor. |
| 62 | + /// On the wire, all blob uploads are `application/octet-stream'. |
| 63 | + /// - data: Object to be uploaded. |
| 64 | + /// - Returns: An ContentDescriptor object representing the |
| 65 | + /// saved blob. |
| 66 | + /// - Throws: If the blob cannot be encoded or the save fails. |
| 67 | + public func putBlob<Body: Encodable>( |
| 68 | + repository: ImageReference.Repository, |
| 69 | + mediaType: String, |
| 70 | + data: Body |
| 71 | + ) async throws -> ContentDescriptor { |
| 72 | + let encoded = try encoder.encode(data) |
| 73 | + return try await putBlob(repository: repository, mediaType: mediaType, data: encoded) |
| 74 | + } |
| 75 | + |
| 76 | + public func blobExists( |
| 77 | + repository: ImageReference.Repository, |
| 78 | + digest: ImageReference.Digest |
| 79 | + ) async throws -> Bool { |
| 80 | + false |
| 81 | + } |
| 82 | + |
| 83 | + public func putManifest( |
| 84 | + repository: ImageReference.Repository, |
| 85 | + reference: (any ImageReference.Reference)?, |
| 86 | + manifest: ImageManifest |
| 87 | + ) async throws -> ContentDescriptor { |
| 88 | + // Manifests are not special in the on-disk representation - they are just stored as blobs |
| 89 | + try await self.putBlob( |
| 90 | + repository: repository, |
| 91 | + mediaType: "application/vnd.oci.image.manifest.v1+json", |
| 92 | + data: manifest |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + public func putIndex( |
| 97 | + repository: ImageReference.Repository, |
| 98 | + reference: (any ImageReference.Reference)?, |
| 99 | + index: ImageIndex |
| 100 | + ) async throws -> ContentDescriptor { |
| 101 | + // Unlike Manifest, Index is not written as a blob |
| 102 | + let encoded = try encoder.encode(index) |
| 103 | + let digest = ImageReference.Digest(of: encoded) |
| 104 | + let mediaType = index.mediaType ?? "application/vnd.oci.image.index.v1+json" |
| 105 | + |
| 106 | + try archive.appendFile(name: "index.json", data: [UInt8](encoded)) |
| 107 | + |
| 108 | + try archive.appendFile(name: "\(digest.value)", prefix: "blobs/\(digest.algorithm)", data: [UInt8](encoded)) |
| 109 | + return .init(mediaType: mediaType, digest: "\(digest)", size: Int64(encoded.count)) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +struct ImageLayoutHeader: Codable { |
| 114 | + var imageLayoutVersion: String = "1.0.0" |
| 115 | +} |
0 commit comments