Merge pull request #61 from riywo/add-file-info

Add AssetInfo() to use FileInfo
This commit is contained in:
Jim Teeuwen 2014-12-08 00:26:27 +01:00
commit 9091c617ee
3 changed files with 130 additions and 20 deletions

View File

@ -33,6 +33,7 @@ func writeDebugHeader(w io.Writer) error {
"fmt"
"io/ioutil"
"strings"
"os"
)
// bindata_read reads the given file from disk. It returns an error on failure.
@ -44,6 +45,11 @@ func bindata_read(path, name string) ([]byte, error) {
return buf, err
}
type asset struct {
bytes []byte
info os.FileInfo
}
`)
return err
}
@ -53,11 +59,21 @@ func bindata_read(path, name string) ([]byte, error) {
// the original file (e.g.: from disk).
func writeDebugAsset(w io.Writer, asset *Asset) error {
_, err := fmt.Fprintf(w, `// %s reads file data from disk. It returns an error on failure.
func %s() ([]byte, error) {
return bindata_read(
%q,
%q,
)
func %s() (*asset, error) {
path := %q
name := %q
bytes, err := bindata_read(path, name)
if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %%s at %%s: %%v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
}
`, asset.Func, asset.Func, asset.Path, asset.Name)

View File

@ -34,19 +34,24 @@ func writeRelease(w io.Writer, c *Config, toc []Asset) error {
// writeReleaseHeader writes output file headers.
// This targets release builds.
func writeReleaseHeader(w io.Writer, c *Config) error {
var err error
if c.NoCompress {
if c.NoMemCopy {
return header_uncompressed_nomemcopy(w)
err = header_uncompressed_nomemcopy(w)
} else {
return header_uncompressed_memcopy(w)
err = header_uncompressed_memcopy(w)
}
} else {
if c.NoMemCopy {
return header_compressed_nomemcopy(w)
err = header_compressed_nomemcopy(w)
} else {
return header_compressed_memcopy(w)
err = header_compressed_memcopy(w)
}
}
if err != nil {
return err
}
return header_release_common(w)
}
// writeReleaseAsset write a release entry for the given asset.
@ -62,17 +67,21 @@ func writeReleaseAsset(w io.Writer, c *Config, asset *Asset) error {
if c.NoCompress {
if c.NoMemCopy {
return uncompressed_nomemcopy(w, asset, fd)
err = uncompressed_nomemcopy(w, asset, fd)
} else {
return uncompressed_memcopy(w, asset, fd)
err = uncompressed_memcopy(w, asset, fd)
}
} else {
if c.NoMemCopy {
return compressed_nomemcopy(w, asset, fd)
err = compressed_nomemcopy(w, asset, fd)
} else {
return compressed_memcopy(w, asset, fd)
err = compressed_memcopy(w, asset, fd)
}
}
if err != nil {
return err
}
return asset_release_common(w, asset)
}
// sanitize prepares a valid UTF-8 string as a raw string constant.
@ -97,6 +106,8 @@ func header_compressed_nomemcopy(w io.Writer) error {
"reflect"
"strings"
"unsafe"
"os"
"time"
)
func bindata_read(data, name string) ([]byte, error) {
@ -135,6 +146,8 @@ func header_compressed_memcopy(w io.Writer) error {
"fmt"
"io"
"strings"
"os"
"time"
)
func bindata_read(data []byte, name string) ([]byte, error) {
@ -164,6 +177,8 @@ func header_uncompressed_nomemcopy(w io.Writer) error {
"reflect"
"strings"
"unsafe"
"os"
"time"
)
func bindata_read(data, name string) ([]byte, error) {
@ -185,11 +200,49 @@ func header_uncompressed_memcopy(w io.Writer) error {
_, err := fmt.Fprintf(w, `import (
"fmt"
"strings"
"os"
"time"
)
`)
return err
}
func header_release_common(w io.Writer) error {
_, err := fmt.Fprintf(w, `type asset struct {
bytes []byte
info os.FileInfo
}
type bindata_file_info struct {
name string
size int64
mode os.FileMode
modTime time.Time
}
func (fi bindata_file_info) Name() string {
return fi.name
}
func (fi bindata_file_info) Size() int64 {
return fi.size
}
func (fi bindata_file_info) Mode() os.FileMode {
return fi.mode
}
func (fi bindata_file_info) ModTime() time.Time {
return fi.modTime
}
func (fi bindata_file_info) IsDir() bool {
return false
}
func (fi bindata_file_info) Sys() interface{} {
return nil
}
`)
return err
}
func compressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err := fmt.Fprintf(w, `var _%s = "`, asset.Func)
if err != nil {
@ -206,7 +259,7 @@ func compressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err = fmt.Fprintf(w, `"
func %s() ([]byte, error) {
func %s_bytes() ([]byte, error) {
return bindata_read(
_%s,
%q,
@ -233,7 +286,7 @@ func compressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err = fmt.Fprintf(w, `")
func %s() ([]byte, error) {
func %s_bytes() ([]byte, error) {
return bindata_read(
_%s,
%q,
@ -257,7 +310,7 @@ func uncompressed_nomemcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err = fmt.Fprintf(w, `"
func %s() ([]byte, error) {
func %s_bytes() ([]byte, error) {
return bindata_read(
_%s,
%q,
@ -286,10 +339,31 @@ func uncompressed_memcopy(w io.Writer, asset *Asset, r io.Reader) error {
_, err = fmt.Fprintf(w, `)
func %s() ([]byte, error) {
func %s_bytes() ([]byte, error) {
return _%s, nil
}
`, asset.Func, asset.Func)
return err
}
func asset_release_common(w io.Writer, asset *Asset) error {
fi, err := os.Stat(asset.Path)
if err != nil {
return err
}
_, err = fmt.Fprintf(w, `func %s() (*asset, error) {
bytes, err := %s_bytes()
if err != nil {
return nil, err
}
info := bindata_file_info{name: %q, size: %d, mode: os.FileMode(%d), modTime: time.Unix(%d, 0)}
a := &asset{bytes: bytes, info: info}
return a, nil
}
`, asset.Func, asset.Func, asset.Name, fi.Size(), uint32(fi.Mode()), fi.ModTime().Unix())
return err
}

26
toc.go
View File

@ -80,7 +80,7 @@ func (root *assetTree) writeGoMap(w io.Writer, nident int) {
func (root *assetTree) WriteAsGoMap(w io.Writer) error {
_, err := fmt.Fprint(w, `type _bintree_t struct {
Func func() ([]byte, error)
Func func() (*asset, error)
Children map[string]*_bintree_t
}
var _bintree = `)
@ -161,11 +161,30 @@ func writeTOCHeader(w io.Writer) error {
func Asset(name string) ([]byte, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
return f()
a, err := f()
if err != nil {
return nil, fmt.Errorf("Asset %%s can't read by error: %%v", name, err)
}
return a.bytes, nil
}
return nil, fmt.Errorf("Asset %%s not found", name)
}
// AssetInfo loads and returns the asset info for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func AssetInfo(name string) (os.FileInfo, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("AssetInfo %%s can't read by error: %%v", name, err)
}
return a.info, nil
}
return nil, fmt.Errorf("AssetInfo %%s not found", name)
}
// AssetNames returns the names of the assets.
func AssetNames() []string {
names := make([]string, 0, len(_bindata))
@ -176,7 +195,7 @@ func AssetNames() []string {
}
// _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() ([]byte, error){
var _bindata = map[string]func() (*asset, error){
`)
return err
}
@ -190,6 +209,7 @@ func writeTOCAsset(w io.Writer, asset *Asset) error {
// writeTOCFooter writes the table of contents file footer.
func writeTOCFooter(w io.Writer) error {
_, err := fmt.Fprintf(w, `}
`)
return err
}