2016-03-25 20:22:29 +00:00
|
|
|
// Copyright (C) 2016 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-03-25 20:22:29 +00:00
|
|
|
|
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2019-11-21 07:41:15 +00:00
|
|
|
"context"
|
2017-12-07 07:08:24 +00:00
|
|
|
"fmt"
|
2020-06-16 07:17:07 +00:00
|
|
|
"net"
|
2016-03-25 20:22:29 +00:00
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-06-18 08:55:41 +00:00
|
|
|
"time"
|
2019-07-09 09:40:30 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
"github.com/thejerf/suture/v4"
|
2016-03-25 20:22:29 +00:00
|
|
|
)
|
|
|
|
|
2019-02-12 06:58:24 +00:00
|
|
|
type defaultParser interface {
|
|
|
|
ParseDefault(string) error
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:22:29 +00:00
|
|
|
// SetDefaults sets default values on a struct, based on the default annotation.
|
2019-02-12 06:58:24 +00:00
|
|
|
func SetDefaults(data interface{}) {
|
2016-03-25 20:22:29 +00:00
|
|
|
s := reflect.ValueOf(data).Elem()
|
|
|
|
t := s.Type()
|
|
|
|
|
|
|
|
for i := 0; i < s.NumField(); i++ {
|
|
|
|
f := s.Field(i)
|
|
|
|
tag := t.Field(i).Tag
|
|
|
|
|
|
|
|
v := tag.Get("default")
|
|
|
|
if len(v) > 0 {
|
2019-02-12 06:58:24 +00:00
|
|
|
if f.CanInterface() {
|
|
|
|
if parser, ok := f.Interface().(defaultParser); ok {
|
|
|
|
if err := parser.ParseDefault(v); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.CanAddr() && f.Addr().CanInterface() {
|
|
|
|
if parser, ok := f.Addr().Interface().(defaultParser); ok {
|
|
|
|
if err := parser.ParseDefault(v); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
continue
|
2017-02-06 10:27:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:22:29 +00:00
|
|
|
switch f.Interface().(type) {
|
|
|
|
case string:
|
|
|
|
f.SetString(v)
|
|
|
|
|
2020-08-25 06:11:14 +00:00
|
|
|
case int, uint32, int32, int64, uint64:
|
2016-03-25 20:22:29 +00:00
|
|
|
i, err := strconv.ParseInt(v, 10, 64)
|
|
|
|
if err != nil {
|
2019-02-12 06:58:24 +00:00
|
|
|
panic(err)
|
2016-03-25 20:22:29 +00:00
|
|
|
}
|
|
|
|
f.SetInt(i)
|
|
|
|
|
2020-08-25 06:11:14 +00:00
|
|
|
case float64, float32:
|
2016-03-25 20:22:29 +00:00
|
|
|
i, err := strconv.ParseFloat(v, 64)
|
|
|
|
if err != nil {
|
2019-02-12 06:58:24 +00:00
|
|
|
panic(err)
|
2016-03-25 20:22:29 +00:00
|
|
|
}
|
|
|
|
f.SetFloat(i)
|
|
|
|
|
|
|
|
case bool:
|
|
|
|
f.SetBool(v == "true")
|
|
|
|
|
|
|
|
case []string:
|
|
|
|
// We don't do anything with string slices here. Any default
|
|
|
|
// we set will be appended to by the XML decoder, so we fill
|
|
|
|
// those after decoding.
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic(f.Type())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-07 07:08:24 +00:00
|
|
|
// CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
|
|
|
|
func CopyMatchingTag(from interface{}, to interface{}, tag string, shouldCopy func(value string) bool) {
|
|
|
|
fromStruct := reflect.ValueOf(from).Elem()
|
|
|
|
fromType := fromStruct.Type()
|
|
|
|
|
|
|
|
toStruct := reflect.ValueOf(to).Elem()
|
|
|
|
toType := toStruct.Type()
|
|
|
|
|
|
|
|
if fromType != toType {
|
|
|
|
panic(fmt.Sprintf("non equal types: %s != %s", fromType, toType))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < toStruct.NumField(); i++ {
|
|
|
|
fromField := fromStruct.Field(i)
|
|
|
|
toField := toStruct.Field(i)
|
|
|
|
|
2017-12-07 08:33:32 +00:00
|
|
|
if !toField.CanSet() {
|
|
|
|
// Unexported fields
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-12-07 07:08:24 +00:00
|
|
|
structTag := toType.Field(i).Tag
|
|
|
|
|
|
|
|
v := structTag.Get(tag)
|
|
|
|
if shouldCopy(v) {
|
|
|
|
toField.Set(fromField)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-29 07:56:40 +00:00
|
|
|
// UniqueTrimmedStrings returns a list on unique strings, trimming at the same time.
|
|
|
|
func UniqueTrimmedStrings(ss []string) []string {
|
|
|
|
// Trim all first
|
|
|
|
for i, v := range ss {
|
|
|
|
ss[i] = strings.Trim(v, " ")
|
2016-03-25 20:22:29 +00:00
|
|
|
}
|
|
|
|
|
2019-05-29 07:56:40 +00:00
|
|
|
var m = make(map[string]struct{}, len(ss))
|
|
|
|
var us = make([]string, 0, len(ss))
|
|
|
|
for _, v := range ss {
|
|
|
|
if _, ok := m[v]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m[v] = struct{}{}
|
|
|
|
us = append(us, v)
|
2016-03-25 20:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return us
|
|
|
|
}
|
|
|
|
|
2020-06-28 18:35:22 +00:00
|
|
|
func FillNil(data interface{}) {
|
|
|
|
s := reflect.ValueOf(data).Elem()
|
|
|
|
for i := 0; i < s.NumField(); i++ {
|
|
|
|
f := s.Field(i)
|
|
|
|
|
|
|
|
for f.Kind() == reflect.Ptr && f.IsZero() && f.CanSet() {
|
|
|
|
newValue := reflect.New(f.Type().Elem())
|
|
|
|
f.Set(newValue)
|
|
|
|
f = f.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.CanSet() {
|
|
|
|
if f.IsZero() {
|
|
|
|
switch f.Kind() {
|
|
|
|
case reflect.Map:
|
|
|
|
f.Set(reflect.MakeMap(f.Type()))
|
|
|
|
case reflect.Slice:
|
|
|
|
f.Set(reflect.MakeSlice(f.Type(), 0, 0))
|
|
|
|
case reflect.Chan:
|
|
|
|
f.Set(reflect.MakeChan(f.Type(), 0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Kind() == reflect.Struct && f.CanAddr() {
|
|
|
|
if addr := f.Addr(); addr.CanInterface() {
|
|
|
|
FillNil(addr.Interface())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-25 20:22:29 +00:00
|
|
|
// FillNilSlices sets default value on slices that are still nil.
|
|
|
|
func FillNilSlices(data interface{}) error {
|
|
|
|
s := reflect.ValueOf(data).Elem()
|
|
|
|
t := s.Type()
|
|
|
|
|
|
|
|
for i := 0; i < s.NumField(); i++ {
|
|
|
|
f := s.Field(i)
|
|
|
|
tag := t.Field(i).Tag
|
|
|
|
|
|
|
|
v := tag.Get("default")
|
|
|
|
if len(v) > 0 {
|
|
|
|
switch f.Interface().(type) {
|
|
|
|
case []string:
|
|
|
|
if f.IsNil() {
|
|
|
|
// Treat the default as a comma separated slice
|
|
|
|
vs := strings.Split(v, ",")
|
|
|
|
for i := range vs {
|
|
|
|
vs[i] = strings.TrimSpace(vs[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
|
|
|
|
for i, v := range vs {
|
|
|
|
rv.Index(i).SetString(v)
|
|
|
|
}
|
|
|
|
f.Set(rv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address constructs a URL from the given network and hostname.
|
|
|
|
func Address(network, host string) string {
|
|
|
|
u := url.URL{
|
|
|
|
Scheme: network,
|
|
|
|
Host: host,
|
|
|
|
}
|
|
|
|
return u.String()
|
|
|
|
}
|
2019-07-09 09:40:30 +00:00
|
|
|
|
2020-06-16 07:17:07 +00:00
|
|
|
// AddressUnspecifiedLess is a comparator function preferring least specific network address (most widely listening,
|
|
|
|
// namely preferring 0.0.0.0 over some IP), if both IPs are equal, it prefers the less restrictive network (prefers tcp
|
|
|
|
// over tcp4)
|
|
|
|
func AddressUnspecifiedLess(a, b net.Addr) bool {
|
|
|
|
aIsUnspecified := false
|
|
|
|
bIsUnspecified := false
|
|
|
|
if host, _, err := net.SplitHostPort(a.String()); err == nil {
|
|
|
|
aIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
|
|
|
|
}
|
|
|
|
if host, _, err := net.SplitHostPort(b.String()); err == nil {
|
|
|
|
bIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
|
|
|
|
}
|
|
|
|
|
|
|
|
if aIsUnspecified == bIsUnspecified {
|
|
|
|
return len(a.Network()) < len(b.Network())
|
|
|
|
}
|
|
|
|
return aIsUnspecified
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
type FatalErr struct {
|
|
|
|
Err error
|
|
|
|
Status ExitStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *FatalErr) Error() string {
|
|
|
|
return e.Err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *FatalErr) Unwrap() error {
|
|
|
|
return e.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *FatalErr) Is(target error) bool {
|
|
|
|
return target == suture.ErrTerminateSupervisorTree
|
|
|
|
}
|
|
|
|
|
2020-11-18 12:43:57 +00:00
|
|
|
// NoRestartErr wraps the given error err (which may be nil) to make sure that
|
|
|
|
// `errors.Is(err, suture.ErrDoNotRestart) == true`.
|
|
|
|
func NoRestartErr(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return suture.ErrDoNotRestart
|
|
|
|
}
|
|
|
|
return &noRestartErr{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
type noRestartErr struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *noRestartErr) Error() string {
|
|
|
|
return e.err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *noRestartErr) Unwrap() error {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *noRestartErr) Is(target error) bool {
|
|
|
|
return target == suture.ErrDoNotRestart
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
type ExitStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ExitSuccess ExitStatus = 0
|
|
|
|
ExitError ExitStatus = 1
|
|
|
|
ExitNoUpgradeAvailable ExitStatus = 2
|
|
|
|
ExitRestart ExitStatus = 3
|
|
|
|
ExitUpgrade ExitStatus = 4
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s ExitStatus) AsInt() int {
|
|
|
|
return int(s)
|
2019-07-09 09:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ServiceWithError interface {
|
|
|
|
suture.Service
|
2019-10-16 07:06:16 +00:00
|
|
|
fmt.Stringer
|
2019-07-09 09:40:30 +00:00
|
|
|
Error() error
|
2019-07-19 17:40:40 +00:00
|
|
|
SetError(error)
|
2019-07-09 09:40:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
// AsService wraps the given function to implement suture.Service. In addition
|
|
|
|
// it keeps track of the returned error and allows querying and setting that error.
|
|
|
|
func AsService(fn func(ctx context.Context) error, creator string) ServiceWithError {
|
|
|
|
return &service{
|
2019-11-21 07:41:15 +00:00
|
|
|
creator: creator,
|
2020-11-17 12:19:04 +00:00
|
|
|
serve: fn,
|
2019-07-09 09:40:30 +00:00
|
|
|
mut: sync.NewMutex(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type service struct {
|
2019-11-21 07:41:15 +00:00
|
|
|
creator string
|
|
|
|
serve func(ctx context.Context) error
|
2019-07-09 09:40:30 +00:00
|
|
|
err error
|
|
|
|
mut sync.Mutex
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
func (s *service) Serve(ctx context.Context) error {
|
2019-07-09 09:40:30 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
s.err = nil
|
|
|
|
s.mut.Unlock()
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
err := s.serve(ctx)
|
2019-07-09 09:40:30 +00:00
|
|
|
|
|
|
|
s.mut.Lock()
|
2020-11-17 12:19:04 +00:00
|
|
|
s.err = err
|
2019-07-09 09:40:30 +00:00
|
|
|
s.mut.Unlock()
|
2020-11-17 12:19:04 +00:00
|
|
|
|
|
|
|
return err
|
2019-07-09 09:40:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Error() error {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
return s.err
|
|
|
|
}
|
2019-07-19 17:40:40 +00:00
|
|
|
|
|
|
|
func (s *service) SetError(err error) {
|
|
|
|
s.mut.Lock()
|
|
|
|
s.err = err
|
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
2019-10-16 07:06:16 +00:00
|
|
|
|
|
|
|
func (s *service) String() string {
|
2019-11-21 07:41:15 +00:00
|
|
|
return fmt.Sprintf("Service@%p created by %v", s, s.creator)
|
2020-11-17 12:19:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnDone calls fn when ctx is cancelled.
|
|
|
|
func OnDone(ctx context.Context, fn func()) {
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
fn()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
type doneService struct {
|
|
|
|
fn func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *doneService) Serve(ctx context.Context) error {
|
|
|
|
<-ctx.Done()
|
|
|
|
s.fn()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnSupervisorDone calls fn when sup is done.
|
|
|
|
func OnSupervisorDone(sup *suture.Supervisor, fn func()) {
|
|
|
|
sup.Add(&doneService{fn})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Spec() suture.Spec {
|
|
|
|
return suture.Spec{
|
|
|
|
PassThroughPanics: true,
|
|
|
|
DontPropagateTermination: false,
|
|
|
|
}
|
2019-10-16 07:06:16 +00:00
|
|
|
}
|
2020-02-24 20:57:15 +00:00
|
|
|
|
|
|
|
func CallWithContext(ctx context.Context, fn func() error) error {
|
|
|
|
var err error
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
err = fn()
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return err
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
2020-06-18 08:55:41 +00:00
|
|
|
|
|
|
|
func NiceDurationString(d time.Duration) string {
|
|
|
|
switch {
|
|
|
|
case d > 24*time.Hour:
|
|
|
|
d = d.Round(time.Hour)
|
|
|
|
case d > time.Hour:
|
|
|
|
d = d.Round(time.Minute)
|
|
|
|
case d > time.Minute:
|
|
|
|
d = d.Round(time.Second)
|
|
|
|
case d > time.Second:
|
|
|
|
d = d.Round(time.Millisecond)
|
|
|
|
case d > time.Millisecond:
|
|
|
|
d = d.Round(time.Microsecond)
|
|
|
|
}
|
|
|
|
return d.String()
|
|
|
|
}
|