Skip to content

Commit

Permalink
Fix access error while reading project files. (#175)
Browse files Browse the repository at this point in the history
* Add 'try with' expression to safely scan directories.

* Fix unauthorized access exception while scanning dirs
  • Loading branch information
sheridanchris authored Oct 18, 2022
1 parent 76913a6 commit bb49dd9
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/Ionide.ProjInfo.ProjectSystem/WorkspacePeek.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,23 @@ module WorkspacePeek =
let scanDir (dirInfo: DirectoryInfo) =
let hasExt (ext: string) (s: FileInfo) = s.FullName.EndsWith(ext)

dirInfo.EnumerateFiles("*.*", SearchOption.TopDirectoryOnly)
let topLevelFiles =
try
dirInfo.EnumerateFiles("*.*", SearchOption.TopDirectoryOnly)
with
| :? UnauthorizedAccessException as ex ->
logger.error (
Log.setMessage "Unauthorized access error while reading files of {dir}"
>> Log.addContextDestructured "dir" dirInfo.Name
>> Log.addExn ex
)

Seq.empty
| ex ->
logger.error (Log.setMessage "Failed to read files of {dir}" >> Log.addContextDestructured "dir" dirInfo.Name >> Log.addExn ex)
Seq.empty

topLevelFiles
|> Seq.choose (fun s ->
match s with
| x when x |> hasExt ".sln" -> Some(UsefulFile.Sln, x)
Expand All @@ -56,12 +72,33 @@ module WorkspacePeek =
|> Seq.toArray

let dirs =
let getDirectories (dirInfo: DirectoryInfo) =
try
dirInfo.GetDirectories()
with
| :? UnauthorizedAccessException as ex ->
logger.error (
Log.setMessage "Unauthorized access error while reading sub directories of {dir}"
>> Log.addContextDestructured "dir" dirInfo.Name
>> Log.addExn ex
)

Array.empty
| ex ->
logger.error (
Log.setMessage "Failed to read sub directories of {dir}"
>> Log.addContextDestructured "dir" dirInfo.Name
>> Log.addExn ex
)

Array.empty

let rec scanDirs (dirInfo: DirectoryInfo) lvl =
seq {
if lvl <= deep then
yield dirInfo

for s in dirInfo.GetDirectories() do
for s in getDirectories dirInfo do
if not (ignored s.Name) then
yield! scanDirs s (lvl + 1)
}
Expand Down

0 comments on commit bb49dd9

Please sign in to comment.