2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-29 23:20:49 +00:00
restic/internal/archiver/archive_reader.go

118 lines
2.4 KiB
Go
Raw Normal View History

2016-08-31 20:39:36 +00:00
package archiver
2016-05-08 19:50:27 +00:00
import (
2017-06-04 09:16:55 +00:00
"context"
2016-05-08 19:50:27 +00:00
"io"
"time"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/debug"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
"github.com/restic/chunker"
2016-05-08 19:50:27 +00:00
)
2017-03-02 14:45:35 +00:00
// Reader allows saving a stream of data to the repository.
type Reader struct {
restic.Repository
Tags []string
Hostname string
}
// Archive reads data from the reader and saves it to the repo.
2017-06-04 09:16:55 +00:00
func (r *Reader) Archive(ctx context.Context, name string, rd io.Reader, p *restic.Progress) (*restic.Snapshot, restic.ID, error) {
if name == "" {
return nil, restic.ID{}, errors.New("no filename given")
}
2016-09-27 20:35:08 +00:00
debug.Log("start archiving %s", name)
2017-03-02 14:45:35 +00:00
sn, err := restic.NewSnapshot([]string{name}, r.Tags, r.Hostname)
2016-05-08 19:50:27 +00:00
if err != nil {
2016-08-31 20:39:36 +00:00
return nil, restic.ID{}, err
2016-05-08 19:50:27 +00:00
}
p.Start()
defer p.Done()
2017-03-02 14:45:35 +00:00
repo := r.Repository
2016-08-31 20:39:36 +00:00
chnker := chunker.New(rd, repo.Config().ChunkerPolynomial)
2016-05-08 19:50:27 +00:00
ids := restic.IDs{}
var fileSize uint64
2016-05-08 19:50:27 +00:00
for {
chunk, err := chnker.Next(getBuf())
if errors.Cause(err) == io.EOF {
2016-05-08 19:50:27 +00:00
break
}
if err != nil {
2016-08-31 20:39:36 +00:00
return nil, restic.ID{}, errors.Wrap(err, "chunker.Next()")
2016-05-08 19:50:27 +00:00
}
2016-08-31 20:39:36 +00:00
id := restic.Hash(chunk.Data)
2016-05-08 19:50:27 +00:00
2016-08-31 20:39:36 +00:00
if !repo.Index().Has(id, restic.DataBlob) {
2017-06-04 09:16:55 +00:00
_, err := repo.SaveBlob(ctx, restic.DataBlob, chunk.Data, id)
2016-05-08 19:50:27 +00:00
if err != nil {
2016-08-31 20:39:36 +00:00
return nil, restic.ID{}, err
2016-05-08 19:50:27 +00:00
}
2016-09-27 20:35:08 +00:00
debug.Log("saved blob %v (%d bytes)\n", id.Str(), chunk.Length)
2016-05-08 19:50:27 +00:00
} else {
2016-09-27 20:35:08 +00:00
debug.Log("blob %v already saved in the repo\n", id.Str())
2016-05-08 19:50:27 +00:00
}
freeBuf(chunk.Data)
ids = append(ids, id)
2016-08-31 20:39:36 +00:00
p.Report(restic.Stat{Bytes: uint64(chunk.Length)})
fileSize += uint64(chunk.Length)
2016-05-08 19:50:27 +00:00
}
2016-08-31 20:39:36 +00:00
tree := &restic.Tree{
Nodes: []*restic.Node{
2017-05-16 23:28:39 +00:00
{
2016-05-08 19:50:27 +00:00
Name: name,
AccessTime: time.Now(),
ModTime: time.Now(),
2016-09-01 19:20:03 +00:00
Type: "file",
2016-05-08 19:50:27 +00:00
Mode: 0644,
Size: fileSize,
2016-05-08 19:50:27 +00:00
UID: sn.UID,
GID: sn.GID,
User: sn.Username,
Content: ids,
},
},
}
2017-06-04 09:16:55 +00:00
treeID, err := repo.SaveTree(ctx, tree)
2016-05-08 19:50:27 +00:00
if err != nil {
2016-08-31 20:39:36 +00:00
return nil, restic.ID{}, err
2016-05-08 19:50:27 +00:00
}
sn.Tree = &treeID
2016-09-27 20:35:08 +00:00
debug.Log("tree saved as %v", treeID.Str())
2016-05-08 19:50:27 +00:00
2017-06-04 09:16:55 +00:00
id, err := repo.SaveJSONUnpacked(ctx, restic.SnapshotFile, sn)
2016-05-08 19:50:27 +00:00
if err != nil {
2016-08-31 20:39:36 +00:00
return nil, restic.ID{}, err
2016-05-08 19:50:27 +00:00
}
2016-09-27 20:35:08 +00:00
debug.Log("snapshot saved as %v", id.Str())
2016-05-08 19:50:27 +00:00
err = repo.Flush()
if err != nil {
2016-08-31 20:39:36 +00:00
return nil, restic.ID{}, err
2016-05-08 19:50:27 +00:00
}
2017-06-04 09:16:55 +00:00
err = repo.SaveIndex(ctx)
2016-05-08 19:50:27 +00:00
if err != nil {
2016-08-31 20:39:36 +00:00
return nil, restic.ID{}, err
2016-05-08 19:50:27 +00:00
}
return sn, id, nil
}