mirror of
https://github.com/vbatts/tar-split.git
synced 2024-11-15 12:58:38 +00:00
Vincent Batts
4e27d04b0b
Have a bit-bucket FilePutter, for when it does not matter. Beginning thoughts on disassembly, but it has things that need thought. Mostly comments in the function for now.
89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package asm
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
type FileGetter interface {
|
|
// Get returns a stream for the provided file path
|
|
Get(string) (io.ReadCloser, error)
|
|
}
|
|
|
|
type FilePutter interface {
|
|
// Put returns a stream for the provided file path
|
|
Put(string, io.Reader) error
|
|
}
|
|
|
|
type FileGetPutter interface {
|
|
FileGetter
|
|
FilePutter
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
type bufferFileGetPutter struct {
|
|
files map[string][]byte
|
|
}
|
|
|
|
func (bfgp bufferFileGetPutter) Get(name string) (io.ReadCloser, error) {
|
|
if _, ok := bfgp.files[name]; !ok {
|
|
return nil, errors.New("no such file")
|
|
}
|
|
b := bytes.NewBuffer(bfgp.files[name])
|
|
return &readCloserWrapper{b}, nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
type readCloserWrapper struct {
|
|
io.Reader
|
|
}
|
|
|
|
func (w *readCloserWrapper) Close() error { return nil }
|
|
|
|
// NewBufferFileGetPutter is simple in memory FileGetPutter
|
|
//
|
|
// Implication is this is memory intensive...
|
|
// Probably best for testing or light weight cases.
|
|
func NewBufferFileGetPutter() FileGetPutter {
|
|
return &bufferFileGetPutter{
|
|
files: map[string][]byte{},
|
|
}
|
|
}
|
|
|
|
// NewDiscardFilePutter is a bit bucket FilePutter
|
|
func NewDiscardFilePutter() FilePutter {
|
|
return &bitBucketFilePutter{}
|
|
}
|
|
|
|
type bitBucketFilePutter struct {
|
|
}
|
|
|
|
func (bbfp *bitBucketFilePutter) Put(name string, r io.Reader) error {
|
|
_, err := io.Copy(ioutil.Discard, r)
|
|
return err
|
|
}
|