Skip to content

Commit

Permalink
support symlinks for archive creation (#559)
Browse files Browse the repository at this point in the history
* support symlinks for archive creation

* fix transporting signatures in overwrite mode
  • Loading branch information
mandelsoft authored Oct 27, 2023
1 parent f29de6f commit b55ecc3
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 13 deletions.
8 changes: 5 additions & 3 deletions pkg/contexts/ocm/cpi/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -1072,10 +1072,12 @@ func (c *componentVersionAccessView) SetResource(meta *internal.ResourceMeta, ac

if old != nil {
eq := res.Equivalent(old)
if !eq.IsLocalHashEqual() && !opts.IsModifyResource() && c.impl.IsPersistent() {
return fmt.Errorf("resource would invalidate signature")
if !eq.IsLocalHashEqual() && c.impl.IsPersistent() {
if !opts.IsModifyResource() {
return fmt.Errorf("resource would invalidate signature")
}
cd.Signatures = nil
}
cd.Signatures = nil
}

if old == nil {
Expand Down
30 changes: 20 additions & 10 deletions pkg/utils/tarutils/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,12 @@ func addFileToTar(fs vfs.FileSystem, tw *tar.Writer, path string, realPath strin
if err != nil {
return err
}
relPath, err := vfs.Rel(fs, realPath, subFilePath)
if err != nil {
return fmt.Errorf("unable to calculate relative path for %s: %w", subFilePath, err)
}
/*
relPath, err := vfs.Rel(fs, realPath, subFilePath)
if err != nil {
return fmt.Errorf("unable to calculate relative path for %s: %w", subFilePath, err)
}*/
relPath := vfs.Base(fs, subFilePath)
err = addFileToTar(fs, tw, vfs.Join(fs, path, relPath), subFilePath, opts)
if err != nil {
return fmt.Errorf("failed to tar the input from %q: %w", subFilePath, err)
Expand Down Expand Up @@ -167,14 +169,22 @@ func addFileToTar(fs vfs.FileSystem, tw *tar.Writer, path string, realPath strin
return nil
case header.Typeflag == tar.TypeSymlink:
if !opts.FollowSymlinks {
// log.Info(fmt.Sprintf("symlink found in %q but symlinks are not followed", path))
link, err := fs.Readlink(realPath)
if err != nil {
return fmt.Errorf("cannot read symlink %s: %w", realPath, err)
}
header.Linkname = link
if err := tw.WriteHeader(header); err != nil {
return fmt.Errorf("unable to write header for %q: %w", path, err)
}
return nil
} else {
effPath, err := vfs.EvalSymlinks(fs, realPath)
if err != nil {
return fmt.Errorf("unable to follow symlink %s: %w", realPath, err)
}
return addFileToTar(fs, tw, path, effPath, opts)
}
effPath, err := vfs.EvalSymlinks(fs, realPath)
if err != nil {
return fmt.Errorf("unable to follow symlink %s: %w", realPath, err)
}
return addFileToTar(fs, tw, path, effPath, opts)
default:
return fmt.Errorf("unsupported file type %s in %s", info.Mode().String(), path)
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/utils/tarutils/pack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package tarutils_test

import (
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/open-component-model/ocm/pkg/testutils"

"github.com/mandelsoft/vfs/pkg/osfs"

"github.com/open-component-model/ocm/pkg/utils/tarutils"
)

var _ = Describe("tar utils mapping", func() {
It("creates config data", func() {
file := Must(os.CreateTemp("", "tar*"))
defer func() {
file.Close()
os.Remove(file.Name())
}()

MustBeSuccessful(tarutils.PackFsIntoTar(osfs.OsFs, "testdata", file, tarutils.TarFileSystemOptions{}))
file.Close()

list := Must(tarutils.ListArchiveContent(file.Name()))
Expect(list).To(HaveExactElements("dir", "dir/dirlink", "dir/link", "dir/regular", "dir/subdir", "dir/subdir/file", "dir2", "dir2/file2", "file"))
})

It("creates config data", func() {
file := Must(os.CreateTemp("", "tar*"))
defer func() {
file.Close()
// os.Remove(file.Name())
}()

MustBeSuccessful(tarutils.PackFsIntoTar(osfs.OsFs, "testdata", file, tarutils.TarFileSystemOptions{FollowSymlinks: true}))
file.Close()

list := Must(tarutils.ListArchiveContent(file.Name()))
Expect(list).To(ConsistOf("dir", "dir/dirlink", "dir/link", "dir/regular", "dir/subdir", "dir/subdir/file", "dir2", "dir2/file2", "file", "dir/dirlink/file2"))
})
})
17 changes: 17 additions & 0 deletions pkg/utils/tarutils/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package tarutils_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Tar Utils Test Suite")
}
1 change: 1 addition & 0 deletions pkg/utils/tarutils/testdata/dir/dirlink
1 change: 1 addition & 0 deletions pkg/utils/tarutils/testdata/dir/link
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit b55ecc3

Please sign in to comment.