github.com/dlintw/docker@v1.5.0-rc4/utils/utils_daemon.go (about)

     1  // +build daemon
     2  
     3  package utils
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"syscall"
     9  )
    10  
    11  // TreeSize walks a directory tree and returns its total size in bytes.
    12  func TreeSize(dir string) (size int64, err error) {
    13  	data := make(map[uint64]struct{})
    14  	err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, e error) error {
    15  		// Ignore directory sizes
    16  		if fileInfo == nil {
    17  			return nil
    18  		}
    19  
    20  		s := fileInfo.Size()
    21  		if fileInfo.IsDir() || s == 0 {
    22  			return nil
    23  		}
    24  
    25  		// Check inode to handle hard links correctly
    26  		inode := fileInfo.Sys().(*syscall.Stat_t).Ino
    27  		// inode is not a uint64 on all platforms. Cast it to avoid issues.
    28  		if _, exists := data[uint64(inode)]; exists {
    29  			return nil
    30  		}
    31  		// inode is not a uint64 on all platforms. Cast it to avoid issues.
    32  		data[uint64(inode)] = struct{}{}
    33  
    34  		size += s
    35  
    36  		return nil
    37  	})
    38  	return
    39  }
    40  
    41  // IsFileOwner checks whether the current user is the owner of the given file.
    42  func IsFileOwner(f string) bool {
    43  	if fileInfo, err := os.Stat(f); err == nil && fileInfo != nil {
    44  		if stat, ok := fileInfo.Sys().(*syscall.Stat_t); ok && int(stat.Uid) == os.Getuid() {
    45  			return true
    46  		}
    47  	}
    48  	return false
    49  }