win: fix language dependent delete script #149
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.
This commit is contained in:
@@ -14354,10 +14354,20 @@ functions:
|
||||
parameters:
|
||||
pathGlob: '{{ $pathGlob }}'
|
||||
recurse: '{{ with $recurse }}{{ . }}{{ end }}'
|
||||
# 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
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user