2017-12-13 23:46:52 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-12-13 23:46:52 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|