winget-cli

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

WinGetSQLiteIndex.cs (11557B)


      1 // -----------------------------------------------------------------------------
      2 // <copyright file="WinGetSQLiteIndex.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     /// Wrapper class for SQLite index operations.
     17     /// </summary>
     18     public sealed class WinGetSQLiteIndex : IWinGetSQLiteIndex
     19     {
     20         private readonly IntPtr indexHandle;
     21 
     22         /// <summary>
     23         /// Initializes a new instance of the <see cref="WinGetSQLiteIndex"/> class.
     24         /// </summary>
     25         /// <param name="indexHandle">Handle of the index.</param>
     26         internal WinGetSQLiteIndex(IntPtr indexHandle)
     27         {
     28             this.indexHandle = indexHandle;
     29         }
     30 
     31         /// <inheritdoc/>
     32         public void MigrateTo(uint majorVersion, uint minorVersion)
     33         {
     34             try
     35             {
     36                 WinGetSQLiteIndexMigrate(this.indexHandle, majorVersion, minorVersion);
     37             }
     38             catch (Exception e)
     39             {
     40                 throw new WinGetSQLiteIndexException(e);
     41             }
     42         }
     43 
     44         /// <inheritdoc/>
     45         public void SetProperty(SQLiteIndexProperty property, string value)
     46         {
     47             try
     48             {
     49                 WinGetSQLiteIndexSetProperty(this.indexHandle, property, value);
     50             }
     51             catch (Exception e)
     52             {
     53                 throw new WinGetSQLiteIndexException(e);
     54             }
     55         }
     56 
     57         /// <inheritdoc/>
     58         public void AddManifest(string manifestPath, string relativePath)
     59         {
     60             try
     61             {
     62                 WinGetSQLiteIndexAddManifest(this.indexHandle, manifestPath, relativePath);
     63                 return;
     64             }
     65             catch (Exception e)
     66             {
     67                 throw new WinGetSQLiteIndexException(e);
     68             }
     69         }
     70 
     71         /// <inheritdoc/>
     72         public bool UpdateManifest(string manifestPath, string relativePath)
     73         {
     74             try
     75             {
     76                 // For now, modifying a manifest implies that the file didn't got moved in the repository. So only
     77                 // contents of the file are modified. However, in the future we might support moving which requires
     78                 // oldManifestPath, oldRelativePath, newManifestPath and oldManifestPath.
     79                 WinGetSQLiteIndexUpdateManifest(
     80                     this.indexHandle,
     81                     manifestPath,
     82                     relativePath,
     83                     out bool indexModified);
     84                 return indexModified;
     85             }
     86             catch (Exception e)
     87             {
     88                 throw new WinGetSQLiteIndexException(e);
     89             }
     90         }
     91 
     92         /// <inheritdoc/>
     93         public bool AddOrUpdateManifest(string manifestPath, string relativePath)
     94         {
     95             try
     96             {
     97                 // For now, modifying a manifest implies that the file didn't got moved in the repository. So only
     98                 // contents of the file are modified. However, in the future we might support moving which requires
     99                 // oldManifestPath, oldRelativePath, newManifestPath and oldManifestPath.
    100                 WinGetSQLiteIndexAddOrUpdateManifest(
    101                     this.indexHandle,
    102                     manifestPath,
    103                     relativePath,
    104                     out bool indexModified);
    105                 return indexModified;
    106             }
    107             catch (Exception e)
    108             {
    109                 throw new WinGetSQLiteIndexException(e);
    110             }
    111         }
    112 
    113         /// <inheritdoc/>
    114         public void RemoveManifest(string manifestPath, string relativePath)
    115         {
    116             try
    117             {
    118                 WinGetSQLiteIndexRemoveManifest(this.indexHandle, manifestPath, relativePath);
    119                 return;
    120             }
    121             catch (Exception e)
    122             {
    123                 throw new WinGetSQLiteIndexException(e);
    124             }
    125         }
    126 
    127         /// <inheritdoc/>
    128         public void PrepareForPackaging()
    129         {
    130             try
    131             {
    132                 WinGetSQLiteIndexPrepareForPackaging(this.indexHandle);
    133                 return;
    134             }
    135             catch (Exception e)
    136             {
    137                 throw new WinGetSQLiteIndexException(e);
    138             }
    139         }
    140 
    141         /// <inheritdoc/>
    142         public bool IsIndexConsistent()
    143         {
    144             try
    145             {
    146                 WinGetSQLiteIndexCheckConsistency(this.indexHandle, out bool indexModified);
    147                 return indexModified;
    148             }
    149             catch (Exception e)
    150             {
    151                 throw new WinGetSQLiteIndexException(e);
    152             }
    153         }
    154 
    155         /// <inheritdoc/>
    156         public IntPtr GetIndexHandle()
    157         {
    158             return this.indexHandle;
    159         }
    160 
    161         /// <summary>
    162         /// Dispose method.
    163         /// </summary>
    164         public void Dispose()
    165         {
    166             this.Dispose(true);
    167             GC.SuppressFinalize(this);
    168         }
    169 
    170         /// <summary>
    171         /// Dispose method to free the sqlite index handle.
    172         /// </summary>
    173         /// <param name="disposing">Bool value indicating if Dispose is being run.</param>
    174         public void Dispose(bool disposing)
    175         {
    176             if (disposing)
    177             {
    178                 if (this.indexHandle != IntPtr.Zero)
    179                 {
    180                     WinGetSQLiteIndexClose(this.indexHandle);
    181                 }
    182             }
    183         }
    184 
    185         /// <summary>
    186         /// Migrates the index to the target version.
    187         /// </summary>
    188         /// <param name="index">Handle of the index.</param>
    189         /// <param name="majorVersion">Major version.</param>
    190         /// <param name="minorVersion">Minor version.</param>
    191         /// <returns>HRESULT.</returns>
    192         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    193         private static extern IntPtr WinGetSQLiteIndexMigrate(IntPtr index, uint majorVersion, uint minorVersion);
    194 
    195         /// <summary>
    196         /// Sets a property on the index.
    197         /// </summary>
    198         /// <param name="index">Handle of the index.</param>
    199         /// <param name="property">The property to set.</param>
    200         /// <param name="value">The value to set.</param>
    201         /// <returns>HRESULT.</returns>
    202         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    203         private static extern IntPtr WinGetSQLiteIndexSetProperty(IntPtr index, SQLiteIndexProperty property, string value);
    204 
    205         /// <summary>
    206         /// Closes the index.
    207         /// </summary>
    208         /// <param name="index">Handle of the index.</param>
    209         /// <returns>HRESULT.</returns>
    210         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    211         private static extern IntPtr WinGetSQLiteIndexClose(IntPtr index);
    212 
    213         /// <summary>
    214         /// Adds the manifest at the repository relative path to the index.
    215         /// If the function succeeds, the manifest has been added.
    216         /// </summary>
    217         /// <param name="index">Handle of the index.</param>
    218         /// <param name="manifestPath">Manifest to add.</param>
    219         /// <param name="relativePath">Path of the manifest in the container.</param>
    220         /// <returns>HRESULT.</returns>
    221         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    222         private static extern IntPtr WinGetSQLiteIndexAddManifest(IntPtr index, string manifestPath, string relativePath);
    223 
    224         /// <summary>
    225         /// Updates the manifest at the repository relative path in the index.
    226         /// The out value indicates whether the index was modified by the function.
    227         /// </summary>
    228         /// <param name="index">Handle of the index.</param>
    229         /// <param name="manifestPath">Old manifest path.</param>
    230         /// <param name="relativePath">Old relative path in the container.</param>
    231         /// <param name="indexModified">Out bool if the index is modified.</param>
    232         /// <returns>HRESULT.</returns>
    233         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    234         private static extern IntPtr WinGetSQLiteIndexUpdateManifest(
    235             IntPtr index,
    236             string manifestPath,
    237             string relativePath,
    238             [MarshalAs(UnmanagedType.U1)] out bool indexModified);
    239 
    240         /// <summary>
    241         /// Adds or Updates the manifest at the repository relative path in the index.
    242         /// The out value indicates whether the index was modified by the function.
    243         /// </summary>
    244         /// <param name="index">Handle of the index.</param>
    245         /// <param name="manifestPath">Manifest path.</param>
    246         /// <param name="relativePath">Relative path in the container.</param>
    247         /// <param name="indexModified">Out bool if the index is modified.</param>
    248         /// <returns>HRESULT.</returns>
    249         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    250         private static extern IntPtr WinGetSQLiteIndexAddOrUpdateManifest(
    251             IntPtr index,
    252             string manifestPath,
    253             string relativePath,
    254             [MarshalAs(UnmanagedType.U1)] out bool indexModified);
    255 
    256         /// <summary>
    257         /// Removes the manifest at the repository relative path from the index.
    258         /// </summary>
    259         /// <param name="index">Index handle.</param>
    260         /// <param name="manifestPath">Manifest path to remove.</param>
    261         /// <param name="relativePath">Relative path in the container.</param>
    262         /// <returns>HRESULT.</returns>
    263         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    264         private static extern IntPtr WinGetSQLiteIndexRemoveManifest(IntPtr index, string manifestPath, string relativePath);
    265 
    266         /// <summary>
    267         /// Removes data that is no longer needed for an index that is to be published.
    268         /// </summary>
    269         /// <param name="index">Index handle.</param>
    270         /// <returns>HRESULT.</returns>
    271         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    272         private static extern IntPtr WinGetSQLiteIndexPrepareForPackaging(IntPtr index);
    273 
    274         /// <summary>
    275         /// Checks the index for consistency, ensuring that at a minimum all referenced rows actually exist.
    276         /// </summary>
    277         /// <param name="index">Index handle.</param>
    278         /// <param name="succeeded">Does the consistency check succeeded.</param>
    279         /// <returns>HRESULT.</returns>
    280         [DllImport(Constants.DllName, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, PreserveSig = false)]
    281         private static extern IntPtr WinGetSQLiteIndexCheckConsistency(IntPtr index, [MarshalAs(UnmanagedType.U1)] out bool succeeded);
    282     }
    283 }