forked from TrueCloudLab/restic
Simply backend interface
Rename CreateBlob() method to Create(), remove old Create* methods
This commit is contained in:
parent
35636a9d92
commit
72c951a6ec
6 changed files with 82 additions and 210 deletions
|
@ -15,7 +15,6 @@ import (
|
|||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/juju/arrar"
|
||||
"github.com/pkg/sftp"
|
||||
)
|
||||
|
||||
|
@ -291,96 +290,6 @@ func (r *SFTP) dirname(t Type, id ID) string {
|
|||
return filepath.Join(r.p, n)
|
||||
}
|
||||
|
||||
// Create stores new content of type t and data and returns the ID. If the blob
|
||||
// is already present, returns ErrAlreadyPresent and the blob's ID.
|
||||
func (r *SFTP) Create(t Type, data []byte) (ID, error) {
|
||||
// TODO: make sure that tempfile is removed upon error
|
||||
|
||||
// check if blob is already present in backend
|
||||
id := IDFromData(data)
|
||||
res, err := r.Test(t, id)
|
||||
if err != nil {
|
||||
return nil, arrar.Annotate(err, "test for presence")
|
||||
}
|
||||
|
||||
if res {
|
||||
return id, ErrAlreadyPresent
|
||||
}
|
||||
|
||||
// create tempfile in backend
|
||||
filename, file, err := r.tempFile()
|
||||
if err != nil {
|
||||
return nil, arrar.Annotate(err, "create tempfile")
|
||||
}
|
||||
|
||||
// write data to tempfile
|
||||
_, err = file.Write(data)
|
||||
if err != nil {
|
||||
return nil, arrar.Annotate(err, "writing data to tempfile")
|
||||
}
|
||||
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
return nil, arrar.Annotate(err, "close tempfile")
|
||||
}
|
||||
|
||||
// return id
|
||||
err = r.renameFile(filename, t, id)
|
||||
if err != nil {
|
||||
return nil, arrar.Annotate(err, "rename file")
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// CreateFrom reads content from rd and stores it as type t. Returned is the
|
||||
// storage ID. If the blob is already present, returns ErrAlreadyPresent and
|
||||
// the blob's ID.
|
||||
func (r *SFTP) CreateFrom(t Type, rd io.Reader) (ID, error) {
|
||||
// TODO: make sure that tempfile is removed upon error
|
||||
|
||||
// check hash while writing
|
||||
hr := NewHashingReader(rd, newHash())
|
||||
|
||||
// create tempfile in backend
|
||||
filename, file, err := r.tempFile()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// write data to tempfile
|
||||
_, err = io.Copy(file, hr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// get ID
|
||||
id := ID(hr.Sum(nil))
|
||||
|
||||
// check for duplicate ID
|
||||
res, err := r.Test(t, id)
|
||||
if err != nil {
|
||||
return nil, arrar.Annotate(err, "test for presence")
|
||||
}
|
||||
|
||||
if res {
|
||||
return id, ErrAlreadyPresent
|
||||
}
|
||||
|
||||
// rename file
|
||||
err = r.renameFile(filename, t, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
type sftpBlob struct {
|
||||
f *sftp.File
|
||||
name string
|
||||
|
@ -434,7 +343,7 @@ func (sb *sftpBlob) ID() (ID, error) {
|
|||
// Create creates a new blob of type t. Blob implements io.WriteCloser. Once
|
||||
// Close() has been called, ID() can be used to retrieve the ID. If the blob is
|
||||
// already present, Close() returns ErrAlreadyPresent.
|
||||
func (r *SFTP) CreateBlob(t Type) (Blob, error) {
|
||||
func (r *SFTP) Create(t Type) (Blob, error) {
|
||||
// TODO: make sure that tempfile is removed upon error
|
||||
|
||||
// create tempfile in backend
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue