-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* CLI: fix tar input handling * incorporate review comments
- Loading branch information
1 parent
3a483ae
commit 5a3c7b9
Showing
5 changed files
with
180 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// Copyright 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file | ||
// | ||
// 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 tarutils | ||
|
||
import ( | ||
"archive/tar" | ||
"fmt" | ||
"io" | ||
"os" | ||
pathutil "path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/mandelsoft/vfs/pkg/vfs" | ||
) | ||
|
||
// TarFileSystemOptions describes additional options for tarring a filesystem. | ||
type TarFileSystemOptions struct { | ||
IncludeFiles []string | ||
ExcludeFiles []string | ||
// PreserveDir defines that the directory specified in the Path field should be included in the blob. | ||
// Only supported for Type dir. | ||
PreserveDir bool | ||
FollowSymlinks bool | ||
|
||
root string | ||
} | ||
|
||
// Included determines whether a file should be included. | ||
func (opts *TarFileSystemOptions) Included(path string) (bool, error) { | ||
// if a root path is given remove it rom the path to be checked | ||
if len(opts.root) != 0 { | ||
path = strings.TrimPrefix(path, opts.root) | ||
} | ||
// first check if a exclude regex matches | ||
for _, ex := range opts.ExcludeFiles { | ||
match, err := filepath.Match(ex, path) | ||
if err != nil { | ||
return false, fmt.Errorf("malformed filepath syntax %q", ex) | ||
} | ||
if match { | ||
return false, nil | ||
} | ||
} | ||
|
||
// if no includes are defined, include all files | ||
if len(opts.IncludeFiles) == 0 { | ||
return true, nil | ||
} | ||
// otherwise check if the file should be included | ||
for _, in := range opts.IncludeFiles { | ||
match, err := filepath.Match(in, path) | ||
if err != nil { | ||
return false, fmt.Errorf("malformed filepath syntax %q", in) | ||
} | ||
if match { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
} | ||
|
||
// TarFileSystem creates a tar archive from a filesystem. | ||
func TarFileSystem(fs vfs.FileSystem, root string, writer io.Writer, opts TarFileSystemOptions) error { | ||
tw := tar.NewWriter(writer) | ||
if opts.PreserveDir { | ||
opts.root = pathutil.Base(root) | ||
} | ||
if err := addFileToTar(fs, tw, opts.root, root, opts); err != nil { | ||
return err | ||
} | ||
return tw.Close() | ||
} | ||
|
||
func addFileToTar(fs vfs.FileSystem, tw *tar.Writer, path string, realPath string, opts TarFileSystemOptions) error { | ||
if len(path) != 0 { // do not check the root | ||
include, err := opts.Included(path) | ||
if err != nil { | ||
return err | ||
} | ||
if !include { | ||
return nil | ||
} | ||
} | ||
info, err := fs.Lstat(realPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
header, err := tar.FileInfoHeader(info, "") | ||
if err != nil { | ||
return err | ||
} | ||
header.Name = path | ||
|
||
switch { | ||
case info.IsDir(): | ||
// do not write root header | ||
if len(path) != 0 { | ||
if err := tw.WriteHeader(header); err != nil { | ||
return fmt.Errorf("unable to write header for %q: %w", path, err) | ||
} | ||
} | ||
err := vfs.Walk(fs, realPath, func(subFilePath string, info os.FileInfo, err error) error { | ||
if subFilePath == realPath { | ||
return nil | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
relPath, err := filepath.Rel(realPath, subFilePath) | ||
if err != nil { | ||
return fmt.Errorf("unable to calculate relative path for %s: %w", subFilePath, err) | ||
} | ||
err = addFileToTar(fs, tw, pathutil.Join(path, relPath), subFilePath, opts) | ||
if err == nil && info.IsDir() { | ||
err = vfs.SkipDir | ||
} | ||
return err | ||
}) | ||
return err | ||
case info.Mode().IsRegular(): | ||
if err := tw.WriteHeader(header); err != nil { | ||
return fmt.Errorf("unable to write header for %q: %w", path, err) | ||
} | ||
file, err := fs.OpenFile(realPath, os.O_RDONLY, os.ModePerm) | ||
if err != nil { | ||
return fmt.Errorf("unable to open file %q: %w", path, err) | ||
} | ||
if _, err := io.Copy(tw, file); err != nil { | ||
_ = file.Close() | ||
return fmt.Errorf("unable to add file to tar %q: %w", path, err) | ||
} | ||
if err := file.Close(); err != nil { | ||
return fmt.Errorf("unable to close file %q: %w", path, err) | ||
} | ||
return nil | ||
case header.Typeflag == tar.TypeSymlink: | ||
if !opts.FollowSymlinks { | ||
//log.Info(fmt.Sprintf("symlink found in %q but symlinks are not followed", path)) | ||
return nil | ||
} | ||
realPath, err := vfs.EvalSymlinks(fs, realPath) | ||
if err != nil { | ||
return fmt.Errorf("unable to follow symlink %s: %w", realPath, err) | ||
} | ||
return addFileToTar(fs, tw, path, realPath, opts) | ||
default: | ||
return fmt.Errorf("unsupported file type %s in %s", info.Mode().String(), path) | ||
} | ||
} |