folder_sendrecv_unix.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. //go:build !windows
  7. // +build !windows
  8. package model
  9. import (
  10. "os/user"
  11. "strconv"
  12. "github.com/syncthing/syncthing/lib/protocol"
  13. )
  14. func (f *sendReceiveFolder) syncOwnership(file *protocol.FileInfo, path string) error {
  15. if file.Platform.Unix == nil {
  16. // No owner data, nothing to do
  17. return nil
  18. }
  19. // Try to look up the user and group by name, defaulting to the
  20. // numerical UID and GID if there is no match.
  21. uid := strconv.Itoa(file.Platform.Unix.UID)
  22. if file.Platform.Unix.OwnerName != "" {
  23. us, err := user.Lookup(file.Platform.Unix.OwnerName)
  24. if err == nil && us.Uid != "" {
  25. uid = us.Uid
  26. }
  27. }
  28. gid := strconv.Itoa(file.Platform.Unix.GID)
  29. if file.Platform.Unix.GroupName != "" {
  30. gr, err := user.LookupGroup(file.Platform.Unix.GroupName)
  31. if err == nil && gr.Gid != "" {
  32. gid = gr.Gid
  33. }
  34. }
  35. return f.mtimefs.Lchown(path, uid, gid)
  36. }