forked from juju/juju
/
dirfuncs.go
100 lines (83 loc) · 2.67 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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package provider
import (
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/juju/errors"
)
// dirFuncs is used to allow the real directory operations to
// be stubbed out for testing.
type dirFuncs interface {
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)
symlink(oldpath, newpath string) 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
}
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 := ioutil.ReadDir(path)
if err != nil {
return 0, errors.Annotate(err, "could not read directory")
}
return len(files), nil
}
func (*osDirFuncs) symlink(oldpath, newpath string) error {
return os.Symlink(oldpath, newpath)
}
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
}
func (o *osDirFuncs) mountPoint(path string) (string, error) {
target, err := df(o.run, path, "target")
return target, err
}
func (o *osDirFuncs) mountPointSource(path string) (string, error) {
source, err := df(o.run, path, "source")
return source, err
}
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
}