1 // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
2 // All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
16 type unixFileLock struct {
20 func (fl *unixFileLock) release() error {
21 if err := setFileLock(fl.f, false, false); err != nil {
27 func newFileLock(path string, readOnly bool) (fl fileLock, err error) {
34 f, err := os.OpenFile(path, flag, 0)
35 if os.IsNotExist(err) {
36 f, err = os.OpenFile(path, flag|os.O_CREATE, 0644)
41 err = setFileLock(f, readOnly, true)
46 fl = &unixFileLock{f: f}
50 func setFileLock(f *os.File, readOnly, lock bool) error {
51 flock := syscall.Flock_t{
52 Type: syscall.F_UNLCK,
59 flock.Type = syscall.F_RDLCK
61 flock.Type = syscall.F_WRLCK
64 return syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &flock)
67 func rename(oldpath, newpath string) error {
68 return os.Rename(oldpath, newpath)
71 func syncDir(name string) error {
72 f, err := os.Open(name)
77 if err := f.Sync(); err != nil {