2
2
mirror of https://github.com/octoleo/restic.git synced 2024-06-22 10:34:51 +00:00

restorer: Fix race condition in partialFile.WriteAt

The restorer can issue multiple calls to WriteAt in parallel. This can
result in unexpected orderings of the Truncate and WriteAt calls and
sometimes too short restored files.
This commit is contained in:
Michael Eischer 2022-08-07 17:56:14 +02:00
parent 5b6a77058a
commit a5ebd5de4b
2 changed files with 20 additions and 32 deletions

View File

@ -24,7 +24,6 @@ type filesWriterBucket struct {
type partialFile struct { type partialFile struct {
*os.File *os.File
size int64 // File size, tracked for sparse writes (not on Windows).
users int // Reference count. users int // Reference count.
sparse bool sparse bool
} }
@ -64,16 +63,15 @@ func (w *filesWriter) writeToFile(path string, blob []byte, offset int64, create
} }
wr := &partialFile{File: f, users: 1, sparse: sparse} wr := &partialFile{File: f, users: 1, sparse: sparse}
if createSize < 0 { bucket.files[path] = wr
info, err := f.Stat()
if createSize >= 0 {
if sparse {
err = f.Truncate(createSize)
if err != nil { if err != nil {
return nil, err return nil, err
} }
wr.size = info.Size() } else {
}
bucket.files[path] = wr
if createSize >= 0 && !sparse {
err := preallocateFile(wr.File, createSize) err := preallocateFile(wr.File, createSize)
if err != nil { if err != nil {
// Just log the preallocate error but don't let it cause the restore process to fail. // Just log the preallocate error but don't let it cause the restore process to fail.
@ -84,6 +82,7 @@ func (w *filesWriter) writeToFile(path string, blob []byte, offset int64, create
debug.Log("Failed to preallocate %v with size %v: %v", path, createSize, err) debug.Log("Failed to preallocate %v with size %v: %v", path, createSize, err)
} }
} }
}
return wr, nil return wr, nil
} }

View File

@ -13,7 +13,6 @@ func (f *partialFile) WriteAt(p []byte, offset int64) (n int, err error) {
} }
n = len(p) n = len(p)
end := offset + int64(n)
// Skip the longest all-zero prefix of p. // Skip the longest all-zero prefix of p.
// If it's long enough, we can punch a hole in the file. // If it's long enough, we can punch a hole in the file.
@ -22,26 +21,16 @@ func (f *partialFile) WriteAt(p []byte, offset int64) (n int, err error) {
offset += int64(skipped) offset += int64(skipped)
switch { switch {
case len(p) == 0 && end > f.size:
// We need to do a Truncate, as WriteAt with length-0 input
// doesn't actually extend the file.
err = f.Truncate(end)
if err != nil {
return 0, err
}
case len(p) == 0: case len(p) == 0:
// All zeros, file already big enough. A previous WriteAt or // All zeros, file already big enough. A previous WriteAt or
// Truncate will have produced the zeros in f.File. // Truncate will have produced the zeros in f.File.
default: default:
n, err = f.File.WriteAt(p, offset) var n2 int
n2, err = f.File.WriteAt(p, offset)
n = skipped + n2
} }
end = offset + int64(n)
if end > f.size {
f.size = end
}
return n, err return n, err
} }