]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/go-getter/get_file_windows.go
Initial transfer of provider code
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / go-getter / get_file_windows.go
1 // +build windows
2
3 package getter
4
5 import (
6 "fmt"
7 "io"
8 "net/url"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "strings"
13 )
14
15 func (g *FileGetter) Get(dst string, u *url.URL) error {
16 path := u.Path
17 if u.RawPath != "" {
18 path = u.RawPath
19 }
20
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")
26 }
27
28 fi, err := os.Lstat(dst)
29 if err != nil && !os.IsNotExist(err) {
30 return err
31 }
32
33 // If the destination already exists, it must be a symlink
34 if err == nil {
35 mode := fi.Mode()
36 if mode&os.ModeSymlink == 0 {
37 return fmt.Errorf("destination exists and is not a symlink")
38 }
39
40 // Remove the destination
41 if err := os.Remove(dst); err != nil {
42 return err
43 }
44 }
45
46 // Create all the parent directories
47 if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
48 return err
49 }
50
51 sourcePath := toBackslash(path)
52
53 // Use mklink to create a junction point
54 output, err := exec.Command("cmd", "/c", "mklink", "/J", dst, sourcePath).CombinedOutput()
55 if err != nil {
56 return fmt.Errorf("failed to run mklink %v %v: %v %q", dst, sourcePath, err, output)
57 }
58
59 return nil
60 }
61
62 func (g *FileGetter) GetFile(dst string, u *url.URL) error {
63 path := u.Path
64 if u.RawPath != "" {
65 path = u.RawPath
66 }
67
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")
73 }
74
75 _, err := os.Lstat(dst)
76 if err != nil && !os.IsNotExist(err) {
77 return err
78 }
79
80 // If the destination already exists, it must be a symlink
81 if err == nil {
82 // Remove the destination
83 if err := os.Remove(dst); err != nil {
84 return err
85 }
86 }
87
88 // Create all the parent directories
89 if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
90 return err
91 }
92
93 // If we're not copying, just symlink and we're done
94 if !g.Copy {
95 return os.Symlink(path, dst)
96 }
97
98 // Copy
99 srcF, err := os.Open(path)
100 if err != nil {
101 return err
102 }
103 defer srcF.Close()
104
105 dstF, err := os.Create(dst)
106 if err != nil {
107 return err
108 }
109 defer dstF.Close()
110
111 _, err = io.Copy(dstF, srcF)
112 return err
113 }
114
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)
120 }