15 func (g *FileGetter) Get(dst string, u *url.URL) error {
21 // The source path must exist and be a directory to be usable.
22 if fi, err := os.Stat(path); err != nil {
23 return fmt.Errorf("source path error: %s", err)
24 } else if !fi.IsDir() {
25 return fmt.Errorf("source path must be a directory")
28 fi, err := os.Lstat(dst)
29 if err != nil && !os.IsNotExist(err) {
33 // If the destination already exists, it must be a symlink
36 if mode&os.ModeSymlink == 0 {
37 return fmt.Errorf("destination exists and is not a symlink")
40 // Remove the destination
41 if err := os.Remove(dst); err != nil {
46 // Create all the parent directories
47 if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
51 sourcePath := toBackslash(path)
53 // Use mklink to create a junction point
54 output, err := exec.Command("cmd", "/c", "mklink", "/J", dst, sourcePath).CombinedOutput()
56 return fmt.Errorf("failed to run mklink %v %v: %v %q", dst, sourcePath, err, output)
62 func (g *FileGetter) GetFile(dst string, u *url.URL) error {
68 // The source path must exist and be a directory to be usable.
69 if fi, err := os.Stat(path); err != nil {
70 return fmt.Errorf("source path error: %s", err)
71 } else if fi.IsDir() {
72 return fmt.Errorf("source path must be a file")
75 _, err := os.Lstat(dst)
76 if err != nil && !os.IsNotExist(err) {
80 // If the destination already exists, it must be a symlink
82 // Remove the destination
83 if err := os.Remove(dst); err != nil {
88 // Create all the parent directories
89 if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
93 // If we're not copying, just symlink and we're done
95 return os.Symlink(path, dst)
99 srcF, err := os.Open(path)
105 dstF, err := os.Create(dst)
111 _, err = io.Copy(dstF, srcF)
115 // toBackslash returns the result of replacing each slash character
116 // in path with a backslash ('\') character. Multiple separators are
117 // replaced by multiple backslashes.
118 func toBackslash(path string) string {
119 return strings.Replace(path, "/", "\\", -1)