utils.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package util
  7. import (
  8. "context"
  9. "fmt"
  10. "net"
  11. "net/url"
  12. "reflect"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/syncthing/syncthing/lib/sync"
  17. "github.com/thejerf/suture/v4"
  18. )
  19. type defaultParser interface {
  20. ParseDefault(string) error
  21. }
  22. // SetDefaults sets default values on a struct, based on the default annotation.
  23. func SetDefaults(data interface{}) {
  24. s := reflect.ValueOf(data).Elem()
  25. t := s.Type()
  26. for i := 0; i < s.NumField(); i++ {
  27. f := s.Field(i)
  28. tag := t.Field(i).Tag
  29. v := tag.Get("default")
  30. if len(v) > 0 {
  31. if f.CanInterface() {
  32. if parser, ok := f.Interface().(defaultParser); ok {
  33. if err := parser.ParseDefault(v); err != nil {
  34. panic(err)
  35. }
  36. continue
  37. }
  38. }
  39. if f.CanAddr() && f.Addr().CanInterface() {
  40. if parser, ok := f.Addr().Interface().(defaultParser); ok {
  41. if err := parser.ParseDefault(v); err != nil {
  42. panic(err)
  43. }
  44. continue
  45. }
  46. }
  47. switch f.Interface().(type) {
  48. case string:
  49. f.SetString(v)
  50. case int, uint32, int32, int64, uint64:
  51. i, err := strconv.ParseInt(v, 10, 64)
  52. if err != nil {
  53. panic(err)
  54. }
  55. f.SetInt(i)
  56. case float64, float32:
  57. i, err := strconv.ParseFloat(v, 64)
  58. if err != nil {
  59. panic(err)
  60. }
  61. f.SetFloat(i)
  62. case bool:
  63. f.SetBool(v == "true")
  64. case []string:
  65. // We don't do anything with string slices here. Any default
  66. // we set will be appended to by the XML decoder, so we fill
  67. // those after decoding.
  68. default:
  69. panic(f.Type())
  70. }
  71. }
  72. }
  73. }
  74. // CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
  75. func CopyMatchingTag(from interface{}, to interface{}, tag string, shouldCopy func(value string) bool) {
  76. fromStruct := reflect.ValueOf(from).Elem()
  77. fromType := fromStruct.Type()
  78. toStruct := reflect.ValueOf(to).Elem()
  79. toType := toStruct.Type()
  80. if fromType != toType {
  81. panic(fmt.Sprintf("non equal types: %s != %s", fromType, toType))
  82. }
  83. for i := 0; i < toStruct.NumField(); i++ {
  84. fromField := fromStruct.Field(i)
  85. toField := toStruct.Field(i)
  86. if !toField.CanSet() {
  87. // Unexported fields
  88. continue
  89. }
  90. structTag := toType.Field(i).Tag
  91. v := structTag.Get(tag)
  92. if shouldCopy(v) {
  93. toField.Set(fromField)
  94. }
  95. }
  96. }
  97. // UniqueTrimmedStrings returns a list on unique strings, trimming at the same time.
  98. func UniqueTrimmedStrings(ss []string) []string {
  99. // Trim all first
  100. for i, v := range ss {
  101. ss[i] = strings.Trim(v, " ")
  102. }
  103. var m = make(map[string]struct{}, len(ss))
  104. var us = make([]string, 0, len(ss))
  105. for _, v := range ss {
  106. if _, ok := m[v]; ok {
  107. continue
  108. }
  109. m[v] = struct{}{}
  110. us = append(us, v)
  111. }
  112. return us
  113. }
  114. func FillNil(data interface{}) {
  115. s := reflect.ValueOf(data).Elem()
  116. for i := 0; i < s.NumField(); i++ {
  117. f := s.Field(i)
  118. for f.Kind() == reflect.Ptr && f.IsZero() && f.CanSet() {
  119. newValue := reflect.New(f.Type().Elem())
  120. f.Set(newValue)
  121. f = f.Elem()
  122. }
  123. if f.CanSet() {
  124. if f.IsZero() {
  125. switch f.Kind() {
  126. case reflect.Map:
  127. f.Set(reflect.MakeMap(f.Type()))
  128. case reflect.Slice:
  129. f.Set(reflect.MakeSlice(f.Type(), 0, 0))
  130. case reflect.Chan:
  131. f.Set(reflect.MakeChan(f.Type(), 0))
  132. }
  133. }
  134. if f.Kind() == reflect.Struct && f.CanAddr() {
  135. if addr := f.Addr(); addr.CanInterface() {
  136. FillNil(addr.Interface())
  137. }
  138. }
  139. }
  140. }
  141. }
  142. // FillNilSlices sets default value on slices that are still nil.
  143. func FillNilSlices(data interface{}) error {
  144. s := reflect.ValueOf(data).Elem()
  145. t := s.Type()
  146. for i := 0; i < s.NumField(); i++ {
  147. f := s.Field(i)
  148. tag := t.Field(i).Tag
  149. v := tag.Get("default")
  150. if len(v) > 0 {
  151. switch f.Interface().(type) {
  152. case []string:
  153. if f.IsNil() {
  154. // Treat the default as a comma separated slice
  155. vs := strings.Split(v, ",")
  156. for i := range vs {
  157. vs[i] = strings.TrimSpace(vs[i])
  158. }
  159. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
  160. for i, v := range vs {
  161. rv.Index(i).SetString(v)
  162. }
  163. f.Set(rv)
  164. }
  165. }
  166. }
  167. }
  168. return nil
  169. }
  170. // Address constructs a URL from the given network and hostname.
  171. func Address(network, host string) string {
  172. u := url.URL{
  173. Scheme: network,
  174. Host: host,
  175. }
  176. return u.String()
  177. }
  178. // AddressUnspecifiedLess is a comparator function preferring least specific network address (most widely listening,
  179. // namely preferring 0.0.0.0 over some IP), if both IPs are equal, it prefers the less restrictive network (prefers tcp
  180. // over tcp4)
  181. func AddressUnspecifiedLess(a, b net.Addr) bool {
  182. aIsUnspecified := false
  183. bIsUnspecified := false
  184. if host, _, err := net.SplitHostPort(a.String()); err == nil {
  185. aIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  186. }
  187. if host, _, err := net.SplitHostPort(b.String()); err == nil {
  188. bIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  189. }
  190. if aIsUnspecified == bIsUnspecified {
  191. return len(a.Network()) < len(b.Network())
  192. }
  193. return aIsUnspecified
  194. }
  195. type FatalErr struct {
  196. Err error
  197. Status ExitStatus
  198. }
  199. func (e *FatalErr) Error() string {
  200. return e.Err.Error()
  201. }
  202. func (e *FatalErr) Unwrap() error {
  203. return e.Err
  204. }
  205. func (e *FatalErr) Is(target error) bool {
  206. return target == suture.ErrTerminateSupervisorTree
  207. }
  208. type ExitStatus int
  209. const (
  210. ExitSuccess ExitStatus = 0
  211. ExitError ExitStatus = 1
  212. ExitNoUpgradeAvailable ExitStatus = 2
  213. ExitRestart ExitStatus = 3
  214. ExitUpgrade ExitStatus = 4
  215. )
  216. func (s ExitStatus) AsInt() int {
  217. return int(s)
  218. }
  219. type ServiceWithError interface {
  220. suture.Service
  221. fmt.Stringer
  222. Error() error
  223. SetError(error)
  224. }
  225. // AsService wraps the given function to implement suture.Service. In addition
  226. // it keeps track of the returned error and allows querying and setting that error.
  227. func AsService(fn func(ctx context.Context) error, creator string) ServiceWithError {
  228. return &service{
  229. creator: creator,
  230. serve: fn,
  231. mut: sync.NewMutex(),
  232. }
  233. }
  234. type service struct {
  235. creator string
  236. serve func(ctx context.Context) error
  237. err error
  238. mut sync.Mutex
  239. }
  240. func (s *service) Serve(ctx context.Context) error {
  241. s.mut.Lock()
  242. s.err = nil
  243. s.mut.Unlock()
  244. err := s.serve(ctx)
  245. s.mut.Lock()
  246. s.err = err
  247. s.mut.Unlock()
  248. return err
  249. }
  250. func (s *service) Error() error {
  251. s.mut.Lock()
  252. defer s.mut.Unlock()
  253. return s.err
  254. }
  255. func (s *service) SetError(err error) {
  256. s.mut.Lock()
  257. s.err = err
  258. s.mut.Unlock()
  259. }
  260. func (s *service) String() string {
  261. return fmt.Sprintf("Service@%p created by %v", s, s.creator)
  262. }
  263. // OnDone calls fn when ctx is cancelled.
  264. func OnDone(ctx context.Context, fn func()) {
  265. go func() {
  266. <-ctx.Done()
  267. fn()
  268. }()
  269. }
  270. type doneService struct {
  271. fn func()
  272. }
  273. func (s *doneService) Serve(ctx context.Context) error {
  274. <-ctx.Done()
  275. s.fn()
  276. return nil
  277. }
  278. // OnSupervisorDone calls fn when sup is done.
  279. func OnSupervisorDone(sup *suture.Supervisor, fn func()) {
  280. sup.Add(&doneService{fn})
  281. }
  282. func Spec() suture.Spec {
  283. return suture.Spec{
  284. PassThroughPanics: true,
  285. DontPropagateTermination: false,
  286. }
  287. }
  288. func CallWithContext(ctx context.Context, fn func() error) error {
  289. var err error
  290. done := make(chan struct{})
  291. go func() {
  292. err = fn()
  293. close(done)
  294. }()
  295. select {
  296. case <-done:
  297. return err
  298. case <-ctx.Done():
  299. return ctx.Err()
  300. }
  301. }
  302. func NiceDurationString(d time.Duration) string {
  303. switch {
  304. case d > 24*time.Hour:
  305. d = d.Round(time.Hour)
  306. case d > time.Hour:
  307. d = d.Round(time.Minute)
  308. case d > time.Minute:
  309. d = d.Round(time.Second)
  310. case d > time.Second:
  311. d = d.Round(time.Millisecond)
  312. case d > time.Millisecond:
  313. d = d.Round(time.Microsecond)
  314. }
  315. return d.String()
  316. }