2015-02-23 14:57:56 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2015-02-27 21:53:31 +00:00
|
|
|
"errors"
|
2015-02-23 14:57:56 +00:00
|
|
|
"io"
|
2015-07-22 14:27:53 +00:00
|
|
|
"path/filepath"
|
2015-09-25 18:33:24 +00:00
|
|
|
"unicode/utf8"
|
2015-02-27 21:53:31 +00:00
|
|
|
)
|
|
|
|
|
2015-06-23 20:13:29 +00:00
|
|
|
// ErrDuplicatePath occurs when a tar archive has more than one entry for the
|
|
|
|
// same file path
|
2015-03-09 18:11:11 +00:00
|
|
|
var ErrDuplicatePath = errors.New("duplicates of file paths not supported")
|
2015-02-23 14:57:56 +00:00
|
|
|
|
2015-02-24 20:36:21 +00:00
|
|
|
// Packer describes the methods to pack Entries to a storage destination
|
2015-02-23 14:57:56 +00:00
|
|
|
type Packer interface {
|
2015-02-24 20:36:21 +00:00
|
|
|
// AddEntry packs the Entry and returns its position
|
2015-02-23 14:57:56 +00:00
|
|
|
AddEntry(e Entry) (int, error)
|
|
|
|
}
|
|
|
|
|
2015-02-24 20:36:21 +00:00
|
|
|
// Unpacker describes the methods to read Entries from a source
|
2015-02-23 14:57:56 +00:00
|
|
|
type Unpacker interface {
|
2015-02-24 20:36:21 +00:00
|
|
|
// Next returns the next Entry being unpacked, or error, until io.EOF
|
2015-02-23 14:57:56 +00:00
|
|
|
Next() (*Entry, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonUnpacker struct {
|
2015-11-30 17:52:44 +00:00
|
|
|
seen seenNames
|
|
|
|
dec *json.Decoder
|
2015-02-23 14:57:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (jup *jsonUnpacker) Next() (*Entry, error) {
|
|
|
|
var e Entry
|
2015-11-30 17:52:44 +00:00
|
|
|
err := jup.dec.Decode(&e)
|
|
|
|
if err != nil {
|
2015-02-23 14:57:56 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-27 21:53:31 +00:00
|
|
|
|
|
|
|
// check for dup name
|
|
|
|
if e.Type == FileType {
|
2015-09-23 19:20:09 +00:00
|
|
|
cName := filepath.Clean(e.GetName())
|
2015-02-27 21:53:31 +00:00
|
|
|
if _, ok := jup.seen[cName]; ok {
|
|
|
|
return nil, ErrDuplicatePath
|
|
|
|
}
|
2015-06-23 20:13:54 +00:00
|
|
|
jup.seen[cName] = struct{}{}
|
2015-02-27 21:53:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 14:57:56 +00:00
|
|
|
return &e, err
|
|
|
|
}
|
|
|
|
|
2015-03-09 18:11:11 +00:00
|
|
|
// NewJSONUnpacker provides an Unpacker that reads Entries (SegmentType and
|
2015-02-24 20:36:21 +00:00
|
|
|
// FileType) as a json document.
|
|
|
|
//
|
|
|
|
// Each Entry read are expected to be delimited by new line.
|
2015-03-09 18:11:11 +00:00
|
|
|
func NewJSONUnpacker(r io.Reader) Unpacker {
|
2015-02-23 14:57:56 +00:00
|
|
|
return &jsonUnpacker{
|
2015-11-30 17:52:44 +00:00
|
|
|
dec: json.NewDecoder(r),
|
2015-02-27 21:53:31 +00:00
|
|
|
seen: seenNames{},
|
2015-02-23 14:57:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonPacker struct {
|
2015-02-27 21:53:31 +00:00
|
|
|
w io.Writer
|
|
|
|
e *json.Encoder
|
|
|
|
pos int
|
|
|
|
seen seenNames
|
2015-02-23 14:57:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 20:13:54 +00:00
|
|
|
type seenNames map[string]struct{}
|
2015-02-27 21:53:31 +00:00
|
|
|
|
2015-02-23 14:57:56 +00:00
|
|
|
func (jp *jsonPacker) AddEntry(e Entry) (int, error) {
|
2015-09-23 19:20:09 +00:00
|
|
|
// if Name is not valid utf8, switch it to raw first.
|
|
|
|
if e.Name != "" {
|
2015-09-25 18:33:24 +00:00
|
|
|
if !utf8.ValidString(e.Name) {
|
2015-09-23 19:20:09 +00:00
|
|
|
e.NameRaw = []byte(e.Name)
|
|
|
|
e.Name = ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 21:53:31 +00:00
|
|
|
// check early for dup name
|
|
|
|
if e.Type == FileType {
|
2015-09-23 19:20:09 +00:00
|
|
|
cName := filepath.Clean(e.GetName())
|
2015-02-27 21:53:31 +00:00
|
|
|
if _, ok := jp.seen[cName]; ok {
|
|
|
|
return -1, ErrDuplicatePath
|
|
|
|
}
|
2015-06-23 20:13:54 +00:00
|
|
|
jp.seen[cName] = struct{}{}
|
2015-02-27 21:53:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-23 14:57:56 +00:00
|
|
|
e.Position = jp.pos
|
|
|
|
err := jp.e.Encode(e)
|
2015-02-27 21:53:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
2015-02-23 14:57:56 +00:00
|
|
|
}
|
2015-02-27 21:53:31 +00:00
|
|
|
|
|
|
|
// made it this far, increment now
|
|
|
|
jp.pos++
|
|
|
|
return e.Position, nil
|
2015-02-23 14:57:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 20:13:29 +00:00
|
|
|
// NewJSONPacker provides a Packer that writes each Entry (SegmentType and
|
2015-02-24 20:36:21 +00:00
|
|
|
// FileType) as a json document.
|
|
|
|
//
|
|
|
|
// The Entries are delimited by new line.
|
2015-03-09 18:11:11 +00:00
|
|
|
func NewJSONPacker(w io.Writer) Packer {
|
2015-02-23 14:57:56 +00:00
|
|
|
return &jsonPacker{
|
2015-02-27 21:53:31 +00:00
|
|
|
w: w,
|
|
|
|
e: json.NewEncoder(w),
|
|
|
|
seen: seenNames{},
|
2015-02-23 14:57:56 +00:00
|
|
|
}
|
|
|
|
}
|