diff --git a/src/Whereis/Program.cs b/src/Whereis/Program.cs index e608069..f3b6506 100644 --- a/src/Whereis/Program.cs +++ b/src/Whereis/Program.cs @@ -14,6 +14,8 @@ public class Program { } WhereisUtils utils = new WhereisUtils(); + + utils.prepare(); string fileName = Path.GetFileNameWithoutExtension(args[0]); string fileExtension = Path.GetExtension(args[0]);; @@ -25,6 +27,7 @@ public class Program { break; } + Console.ResetColor(); ConcurrentBag files = utils.FindFiles(fileName, fileExtension, searchPath, maxDepth: 5); bool foundFiles = false; diff --git a/src/Whereis/Whereis.cs b/src/Whereis/Whereis.cs index 23fbc70..5a46a67 100644 --- a/src/Whereis/Whereis.cs +++ b/src/Whereis/Whereis.cs @@ -2,9 +2,39 @@ namespace Whereis.Utils; public class WhereisUtils { - private readonly string[] ExcludedDirectories = { "/dev", "/proc", "/tmp", "/boot", "/run", "/root", "/sys", "/etc", "/var", "/lib", "/lib64", "/usr", "/sbin", "/lost+found" }; - public ConcurrentBag FindFiles(string fileName, string fileExtension, string searchPath, int depth = 0, int maxDepth = 5) { + public string[] ExcludedDirectories = new string[] {}; + public string[] prepare() { + if (wasExecutedWithSudo() == false) { + ExcludedDirectories = new string[] { "/dev", "/proc", "/tmp", "/boot", "/run", "/root", "/sys", "/etc", "/var", "/lib", "/lib64", "/usr", "/sbin", "/lost+found" }; + } else { + ExcludedDirectories = new string[] { "/dev", "/proc" }; + } + + return ExcludedDirectories; + } + + private bool wasExecutedWithSudo() { + bool sudo = false; + try { + string protectedDirectoryPath = "/root"; + + // Attempt to open the protected directory + Directory.GetFiles(protectedDirectoryPath); + + // Access succeeded + Console.WriteLine("Program executed with sudo privileges."); + sudo = true; + } + catch (UnauthorizedAccessException) { + // Access denied + Console.WriteLine("Program executed without sudo privileges."); + } + + return sudo; + } + + public ConcurrentBag FindFiles(string fileName, string fileExtension, string searchPath, int depth = 0, int maxDepth = 5) { var foundFiles = new ConcurrentBag(); if (depth > maxDepth) { @@ -52,4 +82,4 @@ public class WhereisUtils { return false; } -} \ No newline at end of file +}