basicfs_platformdata_windows.go 2.4 KB

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