Skip to content

Commit e11f761

Browse files
committed
Add raw subpackage.
1 parent 0a7b37e commit e11f761

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

raw/diff/diff.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package diff
2+
3+
import (
4+
"io"
5+
"io/ioutil"
6+
7+
"github.com/icedream/go-bsdiff/internal/native"
8+
)
9+
10+
/*
11+
Diff generates a raw patch from old content that will be read in completely from
12+
oldReader and new content that will be read in completely from newReader and
13+
saves that patch to patchWriter.
14+
15+
It may be helpful to save away the new content size along with the actual
16+
patch as it will be needed in order to reuse the patch.
17+
*/
18+
func Diff(oldReader, newReader io.Reader, patchWriter io.Writer) (err error) {
19+
oldBytes, err := ioutil.ReadAll(oldReader)
20+
if err != nil {
21+
return
22+
}
23+
newBytes, err := ioutil.ReadAll(newReader)
24+
if err != nil {
25+
return
26+
}
27+
28+
return native.Diff(oldBytes, newBytes, patchWriter)
29+
}

raw/doc.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
This subpackage directly exposes bsdiff functionality without any
3+
file format or compression code wrapped around it.
4+
*/
5+
package raw

raw/patch/patch.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package patch
2+
3+
import (
4+
"io"
5+
"io/ioutil"
6+
7+
"github.com/icedream/go-bsdiff/internal/native"
8+
)
9+
10+
/*
11+
Patch reads a raw patch from patchReader and applies it on top of the old
12+
content which will be read from oldReader, saving the resulting new content to
13+
newWriter.
14+
15+
newSize needs to be exactly the size of the new file that should be generated
16+
from the patch.
17+
*/
18+
func Patch(oldReader io.Reader, newWriter io.Writer, patchReader io.Reader, newSize uint64) (err error) {
19+
oldBytes, err := ioutil.ReadAll(oldReader)
20+
if err != nil {
21+
return
22+
}
23+
24+
newBytes := make([]byte, newSize)
25+
26+
err = native.Patch(oldBytes, newBytes, oldReader)
27+
28+
newWriter.Write(newBytes)
29+
return
30+
}

0 commit comments

Comments
 (0)