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