|
| 1 | +# bsdiff for Go |
| 2 | + |
| 3 | +This wrapper implementation for Golang reuses the existing |
| 4 | +C version of bsdiff as provided by @mendsley and wraps it |
| 5 | +into a Go package, abstracting away all the cgo work that |
| 6 | +would need to be done otherwise. |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | +The library and the helper binaries `go-bsdiff` and `go-bspatch` can be installed like this: |
| 11 | + |
| 12 | + go get -v github.com/icedream/go-bsdiff/... |
| 13 | + |
| 14 | +## Usage in application code |
| 15 | + |
| 16 | +For exact documentation of the library check out [GoDoc](https://godoc.org/github.com/icedream/go-bsdiff). |
| 17 | + |
| 18 | +Library functionality is provided both as a package `bsdiff` containing both |
| 19 | +methods `Diff` and `Patch`, or as subpackages `diff` and `patch` which each |
| 20 | +only link the wanted functionality. |
| 21 | + |
| 22 | +Below example will generate a patch and apply it again in one go. This code |
| 23 | +is not safe against errors but it shows how to use the provided routines: |
| 24 | + |
| 25 | +```go |
| 26 | +package main |
| 27 | + |
| 28 | +import ( |
| 29 | + "os" |
| 30 | + "github.com/icedream/go-bsdiff" |
| 31 | + // Or use the subpackages to only link what you need: |
| 32 | + //"github.com/icedream/go-bsdiff/diff" |
| 33 | + //"github.com/icedream/go-bsdiff/patch" |
| 34 | +) |
| 35 | + |
| 36 | +const ( |
| 37 | + oldFilePath = "your_old_file.dat" |
| 38 | + newFilePath = "your_new_file.dat" |
| 39 | + patchFilePath = "the_generated.patch" |
| 40 | +) |
| 41 | + |
| 42 | +func generatePatch() error { |
| 43 | + oldFile, _ := os.Open(oldFilePath) |
| 44 | + defer oldFile.Close() |
| 45 | + newFile, _ := os.Open(newFilePath) |
| 46 | + defer newFile.Close() |
| 47 | + patchFile, _ := os.Create(patchFilePath) |
| 48 | + defer patchFile.Close() |
| 49 | + |
| 50 | + return bsdiff.Diff(oldFile, newFile, patchFile) |
| 51 | +} |
| 52 | + |
| 53 | +func applyPatch() error { |
| 54 | + oldFile, _ := os.Open(oldFilePath) |
| 55 | + defer oldFile.Close() |
| 56 | + newFile, _ := os.Create(newFilePath) |
| 57 | + defer newFile.Close() |
| 58 | + patchFile, _ := os.Open(patchFilePath) |
| 59 | + defer patchFile.Close() |
| 60 | + |
| 61 | + return bsdiff.Patch(oldFile, newFile, patchFile) |
| 62 | +} |
| 63 | + |
| 64 | +func main() { |
| 65 | + generatePatch() |
| 66 | + applyPatch() |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## Usage of the tools |
| 71 | + |
| 72 | +The tools `go-bsdiff` and `go-bspatch` both provide a `--help` flag to print |
| 73 | +out all information but in their simplest form, they can be used like this: |
| 74 | + |
| 75 | +```sh |
| 76 | +# Creates a patch file $the_generated with differences from |
| 77 | +# $your_old_file to $your_new_file. |
| 78 | +go-bsdiff "$your_old_file" "$your_new_file" "$the_generated" |
| 79 | + |
| 80 | +# Applies a patch file $the_generated on "your_old_file |
| 81 | +# and saves the new file to $your_new_file. |
| 82 | +go-bspatch "$your_old_file" "$your_new_file" "$the_generated" |
| 83 | +``` |
| 84 | + |
| 85 | +## Motivation |
| 86 | + |
| 87 | +There is [a Go implementation of an older version of bsdiff called binarydist](https://github.com/kr/binarydist). The original bsdiff tool has since been updated so patches generating using the original tool are no longer compatible with the Go implementation. I don't know what the changes between the versions are and unfortunately I don't have the time to search for these changes and port them over as a pull request, otherwise I'd have done that instead. |
| 88 | + |
| 89 | +Additionally, @mendsley has already done the extra work of rewriting the code to be embeddable in any application code as a library. So why not make use of cgo, which I was going to look into in more detail at some point anyways? |
0 commit comments