From f11e3ba120f70c03914e845ecf7f79c33d5ebf30 Mon Sep 17 00:00:00 2001 From: shirou Date: Mon, 4 Jul 2022 08:41:25 +0000 Subject: [PATCH] fix(net,linux): move IsLittleEndian to internal --- internal/common/endian.go | 10 ++++++++++ net/net.go | 8 -------- net/net_linux.go | 2 +- net/net_linux_test.go | 2 +- 4 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 internal/common/endian.go diff --git a/internal/common/endian.go b/internal/common/endian.go new file mode 100644 index 0000000..147cfdc --- /dev/null +++ b/internal/common/endian.go @@ -0,0 +1,10 @@ +package common + +import "unsafe" + +// IsLittleEndian checks if the current platform uses little-endian. +// copied from https://github.com/ntrrg/ntgo/blob/v0.8.0/runtime/infrastructure.go#L16 (MIT License) +func IsLittleEndian() bool { + var x int16 = 0x0011 + return *(*byte)(unsafe.Pointer(&x)) == 0x11 +} diff --git a/net/net.go b/net/net.go index 0f28f28..0f3a62f 100644 --- a/net/net.go +++ b/net/net.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "net" - "unsafe" "github.com/shirou/gopsutil/v3/internal/common" ) @@ -272,10 +271,3 @@ func getIOCountersAll(n []IOCountersStat) ([]IOCountersStat, error) { return []IOCountersStat{r}, nil } - -// IsLittleEndian checks if the current platform uses little-endian. -// copied from https://github.com/ntrrg/ntgo/blob/v0.8.0/runtime/infrastructure.go#L16 (MIT License) -func IsLittleEndian() bool { - var x int16 = 0x0011 - return *(*byte)(unsafe.Pointer(&x)) == 0x11 -} diff --git a/net/net_linux.go b/net/net_linux.go index 21d2a24..c089971 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -723,7 +723,7 @@ func decodeAddressWithContext(ctx context.Context, family uint32, src string) (A var ip net.IP if family == syscall.AF_INET { - if IsLittleEndian() { + if common.IsLittleEndian() { ip = net.IP(ReverseWithContext(ctx, decoded)) } else { ip = net.IP(decoded) diff --git a/net/net_linux_test.go b/net/net_linux_test.go index 05ff023..d5d6252 100644 --- a/net/net_linux_test.go +++ b/net/net_linux_test.go @@ -151,7 +151,7 @@ func TestDecodeAddress(t *testing.T) { Error: true, }, } - if IsLittleEndian() { + if common.IsLittleEndian() { addr["0500000A:0016"] = AddrTest{ IP: "10.0.0.5", Port: 22,