Skip to content

Commit

Permalink
fix: correctly resolve executable path on windows
Browse files Browse the repository at this point in the history
- closes: #51

Signed-off-by: Liam Stanley <liam@liam.sh>
  • Loading branch information
lrstanley committed Jan 31, 2025
1 parent 66047ac commit 973bd5e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions command_others.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
package ytdlp

import (
"os"
"os/exec"
)

// applySyscall applies any OS-specific syscall attributes to the command.
func (c *Command) applySyscall(_ *exec.Cmd) {
// No-op by default.
}

func isExecutable(_ string, stat os.FileInfo) bool {
return true // no-op.
}
6 changes: 6 additions & 0 deletions command_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package ytdlp

import (
"os"
"os/exec"
"syscall"
)
Expand All @@ -17,3 +18,8 @@ func (c *Command) applySyscall(cmd *exec.Cmd) {
Setpgid: c.separateProcessGroup,
}
}

func isExecutable(_ string, stat os.FileInfo) bool {
// On Unix systems, check if executable bit is set (user, group, or others).
return stat.Mode().Perm()&0o100 != 0 || stat.Mode().Perm()&0o010 != 0 || stat.Mode().Perm()&0o001 != 0

This comment has been minimized.

Copy link
@tarik02

tarik02 Feb 1, 2025

Contributor

wouldn't this look better?

	return stat.Mode().Perm()&0o111 != 0
}
12 changes: 12 additions & 0 deletions command_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package ytdlp

import (
"debug/pe"
"os"
"os/exec"
"syscall"
)
Expand All @@ -21,3 +23,13 @@ func (c *Command) applySyscall(cmd *exec.Cmd) {
cmd.SysProcAttr.CreationFlags |= syscall.CREATE_NEW_PROCESS_GROUP
}
}

func isExecutable(path string, _ os.FileInfo) bool {
// Try to parse as PE (Portable Executable) format.
f, err := pe.Open(path)
if err != nil {
return false
}
f.Close()
return true
}
2 changes: 1 addition & 1 deletion install.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ func resolveExecutable(fromCache, calleeIsDownloader bool) (r *ResolvedInstall,
continue
}

if !stat.IsDir() && (stat.Mode().Perm()&0o100 != 0 || stat.Mode().Perm()&0o010 != 0 || stat.Mode().Perm()&0o001 != 0) {
if !stat.IsDir() && isExecutable(bin, stat) {
r = &ResolvedInstall{
Executable: bin,
FromCache: true,
Expand Down

0 comments on commit 973bd5e

Please sign in to comment.