-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileutils.go
53 lines (43 loc) · 1.08 KB
/
fileutils.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
package a1upgrade
import (
"io/ioutil"
"os"
"github.com/pkg/errors"
"github.com/chef/automate/lib/platform/sys"
)
// IsEmptyDir returns true if an only if the given path is a directory
// (or symlink to a directory) and is empty. An error is returned if
// Stat() or ReadDir() fails.
func IsEmptyDir(dir string) (bool, error) {
// Stat follows symlinks
stat, err := os.Stat(dir)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
if stat.IsDir() {
dentries, err := ioutil.ReadDir(dir)
if err != nil {
return false, err
}
if len(dentries) == 0 {
return true, nil
}
}
return false, nil
}
// IsSameFilesystem returns true if both of the paths exist on the
// same filesystem.
func IsSameFilesystem(path1 string, path2 string) (bool, error) {
mnt1, err := sys.MountFor(path1)
if err != nil {
return false, errors.Wrapf(err, "error looking up mount for path %q", path1)
}
mnt2, err := sys.MountFor(path2)
if err != nil {
return false, errors.Wrapf(err, "error looking up mount for path %q", path2)
}
return mnt2 == mnt1, nil
}