Skip to content

Commit

Permalink
internal/fs: don't clone symlinks on windows
Browse files Browse the repository at this point in the history
copyFile calls copySymlink on Windows which fails if the user doesn't
have the required permission. This is a very common case since symlinks
are used heavily on Windows.

This change renames copySymlink to cloneSymlink to clarify the intention
and skips calling it when on Windows to fallback to copy the file
content instead.

Fixes golang#773

Signed-off-by: Ibrahim AshShohail <[email protected]>
  • Loading branch information
ibrasho committed Jun 21, 2017
1 parent c79b048 commit c33d65f
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions internal/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"unicode"

Expand Down Expand Up @@ -269,12 +270,15 @@ func CopyDir(src, dst string) error {
// of the source file. The file mode will be copied from the source and
// the copied data is synced/flushed to stable storage.
func copyFile(src, dst string) (err error) {
if sym, err := IsSymlink(src); err != nil {
return err
} else if sym {
err := copySymlink(src, dst)
sym, err := IsSymlink(src)
if err != nil {
return err
}
// Skip cloning the symlink on Windows and fallback to copying the file content
// since creating a symlink on Windows requires admin permissions.
if sym && runtime.GOOS != "windows" {
return cloneSymlink(src, dst)
}

in, err := os.Open(src)
if err != nil {
Expand Down Expand Up @@ -314,17 +318,17 @@ func copyFile(src, dst string) (err error) {
return
}

// copySymlink will resolve the src symlink and create a new symlink in dst.
// If src is a relative symlink, dst will also be a relative symlink.
func copySymlink(src, dst string) error {
resolved, err := os.Readlink(src)
// cloneSymlink will resolve sl and create a new symlink in dst.
// If sl is a relative symlink, dst will also be a relative symlink.
func cloneSymlink(sl, dst string) error {
resolved, err := os.Readlink(sl)
if err != nil {
return errors.Wrap(err, "failed to resolve symlink")
}

err = os.Symlink(resolved, dst)
if err != nil {
return errors.Wrapf(err, "failed to create symlink %s to %s", src, resolved)
return errors.Wrapf(err, "failed to create symlink %s to %s", dst, resolved)
}

return nil
Expand Down

0 comments on commit c33d65f

Please sign in to comment.