From 22a8bbd3b15a75a72b7a0c20c129e51fa8cf3c8a Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Wed, 9 Mar 2016 09:19:56 -0800 Subject: [PATCH] pkg/fileutil: clean up interface, comments --- pkg/fileutil/lock.go | 29 +++++++++++++++++++++++++++++ pkg/fileutil/lock_plan9.go | 11 ----------- pkg/fileutil/lock_solaris.go | 11 ----------- pkg/fileutil/lock_unix.go | 11 ----------- pkg/fileutil/lock_windows.go | 11 ----------- 5 files changed, 29 insertions(+), 44 deletions(-) create mode 100644 pkg/fileutil/lock.go diff --git a/pkg/fileutil/lock.go b/pkg/fileutil/lock.go new file mode 100644 index 000000000..bf411d3a1 --- /dev/null +++ b/pkg/fileutil/lock.go @@ -0,0 +1,29 @@ +// Copyright 2016 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package fileutil + +type Lock interface { + // Name returns the name of the file. + Name() string + // TryLock acquires exclusivity on the lock without blocking. + TryLock() error + // Lock acquires exclusivity on the lock. + Lock() error + // Unlock unlocks the lock. + Unlock() error + // Destroy should be called after Unlock to clean up + // the resources. + Destroy() error +} diff --git a/pkg/fileutil/lock_plan9.go b/pkg/fileutil/lock_plan9.go index 311288de0..bd2bc8676 100644 --- a/pkg/fileutil/lock_plan9.go +++ b/pkg/fileutil/lock_plan9.go @@ -25,14 +25,6 @@ var ( ErrLocked = errors.New("file already locked") ) -type Lock interface { - Name() string - TryLock() error - Lock() error - Unlock() error - Destroy() error -} - type lock struct { fname string file *os.File @@ -42,7 +34,6 @@ func (l *lock) Name() string { return l.fname } -// TryLock acquires exclusivity on the lock without blocking func (l *lock) TryLock() error { err := os.Chmod(l.fname, syscall.DMEXCL|0600) if err != nil { @@ -58,7 +49,6 @@ func (l *lock) TryLock() error { return nil } -// Lock acquires exclusivity on the lock with blocking func (l *lock) Lock() error { err := os.Chmod(l.fname, syscall.DMEXCL|0600) if err != nil { @@ -75,7 +65,6 @@ func (l *lock) Lock() error { } } -// Unlock unlocks the lock func (l *lock) Unlock() error { return l.file.Close() } diff --git a/pkg/fileutil/lock_solaris.go b/pkg/fileutil/lock_solaris.go index 1929bd1fc..e3b0a0176 100644 --- a/pkg/fileutil/lock_solaris.go +++ b/pkg/fileutil/lock_solaris.go @@ -26,14 +26,6 @@ var ( ErrLocked = errors.New("file already locked") ) -type Lock interface { - Name() string - TryLock() error - Lock() error - Unlock() error - Destroy() error -} - type lock struct { fd int file *os.File @@ -43,7 +35,6 @@ func (l *lock) Name() string { return l.file.Name() } -// TryLock acquires exclusivity on the lock without blocking func (l *lock) TryLock() error { var lock syscall.Flock_t lock.Start = 0 @@ -59,7 +50,6 @@ func (l *lock) TryLock() error { return err } -// Lock acquires exclusivity on the lock without blocking func (l *lock) Lock() error { var lock syscall.Flock_t lock.Start = 0 @@ -70,7 +60,6 @@ func (l *lock) Lock() error { return syscall.FcntlFlock(uintptr(l.fd), syscall.F_SETLK, &lock) } -// Unlock unlocks the lock func (l *lock) Unlock() error { var lock syscall.Flock_t lock.Start = 0 diff --git a/pkg/fileutil/lock_unix.go b/pkg/fileutil/lock_unix.go index f6e69cc34..4f90e42ac 100644 --- a/pkg/fileutil/lock_unix.go +++ b/pkg/fileutil/lock_unix.go @@ -26,14 +26,6 @@ var ( ErrLocked = errors.New("file already locked") ) -type Lock interface { - Name() string - TryLock() error - Lock() error - Unlock() error - Destroy() error -} - type lock struct { fd int file *os.File @@ -43,7 +35,6 @@ func (l *lock) Name() string { return l.file.Name() } -// TryLock acquires exclusivity on the lock without blocking func (l *lock) TryLock() error { err := syscall.Flock(l.fd, syscall.LOCK_EX|syscall.LOCK_NB) if err != nil && err == syscall.EWOULDBLOCK { @@ -52,12 +43,10 @@ func (l *lock) TryLock() error { return err } -// Lock acquires exclusivity on the lock without blocking func (l *lock) Lock() error { return syscall.Flock(l.fd, syscall.LOCK_EX) } -// Unlock unlocks the lock func (l *lock) Unlock() error { return syscall.Flock(l.fd, syscall.LOCK_UN) } diff --git a/pkg/fileutil/lock_windows.go b/pkg/fileutil/lock_windows.go index a0a928b90..ddca9a669 100644 --- a/pkg/fileutil/lock_windows.go +++ b/pkg/fileutil/lock_windows.go @@ -25,14 +25,6 @@ var ( ErrLocked = errors.New("file already locked") ) -type Lock interface { - Name() string - TryLock() error - Lock() error - Unlock() error - Destroy() error -} - type lock struct { fd int file *os.File @@ -42,17 +34,14 @@ func (l *lock) Name() string { return l.file.Name() } -// TryLock acquires exclusivity on the lock without blocking func (l *lock) TryLock() error { return nil } -// Lock acquires exclusivity on the lock without blocking func (l *lock) Lock() error { return nil } -// Unlock unlocks the lock func (l *lock) Unlock() error { return nil }