Skip to content

Commit

Permalink
Use ByteSliceToString from golang.org/x/sys/unix (#2924)
Browse files Browse the repository at this point in the history
Use unix.ByteSliceToString to convert Utsname []byte fields to strings.

This also allows to drop the charsToString helper which serves the same
purpose and matches ByteSliceToString's implementation.

Signed-off-by: Tobias Klauser <[email protected]>

Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
tklauser and MrAlias authored May 26, 2022
1 parent 7458aa9 commit 4155b35
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 40 deletions.
1 change: 0 additions & 1 deletion sdk/resource/export_common_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package resource // import "go.opentelemetry.io/otel/sdk/resource"

var (
Uname = uname
CharsToString = charsToString
GetFirstAvailableFile = getFirstAvailableFile
)

Expand Down
20 changes: 5 additions & 15 deletions sdk/resource/os_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package resource // import "go.opentelemetry.io/otel/sdk/resource"

import (
"bytes"
"fmt"
"os"

Expand Down Expand Up @@ -69,23 +68,14 @@ func uname() (string, error) {
}

return fmt.Sprintf("%s %s %s %s %s",
charsToString(utsName.Sysname[:]),
charsToString(utsName.Nodename[:]),
charsToString(utsName.Release[:]),
charsToString(utsName.Version[:]),
charsToString(utsName.Machine[:]),
unix.ByteSliceToString(utsName.Sysname[:]),
unix.ByteSliceToString(utsName.Nodename[:]),
unix.ByteSliceToString(utsName.Release[:]),
unix.ByteSliceToString(utsName.Version[:]),
unix.ByteSliceToString(utsName.Machine[:]),
), nil
}

// charsToString converts a C-like null-terminated char array to a Go string.
func charsToString(charArray []byte) string {
if i := bytes.IndexByte(charArray, 0); i >= 0 {
charArray = charArray[:i]
}

return string(charArray)
}

// getFirstAvailableFile returns an *os.File of the first available
// file from a list of candidate file paths.
func getFirstAvailableFile(candidates []string) (*os.File, error) {
Expand Down
24 changes: 0 additions & 24 deletions sdk/resource/os_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,6 @@ func TestUnameError(t *testing.T) {
resource.SetDefaultUnameProvider()
}

func TestCharsToString(t *testing.T) {
tt := []struct {
Name string
Bytes []byte
Expected string
}{
{"Nil array", nil, ""},
{"Empty array", []byte{}, ""},
{"Empty string (null terminated)", []byte{0x00}, ""},
{"Nonempty string (null terminated)", []byte{0x31, 0x32, 0x33, 0x00}, "123"},
{"Nonempty string (non-null terminated)", []byte{0x31, 0x32, 0x33}, "123"},
{"Nonempty string with values after null", []byte{0x31, 0x32, 0x33, 0x00, 0x34}, "123"},
}

for _, tc := range tt {
tc := tc

t.Run(tc.Name, func(t *testing.T) {
result := resource.CharsToString(tc.Bytes)
require.EqualValues(t, tc.Expected, result)
})
}
}

func TestGetFirstAvailableFile(t *testing.T) {
tempDir := t.TempDir()

Expand Down

0 comments on commit 4155b35

Please sign in to comment.