tstor/src/host/vfs/utils.go
royalcat bcda69daad
All checks were successful
docker / build-docker (linux/386) (push) Successful in 14m50s
docker / build-docker (linux/amd64) (push) Successful in 15m5s
docker / build-docker (linux/arm64/v8) (push) Successful in 23m36s
docker / build-docker (linux/arm/v7) (push) Successful in 24m3s
docker / build-docker (linux/arm64) (push) Successful in 11m55s
sub timeout for fs init
2024-04-06 16:51:36 +03:00

71 lines
1.3 KiB
Go

package vfs
import (
"context"
"path"
"strings"
"sync"
"time"
)
const Separator = "/"
func isRoot(filename string) bool {
return path.Clean(filename) == Separator
}
func trimRelPath(p, t string) string {
return strings.Trim(strings.TrimPrefix(p, t), "/")
}
// func clean(p string) string {
// return path.Clean(Separator + strings.ReplaceAll(p, "\\", "/"))
// }
func AbsPath(p string) string {
if p == "" || p[0] != '/' {
return Separator + p
}
return p
}
func AddTrailSlash(p string) string {
if p == "" || p[len(p)-1] != '/' {
return p + Separator
}
return p
}
// OnceValueWOErr returns a function that invokes f only once and returns the value
// returned by f . The returned function may be called concurrently.
//
// If f panics, the returned function will panic with the same value on every call.
func OnceValueWOErr[T any](f func() (T, error)) func() (T, error) {
var (
mu sync.Mutex
isExecuted bool
r1 T
err error
)
return func() (T, error) {
mu.Lock()
defer mu.Unlock()
if isExecuted && err == nil {
return r1, nil
}
r1, err = f()
return r1, err
}
}
func subTimeout(ctx context.Context) (context.Context, context.CancelFunc) {
if deadline, ok := ctx.Deadline(); ok {
timeout := time.Until(deadline) / 2
return context.WithTimeout(ctx, timeout)
}
return ctx, func() {}
}