mirror of
https://github.com/cubefs/cubefs.git
synced 2026-08-02 02:00:56 +00:00
feat(util): add util function implements
with #22357426 Signed-off-by: Cloudstriff <chenjiongwendao@qq.com>
This commit is contained in:
parent
562c16fc63
commit
db9ffe9d1c
77
blobstore/util/util.go
Normal file
77
blobstore/util/util.go
Normal file
@ -0,0 +1,77 @@
|
||||
// Copyright 2023 The Cuber Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
// implied. See the License for the specific language governing
|
||||
// permissions and limitations under the License.
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"os"
|
||||
"unsafe"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// GenTmpPath create a temporary path
|
||||
func GenTmpPath() (string, error) {
|
||||
id := uuid.NewString()
|
||||
path := os.TempDir() + "/" + id
|
||||
if err := os.RemoveAll(path); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := os.MkdirAll(path, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func StringToBytes(s string) []byte {
|
||||
return unsafe.Slice(unsafe.StringData(s), len(s))
|
||||
}
|
||||
|
||||
func BytesToString(b []byte) string {
|
||||
return unsafe.String(unsafe.SliceData(b), len(b))
|
||||
}
|
||||
|
||||
func GetLocalIP() (string, error) {
|
||||
addresses, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, address := range addresses {
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
return ipnet.IP.String(), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.New("can not find the local ip address")
|
||||
}
|
||||
|
||||
func GenUnusedPort() int {
|
||||
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
l, err := net.ListenTCP("tcp", addr)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
defer l.Close()
|
||||
|
||||
return l.Addr().(*net.TCPAddr).Port
|
||||
}
|
||||
41
blobstore/util/util_test.go
Normal file
41
blobstore/util/util_test.go
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright 2023 The Cuber Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
// implied. See the License for the specific language governing
|
||||
// permissions and limitations under the License.
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGenTmpPath(t *testing.T) {
|
||||
path, err := GenTmpPath()
|
||||
require.NoError(t, err)
|
||||
require.NotEqual(t, "", path)
|
||||
}
|
||||
|
||||
func TestStringsToBytes(t *testing.T) {
|
||||
str := "test"
|
||||
b := StringToBytes(str)
|
||||
require.Equal(t, str, string(b))
|
||||
require.Equal(t, 0, len(StringToBytes("")))
|
||||
}
|
||||
|
||||
func TestBytesToString(t *testing.T) {
|
||||
b := []byte("test")
|
||||
str := BytesToString(b)
|
||||
require.Equal(t, str, string(b))
|
||||
require.Equal(t, "", BytesToString(nil))
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user