2015-02-25 17:56:40 +00:00
|
|
|
package asm
|
|
|
|
|
|
|
|
import (
|
2015-02-27 22:36:24 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2015-02-25 17:56:40 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileGetter interface {
|
|
|
|
// Get returns a stream for the provided file path
|
|
|
|
Get(string) (io.ReadCloser, error)
|
|
|
|
}
|
|
|
|
|
2015-02-27 21:54:04 +00:00
|
|
|
type FilePutter interface {
|
|
|
|
// Put returns a stream for the provided file path
|
2015-02-27 22:36:24 +00:00
|
|
|
Put(string, io.Reader) error
|
2015-02-27 21:54:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FileGetPutter interface {
|
|
|
|
FileGetter
|
|
|
|
FilePutter
|
|
|
|
}
|
|
|
|
|
2015-02-25 17:56:40 +00:00
|
|
|
// NewPathFileGetter returns a FileGetter that is for files relative to path relpath.
|
|
|
|
func NewPathFileGetter(relpath string) FileGetter {
|
|
|
|
return &pathFileGetter{root: relpath}
|
|
|
|
}
|
|
|
|
|
|
|
|
type pathFileGetter struct {
|
|
|
|
root string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pfg pathFileGetter) Get(filename string) (io.ReadCloser, error) {
|
|
|
|
// FIXME might should have a check for '../../../../etc/passwd' attempts?
|
|
|
|
return os.Open(path.Join(pfg.root, filename))
|
|
|
|
}
|
2015-02-27 21:54:04 +00:00
|
|
|
|
|
|
|
type bufferFileGetPutter struct {
|
|
|
|
files map[string][]byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bfgp bufferFileGetPutter) Get(name string) (io.ReadCloser, error) {
|
2015-02-27 22:36:24 +00:00
|
|
|
if _, ok := bfgp.files[name]; !ok {
|
|
|
|
return nil, errors.New("no such file")
|
|
|
|
}
|
|
|
|
b := bytes.NewBuffer(bfgp.files[name])
|
|
|
|
return &readCloserWrapper{b}, nil
|
|
|
|
}
|
2015-02-27 21:54:04 +00:00
|
|
|
|
2015-02-27 22:36:24 +00:00
|
|
|
func (bfgp *bufferFileGetPutter) Put(name string, r io.Reader) error {
|
|
|
|
b := bytes.NewBuffer([]byte{})
|
|
|
|
if _, err := io.Copy(b, r); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bfgp.files[name] = b.Bytes()
|
|
|
|
return nil
|
2015-02-27 21:54:04 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 22:36:24 +00:00
|
|
|
type readCloserWrapper struct {
|
|
|
|
io.Reader
|
2015-02-27 21:54:04 +00:00
|
|
|
}
|
|
|
|
|
2015-02-27 22:36:24 +00:00
|
|
|
func (w *readCloserWrapper) Close() error { return nil }
|
2015-02-27 21:54:04 +00:00
|
|
|
|
2015-02-27 22:36:24 +00:00
|
|
|
// NewBufferFileGetPutter is simple in memory FileGetPutter
|
2015-02-27 21:54:04 +00:00
|
|
|
func NewBufferFileGetPutter() FileGetPutter {
|
|
|
|
return &bufferFileGetPutter{}
|
|
|
|
}
|