Skip to content

Commit

Permalink
Renaming an open file does not work on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalif committed Jun 19, 2023
1 parent ff7c657 commit 5bf3c5e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 88 deletions.
88 changes: 0 additions & 88 deletions internal/goofys_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,94 +134,6 @@ func (s *GoofysTest) TestWriteSyncWriteFuse(t *C) {
t.Assert(err, IsNil)
}

func (s *GoofysTest) TestCreateRenameBeforeCloseFuse(t *C) {
if s.azurite {
// Azurite returns 400 when copy source doesn't exist
// https://github.com/Azure/Azurite/issues/219
// so our code to ignore ENOENT fails
t.Skip("https://github.com/Azure/Azurite/issues/219")
}

mountPoint := s.tmp + "/mnt" + s.fs.bucket

s.mount(t, mountPoint)
defer s.umount(t, mountPoint)

from := mountPoint + "/newfile"
to := mountPoint + "/newfile2"

fh, err := os.Create(from)
t.Assert(err, IsNil)
defer func() {
// close the file if the test failed so we can unmount
if fh != nil {
fh.Close()
}
}()

_, err = fh.WriteString("hello world")
t.Assert(err, IsNil)

err = os.Rename(from, to)
t.Assert(err, IsNil)

err = fh.Close()
t.Assert(err, IsNil)
fh = nil

_, err = os.Stat(from)
t.Assert(err, NotNil)
pathErr, ok := err.(*os.PathError)
t.Assert(ok, Equals, true)
t.Assert(pathErr.Err, Equals, syscall.ENOENT)

content, err := ioutil.ReadFile(to)
t.Assert(err, IsNil)
t.Assert(string(content), Equals, "hello world")
}

func (s *GoofysTest) TestRenameBeforeCloseFuse(t *C) {
mountPoint := s.tmp + "/mnt" + s.fs.bucket

s.mount(t, mountPoint)
defer s.umount(t, mountPoint)

from := mountPoint + "/newfile"
to := mountPoint + "/newfile2"

err := ioutil.WriteFile(from, []byte(""), 0600)
t.Assert(err, IsNil)

fh, err := os.OpenFile(from, os.O_WRONLY, 0600)
t.Assert(err, IsNil)
defer func() {
// close the file if the test failed so we can unmount
if fh != nil {
fh.Close()
}
}()

_, err = fh.WriteString("hello world")
t.Assert(err, IsNil)

err = os.Rename(from, to)
t.Assert(err, IsNil)

err = fh.Close()
t.Assert(err, IsNil)
fh = nil

_, err = os.Stat(from)
t.Assert(err, NotNil)
pathErr, ok := err.(*os.PathError)
t.Assert(ok, Equals, true)
t.Assert(pathErr.Err, Equals, syscall.ENOENT)

content, err := ioutil.ReadFile(to)
t.Assert(err, IsNil)
t.Assert(string(content), Equals, "hello world")
}

func (s *GoofysTest) TestReadDirSlurpContinuation(t *C) {
if _, ok := s.cloud.Delegate().(*S3Backend); !ok {
t.Skip("only for S3")
Expand Down
88 changes: 88 additions & 0 deletions internal/goofys_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,94 @@ func (s *GoofysTest) TestPythonCopyTree(t *C) {
mountPoint)
}

func (s *GoofysTest) TestCreateRenameBeforeCloseFuse(t *C) {
if s.azurite {
// Azurite returns 400 when copy source doesn't exist
// https://github.com/Azure/Azurite/issues/219
// so our code to ignore ENOENT fails
t.Skip("https://github.com/Azure/Azurite/issues/219")
}

mountPoint := s.tmp + "/mnt" + s.fs.bucket

s.mount(t, mountPoint)
defer s.umount(t, mountPoint)

from := mountPoint + "/newfile"
to := mountPoint + "/newfile2"

fh, err := os.Create(from)
t.Assert(err, IsNil)
defer func() {
// close the file if the test failed so we can unmount
if fh != nil {
fh.Close()
}
}()

_, err = fh.WriteString("hello world")
t.Assert(err, IsNil)

err = os.Rename(from, to)
t.Assert(err, IsNil)

err = fh.Close()
t.Assert(err, IsNil)
fh = nil

_, err = os.Stat(from)
t.Assert(err, NotNil)
pathErr, ok := err.(*os.PathError)
t.Assert(ok, Equals, true)
t.Assert(pathErr.Err, Equals, syscall.ENOENT)

content, err := ioutil.ReadFile(to)
t.Assert(err, IsNil)
t.Assert(string(content), Equals, "hello world")
}

func (s *GoofysTest) TestRenameBeforeCloseFuse(t *C) {
mountPoint := s.tmp + "/mnt" + s.fs.bucket

s.mount(t, mountPoint)
defer s.umount(t, mountPoint)

from := mountPoint + "/newfile"
to := mountPoint + "/newfile2"

err := ioutil.WriteFile(from, []byte(""), 0600)
t.Assert(err, IsNil)

fh, err := os.OpenFile(from, os.O_WRONLY, 0600)
t.Assert(err, IsNil)
defer func() {
// close the file if the test failed so we can unmount
if fh != nil {
fh.Close()
}
}()

_, err = fh.WriteString("hello world")
t.Assert(err, IsNil)

err = os.Rename(from, to)
t.Assert(err, IsNil)

err = fh.Close()
t.Assert(err, IsNil)
fh = nil

_, err = os.Stat(from)
t.Assert(err, NotNil)
pathErr, ok := err.(*os.PathError)
t.Assert(ok, Equals, true)
t.Assert(pathErr.Err, Equals, syscall.ENOENT)

content, err := ioutil.ReadFile(to)
t.Assert(err, IsNil)
t.Assert(string(content), Equals, "hello world")
}

func containsFile(dir, wantedFile string) bool {
files, err := os.ReadDir(dir)
if err != nil {
Expand Down

0 comments on commit 5bf3c5e

Please sign in to comment.