Пример #1
0
    /// <summary>
    /// Calculates a <see cref="ManifestDigest"/> for a retrieval method. Sets missing properties in the process.
    /// </summary>
    /// <param name="retrievalMethod">The retrieval method.</param>
    /// <param name="executor">Used to modify properties in an undoable fashion.</param>
    /// <param name="handler">A callback object used when the the user is to be informed about progress.</param>
    /// <param name="format">The manifest format. Leave <c>null</c> for default.</param>
    /// <returns>The generated digest.</returns>
    /// <exception cref="OperationCanceledException">The user canceled the task.</exception>
    /// <exception cref="WebException">A file could not be downloaded from the internet.</exception>
    public static ManifestDigest CalculateDigest(this RetrievalMethod retrievalMethod, ICommandExecutor executor, ITaskHandler handler, ManifestFormat?format = null)
    {
        #region Sanity checks
        if (retrievalMethod == null)
        {
            throw new ArgumentNullException(nameof(retrievalMethod));
        }
        if (executor == null)
        {
            throw new ArgumentNullException(nameof(executor));
        }
        if (handler == null)
        {
            throw new ArgumentNullException(nameof(handler));
        }
        #endregion

        var builder = new ManifestBuilder(format ?? ManifestFormat.Sha256New);
        builder.Add(retrievalMethod, executor, handler);

        var digest = new ManifestDigest(builder.Manifest.CalculateDigest());
        if (digest.PartialEquals(ManifestDigest.Empty))
        {
            Log.Warn(Resources.EmptyImplementation);
        }
        return(digest);
    }
        public void TestPartialEqual()
        {
            var digest1 = new ManifestDigest(sha1: "test1");
            var digest2 = new ManifestDigest(sha1: "test1", sha1New: "test2");
            Assert.IsTrue(digest1.PartialEquals(digest2));

            digest1 = new ManifestDigest(sha1: "test1");
            digest2 = new ManifestDigest(sha1: "test2");
            Assert.IsFalse(digest1.PartialEquals(digest2));

            digest1 = new ManifestDigest(sha1: "test1");
            digest2 = new ManifestDigest(sha1New: "test2");
            Assert.IsFalse(digest1.PartialEquals(digest2));

            digest1 = new ManifestDigest(sha1New: "test1");
            digest2 = new ManifestDigest(sha256: "test2");
            Assert.IsFalse(digest1.PartialEquals(digest2));
        }
Пример #3
0
        public void TestPartialEqual()
        {
            var digest1 = new ManifestDigest(sha1: "test1");
            var digest2 = new ManifestDigest(sha1: "test1", sha1New: "test2");
            digest1.PartialEquals(digest2).Should().BeTrue();

            digest1 = new ManifestDigest(sha1: "test1");
            digest2 = new ManifestDigest(sha1: "test2");
            digest1.PartialEquals(digest2).Should().BeFalse();

            digest1 = new ManifestDigest(sha1: "test1");
            digest2 = new ManifestDigest(sha1New: "test2");
            digest1.PartialEquals(digest2).Should().BeFalse();

            digest1 = new ManifestDigest(sha1New: "test1");
            digest2 = new ManifestDigest(sha256: "test2");
            digest1.PartialEquals(digest2).Should().BeFalse();
        }
Пример #4
0
    public void PartialEqual()
    {
        var digest1 = new ManifestDigest(Sha1: "test1");
        var digest2 = new ManifestDigest(Sha1: "test1", Sha1New: "test2");

        digest1.PartialEquals(digest2).Should().BeTrue();

        digest1 = new ManifestDigest(Sha1: "test1");
        digest2 = new ManifestDigest(Sha1: "test2");
        digest1.PartialEquals(digest2).Should().BeFalse();

        digest1 = new ManifestDigest(Sha1: "test1");
        digest2 = new ManifestDigest(Sha1New: "test2");
        digest1.PartialEquals(digest2).Should().BeFalse();

        digest1 = new ManifestDigest(Sha1New: "test1");
        digest2 = new ManifestDigest(Sha256: "test2");
        digest1.PartialEquals(digest2).Should().BeFalse();
    }
        /// <summary>
        /// Generates the <see cref="ManifestDigest"/> for a directory.
        /// </summary>
        /// <param name="path">The path of the directory to generate the digest for.</param>
        /// <param name="handler">A callback object used when the the user is to be informed about progress.</param>
        /// <param name="keepDownloads"><see langword="true"/> to store the directory as an implementation in the default <see cref="IStore"/>.</param>
        /// <returns>The newly generated digest.</returns>
        /// <exception cref="OperationCanceledException">The user canceled the operation.</exception>
        /// <exception cref="IOException">There is a problem access a temporary file.</exception>
        /// <exception cref="UnauthorizedAccessException">Read or write access to a temporary file is not permitted.</exception>
        public static ManifestDigest GenerateDigest([NotNull] string path, [NotNull] ITaskHandler handler, bool keepDownloads = false)
        {
            #region Sanity checks
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }
            #endregion

            var digest = new ManifestDigest();

            // Generate manifest for each available format...
            foreach (var format in ManifestFormat.Recommended)
            // ... and add the resulting digest to the return value
            {
                var generator = new ManifestGenerator(path, format);
                handler.RunTask(generator);
                digest.ParseID(generator.Result.CalculateDigest());
            }

            if (digest.PartialEquals(ManifestDigest.Empty))
            {
                Log.Warn(Resources.EmptyImplementation);
            }

            if (keepDownloads)
            {
                try
                {
                    StoreFactory.CreateDefault().AddDirectory(path, digest, handler);
                }
                catch (ImplementationAlreadyInStoreException)
                {}
            }

            return(digest);
        }
Пример #6
0
        public static ManifestDigest GenerateDigest(string path, ITaskHandler handler)
        {
            #region Sanity checks
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }
            #endregion

            var digest = new ManifestDigest(CalculateDigest(path, ManifestFormat.Sha256New, handler));

            if (digest.PartialEquals(ManifestDigest.Empty))
            {
                Log.Warn(Resources.EmptyImplementation);
            }

            return(digest);
        }
Пример #7
0
        public static ManifestDigest GenerateDigest([NotNull] string path, [NotNull] ITaskHandler handler, [CanBeNull] IStore keepDownloads = null)
        {
            #region Sanity checks
            if (string.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path));
            if (handler == null) throw new ArgumentNullException(nameof(handler));
            #endregion

            var digest = new ManifestDigest();

            // Generate manifest for each available format...
            foreach (var format in ManifestFormat.All)
                // ... and add the resulting digest to the return value
            {
                var generator = new ManifestGenerator(path, format);
                handler.RunTask(generator);
                digest.ParseID(generator.Manifest.CalculateDigest());
            }

            if (digest.PartialEquals(ManifestDigest.Empty))
                Log.Warn(Resources.EmptyImplementation);

            if (keepDownloads != null)
            {
                try
                {
                    keepDownloads.AddDirectory(path, digest, handler);
                }
                catch (ImplementationAlreadyInStoreException)
                {}
            }

            return digest;
        }