2015-11-18 19:20:25 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2017-01-22 11:32:20 +00:00
|
|
|
"bytes"
|
2016-03-06 11:26:25 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2016-08-31 18:29:54 +00:00
|
|
|
"restic"
|
2015-11-18 19:20:25 +00:00
|
|
|
"sync"
|
|
|
|
|
2016-09-01 20:17:37 +00:00
|
|
|
"restic/errors"
|
2016-08-21 15:48:36 +00:00
|
|
|
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/crypto"
|
|
|
|
"restic/debug"
|
Fix 567 (#570)
* Patch for https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).
as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.
I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."
* fixed building tests
* Restructured patches
Add Wrapper for filepath.Walk
* using \\?\ requires absolute pathes to be used.
Now all tests run
* used gofmt on the code
* Restructured Code. No patches dir, integrate the file functions into restic/fs/
There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string
* Changed the last os.Open() calls to fs.Open() after extending the File interface
* fixed name-clash of restic.fs and fuse.fs detected by travis
* fixed fmt with gofmt
* c&p failure: removed fixpath() call.
* missing include
* fixed includes in linux variant
* Fix for Linux. Fd() is required on File interface
* done gofmt
2016-08-15 19:59:13 +00:00
|
|
|
"restic/fs"
|
2016-02-14 14:29:28 +00:00
|
|
|
"restic/pack"
|
2015-11-18 19:20:25 +00:00
|
|
|
)
|
|
|
|
|
2016-03-05 14:58:39 +00:00
|
|
|
// Saver implements saving data in a backend.
|
|
|
|
type Saver interface {
|
2017-01-22 11:32:20 +00:00
|
|
|
Save(restic.Handle, io.Reader) error
|
2016-03-05 14:58:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 19:20:25 +00:00
|
|
|
// packerManager keeps a list of open packs and creates new on demand.
|
|
|
|
type packerManager struct {
|
2016-03-05 14:58:39 +00:00
|
|
|
be Saver
|
2015-11-18 19:20:25 +00:00
|
|
|
key *crypto.Key
|
|
|
|
pm sync.Mutex
|
|
|
|
packs []*pack.Packer
|
2016-03-06 11:26:25 +00:00
|
|
|
|
2016-03-06 12:14:06 +00:00
|
|
|
pool sync.Pool
|
2015-11-18 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 20:09:21 +00:00
|
|
|
const minPackSize = 4 * 1024 * 1024
|
|
|
|
const maxPackSize = 16 * 1024 * 1024
|
2015-11-18 19:20:25 +00:00
|
|
|
const maxPackers = 200
|
|
|
|
|
2016-03-06 13:20:48 +00:00
|
|
|
// newPackerManager returns an new packer manager which writes temporary files
|
2016-03-06 11:26:25 +00:00
|
|
|
// to a temporary directory
|
2016-03-06 13:20:48 +00:00
|
|
|
func newPackerManager(be Saver, key *crypto.Key) *packerManager {
|
2016-03-06 12:14:06 +00:00
|
|
|
return &packerManager{
|
2016-03-06 11:26:25 +00:00
|
|
|
be: be,
|
|
|
|
key: key,
|
2016-03-06 12:14:06 +00:00
|
|
|
pool: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return make([]byte, (minPackSize+maxPackSize)/2)
|
|
|
|
},
|
|
|
|
},
|
2016-03-06 11:26:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 19:20:25 +00:00
|
|
|
// findPacker returns a packer for a new blob of size bytes. Either a new one is
|
|
|
|
// created or one is returned that already has some blobs.
|
2016-03-06 12:14:06 +00:00
|
|
|
func (r *packerManager) findPacker(size uint) (packer *pack.Packer, err error) {
|
2015-11-18 19:20:25 +00:00
|
|
|
r.pm.Lock()
|
|
|
|
defer r.pm.Unlock()
|
|
|
|
|
|
|
|
// search for a suitable packer
|
|
|
|
if len(r.packs) > 0 {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("searching packer for %d bytes\n", size)
|
2015-11-18 19:20:25 +00:00
|
|
|
for i, p := range r.packs {
|
|
|
|
if p.Size()+size < maxPackSize {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("found packer %v", p)
|
2015-11-18 19:20:25 +00:00
|
|
|
// remove from list
|
|
|
|
r.packs = append(r.packs[:i], r.packs[i+1:]...)
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no suitable packer found, return new
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("create new pack for %d bytes", size)
|
2016-03-06 12:14:06 +00:00
|
|
|
tmpfile, err := ioutil.TempFile("", "restic-temp-pack-")
|
2016-03-06 11:26:25 +00:00
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return nil, errors.Wrap(err, "ioutil.TempFile")
|
2016-03-06 11:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return pack.NewPacker(r.key, tmpfile), nil
|
2015-11-18 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// insertPacker appends p to s.packs.
|
|
|
|
func (r *packerManager) insertPacker(p *pack.Packer) {
|
|
|
|
r.pm.Lock()
|
|
|
|
defer r.pm.Unlock()
|
|
|
|
|
|
|
|
r.packs = append(r.packs, p)
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("%d packers\n", len(r.packs))
|
2015-11-18 19:20:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// savePacker stores p in the backend.
|
|
|
|
func (r *Repository) savePacker(p *pack.Packer) error {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("save packer with %d blobs\n", p.Count())
|
2016-03-06 11:26:25 +00:00
|
|
|
n, err := p.Finalize()
|
2015-11-18 19:20:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-03-06 11:26:25 +00:00
|
|
|
tmpfile := p.Writer().(*os.File)
|
Fix 567 (#570)
* Patch for https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).
as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.
I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."
* fixed building tests
* Restructured patches
Add Wrapper for filepath.Walk
* using \\?\ requires absolute pathes to be used.
Now all tests run
* used gofmt on the code
* Restructured Code. No patches dir, integrate the file functions into restic/fs/
There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string
* Changed the last os.Open() calls to fs.Open() after extending the File interface
* fixed name-clash of restic.fs and fuse.fs detected by travis
* fixed fmt with gofmt
* c&p failure: removed fixpath() call.
* missing include
* fixed includes in linux variant
* Fix for Linux. Fd() is required on File interface
* done gofmt
2016-08-15 19:59:13 +00:00
|
|
|
f, err := fs.Open(tmpfile.Name())
|
2016-03-06 11:26:25 +00:00
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return errors.Wrap(err, "Open")
|
2016-03-06 11:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data := make([]byte, n)
|
|
|
|
m, err := io.ReadFull(f, data)
|
2016-08-29 20:16:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "ReadFul")
|
|
|
|
}
|
2016-03-06 11:26:25 +00:00
|
|
|
|
|
|
|
if uint(m) != n {
|
2016-08-21 15:48:36 +00:00
|
|
|
return errors.Errorf("read wrong number of bytes from %v: want %v, got %v", tmpfile.Name(), n, m)
|
2016-03-06 11:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = f.Close(); err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return errors.Wrap(err, "Close")
|
2016-03-06 11:26:25 +00:00
|
|
|
}
|
|
|
|
|
2016-08-31 18:29:54 +00:00
|
|
|
id := restic.Hash(data)
|
2016-09-01 19:19:30 +00:00
|
|
|
h := restic.Handle{Type: restic.DataFile, Name: id.String()}
|
2016-01-24 18:30:14 +00:00
|
|
|
|
2017-01-22 11:32:20 +00:00
|
|
|
err = r.be.Save(h, bytes.NewReader(data))
|
2015-11-18 19:20:25 +00:00
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("Save(%v) error: %v", h, err)
|
2015-11-18 19:20:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("saved as %v", h)
|
2015-11-18 19:20:25 +00:00
|
|
|
|
Fix 567 (#570)
* Patch for https://github.com/restic/restic/issues/567
Backup also files on windows with longer pathnames than 255 chars (e.g. from node).
as fd0 says "So, as far as I can see, we need to have custom methods for all functions that accept a path, so that on Windows we can substitute the normal (possibly relative) path used within restic by an (absolute) UNC path, and only then call the underlying functions like os.Stat(), os.Lstat(), os.Open() and so on.
I've already thought about adding a generic abstraction for the file system (so we can mock this easier in tests), and this looks like a good opportunity to build it."
* fixed building tests
* Restructured patches
Add Wrapper for filepath.Walk
* using \\?\ requires absolute pathes to be used.
Now all tests run
* used gofmt on the code
* Restructured Code. No patches dir, integrate the file functions into restic/fs/
There is still an issue, because restic.fs.Open has a different api the os.Open, which returns the result of OpenFile, but takes only a string
* Changed the last os.Open() calls to fs.Open() after extending the File interface
* fixed name-clash of restic.fs and fuse.fs detected by travis
* fixed fmt with gofmt
* c&p failure: removed fixpath() call.
* missing include
* fixed includes in linux variant
* Fix for Linux. Fd() is required on File interface
* done gofmt
2016-08-15 19:59:13 +00:00
|
|
|
err = fs.Remove(tmpfile.Name())
|
2016-03-06 12:14:06 +00:00
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return errors.Wrap(err, "Remove")
|
2016-03-06 12:14:06 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 19:20:25 +00:00
|
|
|
// update blobs in the index
|
|
|
|
for _, b := range p.Blobs() {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log(" updating blob %v to pack %v", b.ID.Str(), id.Str())
|
2017-01-02 13:14:51 +00:00
|
|
|
r.idx.Store(restic.PackedBlob{
|
2016-08-31 20:39:36 +00:00
|
|
|
Blob: restic.Blob{
|
|
|
|
Type: b.Type,
|
|
|
|
ID: b.ID,
|
|
|
|
Offset: b.Offset,
|
|
|
|
Length: uint(b.Length),
|
|
|
|
},
|
2016-01-24 18:30:14 +00:00
|
|
|
PackID: id,
|
2015-11-18 19:20:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// countPacker returns the number of open (unfinished) packers.
|
|
|
|
func (r *packerManager) countPacker() int {
|
|
|
|
r.pm.Lock()
|
|
|
|
defer r.pm.Unlock()
|
|
|
|
|
|
|
|
return len(r.packs)
|
|
|
|
}
|