Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions lib/decompress.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,36 @@ function Expand-7zipArchive {
abort "Failed to list files in $Path.`nNot a 7-Zip supported archive file."
}
}
if (!$IsTar -and $ExtractDir) {
movedir "$DestinationPath\$ExtractDir" $DestinationPath | Out-Null
# Remove temporary directory if it is empty
$ExtractDirTopPath = [string] "$DestinationPath\$($ExtractDir -replace '[\\/].*')"
if ((Get-ChildItem -Path $ExtractDirTopPath -Force -ErrorAction Ignore).Count -eq 0) {
Remove-Item -Path $ExtractDirTopPath -Recurse -Force -ErrorAction Ignore
if (-not $IsTar -and $ExtractDir) {
# Assets
$ExtractDirPath = [string] [System.IO.Path]::GetFullPath(
[System.IO.Path]::Combine($DestinationPath, $ExtractDir)
)
$ExtractDirTopPath = [string] [System.IO.Path]::GetFullPath(
[System.IO.Path]::Combine(
$DestinationPath,
(
$ExtractDir -split '[\\/]' |
Where-Object -FilterScript {-not [string]::IsNullOrWhiteSpace($_)} |
Select-Object -First 1
)
)
)
# Check if $ExtractDirPath contains a directory with the same name as $ExtractDirTopPath
$DeleteExtractDirTopPath = [bool](
([System.IO.DirectoryInfo]($ExtractDirTopPath)).'Name' -notin [System.IO.Directory]::GetDirectories(
$ExtractDirPath
).ForEach{
$_.Split(
[System.IO.Path]::DirectorySeparatorChar
)[-1]
}
)
Comment on lines +140 to +148
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add error handling for directory enumeration.

If $ExtractDirPath doesn't exist or is inaccessible, GetDirectories() will throw an exception and halt the extraction. Consider wrapping this in a try-catch or checking path existence first.

Apply this diff to add error handling:

         )
     )
-    # Check if $ExtractDirPath contains a directory with the same name as $ExtractDirTopPath
-    $DeleteExtractDirTopPath = [bool](
-        ([System.IO.DirectoryInfo]($ExtractDirTopPath)).'Name' -notin [System.IO.Directory]::GetDirectories(
-            $ExtractDirPath
-        ).ForEach{
-            $_.Split(
-                [System.IO.Path]::DirectorySeparatorChar
-            )[-1]
-        }
-    )
+    # Check if $ExtractDirPath contains a directory with the same name as $ExtractDirTopPath
+    $DeleteExtractDirTopPath = $true
+    if (Test-Path -Path $ExtractDirPath -PathType Container) {
+        $DeleteExtractDirTopPath = [bool](
+            ([System.IO.DirectoryInfo]($ExtractDirTopPath)).'Name' -notin [System.IO.Directory]::GetDirectories(
+                $ExtractDirPath
+            ).ForEach{
+                $_.Split(
+                    [System.IO.Path]::DirectorySeparatorChar
+                )[-1]
+            }
+        )
+    }
     # Move content of $ExtractDir to destination
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$DeleteExtractDirTopPath = [bool](
([System.IO.DirectoryInfo]($ExtractDirTopPath)).'Name' -notin [System.IO.Directory]::GetDirectories(
$ExtractDirPath
).ForEach{
$_.Split(
[System.IO.Path]::DirectorySeparatorChar
)[-1]
}
)
# Check if $ExtractDirPath contains a directory with the same name as $ExtractDirTopPath
$DeleteExtractDirTopPath = $true
if (Test-Path -Path $ExtractDirPath -PathType Container) {
$DeleteExtractDirTopPath = [bool](
([System.IO.DirectoryInfo]($ExtractDirTopPath)).'Name' -notin [System.IO.Directory]::GetDirectories(
$ExtractDirPath
).ForEach{
$_.Split(
[System.IO.Path]::DirectorySeparatorChar
)[-1]
}
)
}
# Move content of $ExtractDir to destination
🤖 Prompt for AI Agents
In lib/decompress.ps1 around lines 140 to 148, the call to
[System.IO.Directory]::GetDirectories($ExtractDirPath) can throw if
$ExtractDirPath doesn't exist or is inaccessible; wrap the directory enumeration
in a check/try-catch: first verify Test-Path -Path $ExtractDirPath -PathType
Container and only enumerate if true, otherwise set the result to an empty
array; alternatively wrap the GetDirectories call in try { ... } catch { log the
error (or set empty array) } so $DeleteExtractDirTopPath calculation never
throws and proceeds safely.

# Move content of $ExtractDir to destination
$null = movedir -from $ExtractDirPath -to $DestinationPath
# Remove leftovers after movedir
if ($DeleteExtractDirTopPath) {
Remove-Item -Path $ExtractDirTopPath -Recurse -Force -ErrorAction 'Ignore'
}
}
if (Test-Path $LogPath) {
Expand Down
Loading