Sync directory to disk after upload

After a file was uploaded, also sync its containing directory to disk to
make sure that also the directory entry is persisted after a system
crash.
This commit is contained in:
Michael Eischer 2021-01-29 19:02:22 +01:00
parent 82816c67e1
commit 2175029c9e

View file

@ -627,9 +627,28 @@ func (h *Handler) saveBlob(w http.ResponseWriter, r *http.Request) {
return return
} }
if err := syncDir(filepath.Dir(path)); err != nil {
// Don't call os.Remove(path) as this is prone to race conditions with parallel upload retries
h.internalServerError(w, err)
return
}
h.sendMetric(objectType, BlobWrite, uint64(written)) h.sendMetric(objectType, BlobWrite, uint64(written))
} }
func syncDir(dirname string) error {
dir, err := os.Open(dirname)
if err != nil {
return err
}
err = dir.Sync()
if err != nil {
_ = dir.Close()
return err
}
return dir.Close()
}
// deleteBlob deletes a blob from the repository. // deleteBlob deletes a blob from the repository.
func (h *Handler) deleteBlob(w http.ResponseWriter, r *http.Request) { func (h *Handler) deleteBlob(w http.ResponseWriter, r *http.Request) {
if h.opt.Debug { if h.opt.Debug {