2018-04-08 12:02:30 +00:00
|
|
|
package restorer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
"github.com/cespare/xxhash"
|
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
|
|
|
|
// 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
|
|
|
|
files map[string]*os.File
|
|
|
|
users map[string]int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFilesWriter(count int) *filesWriter {
|
|
|
|
buckets := make([]filesWriterBucket, count)
|
|
|
|
for b := 0; b < count; b++ {
|
|
|
|
buckets[b].files = make(map[string]*os.File)
|
|
|
|
buckets[b].users = make(map[string]int)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-11-27 12:22:38 +00:00
|
|
|
func (w *filesWriter) writeToFile(path string, blob []byte, offset int64, create bool) error {
|
|
|
|
bucket := &w.buckets[uint(xxhash.Sum64String(path))%uint(len(w.buckets))]
|
|
|
|
|
2019-02-25 05:50:40 +00:00
|
|
|
acquireWriter := func() (*os.File, error) {
|
2019-11-27 12:22:38 +00:00
|
|
|
bucket.lock.Lock()
|
|
|
|
defer bucket.lock.Unlock()
|
|
|
|
|
|
|
|
if wr, ok := bucket.files[path]; ok {
|
|
|
|
bucket.users[path]++
|
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
|
2019-11-27 12:22:38 +00:00
|
|
|
if create {
|
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
|
|
|
|
2018-09-15 00:18:37 +00:00
|
|
|
wr, 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
|
|
|
|
|
|
|
bucket.files[path] = wr
|
|
|
|
bucket.users[path] = 1
|
|
|
|
|
2018-04-08 12:02:30 +00:00
|
|
|
return wr, nil
|
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
|
|
|
|
releaseWriter := func(wr *os.File) error {
|
|
|
|
bucket.lock.Lock()
|
|
|
|
defer bucket.lock.Unlock()
|
|
|
|
|
|
|
|
if bucket.users[path] == 1 {
|
|
|
|
delete(bucket.files, path)
|
|
|
|
delete(bucket.users, path)
|
|
|
|
return wr.Close()
|
2019-02-25 05:50:40 +00:00
|
|
|
}
|
2019-11-27 12:22:38 +00:00
|
|
|
bucket.users[path]--
|
|
|
|
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 {
|
2019-11-27 12:22:38 +00:00
|
|
|
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
|
|
|
}
|