Skip to content

Commit

Permalink
Fix crash when the user does not have permission to access a subdirec…
Browse files Browse the repository at this point in the history
…tory in a folder during the check to ensure the folder is not empty. Closes #395

- Version bump to 3.2.0
  • Loading branch information
Iridium-IO committed Feb 29, 2024
1 parent 5acfbe1 commit d98c1b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 34 additions & 1 deletion CompactGUI.Core/SharedMethods.vb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,47 @@ Public Module SharedMethods
If Not IO.Directory.Exists(folder) Then : Return (False, "Directory does not exist")
ElseIf folder.Contains((Environment.GetFolderPath(Environment.SpecialFolder.Windows))) Then : Return (False, "Cannot compress system directory")
ElseIf folder.EndsWith(":\") Then : Return (False, "Cannot compress root directory")
ElseIf Not IO.Directory.EnumerateFiles(folder, "*", SearchOption.AllDirectories).Any() Then : Return (False, "Directory is empty")
ElseIf IsDirectoryEmptySafe(folder) Then : Return (False, "This directory is either empty or you are not authorized to access its files.")
ElseIf DriveInfo.GetDrives().First(Function(f) folder.StartsWith(f.Name)).DriveFormat <> "NTFS" Then : Return (False, "Cannot compress a directory on a non-NTFS drive")
End If

Return (True, "")

End Function

Function IsDirectoryEmptySafe(folder As String)

Try
Return Not IO.Directory.EnumerateFileSystemEntries(folder).Any()

For Each subdir In IO.Directory.EnumerateDirectories(folder)
Try
If Not IsDirectoryEmptySafe(subdir) Then Return False
Catch ex As System.UnauthorizedAccessException

End Try
Next

For Each file In IO.Directory.EnumerateFiles(folder)
Try
Return False
Catch ex As System.UnauthorizedAccessException

End Try
Next

Return True

Catch ex As System.UnauthorizedAccessException
MsgBox("You are not authorized to access some items in this folder." & vbCrLf & "Please try running CompactGUI as an administrator, otherwise these items will be skipped.", MsgBoxStyle.Exclamation, "Unauthorized Access")
Return False

Catch ex As Exception
Return False
End Try

End Function

Function GetFileSizeOnDisk(file As String) As Long
Dim hosize As UInteger
Dim losize As UInteger = GetCompressedFileSizeW(file, hosize)
Expand Down
2 changes: 1 addition & 1 deletion CompactGUI/Models/UpdateHandler.vb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Imports System.Text.Json
Public Class UpdateHandler

Public Shared CurrentVersion As New SemVersion(3, 1, 1)
Public Shared CurrentVersion As New SemVersion(3, 2, 0)
Public Shared NewVersion As SemVersion
Shared UpdateURL As String = "https://raw.githubusercontent.com/IridiumIO/CompactGUI/database/version.json"

Expand Down

0 comments on commit d98c1b9

Please sign in to comment.