This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
/
rsync.go
72 lines (68 loc) · 1.93 KB
/
rsync.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
package main
import (
"fmt"
"gopkg.in/fatih/set.v0"
"log"
"os"
"path/filepath"
"strings"
)
// copyFileFromRemote copies one file from remote server to local disk folder.
func copyFileFromRemote(ctx *context, file string) error {
source := fmt.Sprintf("%s%s", ctx.server, file)
// Ex: $HOME/temp/blast/db
log.Print("Local dir to make: " + ctx.temp + filepath.Dir(file))
err := ctx.os.MkdirAll(ctx.temp+filepath.Dir(file), os.ModePerm)
if err != nil {
return handle("Couldn't make dir.", err)
}
// Ex: $HOME/temp/blast/db/README
dest := fmt.Sprintf("%s%s", ctx.temp, file)
//template := "rsync -arzv --inplace --size-only --no-motd --progress " +
// "--copy-links %s %s"
template := "wget -S -nv -O %s %s"
cmd := fmt.Sprintf(template, dest, source)
_, _, err = commandVerbose(cmd)
if err != nil {
return handle("Couldn't rsync file to local disk.", err)
}
return err
}
// listFromRsync gets a list of inspected files from rsync. Used to leverage
// rsync's built-in filtering capabilities.
func listFromRsync(out string, base string) *set.Set {
res := set.New()
lines := strings.Split(out, "\n")
if len(lines) < 5 {
return res
}
lines = lines[1 : len(lines)-4] // Remove junk lines
for _, line := range lines {
if !strings.Contains(line, " ") {
continue
}
col := strings.SplitN(line, " ", 2)
file := col[1]
path := base + "/" + file
res.Add(path)
}
return res
}
// extractSubfolders parses rsync's stdout and extracts a set of sub-folders
// that were recursively inspected. Used to make FTP listing calls more
// efficient per sub-directory.
func extractSubfolders(out string, base string) *set.Set {
res := set.New()
lines := strings.Split(out, "\n")
if len(lines) < 5 {
return res
}
lines = lines[2 : len(lines)-4] // Remove junk lines
for _, line := range lines {
col := strings.SplitN(line, " ", 2)
file := col[1]
dir := filepath.Dir(base + "/" + file) // Grab the folder path
res.Add(dir)
}
return res
}