-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
73 lines (65 loc) · 1.16 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package fs
import (
"io"
"os"
)
func Append(p string, b []byte) error {
f, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return err
}
_, err = f.Write(b)
if err2 := f.Close(); err == nil {
err = err2
}
return err
}
func OpenAppend(p string) (*os.File, error) {
return os.OpenFile(p, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
}
func Copy(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
info, err := os.Stat(src)
if err != nil {
return
}
out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, info.Mode())
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
return
}
func CopyWithMode(src, dst string, mode os.FileMode) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
return
}