Skip to content

Commit

Permalink
Preserve symlinks when copying files (#3336)
Browse files Browse the repository at this point in the history
* Preserve symlinks when copying files

* Preserve symlinks

* Release a patch:

* Update src/fs/copy.go

Co-authored-by: Samuel Littley <[email protected]>

* debug nonesense

* Lint

* Lint

---------

Co-authored-by: Samuel Littley <[email protected]>
  • Loading branch information
Tatskaari and toastwaffle authored Jan 22, 2025
1 parent 4a39ba3 commit 674b695
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
5 changes: 4 additions & 1 deletion ChangeLog
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
Version 17.13.1
---------------
* Preserve symlinks when copying files

Version 17.13.0
---------------
* Add a `break` statement to the build language (#3320)
* Handle symlinks to directories in a number of places, including but not limited
to output directories in remote execution (#3330)
* Make output target display not have local targets hanging around so much (#3276)


Version 17.12.7
---------------
* Fix BuildTarget.ProvideFor not checking named data when deciding whether
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
17.13.0
17.13.1
14 changes: 4 additions & 10 deletions src/fs/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,21 @@ func RecursiveCopyOrLinkFile(from string, to string, mode os.FileMode, link, fal
return os.MkdirAll(dest, DirPermissions)
}
if fileMode.IsSymlink() {
return recursiveCopyOrLinkSymlink(name, dest, mode, link, fallback)
return copySymlink(name, dest)
}
return CopyOrLinkFile(name, dest, fileMode.ModeType(), mode, link, fallback)
})
}
return CopyOrLinkFile(from, to, info.Mode(), mode, link, fallback)
}

// recursiveCopyOrLinkSymlink will resolve the symlink, and recursively copy the content to the destination
func recursiveCopyOrLinkSymlink(name, dest string, mode os.FileMode, link, fallback bool) error {
// copySymlink will resolve the symlink and create an equivalent symlink at dest. Assumes the symlink is relative, not absolute.
func copySymlink(name, dest string) error {
resolvedPath, err := os.Readlink(name)
if err != nil {
return err
}
if !filepath.IsAbs(resolvedPath) {
resolvedPath = filepath.Join(filepath.Dir(name), resolvedPath)
}
if err := RecursiveCopyOrLinkFile(resolvedPath, dest, mode, link, fallback); err != nil {
return err
}
return nil
return os.Symlink(resolvedPath, dest)
}

type LinkFunc func(string, string) error
Expand Down

0 comments on commit 674b695

Please sign in to comment.