-
Notifications
You must be signed in to change notification settings - Fork 2
/
lockedFile.go
46 lines (42 loc) · 1.59 KB
/
lockedFile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"os"
"syscall"
)
// lockedFile is a wrapper around `os.File` that adds `.Open()` and overrides `.Close()` methods so that the
// underlying file is exclusively locked (via `flock(..., LOCK_EX)`) when accessed.
type lockedFile struct {
*os.File
}
// Open opens the file as in `os.Open` and locks the file in exclusive mode via `flock(..., LOCK_EX)`,
// possibly blocking.
//
// If an error occurs in either step, it is reported and the internals are cleaned up (i.e. no need for the caller to
// call `.Close()`), otherwise the object must be `.Close()`d to release the lock and the file descriptor.
func (lf *lockedFile) Open(path string) error {
var err error
lf.File, err = os.Open(path)
if err != nil {
log.Errorf("Failed to Open: %v", err)
return internalError("failed to Open inside lockedFile", err)
}
err = syscall.Flock(int(lf.File.Fd()), syscall.LOCK_EX)
if err != nil {
log.Errorf("Failed to get exclusive lock on %s: %v", lf.File.Name(), err)
lf.File.Close() // An error is going to be returned, so the caller won't call `.Close()`
return internalError("failed to get exclusive Flock", err)
}
return nil
}
// Close releases the lock on the underlying file and closes the file using its original `.Close()`.
// If an error occurs when releasing the lock, it is logged and returned; the error from the original
// `.Close()` is ignored.
func (lf *lockedFile) Close() error {
defer lf.File.Close()
err := syscall.Flock(int(lf.File.Fd()), syscall.LOCK_UN)
if err != nil {
log.Criticalf("Failed to release lock on %s: %v", lf.File.Name(), err)
return err
}
return nil
}