-
Notifications
You must be signed in to change notification settings - Fork 13
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
Changes from 5 commits
d33a4d0
da5fee5
9dd2ff9
f0c7784
b75482b
d1a7e18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -47,6 +47,13 @@ func Link(src, dest string) error { | |||||||||
} | ||||||||||
|
||||||||||
if fileutils.IsDir(src) { | ||||||||||
if isSymlink(src) { | ||||||||||
// Links to directories are okay on Linux and macOS, but will fail on Windows. | ||||||||||
// If we ever get here on Windows, the artifact being deployed is bad and there's nothing we | ||||||||||
// can do about it except receive the report from Rollbar and report it internally. | ||||||||||
return linkFile(src, dest) | ||||||||||
} | ||||||||||
|
||||||||||
if err := fileutils.Mkdir(dest); err != nil { | ||||||||||
return errs.Wrap(err, "could not create directory %s", dest) | ||||||||||
} | ||||||||||
|
@@ -131,15 +138,20 @@ func UnlinkContents(src, dest string) error { | |||||||||
return nil | ||||||||||
} | ||||||||||
|
||||||||||
func isSymlink(src string) bool { | ||||||||||
target, err := fileutils.SymlinkTarget(src) | ||||||||||
return err == nil && src != target | ||||||||||
} | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
// resolvePaths will resolve src and dest to absolute paths and return them. | ||||||||||
// This is to ensure that we're always comparing apples to apples when doing string comparisons on paths. | ||||||||||
func resolvePaths(src, dest string) (string, string, error) { | ||||||||||
var err error | ||||||||||
src, err = fileutils.ResolveUniquePath(src) | ||||||||||
src, err = filepath.Abs(filepath.Clean(src)) | ||||||||||
if err != nil { | ||||||||||
return "", "", errs.Wrap(err, "Could not resolve src path") | ||||||||||
} | ||||||||||
dest, err = fileutils.ResolveUniquePath(dest) | ||||||||||
dest, err = filepath.Abs(filepath.Clean(dest)) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please revert. Absolute path does not give a canonical result, whereas ResolveUniquePath does (or at least -tries to-). |
||||||||||
if err != nil { | ||||||||||
return "", "", errs.Wrap(err, "Could not resolve dest path") | ||||||||||
} | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package smartlink | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/ActiveState/cli/internal/fileutils" | ||
"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) | ||
if runtime.GOOS == "windows" { | ||
require.Error(t, err) | ||
return // hard links to directories are not allowed on Windows | ||
} | ||
require.NoError(t, err) | ||
|
||
// Verify file structure. | ||
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)) | ||
|
||
destCircular := filepath.Join(destDir, "subdir", "circle") | ||
require.FileExists(t, destCircular) | ||
target, err := fileutils.ResolveUniquePath(destCircular) | ||
require.NoError(t, err) | ||
srcCircular := filepath.Join(srcDir, "subdir") | ||
if runtime.GOOS == "darwin" { | ||
srcCircular, err = fileutils.ResolveUniquePath(srcCircular) // needed for full $TMPDIR resolution | ||
require.NoError(t, err) | ||
} | ||
require.Equal(t, target, srcCircular) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.