repo: fixed race condition during Open() where we may read incomplete file

This commit is contained in:
Jarek Kowalski
2020-03-23 06:16:12 -07:00
parent f2e6d51530
commit ea510f30fa

View File

@@ -1,12 +1,14 @@
package repo
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"path/filepath"
"time"
"github.com/natefinch/atomic"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo/blob"
@@ -196,7 +198,7 @@ func readAndCacheFormatBlobBytes(ctx context.Context, st blob.Storage, cacheDire
}
if cacheDirectory != "" {
if err := ioutil.WriteFile(cachedFile, b, 0600); err != nil {
if err := atomic.WriteFile(cachedFile, bytes.NewReader(b)); err != nil {
log(ctx).Warningf("warning: unable to write cache: %v", err)
}
}