]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | // +build !windows |
2 | ||
3 | package getter | |
4 | ||
5 | import ( | |
6 | "fmt" | |
bae9f6d2 JC |
7 | "net/url" |
8 | "os" | |
9 | "path/filepath" | |
10 | ) | |
11 | ||
12 | func (g *FileGetter) Get(dst string, u *url.URL) error { | |
13 | path := u.Path | |
14 | if u.RawPath != "" { | |
15 | path = u.RawPath | |
16 | } | |
17 | ||
18 | // The source path must exist and be a directory to be usable. | |
19 | if fi, err := os.Stat(path); err != nil { | |
20 | return fmt.Errorf("source path error: %s", err) | |
21 | } else if !fi.IsDir() { | |
22 | return fmt.Errorf("source path must be a directory") | |
23 | } | |
24 | ||
25 | fi, err := os.Lstat(dst) | |
26 | if err != nil && !os.IsNotExist(err) { | |
27 | return err | |
28 | } | |
29 | ||
30 | // If the destination already exists, it must be a symlink | |
31 | if err == nil { | |
32 | mode := fi.Mode() | |
33 | if mode&os.ModeSymlink == 0 { | |
34 | return fmt.Errorf("destination exists and is not a symlink") | |
35 | } | |
36 | ||
37 | // Remove the destination | |
38 | if err := os.Remove(dst); err != nil { | |
39 | return err | |
40 | } | |
41 | } | |
42 | ||
43 | // Create all the parent directories | |
44 | if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil { | |
45 | return err | |
46 | } | |
47 | ||
48 | return os.Symlink(path, dst) | |
49 | } | |
50 | ||
51 | func (g *FileGetter) GetFile(dst string, u *url.URL) error { | |
107c1cdb | 52 | ctx := g.Context() |
bae9f6d2 JC |
53 | path := u.Path |
54 | if u.RawPath != "" { | |
55 | path = u.RawPath | |
56 | } | |
57 | ||
58 | // The source path must exist and be a file to be usable. | |
59 | if fi, err := os.Stat(path); err != nil { | |
60 | return fmt.Errorf("source path error: %s", err) | |
61 | } else if fi.IsDir() { | |
62 | return fmt.Errorf("source path must be a file") | |
63 | } | |
64 | ||
65 | _, err := os.Lstat(dst) | |
66 | if err != nil && !os.IsNotExist(err) { | |
67 | return err | |
68 | } | |
69 | ||
70 | // If the destination already exists, it must be a symlink | |
71 | if err == nil { | |
72 | // Remove the destination | |
73 | if err := os.Remove(dst); err != nil { | |
74 | return err | |
75 | } | |
76 | } | |
77 | ||
78 | // Create all the parent directories | |
79 | if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil { | |
80 | return err | |
81 | } | |
82 | ||
83 | // If we're not copying, just symlink and we're done | |
84 | if !g.Copy { | |
85 | return os.Symlink(path, dst) | |
86 | } | |
87 | ||
88 | // Copy | |
89 | srcF, err := os.Open(path) | |
90 | if err != nil { | |
91 | return err | |
92 | } | |
93 | defer srcF.Close() | |
94 | ||
95 | dstF, err := os.Create(dst) | |
96 | if err != nil { | |
97 | return err | |
98 | } | |
99 | defer dstF.Close() | |
100 | ||
107c1cdb | 101 | _, err = Copy(ctx, dstF, srcF) |
bae9f6d2 JC |
102 | return err |
103 | } |