From d58a0d27943dd6b24c7242f0f3ccaedd2bffbd4a Mon Sep 17 00:00:00 2001 From: andrew2nelson Date: Mon, 31 Aug 2020 14:54:29 -0700 Subject: [PATCH] File read-able check should ignore file creation flags Flags like O_CREATE and O_TRUNCATE don't actually impact the access that a process can have to an open file. When checking whether a process has read access, all that really matters is that the file isn't opened with O_WRONLY. --- memfs/memory.go | 2 +- test/basic.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/memfs/memory.go b/memfs/memory.go index d4ac056..7258c02 100644 --- a/memfs/memory.go +++ b/memfs/memory.go @@ -229,7 +229,7 @@ func (f *file) ReadAt(b []byte, off int64) (int, error) { return 0, os.ErrClosed } - if !isReadAndWrite(f.flag) && !isReadOnly(f.flag) { + if isWriteOnly(f.flag) { return 0, errors.New("read not supported") } diff --git a/test/basic.go b/test/basic.go index ec569dc..37c9d22 100644 --- a/test/basic.go +++ b/test/basic.go @@ -116,6 +116,12 @@ func (s *BasicSuite) TestOpenFile(c *C) { c.Assert(f.Name(), Equals, "foo1") s.testReadClose(c, f, "foo1overwritten") + // Read-only if it exists + f, err = s.FS.OpenFile("foo1", os.O_RDONLY|os.O_CREATE, defaultMode) + c.Assert(err, IsNil) + c.Assert(f.Name(), Equals, "foo1") + s.testReadClose(c, f, "foo1overwritten") + // Create when it does exist f, err = s.FS.OpenFile("foo1", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, defaultMode) c.Assert(err, IsNil)