lock_windows.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2015 CoreOS, Inc.
  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. "errors"
  18. "os"
  19. )
  20. var (
  21. ErrLocked = errors.New("file already locked")
  22. )
  23. type Lock interface {
  24. Name() string
  25. TryLock() error
  26. Lock() error
  27. Unlock() error
  28. Destroy() error
  29. }
  30. type lock struct {
  31. fd int
  32. file *os.File
  33. }
  34. func (l *lock) Name() string {
  35. return l.file.Name()
  36. }
  37. // TryLock acquires exclusivity on the lock without blocking
  38. func (l *lock) TryLock() error {
  39. return nil
  40. }
  41. // Lock acquires exclusivity on the lock without blocking
  42. func (l *lock) Lock() error {
  43. return nil
  44. }
  45. // Unlock unlocks the lock
  46. func (l *lock) Unlock() error {
  47. return nil
  48. }
  49. func (l *lock) Destroy() error {
  50. return l.file.Close()
  51. }
  52. func NewLock(file string) (Lock, error) {
  53. f, err := os.Open(file)
  54. if err != nil {
  55. return nil, err
  56. }
  57. l := &lock{int(f.Fd()), f}
  58. return l, nil
  59. }