Skip to content

Commit

Permalink
Merge pull request moby#2304 from unclejack/fix_layer_size_computation
Browse files Browse the repository at this point in the history
Fix layer size computation: handle hard links correctly
  • Loading branch information
crosbymichael committed Nov 22, 2013
2 parents 8498b44 + 78c843c commit f7c2a00
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
34 changes: 30 additions & 4 deletions container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1346,20 +1346,46 @@ func validateID(id string) error {
// GetSize, return real size, virtual size
func (container *Container) GetSize() (int64, int64) {
var sizeRw, sizeRootfs int64
data := make(map[uint64]bool)

filepath.Walk(container.rwPath(), func(path string, fileInfo os.FileInfo, err error) error {
if fileInfo != nil {
sizeRw += fileInfo.Size()
if fileInfo == nil {
return nil
}
size := fileInfo.Size()
if size == 0 {
return nil
}

inode := fileInfo.Sys().(*syscall.Stat_t).Ino
if _, entryExists := data[inode]; entryExists {
return nil
}
data[inode] = false

sizeRw += size
return nil
})

data = make(map[uint64]bool)
_, err := os.Stat(container.RootfsPath())
if err == nil {
filepath.Walk(container.RootfsPath(), func(path string, fileInfo os.FileInfo, err error) error {
if fileInfo != nil {
sizeRootfs += fileInfo.Size()
if fileInfo == nil {
return nil
}
size := fileInfo.Size()
if size == 0 {
return nil
}

inode := fileInfo.Sys().(*syscall.Stat_t).Ino
if _, entryExists := data[inode]; entryExists {
return nil
}
data[inode] = false

sizeRootfs += size
return nil
})
}
Expand Down
15 changes: 14 additions & 1 deletion image.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
)

Expand Down Expand Up @@ -114,10 +115,22 @@ func StoreImage(img *Image, jsonData []byte, layerData archive.Archive, root str

func StoreSize(img *Image, root string) error {
layer := layerPath(root)
data := make(map[uint64]bool)

var totalSize int64
filepath.Walk(layer, func(path string, fileInfo os.FileInfo, err error) error {
totalSize += fileInfo.Size()
size := fileInfo.Size()
if size == 0 {
return nil
}

inode := fileInfo.Sys().(*syscall.Stat_t).Ino
if _, entryExists := data[inode]; entryExists {
return nil
}
data[inode] = false

totalSize += size
return nil
})
img.Size = totalSize
Expand Down

0 comments on commit f7c2a00

Please sign in to comment.