Skip to content

Commit

Permalink
win: fix language dependent delete script #149
Browse files Browse the repository at this point in the history
This commit addresses the language dependency of the `takeown /d y`
command in non-English Windows versions by using the `choice` utility.
This utility dynamically determines the equivalent of 'yes' in the
current system language, resolving issues encountered in the delete
script.

Other solution options such as enumerating language equivalents,
adjusting script culture settings, using side-effects of the `copy`
command, and parsing `takeown` help documentation proved either
impractical or unreliable.

The `choice` command has been successfully tested in both English and
German environments, ensuring reliable execution across various locales.
This change replaces the previous `takeown` usage in the script,
its reliability across diverse Windows locales.
  • Loading branch information
undergroundwires committed Dec 30, 2023
1 parent 86fde6d commit 8f4b34f
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/application/collections/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14354,10 +14354,20 @@ functions:
parameters:
pathGlob: '{{ $pathGlob }}'
recurse: '{{ with $recurse }}{{ . }}{{ end }}'
# Granting permissions has limitations for wildcard due to `takeown` and `icacls`. These commands are used for their simplicity to avoid adjusting token privileges.
# Marked: refactor-with-variables (optionally)
# Granting permissions has limitations for wildcard due to `takeown` and `icacls`. These commands are used for their simplicity to avoid adjusting token privileges.
# However, adjusting token privileges is already implemented by `SoftFileDelete`, when this kind of implementations are reusable, this script can be improved to
# use `Get-Acl`, `Set-Acl` instead for better wildcards support.
# Marked: refactor-with-variables
# use `Get-Acl`, `Set-Acl` instead for better wildcards support. When using `Get-Acl`, `Set-Acl`, think also about a way to handle when the user is lacking "List Folder"
# Considerations for using `Get-Acl` and `Set-Acl`:
# These commands may encounter issues when the user lacks "List Folder" permissions on a parent directory, which is essential for the `DeleteGlob` function.
# This is robustly handled by `takeown`.
# `takeown` effectively handles scenarios where the user lacks "List Folder" permissions.
# It requires a localized 'yes' flag, which varies with the system language ('y' for English).
# To find the localized 'yes', the script uses the `choice` command. This approach is simpler and more reliable
# than parsing `takeown /?`, which has proven to be inconsistent across different languages.
# For future enhancements:
# - Explore handling folder listing permission issues when transitioning to `Get-Acl` and `Set-Acl`.
# - Currently, `takeown` is preferred for its reliability in permission handling, especially in wildcard scenarios.
beforeIteration: |-
{{ with $grantPermissions }}
# Not using `Get-Acl`/`Set-Acl` to avoid adjusting token privileges
Expand All @@ -14376,7 +14386,18 @@ functions:
}
$takeOwnershipCommand = "takeown /f `"$cmdPath`" /a" # `icacls /setowner` does not succeed, so use `takeown` instead.
if (-not (Test-Path -Path "$expandedPath" -PathType Leaf)) {
$takeOwnershipCommand += ' /r /d y'
$localizedYes = 'Y' # Default 'Yes' flag (fallback)
try {
$choiceOutput = cmd /c "choice <nul 2>nul"
if ($choiceOutput -and $choiceOutput.Length -ge 2) {
$localizedYes = $choiceOutput[1]
} else {
Write-Warning "Failed to determine localized 'Yes' character. Output: `"$choiceOutput`""
}
} catch {
Write-Warning "Failed to determine localized 'Yes' character. Error: $_"
}
$takeOwnershipCommand += " /r /d $localizedYes"
}
$takeOwnershipOutput = cmd /c "$takeOwnershipCommand 2>&1" # `stderr` message is misleading, e.g. "ERROR: The system cannot find the file specified." is not an error.
if ($LASTEXITCODE -eq 0) {
Expand Down

0 comments on commit 8f4b34f

Please sign in to comment.