1
0

platform_common.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2022 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 fs
  7. import (
  8. "strconv"
  9. "sync"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. )
  13. // unixPlatformData is used on all platforms, because apart from being the
  14. // implementation for BasicFilesystem on Unixes it's also the implementation
  15. // in fakeFS.
  16. func unixPlatformData(fs Filesystem, name string, userCache *userCache, groupCache *groupCache, scanOwnership, scanXattrs bool, xattrFilter XattrFilter) (protocol.PlatformData, error) {
  17. var pd protocol.PlatformData
  18. if scanOwnership {
  19. var ud protocol.UnixData
  20. stat, err := fs.Lstat(name)
  21. if err != nil {
  22. return protocol.PlatformData{}, err
  23. }
  24. ud.UID = stat.Owner()
  25. if user := userCache.lookup(strconv.Itoa(ud.UID)); user != nil {
  26. ud.OwnerName = user.Username
  27. } else if ud.UID == 0 {
  28. // We couldn't look up a name, but UID zero should be "root". This
  29. // fixup works around the (unlikely) situation where the ownership
  30. // is 0:0 but we can't look up a name for either uid zero or gid
  31. // zero. If that were the case we'd return a zero PlatformData which
  32. // wouldn't get serialized over the wire and the other side would
  33. // assume a lack of ownership info...
  34. ud.OwnerName = "root"
  35. }
  36. ud.GID = stat.Group()
  37. if group := groupCache.lookup(strconv.Itoa(ud.GID)); group != nil {
  38. ud.GroupName = group.Name
  39. } else if ud.GID == 0 {
  40. ud.GroupName = "root"
  41. }
  42. pd.Unix = &ud
  43. }
  44. if scanXattrs {
  45. xattrs, err := fs.GetXattr(name, xattrFilter)
  46. if err != nil {
  47. return protocol.PlatformData{}, err
  48. }
  49. pd.SetXattrs(xattrs)
  50. }
  51. return pd, nil
  52. }
  53. type valueCache[K comparable, V any] struct {
  54. validity time.Duration
  55. fill func(K) (V, error)
  56. mut sync.Mutex
  57. cache map[K]cacheEntry[V]
  58. }
  59. type cacheEntry[V any] struct {
  60. value V
  61. when time.Time
  62. }
  63. func newValueCache[K comparable, V any](validity time.Duration, fill func(K) (V, error)) *valueCache[K, V] {
  64. return &valueCache[K, V]{
  65. validity: validity,
  66. fill: fill,
  67. cache: make(map[K]cacheEntry[V]),
  68. }
  69. }
  70. func (c *valueCache[K, V]) lookup(key K) V {
  71. c.mut.Lock()
  72. defer c.mut.Unlock()
  73. if e, ok := c.cache[key]; ok && time.Since(e.when) < c.validity {
  74. return e.value
  75. }
  76. var e cacheEntry[V]
  77. if val, err := c.fill(key); err == nil {
  78. e.value = val
  79. }
  80. e.when = time.Now()
  81. c.cache[key] = e
  82. return e.value
  83. }