lock_windows.go 823 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // +build windows
  2. package fileutil
  3. import (
  4. "errors"
  5. "os"
  6. )
  7. var (
  8. ErrLocked = errors.New("file already locked")
  9. )
  10. type Lock interface {
  11. Name() string
  12. TryLock() error
  13. Lock() error
  14. Unlock() error
  15. Destroy() error
  16. }
  17. type lock struct {
  18. fd int
  19. file *os.File
  20. }
  21. func (l *lock) Name() string {
  22. return l.file.Name()
  23. }
  24. // TryLock acquires exclusivity on the lock without blocking
  25. func (l *lock) TryLock() error {
  26. return nil
  27. }
  28. // Lock acquires exclusivity on the lock without blocking
  29. func (l *lock) Lock() error {
  30. return nil
  31. }
  32. // Unlock unlocks the lock
  33. func (l *lock) Unlock() error {
  34. return nil
  35. }
  36. func (l *lock) Destroy() error {
  37. return l.file.Close()
  38. }
  39. func NewLock(file string) (Lock, error) {
  40. f, err := os.Open(file)
  41. if err != nil {
  42. return nil, err
  43. }
  44. l := &lock{int(f.Fd()), f}
  45. return l, nil
  46. }