-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfshelper.go
54 lines (47 loc) · 1.06 KB
/
fshelper.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
package uklam
import (
"io"
"os"
"github.com/eaciit/toolkit"
)
/*FSCopy copy file from source to dst*/
func FSCopy(source, dst string, ismove bool) error {
fsource, e := os.Open(source)
if e != nil {
return toolkit.Errorf("FSCopy: %s %s", source, e.Error())
}
defer fsource.Close()
fdst, e := os.Create(dst)
if e != nil {
os.Remove(dst)
fdst, e = os.Create(dst)
}
if e != nil {
return toolkit.Errorf("FSCopy: %s %s", dst, e.Error())
}
defer fdst.Close()
_, e = io.Copy(fdst, fsource)
if e != nil {
return toolkit.Errorf("FSCopy: Fail to copy %s", e.Error())
}
e = fdst.Sync()
if e != nil {
return toolkit.Errorf("FSCopy: Fail to sync %s", e.Error())
}
if ismove {
fsource.Close()
e = os.Remove(source)
if e != nil {
return toolkit.Errorf("FSCopy: Fail to remove source %s", e.Error())
}
}
return nil
}
func FSWalkFn(dw IDataWalker, in toolkit.M) *toolkit.Result {
r := toolkit.NewResult()
infos := in.Get("data", []os.FileInfo{}).([]os.FileInfo)
for _, info := range infos {
dw.(*FSWalker).EachFn(dw, in, info, r)
}
return r
}