dir_windows.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2016 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // +build windows
  15. package fileutil
  16. import (
  17. "os"
  18. "syscall"
  19. )
  20. // OpenDir opens a directory in windows with write access for syncing.
  21. func OpenDir(path string) (*os.File, error) {
  22. fd, err := openDir(path)
  23. if err != nil {
  24. return nil, err
  25. }
  26. return os.NewFile(uintptr(fd), path), nil
  27. }
  28. func openDir(path string) (fd syscall.Handle, err error) {
  29. if len(path) == 0 {
  30. return syscall.InvalidHandle, syscall.ERROR_FILE_NOT_FOUND
  31. }
  32. pathp, err := syscall.UTF16PtrFromString(path)
  33. if err != nil {
  34. return syscall.InvalidHandle, err
  35. }
  36. access := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE)
  37. sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE)
  38. createmode := uint32(syscall.OPEN_EXISTING)
  39. fl := uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS)
  40. return syscall.CreateFile(pathp, access, sharemode, nil, createmode, fl, 0)
  41. }