/
patch.go
106 lines (87 loc) · 2.17 KB
/
patch.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"os"
"runtime"
gosync_main "github.com/Redundancy/go-sync"
"github.com/urfave/cli/v2"
)
const usage = "gosync patch <localfile> <reference index> <reference source> [<output>]"
func init() {
app.Commands = append(
app.Commands,
&cli.Command{
Name: "patch",
Aliases: []string{"p"},
Usage: usage,
Description: `Recreate the reference source file, using an index and a local file that is believed to be similar.
The index should be produced by "gosync build".
<reference index> is a .gosync file and may be a local, unc network path or http/https url
<reference source> is corresponding target and may be a local, unc network path or http/https url
<output> is optional. If not specified, the local file will be overwritten when done.`,
Action: Patch,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "p",
Value: runtime.NumCPU(),
Usage: "The number of streams to use concurrently",
},
},
},
)
}
// Patch a file
func Patch(c *cli.Context) error {
errorWrapper(c, func(c *cli.Context) error {
fmt.Fprintln(os.Stderr, "Starting patching process")
if l := c.Args().Len(); l < 3 || l > 4 {
return fmt.Errorf(
"Usage is \"%v\" (invalid number of arguments)",
usage,
)
}
localFilename := c.Args().Get(0)
summaryFile := c.Args().Get(1)
referencePath := c.Args().Get(2)
outFilename := localFilename
if c.Args().Len() == 4 {
outFilename = c.Args().Get(3)
}
indexReader, e := os.Open(summaryFile)
if e != nil {
return e
}
defer indexReader.Close()
_, _, _, filesize, blocksize, e := readHeadersAndCheck(
indexReader,
magicString,
majorVersion,
)
index, checksumLookup, blockCount, err := readIndex(
indexReader,
uint(blocksize),
)
fs := &gosync_main.BasicSummary{
ChecksumIndex: index,
ChecksumLookup: checksumLookup,
BlockCount: blockCount,
BlockSize: uint(blocksize),
FileSize: filesize,
}
rsync, err := gosync_main.MakeRSync(
localFilename,
referencePath,
outFilename,
fs,
)
if err != nil {
return err
}
err = rsync.Patch()
if err != nil {
return err
}
return rsync.Close()
})
return nil
}