utils.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. } else if f.CanSet() && f.Kind() == reflect.Struct && f.CanAddr() {
  72. if addr := f.Addr(); addr.CanInterface() {
  73. SetDefaults(addr.Interface())
  74. }
  75. }
  76. }
  77. }
  78. // CopyMatchingTag copies fields tagged tag:"value" from "from" struct onto "to" struct.
  79. func CopyMatchingTag(from interface{}, to interface{}, tag string, shouldCopy func(value string) bool) {
  80. fromStruct := reflect.ValueOf(from).Elem()
  81. fromType := fromStruct.Type()
  82. toStruct := reflect.ValueOf(to).Elem()
  83. toType := toStruct.Type()
  84. if fromType != toType {
  85. panic(fmt.Sprintf("non equal types: %s != %s", fromType, toType))
  86. }
  87. for i := 0; i < toStruct.NumField(); i++ {
  88. fromField := fromStruct.Field(i)
  89. toField := toStruct.Field(i)
  90. if !toField.CanSet() {
  91. // Unexported fields
  92. continue
  93. }
  94. structTag := toType.Field(i).Tag
  95. v := structTag.Get(tag)
  96. if shouldCopy(v) {
  97. toField.Set(fromField)
  98. }
  99. }
  100. }
  101. // UniqueTrimmedStrings returns a list on unique strings, trimming at the same time.
  102. func UniqueTrimmedStrings(ss []string) []string {
  103. // Trim all first
  104. for i, v := range ss {
  105. ss[i] = strings.Trim(v, " ")
  106. }
  107. var m = make(map[string]struct{}, len(ss))
  108. var us = make([]string, 0, len(ss))
  109. for _, v := range ss {
  110. if _, ok := m[v]; ok {
  111. continue
  112. }
  113. m[v] = struct{}{}
  114. us = append(us, v)
  115. }
  116. return us
  117. }
  118. func FillNilExceptDeprecated(data interface{}) {
  119. fillNil(data, true)
  120. }
  121. func FillNil(data interface{}) {
  122. fillNil(data, false)
  123. }
  124. func fillNil(data interface{}, skipDeprecated bool) {
  125. s := reflect.ValueOf(data).Elem()
  126. t := s.Type()
  127. for i := 0; i < s.NumField(); i++ {
  128. if skipDeprecated && strings.HasPrefix(t.Field(i).Name, "Deprecated") {
  129. continue
  130. }
  131. f := s.Field(i)
  132. for f.Kind() == reflect.Ptr && f.IsZero() && f.CanSet() {
  133. newValue := reflect.New(f.Type().Elem())
  134. f.Set(newValue)
  135. f = f.Elem()
  136. }
  137. if f.CanSet() {
  138. if f.IsZero() {
  139. switch f.Kind() {
  140. case reflect.Map:
  141. f.Set(reflect.MakeMap(f.Type()))
  142. case reflect.Slice:
  143. f.Set(reflect.MakeSlice(f.Type(), 0, 0))
  144. case reflect.Chan:
  145. f.Set(reflect.MakeChan(f.Type(), 0))
  146. }
  147. }
  148. switch f.Kind() {
  149. case reflect.Slice:
  150. if f.Type().Elem().Kind() != reflect.Struct {
  151. continue
  152. }
  153. for i := 0; i < f.Len(); i++ {
  154. fillNil(f.Index(i).Addr().Interface(), skipDeprecated)
  155. }
  156. case reflect.Struct:
  157. if f.CanAddr() {
  158. if addr := f.Addr(); addr.CanInterface() {
  159. fillNil(addr.Interface(), skipDeprecated)
  160. }
  161. }
  162. }
  163. }
  164. }
  165. }
  166. // FillNilSlices sets default value on slices that are still nil.
  167. func FillNilSlices(data interface{}) error {
  168. s := reflect.ValueOf(data).Elem()
  169. t := s.Type()
  170. for i := 0; i < s.NumField(); i++ {
  171. f := s.Field(i)
  172. tag := t.Field(i).Tag
  173. v := tag.Get("default")
  174. if len(v) > 0 {
  175. switch f.Interface().(type) {
  176. case []string:
  177. if f.IsNil() {
  178. // Treat the default as a comma separated slice
  179. vs := strings.Split(v, ",")
  180. for i := range vs {
  181. vs[i] = strings.TrimSpace(vs[i])
  182. }
  183. rv := reflect.MakeSlice(reflect.TypeOf([]string{}), len(vs), len(vs))
  184. for i, v := range vs {
  185. rv.Index(i).SetString(v)
  186. }
  187. f.Set(rv)
  188. }
  189. }
  190. }
  191. }
  192. return nil
  193. }
  194. // Address constructs a URL from the given network and hostname.
  195. func Address(network, host string) string {
  196. u := url.URL{
  197. Scheme: network,
  198. Host: host,
  199. }
  200. return u.String()
  201. }
  202. // AddressUnspecifiedLess is a comparator function preferring least specific network address (most widely listening,
  203. // namely preferring 0.0.0.0 over some IP), if both IPs are equal, it prefers the less restrictive network (prefers tcp
  204. // over tcp4)
  205. func AddressUnspecifiedLess(a, b net.Addr) bool {
  206. aIsUnspecified := false
  207. bIsUnspecified := false
  208. if host, _, err := net.SplitHostPort(a.String()); err == nil {
  209. aIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  210. }
  211. if host, _, err := net.SplitHostPort(b.String()); err == nil {
  212. bIsUnspecified = host == "" || net.ParseIP(host).IsUnspecified()
  213. }
  214. if aIsUnspecified == bIsUnspecified {
  215. return len(a.Network()) < len(b.Network())
  216. }
  217. return aIsUnspecified
  218. }
  219. type FatalErr struct {
  220. Err error
  221. Status ExitStatus
  222. }
  223. func (e *FatalErr) Error() string {
  224. return e.Err.Error()
  225. }
  226. func (e *FatalErr) Unwrap() error {
  227. return e.Err
  228. }
  229. func (e *FatalErr) Is(target error) bool {
  230. return target == suture.ErrTerminateSupervisorTree
  231. }
  232. // NoRestartErr wraps the given error err (which may be nil) to make sure that
  233. // `errors.Is(err, suture.ErrDoNotRestart) == true`.
  234. func NoRestartErr(err error) error {
  235. if err == nil {
  236. return suture.ErrDoNotRestart
  237. }
  238. return &noRestartErr{err}
  239. }
  240. type noRestartErr struct {
  241. err error
  242. }
  243. func (e *noRestartErr) Error() string {
  244. return e.err.Error()
  245. }
  246. func (e *noRestartErr) Unwrap() error {
  247. return e.err
  248. }
  249. func (e *noRestartErr) Is(target error) bool {
  250. return target == suture.ErrDoNotRestart
  251. }
  252. type ExitStatus int
  253. const (
  254. ExitSuccess ExitStatus = 0
  255. ExitError ExitStatus = 1
  256. ExitNoUpgradeAvailable ExitStatus = 2
  257. ExitRestart ExitStatus = 3
  258. ExitUpgrade ExitStatus = 4
  259. )
  260. func (s ExitStatus) AsInt() int {
  261. return int(s)
  262. }
  263. type ServiceWithError interface {
  264. suture.Service
  265. fmt.Stringer
  266. Error() error
  267. SetError(error)
  268. }
  269. // AsService wraps the given function to implement suture.Service. In addition
  270. // it keeps track of the returned error and allows querying and setting that error.
  271. func AsService(fn func(ctx context.Context) error, creator string) ServiceWithError {
  272. return &service{
  273. creator: creator,
  274. serve: fn,
  275. mut: sync.NewMutex(),
  276. }
  277. }
  278. type service struct {
  279. creator string
  280. serve func(ctx context.Context) error
  281. err error
  282. mut sync.Mutex
  283. }
  284. func (s *service) Serve(ctx context.Context) error {
  285. s.mut.Lock()
  286. s.err = nil
  287. s.mut.Unlock()
  288. err := s.serve(ctx)
  289. s.mut.Lock()
  290. s.err = err
  291. s.mut.Unlock()
  292. return err
  293. }
  294. func (s *service) Error() error {
  295. s.mut.Lock()
  296. defer s.mut.Unlock()
  297. return s.err
  298. }
  299. func (s *service) SetError(err error) {
  300. s.mut.Lock()
  301. s.err = err
  302. s.mut.Unlock()
  303. }
  304. func (s *service) String() string {
  305. return fmt.Sprintf("Service@%p created by %v", s, s.creator)
  306. }
  307. // OnDone calls fn when ctx is cancelled.
  308. func OnDone(ctx context.Context, fn func()) {
  309. go func() {
  310. <-ctx.Done()
  311. fn()
  312. }()
  313. }
  314. type doneService struct {
  315. fn func()
  316. }
  317. func (s *doneService) Serve(ctx context.Context) error {
  318. <-ctx.Done()
  319. s.fn()
  320. return nil
  321. }
  322. // OnSupervisorDone calls fn when sup is done.
  323. func OnSupervisorDone(sup *suture.Supervisor, fn func()) {
  324. sup.Add(&doneService{fn})
  325. }
  326. func Spec() suture.Spec {
  327. return suture.Spec{
  328. PassThroughPanics: true,
  329. DontPropagateTermination: false,
  330. }
  331. }
  332. func CallWithContext(ctx context.Context, fn func() error) error {
  333. var err error
  334. done := make(chan struct{})
  335. go func() {
  336. err = fn()
  337. close(done)
  338. }()
  339. select {
  340. case <-done:
  341. return err
  342. case <-ctx.Done():
  343. return ctx.Err()
  344. }
  345. }
  346. func NiceDurationString(d time.Duration) string {
  347. switch {
  348. case d > 24*time.Hour:
  349. d = d.Round(time.Hour)
  350. case d > time.Hour:
  351. d = d.Round(time.Minute)
  352. case d > time.Minute:
  353. d = d.Round(time.Second)
  354. case d > time.Second:
  355. d = d.Round(time.Millisecond)
  356. case d > time.Millisecond:
  357. d = d.Round(time.Microsecond)
  358. }
  359. return d.String()
  360. }