move code supporting push, pull, and inspect to libkpod and libkpod/image

Signed-off-by: Ryan Cole <rcyoalne@gmail.com>
This commit is contained in:
Ryan Cole 2017-07-23 19:01:37 -04:00
parent 2c1fd1ad3f
commit 14864f820e
16 changed files with 319 additions and 269 deletions

View file

@ -2,6 +2,7 @@ package libkpod
import (
cstorage "github.com/containers/storage"
"github.com/pkg/errors"
)
// FindContainer searches for a container with the given name or ID in the given store
@ -46,3 +47,51 @@ func GetContainerRwSize(store cstorage.Store, containerID string) (int64, error)
}
return lstore.DiffSize(layer.Parent, layer.ID)
}
// GetContainerRootFsSize gets the size of the container's root filesystem
// A container FS is split into two parts. The first is the top layer, a
// mutable layer, and the rest is the RootFS: the set of immutable layers
// that make up the image on which the container is based
func GetContainerRootFsSize(store cstorage.Store, containerID string) (int64, error) {
ctrStore, err := store.ContainerStore()
if err != nil {
return 0, err
}
container, err := ctrStore.Get(containerID)
if err != nil {
return 0, err
}
lstore, err := store.LayerStore()
if err != nil {
return 0, err
}
// Ignore the size of the top layer. The top layer is a mutable RW layer
// and is not considered a part of the rootfs
rwLayer, err := lstore.Get(container.LayerID)
if err != nil {
return 0, err
}
layer, err := lstore.Get(rwLayer.Parent)
if err != nil {
return 0, err
}
size := int64(0)
for layer.Parent != "" {
layerSize, err := lstore.DiffSize(layer.Parent, layer.ID)
if err != nil {
return size, errors.Wrapf(err, "getting diffsize of layer %q and its parent %q", layer.ID, layer.Parent)
}
size += layerSize
layer, err = lstore.Get(layer.Parent)
if err != nil {
return 0, err
}
}
// Get the size of the last layer. Has to be outside of the loop
// because the parent of the last layer is "", andlstore.Get("")
// will return an error
layerSize, err := lstore.DiffSize(layer.Parent, layer.ID)
return size + layerSize, err
}