Program paths fix + #1779 (#1780)

* ProgramPaths now searches for files on a per-folder basis using a Queue instead of calling EnumerateFiles.  This allows us to escape permission errors.
Issue #1779 is fixed because of this too.
This commit is contained in:
Boris Makogonyuk 2017-12-24 11:52:53 +01:00 committed by GitHub
parent e251c59da1
commit 1c10ee106e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -206,28 +206,37 @@ namespace Wox.Plugin.Program.Programs
{ {
if (!Directory.Exists(directory)) if (!Directory.Exists(directory))
return new string[] { }; return new string[] { };
var files = new List<string>();
var ds = Directory.GetDirectories(directory); var folderQueue = new Queue<string>();
folderQueue.Enqueue(directory);
var paths = ds.SelectMany(d => do
{ {
var currentDirectory = folderQueue.Dequeue();
try try
{ {
var paths_for_suffixes = suffixes.SelectMany(s => foreach (var suffix in suffixes)
{ {
var pattern = $"*.{s}"; files.AddRange(Directory.EnumerateFiles(currentDirectory, $"*.{suffix}", SearchOption.TopDirectoryOnly));
var ps = Directory.EnumerateFiles(d, pattern, SearchOption.AllDirectories); }
return ps;
});
return paths_for_suffixes;
} }
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException) catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
{ {
Log.Exception($"|Program.Win32.ProgramPaths|Don't have permission on <{directory}>", e); Log.Exception($"|Program.Win32.ProgramPaths|Don't have permission on <{currentDirectory}>", e);
return new List<string>();
} }
});
return paths; try
{
foreach (var childDirectory in Directory.EnumerateDirectories(currentDirectory, "*", SearchOption.TopDirectoryOnly))
{
folderQueue.Enqueue(childDirectory);
}
}
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
{
Log.Exception($"|Program.Win32.ProgramPaths|Don't have permission on <{currentDirectory}>", e);
}
} while (folderQueue.Any());
return files;
} }
private static string Extension(string path) private static string Extension(string path)