-
Notifications
You must be signed in to change notification settings - Fork 77
/
file_sync.go
53 lines (48 loc) · 1.08 KB
/
file_sync.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
package common
import (
//"github.com/Redundancy/go-sync"
"log"
"os"
"os/exec"
"path/filepath"
)
var runningCh chan struct{}
func init() {
runningCh = make(chan struct{}, 2)
}
func RunFileSync(remote string, srcPath string, dstPath string, stopCh chan struct{}) error {
select {
case runningCh <- struct{}{}:
case <-stopCh:
return ErrStopped
}
defer func() {
select {
case <-runningCh:
default:
}
}()
var cmd *exec.Cmd
if filepath.Base(srcPath) == filepath.Base(dstPath) {
dir := filepath.Dir(dstPath)
os.MkdirAll(dir, DIR_PERM)
} else {
os.MkdirAll(dstPath, DIR_PERM)
}
if remote == "" {
log.Printf("copy local :%v to %v\n", srcPath, dstPath)
cmd = exec.Command("cp", "-rp", srcPath, dstPath)
} else {
log.Printf("copy from remote :%v/%v to local: %v\n", remote, srcPath, dstPath)
// limit rate in kilobytes
cmd = exec.Command("rsync", "-avP", "--bwlimit=25600",
"rsync://"+remote+"/"+srcPath, dstPath)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
}
err := cmd.Run()
if err != nil {
log.Printf("cmd %v error: %v", cmd, err)
}
return err
}