GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - icedream/go-bsdiff: Golang wrapper for @mendsley's bsdiff C library.

BranchesTags
Open more actions menu

Latest commit

 

History

40 Commits
40 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
bsdiff
bsdiff
 
 
cmd
cmd
 
 
diff
diff
 
 
internal
internal
 
 
patch
patch
 
 
raw
raw
 
 
.gitignore
.gitignore
 
 
CHANGELOG.md
CHANGELOG.md
 
 
LICENSE.txt
LICENSE.txt
 
 
README.md
README.md
 
 
doc.go
doc.go
 
 
go.mod
go.mod
 
 
go.sum
go.sum
 
 
main.go
main.go
 
 
renovate.json
renovate.json
 
 
tools.go
tools.go
 
 

Repository files navigation

bsdiff for Go

This wrapper implementation for Golang reuses the existing C version of bsdiff as provided by @mendsley and wraps it into a Go package, abstracting away all the cgo work that would need to be done otherwise.

Installation

The library and the helper binaries go-bsdiff and go-bspatch can be installed like this:

go get -v github.com/icedream/go-bsdiff/...

Usage in application code

For exact documentation of the library check out GoDoc.

Library functionality is provided both as a package bsdiff containing both methods Diff and Patch, or as subpackages diff and patch which each only link the wanted functionality.

Below example will generate a patch and apply it again in one go. This code is not safe against errors but it shows how to use the provided routines:

package main

import (
    "os"
    "github.com/icedream/go-bsdiff"
    // Or use the subpackages to only link what you need:
    //"github.com/icedream/go-bsdiff/diff"
    //"github.com/icedream/go-bsdiff/patch"
)

const (
    oldFilePath = "your_old_file.dat"
    newFilePath = "your_new_file.dat"
    patchFilePath = "the_generated.patch"
)

func generatePatch() error {
    oldFile, _ := os.Open(oldFilePath)
    defer oldFile.Close()
    newFile, _ := os.Open(newFilePath)
    defer newFile.Close()
    patchFile, _ := os.Create(patchFilePath)
    defer patchFile.Close()

    return bsdiff.Diff(oldFile, newFile, patchFile)
}

func applyPatch() error {
    oldFile, _ := os.Open(oldFilePath)
    defer oldFile.Close()
    newFile, _ := os.Create(newFilePath)
    defer newFile.Close()
    patchFile, _ := os.Open(patchFilePath)
    defer patchFile.Close()

    return bsdiff.Patch(oldFile, newFile, patchFile)
}

func main() {
    generatePatch()
    applyPatch()
}

Usage of the tools

The tools go-bsdiff and go-bspatch both provide a --help flag to print out all information but in their simplest form, they can be used like this:

# Creates a patch file $the_generated with differences from
# $your_old_file to $your_new_file.
go-bsdiff "$your_old_file" "$your_new_file" "$the_generated"

# Applies a patch file $the_generated on $your_old_file
# and saves the new file to $your_new_file.
go-bspatch "$your_old_file" "$your_new_file" "$the_generated"

Motivation

There is a Go implementation of an older version of bsdiff called 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.

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?

By now, there is also TotallyGamerJet's purely Go implementation of bsdiff 4. It does not rely on cgo at all and may suit your use-case better.

License

Just like bsdiff, this project is licensed under the 2-clause BSD license (or Simplified BSD license). You can check LICENSE.txt for more information.

About

Golang wrapper for @mendsley's bsdiff C library.

Topics

binary-diffingbsdiffgogolanggolang-librarypatch

Resources

Readme
BSD-2-Clause license
Activity

Stars

26 stars

Watchers

1 watching

Forks

7 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.