| 
 | 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.JSONEncoder  | 
 | 17 | + | 
 | 18 | +/// ScratchImage is a special-purpose ImageSource which represents the scratch image.  | 
 | 19 | +public struct ScratchImage {  | 
 | 20 | +    var encoder: JSONEncoder  | 
 | 21 | + | 
 | 22 | +    var architecture: String  | 
 | 23 | +    var os: String  | 
 | 24 | + | 
 | 25 | +    var configuration: ImageConfiguration  | 
 | 26 | +    var manifest: ImageManifest  | 
 | 27 | +    var manifestDescriptor: ContentDescriptor  | 
 | 28 | +    var index: ImageIndex  | 
 | 29 | + | 
 | 30 | +    public init(architecture: String, os: String) {  | 
 | 31 | +        self.encoder = containerJSONEncoder()  | 
 | 32 | + | 
 | 33 | +        self.architecture = architecture  | 
 | 34 | +        self.os = os  | 
 | 35 | + | 
 | 36 | +        self.configuration = ImageConfiguration(  | 
 | 37 | +            architecture: architecture,  | 
 | 38 | +            os: os,  | 
 | 39 | +            rootfs: .init(_type: "layers", diff_ids: [])  | 
 | 40 | +        )  | 
 | 41 | +        let encodedConfiguration = try! encoder.encode(self.configuration)  | 
 | 42 | + | 
 | 43 | +        self.manifest = ImageManifest(  | 
 | 44 | +            schemaVersion: 2,  | 
 | 45 | +            config: ContentDescriptor(  | 
 | 46 | +                mediaType: "application/vnd.oci.image.config.v1+json",  | 
 | 47 | +                digest: "\(ImageReference.Digest(of: encodedConfiguration))",  | 
 | 48 | +                size: Int64(encodedConfiguration.count)  | 
 | 49 | +            ),  | 
 | 50 | +            layers: []  | 
 | 51 | +        )  | 
 | 52 | +        let encodedManifest = try! encoder.encode(self.manifest)  | 
 | 53 | + | 
 | 54 | +        self.manifestDescriptor = ContentDescriptor(  | 
 | 55 | +            mediaType: "application/vnd.oci.image.manifest.v1+json",  | 
 | 56 | +            digest: "\(ImageReference.Digest(of: encodedManifest))",  | 
 | 57 | +            size: Int64(encodedManifest.count)  | 
 | 58 | +        )  | 
 | 59 | + | 
 | 60 | +        self.index = ImageIndex(  | 
 | 61 | +            schemaVersion: 2,  | 
 | 62 | +            mediaType: "application/vnd.oci.image.index.v1+json",  | 
 | 63 | +            manifests: [  | 
 | 64 | +                ContentDescriptor(  | 
 | 65 | +                    mediaType: "application/vnd.oci.image.manifest.v1+json",  | 
 | 66 | +                    digest: "\(ImageReference.Digest(of: encodedManifest))",  | 
 | 67 | +                    size: Int64(encodedManifest.count),  | 
 | 68 | +                    platform: .init(architecture: architecture, os: os)  | 
 | 69 | +                )  | 
 | 70 | +            ]  | 
 | 71 | +        )  | 
 | 72 | +    }  | 
 | 73 | +}  | 
 | 74 | + | 
 | 75 | +extension ScratchImage: ImageSource {  | 
 | 76 | +    /// The scratch image has no data layers, so `getBlob` returns an empty data blob.  | 
 | 77 | +    ///  | 
 | 78 | +    /// - Parameters:  | 
 | 79 | +    ///   - repository: Name of the repository containing the blob.  | 
 | 80 | +    ///   - digest: Digest of the blob.  | 
 | 81 | +    /// - Returns: An empty blob.  | 
 | 82 | +    /// - Throws: Does not throw, but signature must match the `ImageSource` protocol requirements.  | 
 | 83 | +    public func getBlob(  | 
 | 84 | +        repository: ImageReference.Repository,  | 
 | 85 | +        digest: ImageReference.Digest  | 
 | 86 | +    ) async throws -> Data {  | 
 | 87 | +        Data()  | 
 | 88 | +    }  | 
 | 89 | + | 
 | 90 | +    /// Returns an empty manifest for the scratch image, with no image layers.  | 
 | 91 | +    ///  | 
 | 92 | +    /// - Parameters:  | 
 | 93 | +    ///   - repository: Name of the source repository.  | 
 | 94 | +    ///   - reference: Tag or digest of the manifest to fetch.  | 
 | 95 | +    /// - Returns: The downloaded manifest.  | 
 | 96 | +    /// - Throws: Does not throw, but signature must match the `ImageSource` protocol requirements.  | 
 | 97 | +    public func getManifest(  | 
 | 98 | +        repository: ImageReference.Repository,  | 
 | 99 | +        reference: any ImageReference.Reference  | 
 | 100 | +    ) async throws -> (ImageManifest, ContentDescriptor) {  | 
 | 101 | +        (self.manifest, self.manifestDescriptor)  | 
 | 102 | +    }  | 
 | 103 | + | 
 | 104 | +    /// Fetches an image index.  | 
 | 105 | +    ///  | 
 | 106 | +    /// - Parameters:  | 
 | 107 | +    ///   - repository: Name of the source repository.  | 
 | 108 | +    ///   - reference: Tag or digest of the index to fetch.  | 
 | 109 | +    /// - Returns: The downloaded index.  | 
 | 110 | +    /// - Throws: Does not throw, but signature must match the `ImageSource` protocol requirements.  | 
 | 111 | +    public func getIndex(  | 
 | 112 | +        repository: ImageReference.Repository,  | 
 | 113 | +        reference: any ImageReference.Reference  | 
 | 114 | +    ) async throws -> ImageIndex {  | 
 | 115 | +        self.index  | 
 | 116 | +    }  | 
 | 117 | + | 
 | 118 | +    /// Returns an almost empty image configuration scratch image.  | 
 | 119 | +    /// The processor architecture and operating system fields are populated,  | 
 | 120 | +    /// but the layer list is empty.  | 
 | 121 | +    ///  | 
 | 122 | +    /// - Parameters:  | 
 | 123 | +    ///   - image: Reference to the image containing the record.  | 
 | 124 | +    ///   - digest: Digest of the record.  | 
 | 125 | +    /// - Returns: A suitable configuration for the scratch image.  | 
 | 126 | +    /// - Throws: Does not throw, but signature must match the `ImageSource` protocol requirements.  | 
 | 127 | +    ///  | 
 | 128 | +    /// Image configuration records are stored as blobs in the registry.  This function retrieves the requested blob and tries to decode it as a configuration record.  | 
 | 129 | +    public func getImageConfiguration(  | 
 | 130 | +        forImage image: ImageReference,  | 
 | 131 | +        digest: ImageReference.Digest  | 
 | 132 | +    ) async throws -> ImageConfiguration {  | 
 | 133 | +        self.configuration  | 
 | 134 | +    }  | 
 | 135 | +}  | 
0 commit comments