Functionality to detect Win32 apps which are installed, deleted or renamed while PowerToys is running (#4960)

* Added file system wrapper and interface

* added win32program repository which would load store app and also handle new apps being added/deleted

* Added event handlers to win32 program repo

* added paths to monitor and setting FSWs for each location

* Events firing as expected

* filter extensions added, events fire as expected

* override gethashcode so that duplicates don't show up. OnCreated and OnDeleted events trigger as expected

* implemented setter for filters in FileSystemWatcher

* Rename adds item but does not seem to delete the previous app

* catching an exception when a duplicate item is inserted

* Removed notify filter for directory because we only need to monitor files

* Added exe programs to be indexed in the desktop and startmenu

* created a new class to init FileSystemHelpers instead of main

* Added fix for shortcut applications to work as expected while renaming and deleting them

* Added wrappers for file system operations

* Added some tests

* Added all tests for appref-ms and added a condition to search in sub directories

* Added tests for Exe applications

* Added lnk app tests

* Added tests for shortcut applications

* removed unnecessary wrappers

* override Equals for win32

* removed debug statements

* Fixed exe issue

* Fixed internet shortcut exception

* fixed renaming shortcut apps

* Added a retry block for ReadAllLines

* capitalized method name - RetrieveTargetPath

* made naming consistent, helper variables start with underscore

* Added the exception condition back

* renamed Win32ProgramRepositoryHelper to Win32ProgramFileSystemWatchers

* made win32Program repository variable static

* changed list to ilist

* disposed file system watchers

* make retrieveTargetPath upper case in tests
This commit is contained in:
Alekhya
2020-07-17 22:32:21 -07:00
committed by GitHub
parent 034079b441
commit d09253e532
17 changed files with 894 additions and 58 deletions

View File

@@ -0,0 +1,23 @@
using System.Diagnostics;
using System.IO;
namespace Wox.Infrastructure.FileSystemHelper
{
public class FileVersionInfoWrapper : IFileVersionInfoWrapper
{
public FileVersionInfoWrapper() { }
public FileVersionInfo GetVersionInfo(string path)
{
if(File.Exists(path))
{
return FileVersionInfo.GetVersionInfo(path);
}
else
{
return null;
}
}
public string FileDescription { get; set; }
}
}

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Wox.Infrastructure.Logger;
using System.Threading;
namespace Wox.Infrastructure.FileSystemHelper
{
public class FileWrapper : IFileWrapper
{
public FileWrapper() { }
public string[] ReadAllLines(string path)
{
int attempt = 0;
int maxRetries = 5;
// Sometimes when files are being installed, url applications are written to line by line.
// During this process their contents cannot be read as they are being accessed by an other process.
// This ensures that if we do face this scenario, we retry after some time.
while(attempt < maxRetries)
{
try
{
return File.ReadAllLines(path);
}
catch (IOException ex)
{
attempt++;
Thread.Sleep(500);
Log.Info($"File {path} is being accessed by another process| {ex.Message}");
}
}
return new string[] { String.Empty };
}
}
}

View File

@@ -0,0 +1,10 @@
using System.Diagnostics;
namespace Wox.Infrastructure.FileSystemHelper
{
public interface IFileVersionInfoWrapper
{
FileVersionInfo GetVersionInfo(string path);
string FileDescription { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Wox.Infrastructure.FileSystemHelper
{
public interface IFileWrapper
{
string[] ReadAllLines(string path);
}
}

View File

@@ -0,0 +1,26 @@
using System.Collections.ObjectModel;
using System.IO;
namespace Wox.Infrastructure.Storage
{
// File System Watcher Wrapper class which implements the IFileSystemWatcherWrapper interface
public class FileSystemWatcherWrapper : FileSystemWatcher, IFileSystemWatcherWrapper
{
public FileSystemWatcherWrapper() { }
Collection<string> IFileSystemWatcherWrapper.Filters
{
get => this.Filters;
set
{
if(value != null)
{
foreach(string filter in value)
{
this.Filters.Add(filter);
}
}
}
}
}
}

View File

@@ -0,0 +1,20 @@
using System.Collections.ObjectModel;
using System.IO;
namespace Wox.Infrastructure.Storage
{
public interface IFileSystemWatcherWrapper
{
// Events to watch out for
event FileSystemEventHandler Created;
event FileSystemEventHandler Deleted;
event RenamedEventHandler Renamed;
// Properties of File System watcher
Collection<string> Filters { get; set; }
bool EnableRaisingEvents { get; set; }
NotifyFilters NotifyFilter { get; set; }
string Path { get; set; }
bool IncludeSubdirectories { get; set; }
}
}

View File

@@ -31,7 +31,14 @@ namespace Wox.Infrastructure.Storage
public void Set(IList<T> items)
{
//enforce that internal representation
_items = new ConcurrentDictionary<int, T>(items.ToDictionary( i => i.GetHashCode()));
try
{
_items = new ConcurrentDictionary<int, T>(items.ToDictionary(i => i.GetHashCode()));
}
catch(ArgumentException e)
{
Log.Info($"|LisRepository.Set| Trying to insert a duplicate item", e.Message);
}
}
public bool Any()
@@ -50,7 +57,6 @@ namespace Wox.Infrastructure.Storage
public void Remove(T removedItem)
{
if (!_items.TryRemove(removedItem.GetHashCode(), out _))
{
Log.Error($"|ListRepository.Remove| Item Not Found <{removedItem}>");
@@ -76,5 +82,10 @@ namespace Wox.Infrastructure.Storage
{
return _items.GetEnumerator();
}
public int Count()
{
return _items.Count;
}
}
}