diff --git a/src/OrasProject.Oras/Oci/Descriptor.cs b/src/OrasProject.Oras/Oci/Descriptor.cs index 3f35c27..99ac2cf 100644 --- a/src/OrasProject.Oras/Oci/Descriptor.cs +++ b/src/OrasProject.Oras/Oci/Descriptor.cs @@ -72,7 +72,7 @@ public static Descriptor Create(Span data, string mediaType) internal BasicDescriptor BasicDescriptor => new BasicDescriptor(MediaType, Digest, Size); - internal static bool IsEmptyOrInvalid(Descriptor? descriptor) + internal static bool IsNullOrInvalid(Descriptor? descriptor) { return descriptor == null || string.IsNullOrEmpty(descriptor.Digest) || string.IsNullOrEmpty(descriptor.MediaType); } diff --git a/src/OrasProject.Oras/Registry/Remote/HttpResponseMessageExtensions.cs b/src/OrasProject.Oras/Registry/Remote/HttpResponseMessageExtensions.cs index 5e672a5..c45552c 100644 --- a/src/OrasProject.Oras/Registry/Remote/HttpResponseMessageExtensions.cs +++ b/src/OrasProject.Oras/Registry/Remote/HttpResponseMessageExtensions.cs @@ -107,9 +107,9 @@ public static void VerifyContentDigest(this HttpResponseMessage response, string /// /// /// - internal static void CheckOCISubjectHeader(this HttpResponseMessage response, Repository repository) + internal static void CheckOciSubjectHeader(this HttpResponseMessage response, Repository repository) { - if (response.Headers.TryGetValues("OCI-Subject", out var values)) + if (response.Headers.Contains("OCI-Subject")) { // Set it to Supported when the response header contains OCI-Subject repository.ReferrersState = Referrers.ReferrersState.Supported; diff --git a/src/OrasProject.Oras/Registry/Remote/ManifestStore.cs b/src/OrasProject.Oras/Registry/Remote/ManifestStore.cs index 724354c..ccecf84 100644 --- a/src/OrasProject.Oras/Registry/Remote/ManifestStore.cs +++ b/src/OrasProject.Oras/Registry/Remote/ManifestStore.cs @@ -15,9 +15,11 @@ using OrasProject.Oras.Oci; using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.IO; using System.Net; using System.Net.Http; +using System.Runtime.InteropServices; using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -251,7 +253,7 @@ private async Task ProcessReferrersAndPushIndex(Descriptor desc, Stream content, } Repository.ReferrersState = Referrers.ReferrersState.NotSupported; - await UpdateReferrersIndex(subject, new Referrers.ReferrerChange(desc, Referrers.ReferrerOperation.ReferrerAdd), cancellationToken).ConfigureAwait(false); + await UpdateReferrersIndex(subject, new Referrers.ReferrerChange(desc, Referrers.ReferrerOperation.Add), cancellationToken).ConfigureAwait(false); } /// @@ -276,10 +278,13 @@ private async Task UpdateReferrersIndex(Descriptor subject, // 2. apply the referrer change to referrers list var (updatedReferrers, updateRequired) = Referrers.ApplyReferrerChanges(oldReferrers, referrerChange); - if (!updateRequired) return; + if (!updateRequired) + { + return; + } // 3. push the updated referrers list using referrers tag schema - if (updatedReferrers.Count > 0 || repository.Options.SkipReferrersGC) + if (updatedReferrers.Count > 0 || repository.Options.SkipReferrersGc) { // push a new index in either case: // 1. the referrers list has been updated with a non-zero size @@ -293,9 +298,9 @@ private async Task UpdateReferrersIndex(Descriptor subject, } } - if (repository.Options.SkipReferrersGC || Descriptor.IsEmptyOrInvalid(oldDesc)) + if (repository.Options.SkipReferrersGc || Descriptor.IsNullOrInvalid(oldDesc)) { - // Skip the delete process if SkipReferrersGC is set to true or the old Descriptor is empty or null + // Skip the delete process if SkipReferrersGc is set to true or the old Descriptor is empty or null return; } @@ -326,7 +331,7 @@ private async Task UpdateReferrersIndex(Descriptor subject, } catch (NotFoundException) { - return (null, new List()); + return (null, ImmutableArray.Empty); } } @@ -351,7 +356,7 @@ private async Task DoPushAsync(Descriptor expected, Stream stream, Reference rem { throw await response.ParseErrorResponseAsync(cancellationToken).ConfigureAwait(false); } - response.CheckOCISubjectHeader(Repository); + response.CheckOciSubjectHeader(Repository); response.VerifyContentDigest(expected.Digest); } diff --git a/src/OrasProject.Oras/Registry/Remote/Referrers.cs b/src/OrasProject.Oras/Registry/Remote/Referrers.cs index de495c2..724105b 100644 --- a/src/OrasProject.Oras/Registry/Remote/Referrers.cs +++ b/src/OrasProject.Oras/Registry/Remote/Referrers.cs @@ -32,8 +32,8 @@ internal record ReferrerChange(Descriptor Referrer, ReferrerOperation ReferrerOp internal enum ReferrerOperation { - ReferrerAdd, - ReferrerDelete, + Add, + Delete, } internal static string BuildReferrersTag(Descriptor descriptor) @@ -51,7 +51,7 @@ internal static string BuildReferrersTag(Descriptor descriptor) /// The updated referrers list, updateRequired internal static (IList, bool) ApplyReferrerChanges(IList oldReferrers, ReferrerChange referrerChange) { - if (Descriptor.IsEmptyOrInvalid(referrerChange.Referrer)) + if (Descriptor.IsNullOrInvalid(referrerChange.Referrer)) { return (oldReferrers, false); } @@ -64,7 +64,7 @@ internal static (IList, bool) ApplyReferrerChanges(IList var updateRequired = false; foreach (var oldReferrer in oldReferrers) { - if (Descriptor.IsEmptyOrInvalid(oldReferrer)) + if (Descriptor.IsNullOrInvalid(oldReferrer)) { // Skip any empty or null referrers updateRequired = true; @@ -79,7 +79,7 @@ internal static (IList, bool) ApplyReferrerChanges(IList } // Update the updatedReferrers list // Add referrer index in the updatedReferrersSet - if (referrerChange.ReferrerOperation == ReferrerOperation.ReferrerDelete && Descriptor.Equals(basicDesc, referrerChange.Referrer.BasicDescriptor)) + if (referrerChange.ReferrerOperation == ReferrerOperation.Delete && Descriptor.Equals(basicDesc, referrerChange.Referrer.BasicDescriptor)) { updateRequired = true; continue; @@ -88,9 +88,8 @@ internal static (IList, bool) ApplyReferrerChanges(IList updatedReferrersSet.Add(basicDesc); } - var basicReferrerDesc = referrerChange.Referrer.BasicDescriptor; - if (referrerChange.ReferrerOperation == ReferrerOperation.ReferrerAdd) + if (referrerChange.ReferrerOperation == ReferrerOperation.Add) { if (!updatedReferrersSet.Contains(basicReferrerDesc)) { @@ -99,7 +98,6 @@ internal static (IList, bool) ApplyReferrerChanges(IList updatedReferrersSet.Add(basicReferrerDesc); } } - // Skip unnecessary update if (!updateRequired && updatedReferrersSet.Count == oldReferrers.Count) diff --git a/src/OrasProject.Oras/Registry/Remote/RepositoryOptions.cs b/src/OrasProject.Oras/Registry/Remote/RepositoryOptions.cs index 4742f9d..77f8259 100644 --- a/src/OrasProject.Oras/Registry/Remote/RepositoryOptions.cs +++ b/src/OrasProject.Oras/Registry/Remote/RepositoryOptions.cs @@ -51,7 +51,7 @@ public struct RepositoryOptions /// public int TagListPageSize { get; set; } - // SkipReferrersGC specifies whether to delete the dangling referrers + // SkipReferrersGc specifies whether to delete the dangling referrers // index when referrers tag schema is utilized. // - If false, the old referrers index will be deleted after the new one is successfully uploaded. // - If true, the old referrers index is kept. @@ -59,5 +59,5 @@ public struct RepositoryOptions // - https://github.com/opencontainers/distribution-spec/blob/v1.1.0/spec.md#referrers-tag-schema // - https://github.com/opencontainers/distribution-spec/blob/v1.1.0/spec.md#pushing-manifests-with-subject // - https://github.com/opencontainers/distribution-spec/blob/v1.1.0/spec.md#deleting-manifests - public bool SkipReferrersGC { get; set; } + public bool SkipReferrersGc { get; set; } } diff --git a/tests/OrasProject.Oras.Tests/Remote/ReferrersTest.cs b/tests/OrasProject.Oras.Tests/Remote/ReferrersTest.cs index 394f272..226023a 100644 --- a/tests/OrasProject.Oras.Tests/Remote/ReferrersTest.cs +++ b/tests/OrasProject.Oras.Tests/Remote/ReferrersTest.cs @@ -59,7 +59,7 @@ public void ApplyReferrerChanges_ShouldAddNewReferrers() }; var referrerChange = new Referrers.ReferrerChange( newDescriptor, - Referrers.ReferrerOperation.ReferrerAdd + Referrers.ReferrerOperation.Add ); var (updatedReferrers, updateRequired) = Referrers.ApplyReferrerChanges(oldReferrers, referrerChange); @@ -92,7 +92,7 @@ public void ApplyReferrerChanges_ShouldDeleteReferrers() }; var referrerChange = new Referrers.ReferrerChange( oldDescriptor2, - Referrers.ReferrerOperation.ReferrerDelete + Referrers.ReferrerOperation.Delete ); var (updatedReferrers, updateRequired) = Referrers.ApplyReferrerChanges(oldReferrers, referrerChange); @@ -129,7 +129,7 @@ public void ApplyReferrerChanges_ShouldDeleteReferrersWithDuplicates() }; var referrerChange = new Referrers.ReferrerChange( oldDescriptor3, - Referrers.ReferrerOperation.ReferrerDelete + Referrers.ReferrerOperation.Delete ); var (updatedReferrers, updateRequired) = Referrers.ApplyReferrerChanges(oldReferrers, referrerChange); @@ -161,7 +161,7 @@ public void ApplyReferrerChanges_ShouldNotDeleteReferrersWhenNoUpdateRequired() }; var referrerChange = new Referrers.ReferrerChange( oldDescriptor3, - Referrers.ReferrerOperation.ReferrerDelete + Referrers.ReferrerOperation.Delete ); var (updatedReferrers, updateRequired) = Referrers.ApplyReferrerChanges(oldReferrers, referrerChange); @@ -196,7 +196,7 @@ public void ApplyReferrerChanges_ShouldDiscardDuplicateReferrers() }; var referrerChange = new Referrers.ReferrerChange( newDescriptor1, - Referrers.ReferrerOperation.ReferrerAdd + Referrers.ReferrerOperation.Add ); var (updatedReferrers, updateRequired) = Referrers.ApplyReferrerChanges(oldReferrers, referrerChange); @@ -220,7 +220,7 @@ public void ApplyReferrerChanges_ShouldNotAddNewDuplicateReferrers() }; var referrerChange = new Referrers.ReferrerChange( oldDescriptor1, - Referrers.ReferrerOperation.ReferrerAdd + Referrers.ReferrerOperation.Add ); var (updatedReferrers, updateRequired) = Referrers.ApplyReferrerChanges(oldReferrers, referrerChange); Assert.Equal(2, updatedReferrers.Count); @@ -245,7 +245,7 @@ public void ApplyReferrerChanges_ShouldNotKeepOldEmptyReferrers() }; var referrerChange = new Referrers.ReferrerChange( newDescriptor, - Referrers.ReferrerOperation.ReferrerAdd + Referrers.ReferrerOperation.Add ); var (updatedReferrers, updateRequired) = Referrers.ApplyReferrerChanges(oldReferrers, referrerChange); @@ -261,7 +261,7 @@ public void ApplyReferrerChanges_ShouldNotKeepOldEmptyReferrers() public void ApplyReferrerChanges_NoUpdateWhenOldAndNewReferrersAreEmpty() { var oldReferrers = new List(); - var referrerChange = new Referrers.ReferrerChange(Descriptor.ZeroDescriptor(), Referrers.ReferrerOperation.ReferrerAdd); + var referrerChange = new Referrers.ReferrerChange(Descriptor.ZeroDescriptor(), Referrers.ReferrerOperation.Add); var (updatedReferrers, updateRequired) = Referrers.ApplyReferrerChanges(oldReferrers, referrerChange); Assert.Empty(updatedReferrers);