From 7c5b416146649c085b296a2f700cf1c8d669c103 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 13 May 2014 10:34:30 -0700 Subject: [PATCH] Move Follow symlink to pkg Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) Upstream-commit: dcf81f95fdfe3ac8e97602d2ef2fef03288c15b1 Component: engine --- .../engine/{utils => pkg/symlink}/fs.go | 33 +------------------ .../engine/{utils => pkg/symlink}/fs_test.go | 2 +- .../{utils => pkg/symlink}/testdata/fs/a/d | 0 .../{utils => pkg/symlink}/testdata/fs/a/e | 0 .../{utils => pkg/symlink}/testdata/fs/a/f | 0 .../{utils => pkg/symlink}/testdata/fs/b/h | 0 .../{utils => pkg/symlink}/testdata/fs/g | 0 components/engine/utils/utils.go | 31 +++++++++++++++++ 8 files changed, 33 insertions(+), 33 deletions(-) rename components/engine/{utils => pkg/symlink}/fs.go (64%) rename components/engine/{utils => pkg/symlink}/fs_test.go (99%) rename components/engine/{utils => pkg/symlink}/testdata/fs/a/d (100%) rename components/engine/{utils => pkg/symlink}/testdata/fs/a/e (100%) rename components/engine/{utils => pkg/symlink}/testdata/fs/a/f (100%) rename components/engine/{utils => pkg/symlink}/testdata/fs/b/h (100%) rename components/engine/{utils => pkg/symlink}/testdata/fs/g (100%) diff --git a/components/engine/utils/fs.go b/components/engine/pkg/symlink/fs.go similarity index 64% rename from components/engine/utils/fs.go rename to components/engine/pkg/symlink/fs.go index e07ced75d7..e91d33db4b 100644 --- a/components/engine/utils/fs.go +++ b/components/engine/pkg/symlink/fs.go @@ -1,43 +1,12 @@ -package utils +package symlink import ( "fmt" "os" "path/filepath" "strings" - "syscall" ) -// TreeSize walks a directory tree and returns its total size in bytes. -func TreeSize(dir string) (size int64, err error) { - data := make(map[uint64]struct{}) - err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, e error) error { - // Ignore directory sizes - if fileInfo == nil { - return nil - } - - s := fileInfo.Size() - if fileInfo.IsDir() || s == 0 { - return nil - } - - // Check inode to handle hard links correctly - inode := fileInfo.Sys().(*syscall.Stat_t).Ino - // inode is not a uint64 on all platforms. Cast it to avoid issues. - if _, exists := data[uint64(inode)]; exists { - return nil - } - // inode is not a uint64 on all platforms. Cast it to avoid issues. - data[uint64(inode)] = struct{}{} - - size += s - - return nil - }) - return -} - // FollowSymlink will follow an existing link and scope it to the root // path provided. func FollowSymlinkInScope(link, root string) (string, error) { diff --git a/components/engine/utils/fs_test.go b/components/engine/pkg/symlink/fs_test.go similarity index 99% rename from components/engine/utils/fs_test.go rename to components/engine/pkg/symlink/fs_test.go index 9affc00e91..1f12aa3a60 100644 --- a/components/engine/utils/fs_test.go +++ b/components/engine/pkg/symlink/fs_test.go @@ -1,4 +1,4 @@ -package utils +package symlink import ( "io/ioutil" diff --git a/components/engine/utils/testdata/fs/a/d b/components/engine/pkg/symlink/testdata/fs/a/d similarity index 100% rename from components/engine/utils/testdata/fs/a/d rename to components/engine/pkg/symlink/testdata/fs/a/d diff --git a/components/engine/utils/testdata/fs/a/e b/components/engine/pkg/symlink/testdata/fs/a/e similarity index 100% rename from components/engine/utils/testdata/fs/a/e rename to components/engine/pkg/symlink/testdata/fs/a/e diff --git a/components/engine/utils/testdata/fs/a/f b/components/engine/pkg/symlink/testdata/fs/a/f similarity index 100% rename from components/engine/utils/testdata/fs/a/f rename to components/engine/pkg/symlink/testdata/fs/a/f diff --git a/components/engine/utils/testdata/fs/b/h b/components/engine/pkg/symlink/testdata/fs/b/h similarity index 100% rename from components/engine/utils/testdata/fs/b/h rename to components/engine/pkg/symlink/testdata/fs/b/h diff --git a/components/engine/utils/testdata/fs/g b/components/engine/pkg/symlink/testdata/fs/g similarity index 100% rename from components/engine/utils/testdata/fs/g rename to components/engine/pkg/symlink/testdata/fs/g diff --git a/components/engine/utils/utils.go b/components/engine/utils/utils.go index 4ef44b5617..7ffcc06b93 100644 --- a/components/engine/utils/utils.go +++ b/components/engine/utils/utils.go @@ -21,6 +21,7 @@ import ( "strconv" "strings" "sync" + "syscall" "time" "github.com/dotcloud/docker/dockerversion" @@ -1091,3 +1092,33 @@ func ParseKeyValueOpt(opt string) (string, string, error) { } return strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1]), nil } + +// TreeSize walks a directory tree and returns its total size in bytes. +func TreeSize(dir string) (size int64, err error) { + data := make(map[uint64]struct{}) + err = filepath.Walk(dir, func(d string, fileInfo os.FileInfo, e error) error { + // Ignore directory sizes + if fileInfo == nil { + return nil + } + + s := fileInfo.Size() + if fileInfo.IsDir() || s == 0 { + return nil + } + + // Check inode to handle hard links correctly + inode := fileInfo.Sys().(*syscall.Stat_t).Ino + // inode is not a uint64 on all platforms. Cast it to avoid issues. + if _, exists := data[uint64(inode)]; exists { + return nil + } + // inode is not a uint64 on all platforms. Cast it to avoid issues. + data[uint64(inode)] = struct{}{} + + size += s + + return nil + }) + return +}