2013-11-07 20:34:01 +00:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-12-12 00:31:02 +00:00
|
|
|
"strings"
|
2013-11-22 01:18:41 +00:00
|
|
|
"syscall"
|
2013-11-07 20:34:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TreeSize walks a directory tree and returns its total size in bytes.
|
|
|
|
func TreeSize(dir string) (size int64, err error) {
|
2013-11-22 01:18:41 +00:00
|
|
|
data := make(map[uint64]bool)
|
2013-11-07 20:34:01 +00:00
|
|
|
err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, e error) error {
|
2013-11-13 18:33:24 +00:00
|
|
|
// Ignore directory sizes
|
2013-11-22 01:18:41 +00:00
|
|
|
if fileInfo == nil {
|
2013-11-13 18:33:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
2013-11-22 01:18:41 +00: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
|
|
|
|
if _, exists := data[inode]; exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
data[inode] = false
|
|
|
|
|
|
|
|
size += s
|
|
|
|
|
2013-11-07 20:34:01 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2013-12-12 00:31:02 +00:00
|
|
|
|
|
|
|
// FollowSymlink will follow an existing link and scope it to the root
|
|
|
|
// path provided.
|
|
|
|
func FollowSymlinkInScope(link, root string) (string, error) {
|
|
|
|
prev := "."
|
|
|
|
|
|
|
|
root, err := filepath.Abs(root)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
root = filepath.Clean(root)
|
|
|
|
link, err := filepath.Abs(link)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
link = filepath.Clean(link)
|
|
|
|
|
|
|
|
for _, p := range strings.Split(link, "/") {
|
|
|
|
prev = filepath.Join(prev, p)
|
|
|
|
prev = filepath.Clean(prev)
|
|
|
|
|
|
|
|
stat, err := os.Lstat(prev)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
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 '.':
|
|
|
|
if prev = filepath.Clean(filepath.Join(filepath.Dir(prev), dest)); len(prev) < len(root) {
|
|
|
|
prev = filepath.Join(root, filepath.Base(dest))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return prev, nil
|
|
|
|
}
|