2017-12-13 23:46:52 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
|
2017-12-17 23:45:12 +00:00
|
|
|
"github.com/containerd/containerd/fs"
|
2017-12-13 23:46:52 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ReadFile(ctx context.Context, ref ImmutableRef, p string) ([]byte, error) {
|
2017-12-21 06:49:39 +00:00
|
|
|
mount, err := ref.Mount(ctx, true)
|
2017-12-13 23:46:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mount)
|
|
|
|
|
|
|
|
root, err := lm.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if lm != nil {
|
|
|
|
lm.Unmount()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-12-17 23:45:12 +00:00
|
|
|
fp, err := fs.RootPath(root, p)
|
2017-12-13 23:46:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(fp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := lm.Unmount(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lm = nil
|
|
|
|
return dt, err
|
|
|
|
}
|