diff --git a/config.go b/config.go index 2bd0d56..9c6b9de 100644 --- a/config.go +++ b/config.go @@ -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. diff --git a/go-bindata/main.go b/go-bindata/main.go index cc7bd2b..0157a11 100644 --- a/go-bindata/main.go +++ b/go-bindata/main.go @@ -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.") diff --git a/release.go b/release.go index 0151fb8..c1a04c9 100644 --- a/release.go +++ b/release.go @@ -7,6 +7,7 @@ package bindata import ( "bytes" "compress/gzip" + "crypto/md5" "fmt" "io" "io/ioutil" @@ -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 { @@ -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 } @@ -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 }