2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-15 01:34:06 +00:00
restic/src/restic/archiver/archive_reader.go

116 lines
2.2 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 (
"io"
2016-08-31 20:39:36 +00:00
"restic"
2016-05-08 19:50:27 +00:00
"restic/debug"
"time"
2016-09-01 20:17:37 +00:00
"restic/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.
func (r *Reader) Archive(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) {
_, err := repo.SaveBlob(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,
},
},
}
2016-09-03 18:55:22 +00:00
treeID, err := repo.SaveTree(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
2016-08-31 20:39:36 +00:00
id, err := repo.SaveJSONUnpacked(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
}
err = repo.SaveIndex()
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
}