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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package request
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/johnandersen777/socialweb-computer-kcp/internal/hostos"
)
const OwnerLockFile = "owner.lock"
type Owner struct {
f *os.File
}
func openOwnerLock(dir string) (*os.File, error) {
path := filepath.Join(dir, OwnerLockFile)
info, err := os.Lstat(path)
if err == nil && (info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular()) {
return nil, fmt.Errorf("request: the owner lock %s is not a regular file", path)
}
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
return nil, fmt.Errorf("request: the owner lock: %w", err)
}
return f, nil
}
func TakeOwner(stateRoot, name string) (*Owner, bool, error) {
return TakeOwnerDir(Dir(stateRoot, name))
}
func TakeOwnerDir(dir string) (*Owner, bool, error) {
f, err := openOwnerLock(dir)
if err != nil {
return nil, false, err
}
if err := hostos.Lock(f, hostos.LockExclusive|hostos.LockNonBlocking); err != nil {
_ = f.Close()
return nil, false, nil
}
return &Owner{f: f}, true, nil
}
func AwaitOwnerGone(ctx context.Context, stateRoot, name string) error {
f, err := openOwnerLock(Dir(stateRoot, name))
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err != nil {
return err
}
done := make(chan error, 1)
go func() {
done <- hostos.Lock(f, hostos.LockExclusive)
}()
select {
case err := <-done:
_ = hostos.Unlock(f)
_ = f.Close()
return err
case <-ctx.Done():
go func() {
<-done
_ = f.Close()
}()
return ctx.Err()
}
}
func (o *Owner) Close() {
if o == nil || o.f == nil {
return
}
_ = hostos.Unlock(o.f)
_ = o.f.Close()
o.f = nil
}