mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-19 02:10:35 +02:00
Added snapshot package and tests
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -47,6 +48,17 @@ func ExtractFileSystemFromImage(img string) error {
|
||||
return pkgutil.GetFileSystemFromReference(ref, imgSrc, constants.RootDir, whitelist)
|
||||
}
|
||||
|
||||
// PathInWhitelist returns true if the path is whitelisted
|
||||
func PathInWhitelist(path, directory string) bool {
|
||||
for _, d := range whitelist {
|
||||
dirPath := filepath.Join(directory, d)
|
||||
if pkgutil.HasFilepathPrefix(path, dirPath) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Get whitelist from roots of mounted files
|
||||
// Each line of /proc/self/mountinfo is in the form:
|
||||
// 36 35 98:0 /mnt1 /mnt2 rw,noatime master:1 - ext3 /dev/root rw,errors=continue
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
Copyright 2018 Google LLC
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// AddToTar adds the file i to tar w at path p
|
||||
func AddToTar(p string, i os.FileInfo, w *tar.Writer) error {
|
||||
linkDst := ""
|
||||
if i.Mode()&os.ModeSymlink != 0 {
|
||||
var err error
|
||||
linkDst, err = os.Readlink(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
hdr, err := tar.FileInfoHeader(i, linkDst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hdr.Name = p
|
||||
w.WriteHeader(hdr)
|
||||
if !i.Mode().IsRegular() {
|
||||
return nil
|
||||
}
|
||||
r, err := os.Open(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := io.Copy(w, r); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -17,8 +17,12 @@ limitations under the License.
|
||||
package util
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// SetLogLevel sets the logrus logging level
|
||||
@@ -30,3 +34,30 @@ func SetLogLevel(logLevel string) error {
|
||||
logrus.SetLevel(lvl)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Hasher returns a hash function, used in snapshotting to determine if a file has changed
|
||||
func Hasher() func(string) string {
|
||||
hasher := func(p string) string {
|
||||
h := md5.New()
|
||||
fi, err := os.Lstat(p)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
h.Write([]byte(fi.Mode().String()))
|
||||
h.Write([]byte(fi.ModTime().String()))
|
||||
|
||||
if fi.Mode().IsRegular() {
|
||||
f, err := os.Open(p)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
return hex.EncodeToString(h.Sum(nil))
|
||||
}
|
||||
return hasher
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user