2022-04-20 16:41:20 +02:00
|
|
|
// Copyright 2022 The TVL Contributors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2019-10-03 19:18:52 +02:00
|
|
|
package builder
|
|
|
|
|
|
|
|
// This file implements logic for walking through a directory and creating a
|
|
|
|
// tarball of it.
|
|
|
|
//
|
|
|
|
// The tarball is written straight to the supplied reader, which makes it
|
|
|
|
// possible to create an image layer from the specified store paths, hash it and
|
|
|
|
// upload it in one reading pass.
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
2019-10-11 02:28:38 +02:00
|
|
|
"compress/gzip"
|
2019-10-11 12:57:14 +02:00
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
2019-10-03 19:18:52 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2019-10-11 02:28:38 +02:00
|
|
|
// Create a new compressed tarball from each of the paths in the list
|
|
|
|
// and write it to the supplied writer.
|
2019-10-11 12:57:14 +02:00
|
|
|
//
|
|
|
|
// The uncompressed tarball is hashed because image manifests must
|
|
|
|
// contain both the hashes of compressed and uncompressed layers.
|
2019-11-11 22:07:16 +01:00
|
|
|
func packStorePaths(l *layer, w io.Writer) (string, error) {
|
2019-10-11 12:57:14 +02:00
|
|
|
shasum := sha256.New()
|
2019-10-11 02:28:38 +02:00
|
|
|
gz := gzip.NewWriter(w)
|
2019-10-11 12:57:14 +02:00
|
|
|
multi := io.MultiWriter(shasum, gz)
|
|
|
|
t := tar.NewWriter(multi)
|
2019-10-03 19:18:52 +02:00
|
|
|
|
|
|
|
for _, path := range l.Contents {
|
|
|
|
err := filepath.Walk(path, tarStorePath(t))
|
|
|
|
if err != nil {
|
2019-10-11 12:57:14 +02:00
|
|
|
return "", err
|
2019-10-03 19:18:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := t.Close(); err != nil {
|
2019-10-11 12:57:14 +02:00
|
|
|
return "", err
|
2019-10-03 19:18:52 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 02:28:38 +02:00
|
|
|
if err := gz.Close(); err != nil {
|
2019-10-11 12:57:14 +02:00
|
|
|
return "", err
|
2019-10-11 02:28:38 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 12:57:14 +02:00
|
|
|
return fmt.Sprintf("sha256:%x", shasum.Sum([]byte{})), nil
|
2019-10-03 19:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func tarStorePath(w *tar.Writer) filepath.WalkFunc {
|
|
|
|
return func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the entry is not a symlink or regular file, skip it.
|
|
|
|
if info.Mode()&os.ModeSymlink == 0 && !info.Mode().IsRegular() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// the symlink target is read if this entry is a symlink, as it
|
|
|
|
// is required when creating the file header
|
|
|
|
var link string
|
|
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
link, err = os.Readlink(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
header, err := tar.FileInfoHeader(info, link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The name retrieved from os.FileInfo only contains the file's
|
|
|
|
// basename, but the full path is required within the layer
|
|
|
|
// tarball.
|
|
|
|
header.Name = path
|
|
|
|
if err = w.WriteHeader(header); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, return if no file content needs to be written
|
|
|
|
if !info.Mode().IsRegular() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(w, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|