lock_solaris.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2015 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 solaris
  15. package fileutil
  16. import (
  17. "os"
  18. "syscall"
  19. )
  20. func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
  21. var lock syscall.Flock_t
  22. lock.Start = 0
  23. lock.Len = 0
  24. lock.Pid = 0
  25. lock.Type = syscall.F_WRLCK
  26. lock.Whence = 0
  27. lock.Pid = 0
  28. f, err := os.OpenFile(path, flag, perm)
  29. if err != nil {
  30. return nil, err
  31. }
  32. if err := syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &lock); err != nil {
  33. f.Close()
  34. if err == syscall.EAGAIN {
  35. err = ErrLocked
  36. }
  37. return nil, err
  38. }
  39. return &LockedFile{f}, nil
  40. }
  41. func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
  42. var lock syscall.Flock_t
  43. lock.Start = 0
  44. lock.Len = 0
  45. lock.Pid = 0
  46. lock.Type = syscall.F_WRLCK
  47. lock.Whence = 0
  48. f, err := os.OpenFile(path, flag, perm)
  49. if err != nil {
  50. return nil, err
  51. }
  52. if err = syscall.FcntlFlock(f.Fd(), syscall.F_SETLKW, &lock); err != nil {
  53. f.Close()
  54. return nil, err
  55. }
  56. return &LockedFile{f}, nil
  57. }