2021-10-23 16:44:48 +00:00
|
|
|
// Copyright © 2021 Ettore Di Giacinto <mudler@mocaccino.org>
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"context"
|
2021-10-23 21:12:04 +00:00
|
|
|
"io"
|
2021-11-04 10:34:36 +00:00
|
|
|
"io/fs"
|
2021-10-23 20:23:39 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-10-23 21:12:04 +00:00
|
|
|
"syscall"
|
2021-10-23 16:44:48 +00:00
|
|
|
|
|
|
|
containerdarchive "github.com/containerd/containerd/archive"
|
2021-10-23 21:12:04 +00:00
|
|
|
"github.com/docker/docker/pkg/system"
|
2021-10-23 16:44:48 +00:00
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
|
|
"github.com/google/go-containerregistry/pkg/v1/mutate"
|
|
|
|
"github.com/mudler/luet/pkg/api/core/types"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2021-10-28 21:42:06 +00:00
|
|
|
// ExtractDeltaAdditionsFromImages is a filter that takes two images
|
|
|
|
// an includes and an excludes list. It computes the delta between the images
|
|
|
|
// considering the added files only, and applies a filter on them based on the regexes
|
|
|
|
// in the lists.
|
2021-10-25 22:46:45 +00:00
|
|
|
func ExtractDeltaAdditionsFiles(
|
2021-10-23 16:44:48 +00:00
|
|
|
ctx *types.Context,
|
2021-10-25 22:46:45 +00:00
|
|
|
srcimg v1.Image,
|
2021-10-23 16:44:48 +00:00
|
|
|
includes []string, excludes []string,
|
2021-10-25 22:46:45 +00:00
|
|
|
) (func(h *tar.Header) (bool, error), error) {
|
2021-10-23 16:44:48 +00:00
|
|
|
|
|
|
|
includeRegexp := compileRegexes(includes)
|
|
|
|
excludeRegexp := compileRegexes(excludes)
|
2021-10-26 13:42:48 +00:00
|
|
|
|
|
|
|
srcfilesd, err := ctx.Config.System.TempDir("srcfiles")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filesSrc := NewCache(srcfilesd, 50*1024*1024, 10000)
|
2021-10-25 22:46:45 +00:00
|
|
|
|
|
|
|
srcReader := mutate.Extract(srcimg)
|
|
|
|
defer srcReader.Close()
|
2021-10-23 16:44:48 +00:00
|
|
|
|
2021-10-25 22:46:45 +00:00
|
|
|
srcTar := tar.NewReader(srcReader)
|
|
|
|
|
|
|
|
for {
|
|
|
|
var hdr *tar.Header
|
|
|
|
hdr, err := srcTar.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
// end of tar archive
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-27 08:58:10 +00:00
|
|
|
|
|
|
|
switch hdr.Typeflag {
|
|
|
|
case tar.TypeDir:
|
|
|
|
filesSrc.Set(filepath.Dir(hdr.Name), "")
|
|
|
|
default:
|
|
|
|
filesSrc.Set(hdr.Name, "")
|
|
|
|
}
|
2021-10-24 19:47:11 +00:00
|
|
|
}
|
2021-10-25 22:46:45 +00:00
|
|
|
|
2021-10-23 16:44:48 +00:00
|
|
|
return func(h *tar.Header) (bool, error) {
|
2021-10-27 08:58:10 +00:00
|
|
|
|
2021-10-24 11:01:51 +00:00
|
|
|
fileName := filepath.Join(string(os.PathSeparator), h.Name)
|
2021-10-26 13:42:48 +00:00
|
|
|
_, exists := filesSrc.Get(h.Name)
|
2021-10-25 22:46:45 +00:00
|
|
|
if exists {
|
2021-10-24 19:47:11 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-10-23 16:44:48 +00:00
|
|
|
switch {
|
|
|
|
case len(includes) == 0 && len(excludes) != 0:
|
2021-10-24 19:47:11 +00:00
|
|
|
for _, i := range excludeRegexp {
|
|
|
|
if i.MatchString(filepath.Join(string(os.PathSeparator), h.Name)) &&
|
|
|
|
fileName == filepath.Join(string(os.PathSeparator), h.Name) {
|
|
|
|
return false, nil
|
2021-10-23 16:44:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-24 19:47:11 +00:00
|
|
|
ctx.Debug("Adding name", fileName)
|
|
|
|
|
|
|
|
return true, nil
|
2021-10-23 16:44:48 +00:00
|
|
|
case len(includes) > 0 && len(excludes) == 0:
|
2021-10-24 19:47:11 +00:00
|
|
|
for _, i := range includeRegexp {
|
|
|
|
if i.MatchString(filepath.Join(string(os.PathSeparator), h.Name)) && fileName == filepath.Join(string(os.PathSeparator), h.Name) {
|
|
|
|
ctx.Debug("Adding name", fileName)
|
2021-10-23 16:44:48 +00:00
|
|
|
|
2021-10-24 19:47:11 +00:00
|
|
|
return true, nil
|
2021-10-23 16:44:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
case len(includes) != 0 && len(excludes) != 0:
|
2021-10-24 19:47:11 +00:00
|
|
|
for _, i := range includeRegexp {
|
|
|
|
if i.MatchString(filepath.Join(string(os.PathSeparator), h.Name)) && fileName == filepath.Join(string(os.PathSeparator), h.Name) {
|
|
|
|
for _, e := range excludeRegexp {
|
|
|
|
if e.MatchString(fileName) {
|
|
|
|
return false, nil
|
2021-10-23 16:44:48 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-24 11:01:51 +00:00
|
|
|
ctx.Debug("Adding name", fileName)
|
2021-10-23 16:44:48 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 19:47:11 +00:00
|
|
|
|
2021-10-23 16:44:48 +00:00
|
|
|
return false, nil
|
2021-10-24 19:47:11 +00:00
|
|
|
default:
|
|
|
|
ctx.Debug("Adding name", fileName)
|
|
|
|
return true, nil
|
2021-10-23 16:44:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 22:46:45 +00:00
|
|
|
}, nil
|
2021-10-23 16:44:48 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 21:42:06 +00:00
|
|
|
// ExtractFiles returns a filter that extracts files from the given path (if not empty)
|
|
|
|
// It then filters files by an include and exclude list.
|
|
|
|
// The list can be regexes
|
2021-10-23 20:23:39 +00:00
|
|
|
func ExtractFiles(
|
|
|
|
ctx *types.Context,
|
|
|
|
prefixPath string,
|
|
|
|
includes []string, excludes []string,
|
|
|
|
) func(h *tar.Header) (bool, error) {
|
|
|
|
includeRegexp := compileRegexes(includes)
|
|
|
|
excludeRegexp := compileRegexes(excludes)
|
|
|
|
|
|
|
|
return func(h *tar.Header) (bool, error) {
|
|
|
|
|
2021-10-23 21:03:18 +00:00
|
|
|
fileName := filepath.Join(string(os.PathSeparator), h.Name)
|
2021-10-23 20:23:39 +00:00
|
|
|
switch {
|
|
|
|
case len(includes) == 0 && len(excludes) != 0:
|
|
|
|
for _, i := range excludeRegexp {
|
2021-10-23 21:03:18 +00:00
|
|
|
if i.MatchString(filepath.Join(prefixPath, fileName)) {
|
2021-10-23 20:23:39 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if prefixPath != "" {
|
2021-10-23 21:03:18 +00:00
|
|
|
return strings.HasPrefix(fileName, prefixPath), nil
|
2021-10-23 20:23:39 +00:00
|
|
|
}
|
2021-10-23 21:03:18 +00:00
|
|
|
ctx.Debug("Adding name", fileName)
|
2021-10-23 20:23:39 +00:00
|
|
|
return true, nil
|
|
|
|
|
|
|
|
case len(includes) > 0 && len(excludes) == 0:
|
|
|
|
for _, i := range includeRegexp {
|
2021-10-23 21:03:18 +00:00
|
|
|
if i.MatchString(filepath.Join(prefixPath, fileName)) {
|
2021-10-23 20:23:39 +00:00
|
|
|
if prefixPath != "" {
|
2021-10-23 21:12:04 +00:00
|
|
|
return strings.HasPrefix(fileName, prefixPath), nil
|
2021-10-23 20:23:39 +00:00
|
|
|
}
|
2021-10-23 21:03:18 +00:00
|
|
|
ctx.Debug("Adding name", fileName)
|
2021-10-23 20:23:39 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
case len(includes) != 0 && len(excludes) != 0:
|
|
|
|
for _, i := range includeRegexp {
|
2021-10-23 21:03:18 +00:00
|
|
|
if i.MatchString(filepath.Join(prefixPath, fileName)) {
|
2021-10-23 20:23:39 +00:00
|
|
|
for _, e := range excludeRegexp {
|
2021-10-23 21:03:18 +00:00
|
|
|
if e.MatchString(filepath.Join(prefixPath, fileName)) {
|
2021-10-23 20:23:39 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if prefixPath != "" {
|
2021-10-23 21:03:18 +00:00
|
|
|
return strings.HasPrefix(fileName, prefixPath), nil
|
2021-10-23 20:23:39 +00:00
|
|
|
}
|
2021-10-23 21:03:18 +00:00
|
|
|
ctx.Debug("Adding name", fileName)
|
2021-10-23 20:23:39 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
if prefixPath != "" {
|
2021-10-23 21:03:18 +00:00
|
|
|
return strings.HasPrefix(fileName, prefixPath), nil
|
2021-10-23 20:23:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-23 21:12:04 +00:00
|
|
|
|
2021-10-28 21:42:06 +00:00
|
|
|
// ExtractReader perform the extracting action over the io.ReadCloser
|
|
|
|
// it extracts the files over output. Accepts a filter as an option
|
|
|
|
// and additional containerd Options
|
2021-10-24 10:09:08 +00:00
|
|
|
func ExtractReader(ctx *types.Context, reader io.ReadCloser, output string, keepPerms bool, filter func(h *tar.Header) (bool, error), opts ...containerdarchive.ApplyOpt) (int64, string, error) {
|
2021-10-23 21:12:04 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
2021-10-25 22:46:45 +00:00
|
|
|
// If no filter is specified, grab all.
|
|
|
|
if filter == nil {
|
|
|
|
filter = func(h *tar.Header) (bool, error) { return true, nil }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep records of permissions as we walk the tar
|
|
|
|
type permData struct {
|
|
|
|
PAX, Xattrs map[string]string
|
|
|
|
Uid, Gid int
|
|
|
|
Name string
|
2021-11-04 10:34:36 +00:00
|
|
|
FileMode fs.FileMode
|
2021-10-25 22:46:45 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 13:42:48 +00:00
|
|
|
permstore, err := ctx.Config.System.TempDir("permstore")
|
|
|
|
if err != nil {
|
|
|
|
return 0, "", err
|
|
|
|
}
|
|
|
|
perms := NewCache(permstore, 50*1024*1024, 10000)
|
2021-10-23 21:12:04 +00:00
|
|
|
|
|
|
|
f := func(h *tar.Header) (bool, error) {
|
2021-10-25 22:46:45 +00:00
|
|
|
res, err := filter(h)
|
|
|
|
if res {
|
2021-10-26 13:42:48 +00:00
|
|
|
perms.SetValue(h.Name, permData{
|
2021-10-25 22:46:45 +00:00
|
|
|
PAX: h.PAXRecords,
|
|
|
|
Uid: h.Uid, Gid: h.Gid,
|
2021-11-04 10:34:36 +00:00
|
|
|
Xattrs: h.Xattrs,
|
|
|
|
Name: h.Name,
|
|
|
|
FileMode: h.FileInfo().Mode(),
|
2021-10-25 22:46:45 +00:00
|
|
|
})
|
2021-10-26 13:42:48 +00:00
|
|
|
//perms = append(perms, })
|
2021-10-23 22:57:50 +00:00
|
|
|
}
|
2021-10-25 22:46:45 +00:00
|
|
|
return res, err
|
2021-10-23 21:12:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:57:50 +00:00
|
|
|
opts = append(opts, containerdarchive.WithFilter(f))
|
2021-10-23 21:12:04 +00:00
|
|
|
|
2021-10-25 22:46:45 +00:00
|
|
|
// Handle the extraction
|
2021-10-23 22:57:50 +00:00
|
|
|
c, err := containerdarchive.Apply(context.Background(), output, reader, opts...)
|
2021-10-23 21:12:04 +00:00
|
|
|
if err != nil {
|
2021-10-23 22:57:50 +00:00
|
|
|
return 0, "", err
|
2021-10-23 21:12:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 22:46:45 +00:00
|
|
|
// Reconstruct permissions
|
2021-10-24 10:09:08 +00:00
|
|
|
if keepPerms {
|
2021-10-26 13:42:48 +00:00
|
|
|
ctx.Debug("Reconstructing permissions")
|
|
|
|
perms.All(func(cr CacheResult) {
|
|
|
|
p := &permData{}
|
|
|
|
cr.Unmarshal(p)
|
2021-10-25 22:46:45 +00:00
|
|
|
ff := filepath.Join(output, p.Name)
|
2021-10-24 10:09:08 +00:00
|
|
|
if _, err := os.Lstat(ff); err == nil {
|
2021-10-25 22:46:45 +00:00
|
|
|
if err := os.Lchown(ff, p.Uid, p.Gid); err != nil {
|
2021-10-24 10:09:08 +00:00
|
|
|
ctx.Warning(err, "failed chowning file")
|
|
|
|
}
|
2021-11-04 10:34:36 +00:00
|
|
|
ctx.Debug("Set", p.Name, p.FileMode)
|
|
|
|
if err := os.Chmod(ff, p.FileMode); err != nil {
|
|
|
|
ctx.Warning(err, "failed chmod file")
|
|
|
|
}
|
2021-10-23 21:12:04 +00:00
|
|
|
}
|
2021-10-25 22:46:45 +00:00
|
|
|
for _, attrs := range []map[string]string{p.Xattrs, p.PAX} {
|
2021-10-24 10:09:08 +00:00
|
|
|
for k, attr := range attrs {
|
|
|
|
if err := system.Lsetxattr(ff, k, []byte(attr), 0); err != nil {
|
|
|
|
if errors.Is(err, syscall.ENOTSUP) {
|
2021-10-25 22:46:45 +00:00
|
|
|
ctx.Debug("ignored xattr %s in archive", ff)
|
2021-10-24 10:09:08 +00:00
|
|
|
}
|
2021-10-23 21:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 13:42:48 +00:00
|
|
|
})
|
2021-10-23 21:12:04 +00:00
|
|
|
}
|
2021-10-23 22:57:50 +00:00
|
|
|
return c, output, nil
|
2021-10-23 21:12:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 21:42:06 +00:00
|
|
|
// Extract is just syntax sugar around ExtractReader. It extracts an image into a dir
|
2021-10-24 10:09:08 +00:00
|
|
|
func Extract(ctx *types.Context, img v1.Image, keepPerms bool, filter func(h *tar.Header) (bool, error), opts ...containerdarchive.ApplyOpt) (int64, string, error) {
|
2021-10-23 21:12:04 +00:00
|
|
|
tmpdiffs, err := ctx.Config.GetSystem().TempDir("extraction")
|
|
|
|
if err != nil {
|
2021-10-23 22:57:50 +00:00
|
|
|
return 0, "", errors.Wrap(err, "Error met while creating tempdir for rootfs")
|
2021-10-23 21:12:04 +00:00
|
|
|
}
|
2021-10-24 10:09:08 +00:00
|
|
|
return ExtractReader(ctx, mutate.Extract(img), tmpdiffs, keepPerms, filter, opts...)
|
2021-10-23 21:12:04 +00:00
|
|
|
}
|
|
|
|
|
2021-10-28 21:42:06 +00:00
|
|
|
// ExtractTo is just syntax sugar around ExtractReader
|
2021-10-24 10:09:08 +00:00
|
|
|
func ExtractTo(ctx *types.Context, img v1.Image, output string, keepPerms bool, filter func(h *tar.Header) (bool, error), opts ...containerdarchive.ApplyOpt) (int64, string, error) {
|
|
|
|
return ExtractReader(ctx, mutate.Extract(img), output, keepPerms, filter, opts...)
|
2021-10-23 21:12:04 +00:00
|
|
|
}
|