utils

package
v0.0.0-...-3e0e934 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2026 License: Apache-2.0, BSD-2-Clause Imports: 44 Imported by: 0

Documentation

Overview

Package util common tools

* Copyright (c) 2020 Tencent Serverless * All rights reserved * Author: jiangdu * Date: 2020-06-15

Index

Examples

Constants

View Source
const (
	Vfio_pci   = "vfio-pci"
	Virtio_pci = "virtio-pci"
)
View Source
const DefaultTimeout = time.Second * 3
View Source
const (
	SuperblockOffset = 1024
)

Variables

View Source
var (
	ErrorKeyNotFound    = fmt.Errorf("key %w", errdefs.ErrNotFound)
	ErrorBucketNotFound = fmt.Errorf("bucket %w", errdefs.ErrNotFound)
)
View Source
var ErrLimitReached = errors.New("the read limit is reached")
View Source
var ErrMetadataUnsupported = errors.New("cloud metadata is not available in the opensource cubelet build")

Functions

func AttachVFIODriver

func AttachVFIODriver(ctx context.Context, pciBus string) error

func BindDriver

func BindDriver(ctx context.Context, pciBus string, driver string) error

func Contains

func Contains(val string, slice []string) bool

func ContainsError

func ContainsError(err error, keys string) bool

func CopyFile

func CopyFile(ctx context.Context, src, dst string) error

func Decode

func Decode(body string, req interface{}) error

func DenExist

func DenExist(path string) (bool, error)

func DetachVFIODriver

func DetachVFIODriver(ctx context.Context, pciBus string) error

func DetachVirtioDevice

func DetachVirtioDevice(ctx context.Context, pciBus string) error

func Exec

func Exec(arg string, timeout time.Duration) (string, string, error)

Exec runs the given string through /usr/bin/bash -lc, so the argument goes through full shell parsing. It is intended only for trusted internal callers (never concatenate external input into arg). The validation below still rejects NUL and control characters to catch obvious injection attempts and accidental misuse.

func ExecBin

func ExecBin(name string, args []string, timeout time.Duration) (string, string, error)

func ExecBinCtx

func ExecBinCtx(ctx context.Context, name string, args []string) (string, string, error)

func ExecV

func ExecV(argv []string, timeout time.Duration) (string, string, error)

func ExecVCtx

func ExecVCtx(ctx context.Context, argv []string) (string, string, error)

func FileExistAndValid

func FileExistAndValid(path string) (bool, error)

func FileExistWithSize

func FileExistWithSize(path string) (bool, int64, error)

func GenGatewayIP

func GenGatewayIP(ip string, subnetMask string) net.IP

func GenLocalMAC

func GenLocalMAC(ipStr string) string

func GenerateID

func GenerateID() string

func GetAllDirname

func GetAllDirname(pathname string) ([]string, error)

func GetBodyData

func GetBodyData(rsp *http.Response, object any) error

func GetDeviceIdleRatio

func GetDeviceIdleRatio(path string) (uint64, uint64, error)

func GetDiskCap

func GetDiskCap(path string) (uint64, uint64, error)

func GetInstanceID

func GetInstanceID() (string, error)

func GetLocalIpv4

func GetLocalIpv4() (string, error)

func GetLoopNameByBackendFile

func GetLoopNameByBackendFile(file string) (string, error)

func GetMountNumByBackendFile

func GetMountNumByBackendFile(file string) (int, error)

func GetMountNumByLoopName

func GetMountNumByLoopName(loop string) (int, error)

func GetProjectID

func GetProjectID() int

func GetRegion

func GetRegion() (string, error)

func GetSha256Value

func GetSha256Value(image string) (string, error)

func GetShortInstanceType

func GetShortInstanceType() (string, error)

func GetSubNetID

func GetSubNetID(mac string) (string, error)

func GetTruncatedID

func GetTruncatedID(id, prefix string) string

func GetVPCIDByMAC

func GetVPCIDByMAC(mac string) (string, error)

func HashCode

func HashCode(dimension string) uint32

func InStringSlice

func InStringSlice(ss []string, str string) bool

func InetAtoNv4

func InetAtoNv4(ip string) (uint, error)

func InetNtoAv4

func InetNtoAv4(ip uint) string

func InitDriverId

func InitDriverId(ctx context.Context, driver string) error

func Int32ToBytes

func Int32ToBytes(n int32) []byte

func Int64ToBytes

func Int64ToBytes(n int64) []byte

func InterfaceToString

func InterfaceToString(obj interface{}) string

func IsDebug

func IsDebug() bool

func IsDriverAttached

func IsDriverAttached(ctx context.Context, pciBus string, driver string) (eq bool, exist bool, err error)

func IsEmpty

func IsEmpty(dirname string) (bool, error)

func IsInteger

func IsInteger(s string) bool

func IsLikelyNotMountPoint

func IsLikelyNotMountPoint(file string) (bool, error)

func IsMountLoop

func IsMountLoop(backendFile string) bool

func IsMountPoint

func IsMountPoint(dst string) (bool, error)

func IsNotFound

func IsNotFound(err error) bool

func LazyUnmount

func LazyUnmount(dest string) error

func MakeBoltDBOption

func MakeBoltDBOption() *bolt.Options

func MapKeys

func MapKeys[K comparable, V any](m map[K]V) []K

func MaxCommonPrefix

func MaxCommonPrefix(arrays []string) string

func MergeStringSlices

func MergeStringSlices(a []string, b []string) []string

func Min

func Min(arg int, args ...int) int

func MountSqfs

func MountSqfs(src, dst string, timeout time.Duration) error

func NormalizeRepoDigest

func NormalizeRepoDigest(repoDigests []string) (repo, digest string)

func NormalizeRepoTagPair

func NormalizeRepoTagPair(repoTags []string, imageName string) (repoTagPairs [][]string)

func OrderedBy

func OrderedBy[T any](less ...lessFunc[T]) *multiSorter[T]

func ParseImageDigestAndTag

func ParseImageDigestAndTag(name string) (repoTag string, repoDigest string, err error)

func PipeExecV

func PipeExecV(argv []string, timeout time.Duration) (string, string, error)

func ProcessExists

func ProcessExists(ctx context.Context, pid int) bool

func QuotaDir

func QuotaDir(ctx context.Context, dir string, quota uint64, projectID int) error

func QuotaDirScale

func QuotaDirScale(ctx context.Context, limit uint64, projectID int) error

func ReadAtMost

func ReadAtMost(r io.Reader, limit int64) ([]byte, error)

func ReadIPFromStrInterface

func ReadIPFromStrInterface(i interface{}) net.IP

func Recover

func Recover() error

func RemoveStringPrefix

func RemoveStringPrefix(arrays []string, prefix string) []string

func SafeCopyFile

func SafeCopyFile(dst, src string) (err error)

func SafeJoinPath

func SafeJoinPath(baseDir, untrusted string) (string, error)

SafeJoinPath joins baseDir with the untrusted component and verifies the resulting path stays within baseDir (prevents path traversal attacks). Returns an error if the resolved path escapes the base directory.

func SkipCI

func SkipCI(t testing.TB)

func String2Slice

func String2Slice(s string) []byte

func ValidateBDF

func ValidateBDF(bdf string) bool

func WriteStrToFile

func WriteStrToFile(path, msg string) error

Types

type BlockGroupDescriptor

type BlockGroupDescriptor struct {
	BlockBitmap       uint32
	InodeBitmap       uint32
	InodeTable        uint32
	FreeBlocksCount   uint16
	FreeInodesCount   uint16
	UsedDirsCount     uint16
	Flags             uint16
	ExcludeBitmapLo   uint32
	BlockBitmapCsumLo uint16
	InodeBitmapCsumLo uint16
	ItableUnused      uint16
	Checksum          uint16
}

func GetExt4BlockGroupDescriptor

func GetExt4BlockGroupDescriptor(device string) (*BlockGroupDescriptor, error)

type CubeStore

type CubeStore struct {
	// contains filtered or unexported fields
}

func NewCubeStore

func NewCubeStore(path string, opts *bolt.Options) (*CubeStore, error)

func NewCubeStoreExt

func NewCubeStoreExt(path string, file string, num uint64, opts *bolt.Options) (*CubeStore, error)

func (*CubeStore) Close

func (cs *CubeStore) Close() error

func (*CubeStore) Delete

func (cs *CubeStore) Delete(bucket, key string) (err error)

func (*CubeStore) DeleteBs

func (cs *CubeStore) DeleteBs(key string, buckets ...[]byte) (err error)

func (*CubeStore) DeleteWithTx

func (cs *CubeStore) DeleteWithTx(bucket, key string, callback func() error) (err error)

func (*CubeStore) Get

func (cs *CubeStore) Get(bucket, key string) (result []byte, err error)

func (*CubeStore) GetBs

func (cs *CubeStore) GetBs(key string, buckets ...[]byte) (result []byte, err error)

func (*CubeStore) ReadAll

func (cs *CubeStore) ReadAll(bucket string) (all map[string][]byte, _ error)

func (*CubeStore) ReadAllBs

func (cs *CubeStore) ReadAllBs(buckets ...[]byte) (all map[string][]byte, _ error)

func (*CubeStore) Set

func (cs *CubeStore) Set(bucket, key string, value []byte) (err error)

func (*CubeStore) SetBs

func (cs *CubeStore) SetBs(key string, value []byte, buckets ...[]byte) (err error)

func (*CubeStore) SetWithTx

func (cs *CubeStore) SetWithTx(bucket, key string, value []byte, callback func() error) (err error)

type HostIdentity

type HostIdentity struct {
	InstanceID   string
	LocalIPv4    string
	InstanceType string
	Region       string
}

func GetHostIdentity

func GetHostIdentity() (HostIdentity, error)

type Less

type Less func(a, b interface{}) bool

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}
Example
q := NewQueue[Tap]()
q.Enqueue(&Tap{Name: "1"})
q.Enqueue(&Tap{Name: "2"})
q.Enqueue(&Tap{Name: "3"})

fmt.Println(q.Dequeue())
fmt.Println(q.Dequeue())
fmt.Println(q.Dequeue())

func NewQueue

func NewQueue[T any]() *Queue[T]

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() *T

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(v *T)

func (*Queue[T]) Length

func (q *Queue[T]) Length() int

type ResMutex

type ResMutex struct {
	// contains filtered or unexported fields
}

type ResourceLocks

type ResourceLocks struct {
	// contains filtered or unexported fields
}

func NewResourceLocks

func NewResourceLocks() *ResourceLocks

func (*ResourceLocks) Len

func (r *ResourceLocks) Len() int

func (*ResourceLocks) Lock

func (r *ResourceLocks) Lock(resource string) func()

type Superblock

type Superblock struct {
	InodesCount     uint32
	BlocksCount     uint32
	RBlocksCount    uint32
	FreeBlocksCount uint32
	FreeInodesCount uint32
	FirstDataBlock  uint32
	LogBlockSize    uint32
	LogFragSize     uint32
	BlocksPerGroup  uint32
	FragsPerGroup   uint32
	InodesPerGroup  uint32
	Mtime           uint32
	Wtime           uint32
	MntCount        uint16
	MaxMntCount     uint16
	Magic           uint16
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL