-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpatch.go
45 lines (41 loc) · 1.4 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
// Copyright 2015 Monmohan Singh. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xferspdy
import (
"github.com/golang/glog"
"io"
"os"
)
//Patch is a wrapper on PatchFile (current version only supports patching of local files)
func Patch(delta []Block, sign Fingerprint, t io.Writer) {
PatchFile(delta, sign.Source, t)
}
// PatchFile takes a source file and Diff as input, and writes out to the Writer.
// The source file would normally be the base version of the file and
// the Diff is the delta computed by using the Fingerprint generated for the base file and the new version of the file
func PatchFile(delta []Block, source string, t io.Writer) error {
s, e := os.Open(source)
defer s.Close()
wptr := int64(0)
for _, block := range delta {
if block.HasData {
glog.V(3).Infof("Writing RawBytes block , wptr=%v , num bytes = %v \n", wptr, len(block.RawBytes))
_, e = t.Write(block.RawBytes)
glog.V(4).Infof("Writing bytes = %v \n", block.RawBytes)
if e != nil {
glog.Fatal(e)
}
wptr += int64(len(block.RawBytes))
} else {
s.Seek(block.Start, 0)
ds := block.End - block.Start
glog.V(3).Infof("Writing RawBytes block, Block=%v\n , wptr=%v , num bytes = %v \n", block, wptr, ds)
if _, e := io.CopyN(t, s, block.End-block.Start); e != nil {
return e
}
wptr += ds
}
}
return nil
}