Respond to review comments
* use LookPath for better error messages * support procfs in containers
This commit is contained in:
parent
84a665b712
commit
7c1aa06a5e
|
@ -24,7 +24,11 @@ func SwapDevices() ([]*SwapDevice, error) {
|
|||
}
|
||||
|
||||
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
|
||||
output, err := exec.Command(swapCommand, "-lk").Output()
|
||||
swapCommandPath, err := exec.LookPath(swapCommand)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not find command %q: %w", swapCommand, err)
|
||||
}
|
||||
output, err := exec.Command(swapCommandPath, "-lk").Output()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not execute %q: %w", swapCommand, err)
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/shirou/gopsutil/internal/common"
|
||||
"github.com/shirou/gopsutil/v3/internal/common"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
|
@ -430,7 +431,7 @@ func calcuateAvailVmem(ret *VirtualMemoryStat, retEx *VirtualMemoryExStat) uint6
|
|||
return availMemory
|
||||
}
|
||||
|
||||
const swapsFilePath = "/proc/swaps"
|
||||
const swapsFilename = "swaps"
|
||||
|
||||
// swaps file column indexes
|
||||
const (
|
||||
|
@ -446,6 +447,7 @@ func SwapDevices() ([]*SwapDevice, error) {
|
|||
}
|
||||
|
||||
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
|
||||
swapsFilePath := common.HostProc(swapsFilename)
|
||||
f, err := os.Open(swapsFilePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -456,6 +458,7 @@ func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
|
|||
}
|
||||
|
||||
func parseSwapsFile(r io.Reader) ([]*SwapDevice, error) {
|
||||
swapsFilePath := common.HostProc(swapsFilename)
|
||||
scanner := bufio.NewScanner(r)
|
||||
if !scanner.Scan() {
|
||||
if err := scanner.Err(); err != nil {
|
||||
|
|
|
@ -141,7 +141,11 @@ func SwapDevices() ([]*SwapDevice, error) {
|
|||
}
|
||||
|
||||
func SwapDevicesWithContext(ctx context.Context) ([]*SwapDevice, error) {
|
||||
output, err := exec.Command(swapsCommand, "-l").Output()
|
||||
swapsCommandPath, err := exec.LookPath(swapsCommand)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not find command %q: %w", swapCommand, err)
|
||||
}
|
||||
output, err := exec.Command(swapsCommandPath, "-l").Output()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not execute %q: %w", swapsCommand, err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue