forked from thatguystone/cog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
52 lines (42 loc) · 867 Bytes
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package cfs
import (
"io"
"io/ioutil"
"os"
)
// Create creates the given file by ensuring that is parent directories exist,
// then it creates the file.
func Create(path string) (*os.File, error) {
err := CreateParents(path)
if err != nil {
return nil, err
}
return os.Create(path)
}
// Write creates the file at the given path (using Create()), then writes the
// given contents to the file.
func Write(path string, c []byte) error {
err := CreateParents(path)
if err != nil {
return err
}
return ioutil.WriteFile(path, c, 0640)
}
// Copy copies a file from src to dst.
func Copy(src, dst string) error {
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()
err = CreateParents(dst)
if err != nil {
return err
}
d, err := os.Create(dst)
if err == nil {
defer d.Close()
_, err = io.Copy(d, s)
}
return err
}