From 1c10ee106e033191e002558ce4a9c51faa9e7ff4 Mon Sep 17 00:00:00 2001 From: Boris Makogonyuk Date: Sun, 24 Dec 2017 11:52:53 +0100 Subject: [PATCH] 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. --- Plugins/Wox.Plugin.Program/Programs/Win32.cs | 37 ++++++++++++-------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/Programs/Win32.cs b/Plugins/Wox.Plugin.Program/Programs/Win32.cs index 1c4fd09e7a..020909fb67 100644 --- a/Plugins/Wox.Plugin.Program/Programs/Win32.cs +++ b/Plugins/Wox.Plugin.Program/Programs/Win32.cs @@ -206,28 +206,37 @@ namespace Wox.Plugin.Program.Programs { if (!Directory.Exists(directory)) return new string[] { }; - - var ds = Directory.GetDirectories(directory); - - var paths = ds.SelectMany(d => + var files = new List(); + var folderQueue = new Queue(); + folderQueue.Enqueue(directory); + do { + var currentDirectory = folderQueue.Dequeue(); try { - var paths_for_suffixes = suffixes.SelectMany(s => + foreach (var suffix in suffixes) { - var pattern = $"*.{s}"; - var ps = Directory.EnumerateFiles(d, pattern, SearchOption.AllDirectories); - return ps; - }); - return paths_for_suffixes; + files.AddRange(Directory.EnumerateFiles(currentDirectory, $"*.{suffix}", SearchOption.TopDirectoryOnly)); + } } catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException) { - Log.Exception($"|Program.Win32.ProgramPaths|Don't have permission on <{directory}>", e); - return new List(); + Log.Exception($"|Program.Win32.ProgramPaths|Don't have permission on <{currentDirectory}>", e); } - }); - 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)