Skip to content

Commit 9308d8f

Browse files
akutzshuLhan
authored andcommitted
Added MD5 Checksum Support
This patch adds support for creating embedded files with their MD5 checksum calculated at time of the buffer generation and stored alongside the asset information. The checksum feature can be enabled via the command line with the `--md5checksum` flag. The default value for the flag is `false`.
1 parent 89e7c37 commit 9308d8f

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ type Config struct {
140140
//
141141
// This parameter can be provided multiple times.
142142
Ignore []*regexp.Regexp
143+
144+
// MD5Checksum is a flag that, when set to true, indicates to calculate
145+
// MD5 checksums for files.
146+
MD5Checksum bool
143147
}
144148

145149
// NewConfig returns a default configuration struct.

go-bindata/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func parseArgs() *bindata.Config {
4848
flag.BoolVar(&c.NoMemCopy, "nomemcopy", c.NoMemCopy, "Use a .rodata hack to get rid of unnecessary memcopies. Refer to the documentation to see what implications this carries.")
4949
flag.BoolVar(&c.NoCompress, "nocompress", c.NoCompress, "Assets will *not* be GZIP compressed when this flag is specified.")
5050
flag.BoolVar(&c.NoMetadata, "nometadata", c.NoMetadata, "Assets will not preserve size, mode, and modtime info.")
51+
flag.BoolVar(&c.MD5Checksum, "md5checksum", c.MD5Checksum, "MD5 checksums will be calculated for assets.")
5152
flag.UintVar(&c.Mode, "mode", c.Mode, "Optional file mode override for all files.")
5253
flag.Int64Var(&c.ModTime, "modtime", c.ModTime, "Optional modification unix timestamp override for all files.")
5354
flag.StringVar(&c.Output, "o", c.Output, "Optional name of the output file to be generated.")

release.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package bindata
77
import (
88
"bytes"
99
"compress/gzip"
10+
"crypto/md5"
1011
"fmt"
1112
"io"
1213
"io/ioutil"
@@ -214,14 +215,20 @@ func header_uncompressed_memcopy(w io.Writer) error {
214215
func header_release_common(w io.Writer) error {
215216
_, err := fmt.Fprintf(w, `type asset struct {
216217
bytes []byte
217-
info os.FileInfo
218+
info fileInfoEx
219+
}
220+
221+
type fileInfoEx interface {
222+
os.FileInfo
223+
MD5Checksum() string
218224
}
219225
220226
type bindataFileInfo struct {
221-
name string
222-
size int64
223-
mode os.FileMode
224-
modTime time.Time
227+
name string
228+
size int64
229+
mode os.FileMode
230+
modTime time.Time
231+
md5checksum string
225232
}
226233
227234
func (fi bindataFileInfo) Name() string {
@@ -236,6 +243,9 @@ func (fi bindataFileInfo) Mode() os.FileMode {
236243
func (fi bindataFileInfo) ModTime() time.Time {
237244
return fi.modTime
238245
}
246+
func (fi bindataFileInfo) MD5Checksum() string {
247+
return fi.md5checksum
248+
}
239249
func (fi bindataFileInfo) IsDir() bool {
240250
return false
241251
}
@@ -371,17 +381,30 @@ func asset_release_common(w io.Writer, c *Config, asset *Asset) error {
371381
if c.ModTime > 0 {
372382
modTime = c.ModTime
373383
}
384+
385+
var md5checksum string
386+
if c.MD5Checksum {
387+
buf, err := ioutil.ReadFile(asset.Path)
388+
if err != nil {
389+
return err
390+
}
391+
h := md5.New()
392+
if _, err := h.Write(buf); err != nil {
393+
return err
394+
}
395+
md5checksum = fmt.Sprintf("%x", h.Sum(nil))
396+
}
374397
_, err = fmt.Fprintf(w, `func %s() (*asset, error) {
375398
bytes, err := %sBytes()
376399
if err != nil {
377400
return nil, err
378401
}
379402
380-
info := bindataFileInfo{name: %q, size: %d, mode: os.FileMode(%d), modTime: time.Unix(%d, 0)}
403+
info := bindataFileInfo{name: %q, size: %d, md5checksum: %q, mode: os.FileMode(%d), modTime: time.Unix(%d, 0)}
381404
a := &asset{bytes: bytes, info: info}
382405
return a, nil
383406
}
384407
385-
`, asset.Func, asset.Func, asset.Name, size, mode, modTime)
408+
`, asset.Func, asset.Func, asset.Name, size, md5checksum, mode, modTime)
386409
return err
387410
}

0 commit comments

Comments
 (0)