From e11f761ecea3e44a632fd92e824a8c88fc5ac1ae Mon Sep 17 00:00:00 2001 From: icedream Date: Sun, 26 Feb 2017 02:23:44 +0100 Subject: [PATCH] Add raw subpackage. --- raw/diff/diff.go | 29 +++++++++++++++++++++++++++++ raw/doc.go | 5 +++++ raw/patch/patch.go | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 raw/diff/diff.go create mode 100644 raw/doc.go create mode 100644 raw/patch/patch.go diff --git a/raw/diff/diff.go b/raw/diff/diff.go new file mode 100644 index 0000000..2a585b3 --- /dev/null +++ b/raw/diff/diff.go @@ -0,0 +1,29 @@ +package diff + +import ( + "io" + "io/ioutil" + + "github.com/icedream/go-bsdiff/internal/native" +) + +/* +Diff generates a raw patch from old content that will be read in completely from +oldReader and new content that will be read in completely from newReader and +saves that patch to patchWriter. + +It may be helpful to save away the new content size along with the actual +patch as it will be needed in order to reuse the patch. +*/ +func Diff(oldReader, newReader io.Reader, patchWriter io.Writer) (err error) { + oldBytes, err := ioutil.ReadAll(oldReader) + if err != nil { + return + } + newBytes, err := ioutil.ReadAll(newReader) + if err != nil { + return + } + + return native.Diff(oldBytes, newBytes, patchWriter) +} diff --git a/raw/doc.go b/raw/doc.go new file mode 100644 index 0000000..da5810c --- /dev/null +++ b/raw/doc.go @@ -0,0 +1,5 @@ +/* +This subpackage directly exposes bsdiff functionality without any +file format or compression code wrapped around it. +*/ +package raw diff --git a/raw/patch/patch.go b/raw/patch/patch.go new file mode 100644 index 0000000..7aa6f27 --- /dev/null +++ b/raw/patch/patch.go @@ -0,0 +1,30 @@ +package patch + +import ( + "io" + "io/ioutil" + + "github.com/icedream/go-bsdiff/internal/native" +) + +/* +Patch reads a raw patch from patchReader and applies it on top of the old +content which will be read from oldReader, saving the resulting new content to +newWriter. + +newSize needs to be exactly the size of the new file that should be generated +from the patch. +*/ +func Patch(oldReader io.Reader, newWriter io.Writer, patchReader io.Reader, newSize uint64) (err error) { + oldBytes, err := ioutil.ReadAll(oldReader) + if err != nil { + return + } + + newBytes := make([]byte, newSize) + + err = native.Patch(oldBytes, newBytes, oldReader) + + newWriter.Write(newBytes) + return +}