Ejemplo n.º 1
0
        public override ExitCode Execute()
        {
            try
            {
                switch (AdditionalArgs.Count)
                {
                case 1:
                    // Verify a directory inside the store
                    ImplementationStore.Verify(new ManifestDigest(AdditionalArgs[0]), Handler);
                    break;

                case 2:
                    // Verify an arbitrary directory
                    ImplementationStoreUtils.Verify(AdditionalArgs[0], new ManifestDigest(AdditionalArgs[1]), Handler);
                    break;
                }
            }
            catch (DigestMismatchException ex)
            {
                Handler.Output(Resources.VerifyImplementation, ex.LongMessage);
                return(ExitCode.DigestMismatch);
            }

            return(ExitCode.OK);
        }
Ejemplo n.º 2
0
    /// <inheritdoc/>
    public void Add(ManifestDigest manifestDigest, Action <IBuilder> build)
    {
        #region Sanity checks
        if (build == null)
        {
            throw new ArgumentNullException(nameof(build));
        }
        #endregion

        if (manifestDigest.AvailableDigests.Any(digest => Directory.Exists(System.IO.Path.Combine(Path, digest))))
        {
            throw new ImplementationAlreadyInStoreException(manifestDigest);
        }
        string expectedDigest = manifestDigest.Best ?? throw new NotSupportedException(Resources.NoKnownDigestMethod);
        Log.Debug($"Storing implementation {expectedDigest} in {this}");
        var format = ManifestFormat.FromPrefix(manifestDigest.Best);

        // Place files in temp directory until digest is verified
        string tempDir = GetTempDir();
        using var _ = new Disposable(() => DeleteTempDir(tempDir));

        var builder = new ManifestBuilder(format);
        build(new DirectoryBuilder(tempDir, builder));
        var manifest = ImplementationStoreUtils.Verify(builder.Manifest, expectedDigest);

        if (manifest == null)
        {
            throw new DigestMismatchException(
                      expectedDigest, actualDigest: builder.Manifest.CalculateDigest(),
                      actualManifest: builder.Manifest);
        }
        manifest.Save(System.IO.Path.Combine(tempDir, Manifest.ManifestFile));

        string target = System.IO.Path.Combine(Path, expectedDigest);
        lock (_renameLock) // Prevent race-conditions when adding the same digest twice
        {
            if (Directory.Exists(target))
            {
                throw new ImplementationAlreadyInStoreException(manifestDigest);
            }

            // Move directory to final destination
            try
            {
                Directory.Move(tempDir, target);
            }
            catch (IOException ex) when(ex.Message.Contains("already exists") || Directory.Exists(target))
            {
                throw new ImplementationAlreadyInStoreException(manifestDigest);
            }
        }

        // Prevent any further changes to the directory
        if (UseWriteProtection)
        {
            EnableWriteProtection(target);
        }
    }