15 func (g *FileGetter) Get(dst string, u *url.URL) error {
22 // The source path must exist and be a directory to be usable.
23 if fi, err := os.Stat(path); err != nil {
24 return fmt.Errorf("source path error: %s", err)
25 } else if !fi.IsDir() {
26 return fmt.Errorf("source path must be a directory")
29 fi, err := os.Lstat(dst)
30 if err != nil && !os.IsNotExist(err) {
34 // If the destination already exists, it must be a symlink
37 if mode&os.ModeSymlink == 0 {
38 return fmt.Errorf("destination exists and is not a symlink")
41 // Remove the destination
42 if err := os.Remove(dst); err != nil {
47 // Create all the parent directories
48 if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
52 sourcePath := toBackslash(path)
54 // Use mklink to create a junction point
55 output, err := exec.CommandContext(ctx, "cmd", "/c", "mklink", "/J", dst, sourcePath).CombinedOutput()
57 return fmt.Errorf("failed to run mklink %v %v: %v %q", dst, sourcePath, err, output)
63 func (g *FileGetter) GetFile(dst string, u *url.URL) error {
70 // The source path must exist and be a directory to be usable.
71 if fi, err := os.Stat(path); err != nil {
72 return fmt.Errorf("source path error: %s", err)
73 } else if fi.IsDir() {
74 return fmt.Errorf("source path must be a file")
77 _, err := os.Lstat(dst)
78 if err != nil && !os.IsNotExist(err) {
82 // If the destination already exists, it must be a symlink
84 // Remove the destination
85 if err := os.Remove(dst); err != nil {
90 // Create all the parent directories
91 if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
95 // If we're not copying, just symlink and we're done
97 if err = os.Symlink(path, dst); err == nil {
100 lerr, ok := err.(*os.LinkError)
105 case syscall.ERROR_PRIVILEGE_NOT_HELD:
106 // no symlink privilege, let's
107 // fallback to a copy to avoid an error.
115 srcF, err := os.Open(path)
121 dstF, err := os.Create(dst)
127 _, err = Copy(ctx, dstF, srcF)
131 // toBackslash returns the result of replacing each slash character
132 // in path with a backslash ('\') character. Multiple separators are
133 // replaced by multiple backslashes.
134 func toBackslash(path string) string {
135 return strings.Replace(path, "/", "\\", -1)