本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1018B

  1. // +build windows
  2. package osfs
  3. import (
  4. "os"
  5. "runtime"
  6. "unsafe"
  7. "golang.org/x/sys/windows"
  8. )
  9. type fileInfo struct {
  10. os.FileInfo
  11. name string
  12. }
  13. func (fi *fileInfo) Name() string {
  14. return fi.name
  15. }
  16. var (
  17. kernel32DLL = windows.NewLazySystemDLL("kernel32.dll")
  18. lockFileExProc = kernel32DLL.NewProc("LockFileEx")
  19. unlockFileProc = kernel32DLL.NewProc("UnlockFile")
  20. )
  21. const (
  22. lockfileExclusiveLock = 0x2
  23. )
  24. func (f *file) Lock() error {
  25. f.m.Lock()
  26. defer f.m.Unlock()
  27. var overlapped windows.Overlapped
  28. // err is always non-nil as per sys/windows semantics.
  29. ret, _, err := lockFileExProc.Call(f.File.Fd(), lockfileExclusiveLock, 0, 0xFFFFFFFF, 0,
  30. uintptr(unsafe.Pointer(&overlapped)))
  31. runtime.KeepAlive(&overlapped)
  32. if ret == 0 {
  33. return err
  34. }
  35. return nil
  36. }
  37. func (f *file) Unlock() error {
  38. f.m.Lock()
  39. defer f.m.Unlock()
  40. // err is always non-nil as per sys/windows semantics.
  41. ret, _, err := unlockFileProc.Call(f.File.Fd(), 0, 0, 0xFFFFFFFF, 0)
  42. if ret == 0 {
  43. return err
  44. }
  45. return nil
  46. }
上海开阖软件有限公司 沪ICP备12045867号-1