2013-11-07 15:34:01 -05:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2013-12-11 20:12:53 -05:00
|
|
|
"fmt"
|
2013-11-07 15:34:01 -05:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-12-11 19:31:02 -05:00
|
|
|
"strings"
|
2013-11-21 20:18:41 -05:00
|
|
|
"syscall"
|
2013-11-07 15:34:01 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// TreeSize walks a directory tree and returns its total size in bytes.
|
|
|
|
func TreeSize(dir string) (size int64, err error) {
|
2014-04-17 13:08:14 -04:00
|
|
|
data := make(map[uint64]struct{})
|
2013-11-07 15:34:01 -05:00
|
|
|
err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, e error) error {
|
2013-11-13 13:33:24 -05:00
|
|
|
// Ignore directory sizes
|
2013-11-21 20:18:41 -05:00
|
|
|
if fileInfo == nil {
|
2013-11-13 13:33:24 -05:00
|
|
|
return nil
|
|
|
|
}
|
2013-11-21 20:18:41 -05:00
|
|
|
|
|
|
|
s := fileInfo.Size()
|
|
|
|
if fileInfo.IsDir() || s == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check inode to handle hard links correctly
|
|
|
|
inode := fileInfo.Sys().(*syscall.Stat_t).Ino
|
2014-01-30 17:41:10 -05:00
|
|
|
// inode is not a uint64 on all platforms. Cast it to avoid issues.
|
|
|
|
if _, exists := data[uint64(inode)]; exists {
|
2013-11-21 20:18:41 -05:00
|
|
|
return nil
|
|
|
|
}
|
2014-01-30 17:41:10 -05:00
|
|
|
// inode is not a uint64 on all platforms. Cast it to avoid issues.
|
2014-04-17 13:08:14 -04:00
|
|
|
data[uint64(inode)] = struct{}{}
|
2013-11-21 20:18:41 -05:00
|
|
|
|
|
|
|
size += s
|
|
|
|
|
2013-11-07 15:34:01 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2013-12-11 19:31:02 -05:00
|
|
|
|
|
|
|
// FollowSymlink will follow an existing link and scope it to the root
|
|
|
|
// path provided.
|
|
|
|
func FollowSymlinkInScope(link, root string) (string, error) {
|
2013-12-11 20:12:53 -05:00
|
|
|
prev := "/"
|
2013-12-11 19:31:02 -05:00
|
|
|
|
|
|
|
root, err := filepath.Abs(root)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-12-11 20:12:53 -05:00
|
|
|
|
|
|
|
link, err = filepath.Abs(link)
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2013-12-11 20:12:53 -05:00
|
|
|
|
|
|
|
if !strings.HasPrefix(filepath.Dir(link), root) {
|
|
|
|
return "", fmt.Errorf("%s is not within %s", link, root)
|
|
|
|
}
|
2013-12-11 19:31:02 -05:00
|
|
|
|
|
|
|
for _, p := range strings.Split(link, "/") {
|
|
|
|
prev = filepath.Join(prev, p)
|
|
|
|
prev = filepath.Clean(prev)
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
for {
|
2014-04-10 18:47:53 -04:00
|
|
|
if !strings.HasPrefix(prev, root) {
|
|
|
|
// Don't resolve symlinks outside of root. For example,
|
|
|
|
// we don't have to check /home in the below.
|
|
|
|
//
|
|
|
|
// /home -> usr/home
|
|
|
|
// FollowSymlinkInScope("/home/bob/foo/bar", "/home/bob/foo")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
stat, err := os.Lstat(prev)
|
2013-12-11 19:31:02 -05:00
|
|
|
if err != nil {
|
2013-12-11 20:12:53 -05:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
break
|
|
|
|
}
|
2013-12-11 19:31:02 -05:00
|
|
|
return "", err
|
|
|
|
}
|
2013-12-11 20:12:53 -05:00
|
|
|
if stat.Mode()&os.ModeSymlink == os.ModeSymlink {
|
|
|
|
dest, err := os.Readlink(prev)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch dest[0] {
|
|
|
|
case '/':
|
|
|
|
prev = filepath.Join(root, dest)
|
|
|
|
case '.':
|
|
|
|
prev, _ = filepath.Abs(prev)
|
2013-12-11 19:31:02 -05:00
|
|
|
|
2013-12-11 20:12:53 -05:00
|
|
|
if prev = filepath.Clean(filepath.Join(filepath.Dir(prev), dest)); len(prev) < len(root) {
|
|
|
|
prev = filepath.Join(root, filepath.Base(dest))
|
|
|
|
}
|
2013-12-11 19:31:02 -05:00
|
|
|
}
|
2013-12-11 20:12:53 -05:00
|
|
|
} else {
|
|
|
|
break
|
2013-12-11 19:31:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prev, nil
|
|
|
|
}
|