utils.go 9.5 KB

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