-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Expand file tree
/
Copy pathSearchCatalogStatusReader.cs
More file actions
76 lines (66 loc) · 2.24 KB
/
SearchCatalogStatusReader.cs
File metadata and controls
76 lines (66 loc) · 2.24 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
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Runtime.CompilerServices;
using System.Threading;
using ManagedCommon;
using ManagedCsWin32;
using Microsoft.CmdPal.Ext.Indexer.Indexer.SystemSearch;
namespace Microsoft.CmdPal.Ext.Indexer.Indexer;
internal static class SearchCatalogStatusReader
{
private const string SystemIndex = "SystemIndex";
private static readonly Lock FailureLoggingLock = new();
private static int? _lastLoggedFailureHResult;
internal static SearchCatalogStatus GetStatus()
{
try
{
var catalogManager = CreateCatalogManager();
var pendingItemsCount = catalogManager.NumberOfItemsToIndex();
ResetFailureLoggingState();
return new SearchCatalogStatus(pendingItemsCount, null);
}
catch (Exception ex)
{
LogFailure(ex);
return new SearchCatalogStatus(0, ex.HResult);
}
}
private static ISearchCatalogManager CreateCatalogManager()
{
var searchManager = ComHelper.CreateComInstance<ISearchManager>(ref Unsafe.AsRef(in CLSID.SearchManager), CLSCTX.LocalServer);
var catalogManager = searchManager.GetCatalog(SystemIndex);
return catalogManager ?? throw new ArgumentException($"Failed to get catalog manager for {SystemIndex}");
}
private static void LogFailure(Exception ex)
{
var shouldLogWarning = false;
lock (FailureLoggingLock)
{
if (_lastLoggedFailureHResult != ex.HResult)
{
_lastLoggedFailureHResult = ex.HResult;
shouldLogWarning = true;
}
}
var message = $"Failed to read Windows Search catalog status. HResult=0x{ex.HResult:X8}, Message={ex.Message}";
if (shouldLogWarning)
{
Logger.LogWarning(message);
}
else
{
Logger.LogDebug(message);
}
}
private static void ResetFailureLoggingState()
{
lock (FailureLoggingLock)
{
_lastLoggedFailureHResult = null;
}
}
}