Skip to content

Commit fed0409

Browse files
Ensure that CopyDirTo really ignores symlinks
There was some code that claimed to ignore symlinks, but: - It would run *after* recursive directory copying, so any symlinks to directories would still be copied (potentially creating a destination directory that is a lot bigger than the original) - It checked for the symlink bit in the result of Stat(), but that resolves the symlink and returns the mode bits of the target, so that code would never fire. This fixes both problems by moving the check for symlinks up, and using Lstat() instead of Stat(), so symlinks are ignored as intended.
1 parent 753b6dd commit fed0409

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

paths.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,25 +435,25 @@ func (p *Path) CopyDirTo(dst *Path) error {
435435
}
436436

437437
for _, srcPath := range srcFiles {
438-
srcPathInfo, err := srcPath.Stat()
438+
srcPathInfo, err := srcPath.Lstat()
439439
if err != nil {
440440
return fmt.Errorf("getting stat info for %s: %s", srcPath, err)
441441
}
442-
dstPath := dst.Join(srcPath.Base())
443442

443+
// Skip symlinks.
444+
if srcPathInfo.Mode()&os.ModeSymlink != 0 {
445+
// TODO
446+
continue
447+
}
448+
449+
dstPath := dst.Join(srcPath.Base())
444450
if srcPathInfo.IsDir() {
445451
if err := srcPath.CopyDirTo(dstPath); err != nil {
446452
return fmt.Errorf("copying %s to %s: %s", srcPath, dstPath, err)
447453
}
448454
continue
449455
}
450456

451-
// Skip symlinks.
452-
if srcPathInfo.Mode()&os.ModeSymlink != 0 {
453-
// TODO
454-
continue
455-
}
456-
457457
if err := srcPath.CopyTo(dstPath); err != nil {
458458
return fmt.Errorf("copying %s to %s: %s", srcPath, dstPath, err)
459459
}

paths_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ func TestCopyDir(t *testing.T) {
243243
require.False(t, isdir)
244244
require.NoError(t, err)
245245

246+
exist, err = tmp.Join("dest", "symlinktofolder").ExistCheck()
247+
require.False(t, exist)
248+
require.NoError(t, err)
249+
246250
err = src.CopyDirTo(tmp.Join("dest"))
247251
require.Error(t, err, "copying dir to already existing")
248252

0 commit comments

Comments
 (0)