copy_windows.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package fs
  14. import (
  15. "errors"
  16. "fmt"
  17. "os"
  18. winio "github.com/Microsoft/go-winio"
  19. "golang.org/x/sys/windows"
  20. )
  21. const (
  22. seTakeOwnershipPrivilege = "SeTakeOwnershipPrivilege"
  23. )
  24. func copyFileInfo(fi os.FileInfo, src, name string) error {
  25. if err := os.Chmod(name, fi.Mode()); err != nil {
  26. return fmt.Errorf("failed to chmod %s: %w", name, err)
  27. }
  28. // Copy file ownership and ACL
  29. // We need SeRestorePrivilege and SeTakeOwnershipPrivilege in order
  30. // to restore security info on a file, especially if we're trying to
  31. // apply security info which includes SIDs not necessarily present on
  32. // the host.
  33. privileges := []string{winio.SeRestorePrivilege, seTakeOwnershipPrivilege}
  34. if err := winio.EnableProcessPrivileges(privileges); err != nil {
  35. return err
  36. }
  37. defer winio.DisableProcessPrivileges(privileges)
  38. secInfo, err := windows.GetNamedSecurityInfo(
  39. src, windows.SE_FILE_OBJECT,
  40. windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION)
  41. if err != nil {
  42. return err
  43. }
  44. dacl, _, err := secInfo.DACL()
  45. if err != nil {
  46. return err
  47. }
  48. sid, _, err := secInfo.Owner()
  49. if err != nil {
  50. return err
  51. }
  52. if err := windows.SetNamedSecurityInfo(
  53. name, windows.SE_FILE_OBJECT,
  54. windows.OWNER_SECURITY_INFORMATION|windows.DACL_SECURITY_INFORMATION,
  55. sid, nil, dacl, nil); err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. func copyXAttrs(dst, src string, excludes map[string]struct{}, errorHandler XAttrErrorHandler) error {
  61. return nil
  62. }
  63. func copyIrregular(dst string, fi os.FileInfo) error {
  64. return errors.New("irregular copy not supported")
  65. }