forked from zlowram/blgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
60 lines (50 loc) · 1.07 KB
/
utils.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
package main
import (
"io"
"log"
"os"
)
func copyFile(src string, dest string) error {
sourcefile, err := os.Open(src)
if err != nil {
return err
}
defer sourcefile.Close()
destfile, err := os.Create(dest)
if err != nil {
return err
}
defer destfile.Close()
if _, err = io.Copy(destfile, sourcefile); err == nil {
sourceinfo, err := os.Stat(src)
if err != nil {
err = os.Chmod(dest, sourceinfo.Mode())
}
}
return err
}
func copyDir(src string, dest string) error {
sourceinfo, err := os.Stat(src)
if err != nil {
return err
}
if err = os.MkdirAll(dest, sourceinfo.Mode()); err != nil {
return err
}
directory, _ := os.Open(src)
objects, err := directory.Readdir(-1)
for _, obj := range objects {
sourcefilepointer := src + "/" + obj.Name()
destinationfilepointer := dest + "/" + obj.Name()
if obj.IsDir() {
if err = copyDir(sourcefilepointer, destinationfilepointer); err != nil {
log.Fatal(err)
}
} else {
if err = copyFile(sourcefilepointer, destinationfilepointer); err != nil {
log.Fatal(err)
}
}
}
return err
}