Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle recursive links in runtime sources. #3600

Merged
merged 6 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions internal/smartlink/smartlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func LinkContents(src, dest string) error {
return errs.Wrap(err, "Reading dir %s failed", src)
}
for _, entry := range entries {
if err := Link(filepath.Join(src, entry.Name()), filepath.Join(dest, entry.Name())); err != nil {
if err := Link(filepath.Join(src, entry.Name()), filepath.Join(dest, entry.Name()), nil); err != nil {
return errs.Wrap(err, "Link failed")
}
}
Expand All @@ -39,13 +39,28 @@ func LinkContents(src, dest string) error {

// Link creates a link from src to target. MS decided to support Symlinks but only if you opt into developer mode (go figure),
// which we cannot reasonably force on our users. So on Windows we will instead create dirs and hardlinks.
func Link(src, dest string) error {
func Link(src, dest string, visited map[string]int) error {
var err error
src, dest, err = resolvePaths(src, dest)
if err != nil {
return errs.Wrap(err, "Could not resolve src and dest paths")
}

if visited == nil {
visited = make(map[string]int)
}
if count, exists := visited[src]; exists {
// We've encountered a recursive link. This is most often the case when the resolved src has
// already been visited. We will recurse into the directory no more than once, so that any
// runtime paths that reference the link will not silently fail.
if count > 1 {
return nil
}
visited[src]++
} else {
visited[src] = 1
}

if fileutils.IsDir(src) {
if err := fileutils.Mkdir(dest); err != nil {
return errs.Wrap(err, "could not create directory %s", dest)
Expand All @@ -55,7 +70,7 @@ func Link(src, dest string) error {
return errs.Wrap(err, "could not read directory %s", src)
}
for _, entry := range entries {
if err := Link(filepath.Join(src, entry.Name()), filepath.Join(dest, entry.Name())); err != nil {
if err := Link(filepath.Join(src, entry.Name()), filepath.Join(dest, entry.Name()), visited); err != nil {
return errs.Wrap(err, "sub link failed")
}
}
Expand Down
73 changes: 73 additions & 0 deletions internal/smartlink/smartlink_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package smartlink

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestLinkContentsWithCircularLink(t *testing.T) {
mitchell-as marked this conversation as resolved.
Show resolved Hide resolved
srcDir, err := os.MkdirTemp("", "src")
require.NoError(t, err)
defer os.RemoveAll(srcDir)

destDir, err := os.MkdirTemp("", "dest")
require.NoError(t, err)
defer os.RemoveAll(destDir)

// Create test file structure:
// src/
// ├── regular.txt
// └── subdir/
// ├── circle -> subdir (circular link)
// └── subfile.txt

testFile := filepath.Join(srcDir, "regular.txt")
err = os.WriteFile(testFile, []byte("test content"), 0644)
require.NoError(t, err)

subDir := filepath.Join(srcDir, "subdir")
err = os.Mkdir(subDir, 0755)
require.NoError(t, err)

subFile := filepath.Join(subDir, "subfile.txt")
err = os.WriteFile(subFile, []byte("sub content"), 0644)
require.NoError(t, err)

circularLink := filepath.Join(subDir, "circle")
err = os.Symlink(subDir, circularLink)
require.NoError(t, err)

err = LinkContents(srcDir, destDir)
require.NoError(t, err)

// Verify file structure.
// src/
// ├── regular.txt
// └── subdir/
// ├── circle
// │ │ (no subdir/)
// │ └── subfile.txt
// └── subfile.txt
destFile := filepath.Join(destDir, "regular.txt")
require.FileExists(t, destFile)
content, err := os.ReadFile(destFile)
require.NoError(t, err)
require.Equal(t, "test content", string(content))

destSubFile := filepath.Join(destDir, "subdir", "subfile.txt")
require.FileExists(t, destSubFile)
subContent, err := os.ReadFile(destSubFile)
require.NoError(t, err)
require.Equal(t, "sub content", string(subContent))

require.NoDirExists(t, filepath.Join(destDir, "subdir", "circle", "circle"))

destCircularSubFile := filepath.Join(destDir, "subdir", "circle", "subfile.txt")
require.FileExists(t, destCircularSubFile)
subContent, err = os.ReadFile(destCircularSubFile)
require.NoError(t, err)
require.Equal(t, "sub content", string(subContent))
}
2 changes: 1 addition & 1 deletion pkg/runtime/depot.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func (d *depot) Undeploy(id strfmt.UUID, relativeSrc, path string) error {
for sharedFile, relinkSrc := range redeploys {
switch deploy.Type {
case deploymentTypeLink:
if err := smartlink.Link(relinkSrc, sharedFile); err != nil {
if err := smartlink.Link(relinkSrc, sharedFile, nil); err != nil {
return errs.Wrap(err, "failed to relink file")
}
case deploymentTypeCopy:
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/links_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func supportsHardLinks(path string) (supported bool) {
}

logging.Debug("Attempting to link '%s' to '%s'", lnk, target)
err = smartlink.Link(target, lnk)
err = smartlink.Link(target, lnk, nil)
if err != nil {
logging.Debug("Test link creation failed: %v", err)
return false
Expand Down
Loading