From 6f08edfb36a512f9a6650e7c38a7a78dea5ec942 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas <634494+krishnasrinivas@users.noreply.github.com> Date: Fri, 1 Feb 2019 19:01:06 -0800 Subject: [PATCH] Use O_EXCL when creating file as we never overwrite an existing file (#7189) --- cmd/posix.go | 5 +++-- cmd/xl-v1-object_test.go | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cmd/posix.go b/cmd/posix.go index 01b49b933..4f391ea5d 100644 --- a/cmd/posix.go +++ b/cmd/posix.go @@ -1105,8 +1105,9 @@ func (s *posix) WriteAll(volume, path string, buf []byte) (err error) { return errFaultyDisk } - // Create file if not found - w, err := s.openFile(volume, path, os.O_CREATE|os.O_SYNC|os.O_WRONLY) + // Create file if not found. Note that it is created with os.O_EXCL flag as the file + // always is supposed to be created in the tmp directory with a unique file name. + w, err := s.openFile(volume, path, os.O_CREATE|os.O_SYNC|os.O_WRONLY|os.O_EXCL) if err != nil { return err } diff --git a/cmd/xl-v1-object_test.go b/cmd/xl-v1-object_test.go index b80f6d37f..597285436 100644 --- a/cmd/xl-v1-object_test.go +++ b/cmd/xl-v1-object_test.go @@ -323,6 +323,11 @@ func TestHealing(t *testing.T) { t.Fatal("HealObject failed") } + err = os.RemoveAll(path.Join(fsDirs[0], bucket, object, "xl.json")) + if err != nil { + t.Fatal(err) + } + // Write xl.json with different modtime to simulate the case where a disk had // gone down when an object was replaced by a new object. xlMetaOutDated := xlMetaPreHeal