From fbb1e168f72d31c22f24933808ef9dd1696c2f07 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 22 Dec 2014 10:52:09 +0100 Subject: [PATCH] Include MD5 sums in archives --- .gitignore | 2 ++ build.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1c2881008..c24a07bd4 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ perfstats*.csv coverage.xml !gui/scripts/syncthing .DS_Store +syncthing.md5 +syncthing.exe.md5 diff --git a/build.go b/build.go index 8b67cfcef..b1594c0fc 100644 --- a/build.go +++ b/build.go @@ -22,6 +22,7 @@ import ( "archive/zip" "bytes" "compress/gzip" + "crypto/md5" "flag" "fmt" "io" @@ -190,7 +191,12 @@ func install(pkg string, tags []string) { } func build(pkg string, tags []string) { - rmr("syncthing", "syncthing.exe") + binary := "syncthing" + if goos == "windows" { + binary += ".exe" + } + + rmr(binary, binary+".md5") args := []string{"build", "-ldflags", ldflags()} if len(tags) > 0 { args = append(args, "-tags", strings.Join(tags, ",")) @@ -201,6 +207,13 @@ func build(pkg string, tags []string) { args = append(args, pkg) setBuildEnv() runPrint("go", args...) + + // Create an md5 checksum of the binary, to be included in the archive for + // automatic upgrades. + err := md5File(binary) + if err != nil { + log.Fatal(err) + } } func buildTar() { @@ -217,6 +230,7 @@ func buildTar() { {"LICENSE", name + "/LICENSE.txt"}, {"AUTHORS", name + "/AUTHORS.txt"}, {"syncthing", name + "/syncthing"}, + {"syncthing.md5", name + "/syncthing.md5"}, } for _, file := range listFiles("etc") { files = append(files, archiveFile{file, name + "/" + file}) @@ -239,6 +253,7 @@ func buildZip() { {"LICENSE", name + "/LICENSE.txt"}, {"AUTHORS", name + "/AUTHORS.txt"}, {"syncthing.exe", name + "/syncthing.exe"}, + {"syncthing.exe.md5", name + "/syncthing.exe.md5"}, } zipFile(filename, files) log.Println(filename) @@ -554,3 +569,29 @@ func zipFile(out string, files []archiveFile) { log.Fatal(err) } } + +func md5File(file string) error { + fd, err := os.Open(file) + if err != nil { + return err + } + defer fd.Close() + + h := md5.New() + _, err = io.Copy(h, fd) + if err != nil { + return err + } + + out, err := os.Create(file + ".md5") + if err != nil { + return err + } + + _, err = fmt.Fprintf(out, "%x\n", h.Sum(nil)) + if err != nil { + return err + } + + return out.Close() +}