2018-04-08 12:02:30 +00:00
|
|
|
package restorer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
)
|
|
|
|
|
2019-02-25 05:50:40 +00:00
|
|
|
// Writes blobs to output files. Each file is written sequentially,
|
|
|
|
// start to finish, but multiple files can be written to concurrently.
|
|
|
|
// Implementation allows virtually unlimited number of logically open
|
|
|
|
// files, but number of phisically open files will never exceed number
|
|
|
|
// of concurrent writeToFile invocations plus cacheCap.
|
2018-04-08 12:02:30 +00:00
|
|
|
type filesWriter struct {
|
2019-02-25 05:50:40 +00:00
|
|
|
lock sync.Mutex // guards concurrent access to open files cache
|
2018-09-15 00:18:37 +00:00
|
|
|
inprogress map[string]struct{} // (logically) opened file writers
|
2019-02-25 05:50:40 +00:00
|
|
|
cache map[string]*os.File // cache of open files
|
|
|
|
cacheCap int // max number of cached open files
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 05:50:40 +00:00
|
|
|
func newFilesWriter(cacheCap int) *filesWriter {
|
|
|
|
return &filesWriter{
|
|
|
|
inprogress: make(map[string]struct{}),
|
|
|
|
cache: make(map[string]*os.File),
|
|
|
|
cacheCap: cacheCap,
|
|
|
|
}
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 05:50:40 +00:00
|
|
|
func (w *filesWriter) writeToFile(path string, blob []byte) error {
|
|
|
|
// First writeToFile invocation for any given path will:
|
|
|
|
// - create and open the file
|
|
|
|
// - write the blob to the file
|
|
|
|
// - cache the open file if there is space, close the file otherwise
|
|
|
|
// Subsequent invocations will:
|
|
|
|
// - remove the open file from the cache _or_ open the file for append
|
|
|
|
// - write the blob to the file
|
|
|
|
// - cache the open file if there is space, close the file otherwise
|
|
|
|
// The idea is to cap maximum number of open files with minimal
|
|
|
|
// coordination among concurrent writeToFile invocations (note that
|
|
|
|
// writeToFile never touches somebody else's open file).
|
|
|
|
|
|
|
|
// TODO measure if caching is useful (likely depends on operating system
|
|
|
|
// and hardware configuration)
|
|
|
|
acquireWriter := func() (*os.File, error) {
|
2018-04-08 12:02:30 +00:00
|
|
|
w.lock.Lock()
|
|
|
|
defer w.lock.Unlock()
|
2019-02-25 05:50:40 +00:00
|
|
|
if wr, ok := w.cache[path]; ok {
|
2018-09-15 00:18:37 +00:00
|
|
|
debug.Log("Used cached writer for %s", path)
|
2019-02-25 05:50:40 +00:00
|
|
|
delete(w.cache, path)
|
|
|
|
return wr, nil
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
var flags int
|
2018-09-15 00:18:37 +00:00
|
|
|
if _, append := w.inprogress[path]; append {
|
2018-04-08 12:02:30 +00:00
|
|
|
flags = os.O_APPEND | os.O_WRONLY
|
|
|
|
} else {
|
2018-09-15 00:18:37 +00:00
|
|
|
w.inprogress[path] = struct{}{}
|
2018-04-08 12:02:30 +00:00
|
|
|
flags = os.O_CREATE | os.O_TRUNC | os.O_WRONLY
|
|
|
|
}
|
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-02-25 05:50:40 +00:00
|
|
|
debug.Log("Opened writer for %s", path)
|
2018-04-08 12:02:30 +00:00
|
|
|
return wr, nil
|
|
|
|
}
|
2019-02-25 05:50:40 +00:00
|
|
|
cacheOrCloseWriter := func(wr *os.File) {
|
|
|
|
w.lock.Lock()
|
|
|
|
defer w.lock.Unlock()
|
|
|
|
if len(w.cache) < w.cacheCap {
|
|
|
|
w.cache[path] = wr
|
|
|
|
} else {
|
|
|
|
wr.Close()
|
|
|
|
}
|
|
|
|
}
|
2018-04-08 12:02:30 +00:00
|
|
|
|
|
|
|
wr, err := acquireWriter()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-25 05:50:40 +00:00
|
|
|
n, err := wr.Write(blob)
|
|
|
|
cacheOrCloseWriter(wr)
|
2018-04-08 12:02:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-25 05:50:40 +00:00
|
|
|
if n != len(blob) {
|
|
|
|
return errors.Errorf("error writing file %v: wrong length written, want %d, got %d", path, len(blob), n)
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 00:18:37 +00:00
|
|
|
func (w *filesWriter) close(path string) {
|
2018-04-08 12:02:30 +00:00
|
|
|
w.lock.Lock()
|
|
|
|
defer w.lock.Unlock()
|
2019-02-25 05:50:40 +00:00
|
|
|
if wr, ok := w.cache[path]; ok {
|
|
|
|
wr.Close()
|
|
|
|
delete(w.cache, path)
|
|
|
|
}
|
2018-09-15 00:18:37 +00:00
|
|
|
delete(w.inprogress, path)
|
2018-04-08 12:02:30 +00:00
|
|
|
}
|