/
dirfuncs.go
179 lines (155 loc) · 4.44 KB
/
dirfuncs.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package provider
import (
"io"
"os"
"strconv"
"strings"
"github.com/juju/errors"
"github.com/moby/sys/mountinfo"
)
// dirFuncs is used to allow the real directory operations to
// be stubbed out for testing.
type dirFuncs interface {
etcDir() string
mkDirAll(path string, perm os.FileMode) error
lstat(path string) (fi os.FileInfo, err error)
fileCount(path string) (int, error)
calculateSize(path string) (sizeInMib uint64, _ error)
// bindMount remounts the directory "source" at "target",
// so that the source tree is available in both locations.
// If "target" already refers to "source" in this manner,
// then the bindMount operation is a no-op.
bindMount(source, target string) error
// mountPoint returns the mount-point that contains the
// specified path.
mountPoint(path string) (string, error)
// mountPointSource returns the source of the mount-point
// that contains the specified path.
mountPointSource(path string) (string, error)
}
// osDirFuncs is an implementation of dirFuncs that operates on the real
// filesystem.
type osDirFuncs struct {
run runCommandFunc
mountInfoRdr io.ReadSeeker
}
func (*osDirFuncs) etcDir() string {
return "/etc"
}
func (*osDirFuncs) mkDirAll(path string, perm os.FileMode) error {
return os.MkdirAll(path, perm)
}
func (*osDirFuncs) lstat(path string) (fi os.FileInfo, err error) {
return os.Lstat(path)
}
func (*osDirFuncs) fileCount(path string) (int, error) {
files, err := os.ReadDir(path)
if err != nil {
return 0, errors.Annotate(err, "could not read directory")
}
return len(files), nil
}
func (o *osDirFuncs) calculateSize(path string) (sizeInMib uint64, _ error) {
output, err := df(o.run, path, "size")
if err != nil {
return 0, errors.Annotate(err, "getting size")
}
numBlocks, err := strconv.ParseUint(output, 10, 64)
if err != nil {
return 0, errors.Annotate(err, "parsing size")
}
return numBlocks / 1024, nil
}
func (o *osDirFuncs) bindMount(source, target string) error {
_, err := o.run("mount", "--bind", source, target)
return err
}
const mountInfoPath = "/proc/self/mountinfo"
func (o *osDirFuncs) infoReader() (io.ReadSeeker, func(), error) {
if o.mountInfoRdr == nil {
f, err := os.Open(mountInfoPath)
if err != nil {
return nil, nil, errors.Trace(err)
}
return f, func() {
_ = f.Close()
}, nil
}
return o.mountInfoRdr, func() {
_, _ = o.mountInfoRdr.Seek(0, 0)
}, nil
}
func (o *osDirFuncs) mountPoint(path string) (string, error) {
infoReader, closer, err := o.infoReader()
if err != nil {
return "", errors.Annotatef(err, "opening %s", mountInfoPath)
}
defer closer()
mi, err := getMountsFromReader(infoReader, mountinfo.SingleEntryFilter(path))
if err != nil {
return "", errors.Annotatef(err, "getting info for mount point %q", path)
}
if len(mi) == 0 {
return "", nil
}
return mi[0].Source, nil
}
func (o *osDirFuncs) mountPointSource(target string) (result string, _ error) {
defer func() {
logger.Debugf("mount point source for %q is %q", target, result)
}()
infoReader, closer, err := o.infoReader()
if err != nil {
return "", errors.Annotate(err, "opening /proc/self/mountinfo")
}
defer closer()
mi, err := getMountsFromReader(infoReader, nil)
if err != nil {
return "", errors.Annotatef(err, "getting info for mount point %q", target)
}
if len(mi) == 0 {
return "", nil
}
mountsById := make(map[int]*mountinfo.Info)
var targetInfo *mountinfo.Info
for _, m := range mi {
mountsById[m.ID] = m
if m.Mountpoint == target {
mp := *m
targetInfo = &mp
}
}
if targetInfo == nil {
return "", nil
}
if targetInfo.FSType == "tmpfs" {
return targetInfo.Source, nil
}
// Step through any parent records to progressively
// resolve the absolute source path.
source := targetInfo.Root
for {
next, ok := mountsById[targetInfo.Parent]
if !ok {
break
}
next.Root = strings.TrimSuffix(next.Root, "/")
next.Mountpoint = strings.TrimSuffix(next.Mountpoint, "/")
if strings.HasPrefix(source, next.Root+"/") {
source = strings.Replace(source, next.Root, next.Mountpoint, 1)
}
targetInfo = next
}
return source, nil
}
func df(run runCommandFunc, path, field string) (string, error) {
output, err := run("df", "--output="+field, path)
if err != nil {
return "", errors.Trace(err)
}
// the first line contains the headers
lines := strings.SplitN(output, "\n", 2)
return strings.TrimSpace(lines[1]), nil
}