Skip to content

Commit

Permalink
Added MD5 Checksum Support
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
akutz authored and shuLhan committed Sep 9, 2017
1 parent 89e7c37 commit 9308d8f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions config.go
Expand Up @@ -140,6 +140,10 @@ type Config struct {
//
// This parameter can be provided multiple times.
Ignore []*regexp.Regexp

// MD5Checksum is a flag that, when set to true, indicates to calculate
// MD5 checksums for files.
MD5Checksum bool
}

// NewConfig returns a default configuration struct.
Expand Down
1 change: 1 addition & 0 deletions go-bindata/main.go
Expand Up @@ -48,6 +48,7 @@ func parseArgs() *bindata.Config {
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.")
flag.BoolVar(&c.NoCompress, "nocompress", c.NoCompress, "Assets will *not* be GZIP compressed when this flag is specified.")
flag.BoolVar(&c.NoMetadata, "nometadata", c.NoMetadata, "Assets will not preserve size, mode, and modtime info.")
flag.BoolVar(&c.MD5Checksum, "md5checksum", c.MD5Checksum, "MD5 checksums will be calculated for assets.")
flag.UintVar(&c.Mode, "mode", c.Mode, "Optional file mode override for all files.")
flag.Int64Var(&c.ModTime, "modtime", c.ModTime, "Optional modification unix timestamp override for all files.")
flag.StringVar(&c.Output, "o", c.Output, "Optional name of the output file to be generated.")
Expand Down
37 changes: 30 additions & 7 deletions release.go
Expand Up @@ -7,6 +7,7 @@ package bindata
import (
"bytes"
"compress/gzip"
"crypto/md5"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -214,14 +215,20 @@ func header_uncompressed_memcopy(w io.Writer) error {
func header_release_common(w io.Writer) error {
_, err := fmt.Fprintf(w, `type asset struct {
bytes []byte
info os.FileInfo
info fileInfoEx
}
type fileInfoEx interface {
os.FileInfo
MD5Checksum() string
}
type bindataFileInfo struct {
name string
size int64
mode os.FileMode
modTime time.Time
name string
size int64
mode os.FileMode
modTime time.Time
md5checksum string
}
func (fi bindataFileInfo) Name() string {
Expand All @@ -236,6 +243,9 @@ func (fi bindataFileInfo) Mode() os.FileMode {
func (fi bindataFileInfo) ModTime() time.Time {
return fi.modTime
}
func (fi bindataFileInfo) MD5Checksum() string {
return fi.md5checksum
}
func (fi bindataFileInfo) IsDir() bool {
return false
}
Expand Down Expand Up @@ -371,17 +381,30 @@ func asset_release_common(w io.Writer, c *Config, asset *Asset) error {
if c.ModTime > 0 {
modTime = c.ModTime
}

var md5checksum string
if c.MD5Checksum {
buf, err := ioutil.ReadFile(asset.Path)
if err != nil {
return err
}
h := md5.New()
if _, err := h.Write(buf); err != nil {
return err
}
md5checksum = fmt.Sprintf("%x", h.Sum(nil))
}
_, err = fmt.Fprintf(w, `func %s() (*asset, error) {
bytes, err := %sBytes()
if err != nil {
return nil, err
}
info := bindataFileInfo{name: %q, size: %d, mode: os.FileMode(%d), modTime: time.Unix(%d, 0)}
info := bindataFileInfo{name: %q, size: %d, md5checksum: %q, mode: os.FileMode(%d), modTime: time.Unix(%d, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
`, asset.Func, asset.Func, asset.Name, size, mode, modTime)
`, asset.Func, asset.Func, asset.Name, size, md5checksum, mode, modTime)
return err
}

0 comments on commit 9308d8f

Please sign in to comment.