mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
pkg/fileutil: clean up interface, comments
This commit is contained in:
parent
a4624666fe
commit
22a8bbd3b1
29
pkg/fileutil/lock.go
Normal file
29
pkg/fileutil/lock.go
Normal file
@ -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
|
||||||
|
}
|
@ -25,14 +25,6 @@ var (
|
|||||||
ErrLocked = errors.New("file already locked")
|
ErrLocked = errors.New("file already locked")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Lock interface {
|
|
||||||
Name() string
|
|
||||||
TryLock() error
|
|
||||||
Lock() error
|
|
||||||
Unlock() error
|
|
||||||
Destroy() error
|
|
||||||
}
|
|
||||||
|
|
||||||
type lock struct {
|
type lock struct {
|
||||||
fname string
|
fname string
|
||||||
file *os.File
|
file *os.File
|
||||||
@ -42,7 +34,6 @@ func (l *lock) Name() string {
|
|||||||
return l.fname
|
return l.fname
|
||||||
}
|
}
|
||||||
|
|
||||||
// TryLock acquires exclusivity on the lock without blocking
|
|
||||||
func (l *lock) TryLock() error {
|
func (l *lock) TryLock() error {
|
||||||
err := os.Chmod(l.fname, syscall.DMEXCL|0600)
|
err := os.Chmod(l.fname, syscall.DMEXCL|0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -58,7 +49,6 @@ func (l *lock) TryLock() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock acquires exclusivity on the lock with blocking
|
|
||||||
func (l *lock) Lock() error {
|
func (l *lock) Lock() error {
|
||||||
err := os.Chmod(l.fname, syscall.DMEXCL|0600)
|
err := os.Chmod(l.fname, syscall.DMEXCL|0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -75,7 +65,6 @@ func (l *lock) Lock() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlock unlocks the lock
|
|
||||||
func (l *lock) Unlock() error {
|
func (l *lock) Unlock() error {
|
||||||
return l.file.Close()
|
return l.file.Close()
|
||||||
}
|
}
|
||||||
|
@ -26,14 +26,6 @@ var (
|
|||||||
ErrLocked = errors.New("file already locked")
|
ErrLocked = errors.New("file already locked")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Lock interface {
|
|
||||||
Name() string
|
|
||||||
TryLock() error
|
|
||||||
Lock() error
|
|
||||||
Unlock() error
|
|
||||||
Destroy() error
|
|
||||||
}
|
|
||||||
|
|
||||||
type lock struct {
|
type lock struct {
|
||||||
fd int
|
fd int
|
||||||
file *os.File
|
file *os.File
|
||||||
@ -43,7 +35,6 @@ func (l *lock) Name() string {
|
|||||||
return l.file.Name()
|
return l.file.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TryLock acquires exclusivity on the lock without blocking
|
|
||||||
func (l *lock) TryLock() error {
|
func (l *lock) TryLock() error {
|
||||||
var lock syscall.Flock_t
|
var lock syscall.Flock_t
|
||||||
lock.Start = 0
|
lock.Start = 0
|
||||||
@ -59,7 +50,6 @@ func (l *lock) TryLock() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock acquires exclusivity on the lock without blocking
|
|
||||||
func (l *lock) Lock() error {
|
func (l *lock) Lock() error {
|
||||||
var lock syscall.Flock_t
|
var lock syscall.Flock_t
|
||||||
lock.Start = 0
|
lock.Start = 0
|
||||||
@ -70,7 +60,6 @@ func (l *lock) Lock() error {
|
|||||||
return syscall.FcntlFlock(uintptr(l.fd), syscall.F_SETLK, &lock)
|
return syscall.FcntlFlock(uintptr(l.fd), syscall.F_SETLK, &lock)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlock unlocks the lock
|
|
||||||
func (l *lock) Unlock() error {
|
func (l *lock) Unlock() error {
|
||||||
var lock syscall.Flock_t
|
var lock syscall.Flock_t
|
||||||
lock.Start = 0
|
lock.Start = 0
|
||||||
|
@ -26,14 +26,6 @@ var (
|
|||||||
ErrLocked = errors.New("file already locked")
|
ErrLocked = errors.New("file already locked")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Lock interface {
|
|
||||||
Name() string
|
|
||||||
TryLock() error
|
|
||||||
Lock() error
|
|
||||||
Unlock() error
|
|
||||||
Destroy() error
|
|
||||||
}
|
|
||||||
|
|
||||||
type lock struct {
|
type lock struct {
|
||||||
fd int
|
fd int
|
||||||
file *os.File
|
file *os.File
|
||||||
@ -43,7 +35,6 @@ func (l *lock) Name() string {
|
|||||||
return l.file.Name()
|
return l.file.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TryLock acquires exclusivity on the lock without blocking
|
|
||||||
func (l *lock) TryLock() error {
|
func (l *lock) TryLock() error {
|
||||||
err := syscall.Flock(l.fd, syscall.LOCK_EX|syscall.LOCK_NB)
|
err := syscall.Flock(l.fd, syscall.LOCK_EX|syscall.LOCK_NB)
|
||||||
if err != nil && err == syscall.EWOULDBLOCK {
|
if err != nil && err == syscall.EWOULDBLOCK {
|
||||||
@ -52,12 +43,10 @@ func (l *lock) TryLock() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock acquires exclusivity on the lock without blocking
|
|
||||||
func (l *lock) Lock() error {
|
func (l *lock) Lock() error {
|
||||||
return syscall.Flock(l.fd, syscall.LOCK_EX)
|
return syscall.Flock(l.fd, syscall.LOCK_EX)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlock unlocks the lock
|
|
||||||
func (l *lock) Unlock() error {
|
func (l *lock) Unlock() error {
|
||||||
return syscall.Flock(l.fd, syscall.LOCK_UN)
|
return syscall.Flock(l.fd, syscall.LOCK_UN)
|
||||||
}
|
}
|
||||||
|
@ -25,14 +25,6 @@ var (
|
|||||||
ErrLocked = errors.New("file already locked")
|
ErrLocked = errors.New("file already locked")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Lock interface {
|
|
||||||
Name() string
|
|
||||||
TryLock() error
|
|
||||||
Lock() error
|
|
||||||
Unlock() error
|
|
||||||
Destroy() error
|
|
||||||
}
|
|
||||||
|
|
||||||
type lock struct {
|
type lock struct {
|
||||||
fd int
|
fd int
|
||||||
file *os.File
|
file *os.File
|
||||||
@ -42,17 +34,14 @@ func (l *lock) Name() string {
|
|||||||
return l.file.Name()
|
return l.file.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
// TryLock acquires exclusivity on the lock without blocking
|
|
||||||
func (l *lock) TryLock() error {
|
func (l *lock) TryLock() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock acquires exclusivity on the lock without blocking
|
|
||||||
func (l *lock) Lock() error {
|
func (l *lock) Lock() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unlock unlocks the lock
|
|
||||||
func (l *lock) Unlock() error {
|
func (l *lock) Unlock() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user