Skip to content

Commit

Permalink
fix: check permissions before attempting chmod
Browse files Browse the repository at this point in the history
closes #363
  • Loading branch information
RobCannon authored and JanDeDobbeleer committed Jan 24, 2021
1 parent 5de5bf3 commit 773bef8
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions packages/powershell/oh-my-posh/oh-my-posh.psm1
Expand Up @@ -14,10 +14,37 @@ function Get-PoshCommand {
return $poshCommand
}

# Set the right binary to executable before doing anything else
if ($PSVersionTable.PSEdition -eq "Core" -and !$IsWindows) {
function Set-ExecutablePermissions {
# Set the right binary to executable before doing anything else
# Permissions don't need to be set on Windows
if ($PSVersionTable.PSEdition -ne "Core" -or $IsWindows) {
return
}

$executable = Get-PoshCommand
Invoke-Expression -Command "chmod +x $executable"
if (-Not (Test-Path $executable)) {
# This should only happend with a corrupt installation
Write-Warning "Executable at $executable was not found"
return
}

# Check the permissions on the file
$permissions = ((ls -l $executable) -split ' ')[0] # $permissions will be something like '-rw-r--r--'
if ((id -u) -eq 0) {
# Running as root, give global executable permissions if needed
$hasWrite = $permissions[2] -eq 'w'
$hasExecutable = $permissions[3] -eq 'x'
if ($hasWrite -and -not $hasExecutable) {
Invoke-Expression -Command "chmod g+x $executable"
}
return
}
# Running as user, give user executable permissions if needed
$hasWrite = $permissions[8] -eq 'w'
$hasExecutable = $permissions[9] -eq 'x'
if ($hasWrite -and -not $hasExecutable) {
Invoke-Expression -Command "chmod +x $executable"
}
}

function Set-PoshPrompt {
Expand Down Expand Up @@ -104,6 +131,8 @@ function ThemeCompletion {
ForEach-Object { New-CompletionResult -CompletionText $_ }
}

Set-ExecutablePermissions

Register-ArgumentCompleter `
-CommandName Set-PoshPrompt `
-ParameterName Theme `
Expand Down

0 comments on commit 773bef8

Please sign in to comment.