mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
refactor: Maintain single code path for package manager execution (#256)
* refactor: Maintain single code path for package manager execution * fix: Avoid context leak during interactive TTY read * fix: PTY flow handling * fix: Linter fixes
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
//go:build !windows
|
||||
|
||||
package pty
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func readInput(ctx context.Context, src io.Reader, buf []byte) (int, error) {
|
||||
file, ok := src.(*os.File)
|
||||
if !ok {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return 0, ctx.Err()
|
||||
default:
|
||||
return src.Read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
fd := int32(file.Fd())
|
||||
pollFds := []unix.PollFd{{Fd: fd, Events: unix.POLLIN}}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return 0, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
n, err := unix.Poll(pollFds, 100)
|
||||
if err != nil {
|
||||
if errors.Is(err, unix.EINTR) {
|
||||
continue
|
||||
}
|
||||
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
revents := pollFds[0].Revents
|
||||
if revents&(unix.POLLERR|unix.POLLHUP|unix.POLLNVAL) != 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
|
||||
if revents&unix.POLLIN != 0 {
|
||||
return file.Read(buf)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user