2018-04-08 12:02:30 +00:00
|
|
|
package restorer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2020-10-05 13:38:39 +00:00
|
|
|
"github.com/cespare/xxhash/v2"
|
2020-08-15 15:45:05 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2018-04-08 12:02:30 +00:00
|
|
|
)
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
// writes blobs to target files.
|
|
|
|
// multiple files can be written to concurrently.
|
|
|
|
// multiple blobs can be concurrently written to the same file.
|
|
|
|
// TODO I am not 100% convinced this is necessary, i.e. it may be okay
|
2022-08-19 17:12:26 +00:00
|
|
|
// to use multiple os.File to write to the same target file
|
2018-04-08 12:02:30 +00:00
|
|
|
type filesWriter struct {
|
2019-11-27 12:22:38 +00:00
|
|
|
buckets []filesWriterBucket
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
type filesWriterBucket struct {
|
|
|
|
lock sync.Mutex
|
2020-02-26 20:48:05 +00:00
|
|
|
files map[string]*partialFile
|
|
|
|
}
|
|
|
|
|
|
|
|
type partialFile struct {
|
|
|
|
*os.File
|
2022-08-07 15:56:14 +00:00
|
|
|
users int // Reference count.
|
2022-08-07 15:26:46 +00:00
|
|
|
sparse bool
|
2019-11-27 12:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newFilesWriter(count int) *filesWriter {
|
|
|
|
buckets := make([]filesWriterBucket, count)
|
|
|
|
for b := 0; b < count; b++ {
|
2020-02-26 20:48:05 +00:00
|
|
|
buckets[b].files = make(map[string]*partialFile)
|
2019-11-27 12:22:38 +00:00
|
|
|
}
|
2019-02-25 05:50:40 +00:00
|
|
|
return &filesWriter{
|
2019-11-27 12:22:38 +00:00
|
|
|
buckets: buckets,
|
2019-02-25 05:50:40 +00:00
|
|
|
}
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
2022-08-07 15:26:46 +00:00
|
|
|
func (w *filesWriter) writeToFile(path string, blob []byte, offset int64, createSize int64, sparse bool) error {
|
2019-11-27 12:22:38 +00:00
|
|
|
bucket := &w.buckets[uint(xxhash.Sum64String(path))%uint(len(w.buckets))]
|
|
|
|
|
2020-02-26 20:48:05 +00:00
|
|
|
acquireWriter := func() (*partialFile, error) {
|
2019-11-27 12:22:38 +00:00
|
|
|
bucket.lock.Lock()
|
|
|
|
defer bucket.lock.Unlock()
|
|
|
|
|
|
|
|
if wr, ok := bucket.files[path]; ok {
|
2020-02-26 20:48:05 +00:00
|
|
|
bucket.files[path].users++
|
2019-02-25 05:50:40 +00:00
|
|
|
return wr, nil
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
|
2018-04-08 12:02:30 +00:00
|
|
|
var flags int
|
2020-08-15 15:45:05 +00:00
|
|
|
if createSize >= 0 {
|
2018-04-08 12:02:30 +00:00
|
|
|
flags = os.O_CREATE | os.O_TRUNC | os.O_WRONLY
|
2019-11-27 12:22:38 +00:00
|
|
|
} else {
|
|
|
|
flags = os.O_WRONLY
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
|
2020-02-26 20:48:05 +00:00
|
|
|
f, err := os.OpenFile(path, flags, 0600)
|
2018-04-08 12:02:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
|
2022-08-07 15:26:46 +00:00
|
|
|
wr := &partialFile{File: f, users: 1, sparse: sparse}
|
2019-11-27 12:22:38 +00:00
|
|
|
bucket.files[path] = wr
|
|
|
|
|
2022-08-07 15:56:14 +00:00
|
|
|
if createSize >= 0 {
|
|
|
|
if sparse {
|
|
|
|
err = f.Truncate(createSize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := preallocateFile(wr.File, createSize)
|
|
|
|
if err != nil {
|
|
|
|
// Just log the preallocate error but don't let it cause the restore process to fail.
|
|
|
|
// Preallocate might return an error if the filesystem (implementation) does not
|
|
|
|
// support preallocation or our parameters combination to the preallocate call
|
|
|
|
// This should yield a syscall.ENOTSUP error, but some other errors might also
|
|
|
|
// show up.
|
|
|
|
debug.Log("Failed to preallocate %v with size %v: %v", path, createSize, err)
|
|
|
|
}
|
2020-08-15 15:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-08 12:02:30 +00:00
|
|
|
return wr, nil
|
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
|
2020-02-26 20:48:05 +00:00
|
|
|
releaseWriter := func(wr *partialFile) error {
|
2019-11-27 12:22:38 +00:00
|
|
|
bucket.lock.Lock()
|
|
|
|
defer bucket.lock.Unlock()
|
|
|
|
|
2020-02-26 20:48:05 +00:00
|
|
|
if bucket.files[path].users == 1 {
|
2019-11-27 12:22:38 +00:00
|
|
|
delete(bucket.files, path)
|
|
|
|
return wr.Close()
|
2019-02-25 05:50:40 +00:00
|
|
|
}
|
2020-02-26 20:48:05 +00:00
|
|
|
bucket.files[path].users--
|
2019-11-27 12:22:38 +00:00
|
|
|
return nil
|
2019-02-25 05:50:40 +00:00
|
|
|
}
|
2018-04-08 12:02:30 +00:00
|
|
|
|
|
|
|
wr, err := acquireWriter()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
|
|
|
|
_, err = wr.WriteAt(blob, offset)
|
|
|
|
|
2018-04-08 12:02:30 +00:00
|
|
|
if err != nil {
|
2021-01-30 18:35:46 +00:00
|
|
|
// ignore subsequent errors
|
|
|
|
_ = releaseWriter(wr)
|
2018-04-08 12:02:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
return releaseWriter(wr)
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|