forked from cybozu-go/aptutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.go
148 lines (127 loc) · 2.67 KB
/
control.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package mirror
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"time"
"github.com/cybozu-go/log"
"github.com/cybozu-go/well"
"github.com/pkg/errors"
)
const (
lockFilename = ".lock"
)
func updateMirrors(ctx context.Context, c *Config, mirrors []string) error {
t := time.Now()
var ml []*Mirror
for _, id := range mirrors {
m, err := NewMirror(t, id, c)
if err != nil {
return err
}
ml = append(ml, m)
}
log.Info("update starts", nil)
// run goroutines in an environment.
env := well.NewEnvironment(ctx)
for _, m := range ml {
env.Go(m.Update)
}
env.Stop()
err := env.Wait()
if err != nil {
log.Error("update failed", map[string]interface{}{
"error": err.Error(),
})
return err
}
log.Info("update ends", nil)
return nil
}
// gc removes old mirror files, if any.
func gc(ctx context.Context, c *Config) error {
using := map[string]bool{
lockFilename: true,
".": true,
"..": true,
}
dentries, err := ioutil.ReadDir(c.Dir)
if err != nil {
return err
}
// search symlinks and its pointing directories
for _, dentry := range dentries {
if (dentry.Mode() & os.ModeSymlink) == 0 {
continue
}
p, err := filepath.EvalSymlinks(filepath.Join(c.Dir, dentry.Name()))
if err != nil {
return errors.Wrap(err, "gc")
}
using[dentry.Name()] = true
using[filepath.Base(filepath.Dir(p))] = true
}
// remove unused dentries.
for _, dentry := range dentries {
if using[dentry.Name()] {
continue
}
select {
case <-ctx.Done():
return ctx.Err()
default:
}
p := filepath.Join(c.Dir, dentry.Name())
log.Info("removing old mirror", map[string]interface{}{
"path": p,
})
err := os.RemoveAll(p)
if err != nil {
return errors.Wrap(err, "gc")
}
}
return nil
}
// Run starts mirroring.
//
// The first thing to do is to acquire flock on the lock file.
//
// mirrors is a list of mirror IDs defined in the configuration file
// (or keys in c.Mirrors). If mirrors is an empty list, all mirrors
// will be updated.
func Run(c *Config, mirrors []string) error {
lockFile := filepath.Join(c.Dir, lockFilename)
f, err := os.Open(lockFile)
switch {
case os.IsNotExist(err):
f2, err := os.OpenFile(lockFile, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
return err
}
f = f2
case err != nil:
return err
}
defer f.Close()
fl := Flock{f}
err = fl.Lock()
if err != nil {
return err
}
defer fl.Unlock()
if len(mirrors) == 0 {
for id := range c.Mirrors {
mirrors = append(mirrors, id)
}
}
well.Go(func(ctx context.Context) error {
err := updateMirrors(ctx, c, mirrors)
if err != nil {
return err
}
return gc(ctx, c)
})
well.Stop()
return well.Wait()
}