basicfs_platformdata_windows.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. "fmt"
  9. "github.com/syncthing/syncthing/lib/protocol"
  10. "golang.org/x/sys/windows"
  11. )
  12. func (f *BasicFilesystem) PlatformData(name string, scanOwnership, _ bool, _ XattrFilter) (protocol.PlatformData, error) {
  13. if !scanOwnership {
  14. // That's the only thing we do, currently
  15. return protocol.PlatformData{}, nil
  16. }
  17. rootedName, err := f.rooted(name)
  18. if err != nil {
  19. return protocol.PlatformData{}, fmt.Errorf("rooted for %s: %w", name, err)
  20. }
  21. hdl, err := openReadOnlyWithBackupSemantics(rootedName)
  22. if err != nil {
  23. return protocol.PlatformData{}, fmt.Errorf("open %s: %w", rootedName, err)
  24. }
  25. defer windows.Close(hdl)
  26. // GetSecurityInfo returns an owner SID.
  27. sd, err := windows.GetSecurityInfo(hdl, windows.SE_FILE_OBJECT, windows.OWNER_SECURITY_INFORMATION)
  28. if err != nil {
  29. return protocol.PlatformData{}, fmt.Errorf("get security info for %s: %w", rootedName, err)
  30. }
  31. owner, _, err := sd.Owner()
  32. if err != nil {
  33. return protocol.PlatformData{}, fmt.Errorf("get owner for %s: %w", rootedName, err)
  34. }
  35. pd := &protocol.WindowsData{}
  36. if us := f.userCache.lookup(owner.String()); us != nil {
  37. pd.OwnerName = us.Username
  38. } else if gr := f.groupCache.lookup(owner.String()); gr != nil {
  39. pd.OwnerName = gr.Name
  40. pd.OwnerIsGroup = true
  41. } else {
  42. l.Debugf("Failed to resolve owner for %s: %v", rootedName, err)
  43. }
  44. return protocol.PlatformData{Windows: pd}, nil
  45. }
  46. func openReadOnlyWithBackupSemantics(path string) (fd windows.Handle, err error) {
  47. // This is windows.Open but simplified to read-only only, and adding
  48. // FILE_FLAG_BACKUP_SEMANTICS which is required to open directories.
  49. if len(path) == 0 {
  50. return windows.InvalidHandle, windows.ERROR_FILE_NOT_FOUND
  51. }
  52. pathp, err := windows.UTF16PtrFromString(path)
  53. if err != nil {
  54. return windows.InvalidHandle, err
  55. }
  56. var access uint32 = windows.GENERIC_READ
  57. var sharemode uint32 = windows.FILE_SHARE_READ | windows.FILE_SHARE_WRITE
  58. var sa *windows.SecurityAttributes
  59. var createmode uint32 = windows.OPEN_EXISTING
  60. var attrs uint32 = windows.FILE_ATTRIBUTE_READONLY | windows.FILE_FLAG_BACKUP_SEMANTICS
  61. return windows.CreateFile(pathp, access, sharemode, sa, createmode, attrs, 0)
  62. }