From d7908c06c9daf151a28c10b81c6135a522052849 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 7 Nov 2017 17:27:53 +0000 Subject: [PATCH] mountlib: ensure we don't open files with read and write intent --- cmd/mountlib/mounttest/file.go | 7 ++++++- cmd/mountlib/mounttest/write.go | 7 +++---- cmd/mountlib/mounttest/write_unix.go | 3 +-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cmd/mountlib/mounttest/file.go b/cmd/mountlib/mounttest/file.go index 60db071db..e36503e35 100644 --- a/cmd/mountlib/mounttest/file.go +++ b/cmd/mountlib/mounttest/file.go @@ -28,6 +28,11 @@ func TestFileModTime(t *testing.T) { run.rm(t, "file") } +// os.Create without opening for write too +func osCreate(name string) (*os.File, error) { + return os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666) +} + // TestFileModTimeWithOpenWriters tests mod time on open files func TestFileModTimeWithOpenWriters(t *testing.T) { run.skipIfNoFUSE(t) @@ -35,7 +40,7 @@ func TestFileModTimeWithOpenWriters(t *testing.T) { mtime := time.Date(2012, 11, 18, 17, 32, 31, 0, time.UTC) filepath := run.path("cp-archive-test") - f, err := os.Create(filepath) + f, err := osCreate(filepath) require.NoError(t, err) _, err = f.Write([]byte{104, 105}) diff --git a/cmd/mountlib/mounttest/write.go b/cmd/mountlib/mounttest/write.go index d28468763..e2ce7868d 100644 --- a/cmd/mountlib/mounttest/write.go +++ b/cmd/mountlib/mounttest/write.go @@ -1,7 +1,6 @@ package mounttest import ( - "os" "testing" "time" @@ -13,7 +12,7 @@ import ( func TestWriteFileNoWrite(t *testing.T) { run.skipIfNoFUSE(t) - fd, err := os.Create(run.path("testnowrite")) + fd, err := osCreate(run.path("testnowrite")) assert.NoError(t, err) err = fd.Close() @@ -31,7 +30,7 @@ func TestWriteFileNoWrite(t *testing.T) { func FIXMETestWriteOpenFileInDirListing(t *testing.T) { run.skipIfNoFUSE(t) - fd, err := os.Create(run.path("testnowrite")) + fd, err := osCreate(run.path("testnowrite")) assert.NoError(t, err) run.checkDir(t, "testnowrite 0") @@ -70,7 +69,7 @@ func TestWriteFileOverwrite(t *testing.T) { // NB the code for this is in file.go rather than write.go func TestWriteFileFsync(t *testing.T) { filepath := run.path("to be synced") - fd, err := os.Create(filepath) + fd, err := osCreate(filepath) require.NoError(t, err) _, err = fd.Write([]byte("hello")) require.NoError(t, err) diff --git a/cmd/mountlib/mounttest/write_unix.go b/cmd/mountlib/mounttest/write_unix.go index d2b6a5337..01f17d41b 100644 --- a/cmd/mountlib/mounttest/write_unix.go +++ b/cmd/mountlib/mounttest/write_unix.go @@ -3,7 +3,6 @@ package mounttest import ( - "os" "syscall" "testing" @@ -14,7 +13,7 @@ import ( func TestWriteFileDoubleClose(t *testing.T) { run.skipIfNoFUSE(t) - out, err := os.Create(run.path("testdoubleclose")) + out, err := osCreate(run.path("testdoubleclose")) assert.NoError(t, err) fd := out.Fd()