winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

WinGetUtilities.cs (2500B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="WinGetUtilities.cs" company="Microsoft Corporation">
      3 //     Copyright (c) Microsoft Corporation. Licensed under the MIT License.
      4 // </copyright>
      5 // -----------------------------------------------------------------------------
      6 
      7 namespace Microsoft.WinGetUtil.Api
      8 {
      9     using System;
     10     using System.Runtime.InteropServices;
     11     using Microsoft.WinGetUtil.Common;
     12     using Microsoft.WinGetUtil.Exceptions;
     13     using Microsoft.WinGetUtil.Interfaces;
     14 
     15     /// <summary>
     16     /// Download wrapper.
     17     /// </summary>
     18     public sealed class WinGetUtilities : IWinGetUtilities
     19     {
     20         /// <inheritdoc/>
     21         public string Download(string url, string filePath, bool enableLogging = false, string logFilePath = null)
     22         {
     23             IWinGetLogging log = null;
     24             if (enableLogging)
     25             {
     26                 if (string.IsNullOrWhiteSpace(logFilePath))
     27                 {
     28                     throw new ArgumentNullException($"Download logging is enabled and log file path `{logFilePath}` is not provided.");
     29                 }
     30 
     31                 log = new WinGetFactory().LoggingInit(logFilePath);
     32             }
     33 
     34             try
     35             {
     36                 byte[] sha256Hash = new byte[32];
     37                 WinGetDownload(url, filePath, sha256Hash, (uint)sha256Hash.Length);
     38                 return BitConverter.ToString(sha256Hash).Replace("-", string.Empty);
     39             }
     40             catch (Exception e)
     41             {
     42                 throw new WinGetDownloadException(e);
     43             }
     44             finally
     45             {
     46                 log?.Dispose();
     47             }
     48         }
     49 
     50         /// <inheritdoc/>
     51         public int CompareVersions(string version1, string version2)
     52         {
     53             WinGetCompareVersions(version1, version2, out int result);
     54             return result;
     55         }
     56 
     57         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
     58         private static extern IntPtr WinGetDownload(string url, string filePath, [MarshalAs(UnmanagedType.LPArray)] byte[] sha26Hash, uint sha256HashLength);
     59 
     60         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
     61         private static extern IntPtr WinGetCompareVersions(string version1, string version2, [MarshalAs(UnmanagedType.U4)] out int comparisonResult);
     62     }
     63 }