Skip to content

Commit

Permalink
fix: use copy when a rename spans volumes (#22995)
Browse files Browse the repository at this point in the history
When a file rename fails with EXDEV
(cross device or volume error), copy the
file and delete the original instead.

closes #22890

Co-authored-by: Chris Pahl <[email protected]>
  • Loading branch information
davidby-influx and sahib authored Dec 14, 2021
1 parent f78c189 commit 4fd4bd0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
39 changes: 39 additions & 0 deletions pkg/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package fs

import (
"fmt"
"io"
"os"

"github.com/influxdata/influxdb/v2/pkg/errors"
)

// A FileExistsError is returned when an operation cannot be completed due to a
Expand All @@ -25,3 +29,38 @@ type DiskStatus struct {
Free uint64
Avail uint64
}

func copyFile(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return err
}

out, err := os.Create(dst)
if err != nil {
return err
}

defer errors.Capture(&err, out.Close)()

defer errors.Capture(&err, in.Close)()

if _, err = io.Copy(out, in); err != nil {
return err
}

return out.Sync()
}

// MoveFileWithReplacement copies the file contents at `src` to `dst`.
// and deletes `src` on success.
//
// If the file at `dst` already exists, it will be truncated and its contents
// overwritten.
func MoveFileWithReplacement(src, dst string) error {
if err := copyFile(src, dst); err != nil {
return fmt.Errorf("copy: %w", err)
}

return os.Remove(src)
}
17 changes: 13 additions & 4 deletions pkg/fs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ import (
)

func TestRenameFileWithReplacement(t *testing.T) {
testFileMoveOrRename(t, "rename", fs.RenameFileWithReplacement)
}

func TestMoveFileWithReplacement(t *testing.T) {
testFileMoveOrRename(t, "move", fs.MoveFileWithReplacement)
}

func testFileMoveOrRename(t *testing.T, name string, testFunc func(src string, dst string) error) {
// sample data for loading into files
sampleData1 := "this is some data"
sampleData2 := "we got some more data"
Expand All @@ -29,8 +37,8 @@ func TestRenameFileWithReplacement(t *testing.T) {
t.Fatalf("got contents %q, expected %q", got, exp)
}

if err := fs.RenameFileWithReplacement(oldpath, newpath); err != nil {
t.Fatalf("ReplaceFileIfExists returned an error: %s", err)
if err := testFunc(oldpath, newpath); err != nil {
t.Fatalf("%s returned an error: %s", name, err)
}

if err := fs.SyncDir(filepath.Dir(oldpath)); err != nil {
Expand Down Expand Up @@ -60,8 +68,9 @@ func TestRenameFileWithReplacement(t *testing.T) {

root := filepath.Dir(oldpath)
newpath := filepath.Join(root, "foo")
if err := fs.RenameFileWithReplacement(oldpath, newpath); err != nil {
t.Fatalf("ReplaceFileIfExists returned an error: %s", err)

if err := testFunc(oldpath, newpath); err != nil {
t.Fatalf("%s returned an error: %s", name, err)
}

if err := fs.SyncDir(filepath.Dir(oldpath)); err != nil {
Expand Down
15 changes: 12 additions & 3 deletions pkg/fs/fs_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package fs

import (
"errors"
"os"
"syscall"

Expand Down Expand Up @@ -34,13 +35,21 @@ func SyncDir(dirName string) error {
}

// RenameFileWithReplacement will replace any existing file at newpath with the contents
// of oldpath.
// of oldpath. It works also if it the rename spans over several file systems.
//
// If no file already exists at newpath, newpath will be created using the contents
// of oldpath. If this function returns successfully, the contents of newpath will
// be identical to oldpath, and oldpath will be removed.
func RenameFileWithReplacement(oldpath, newpath string) error {
return os.Rename(oldpath, newpath)
if err := os.Rename(oldpath, newpath); !errors.Is(err, syscall.EXDEV) {
// note: also includes err == nil
return err
}

// move over filesystem boundaries, we have to copy.
// (if there was another error, it will likely fail a second time)
return MoveFileWithReplacement(oldpath, newpath)

}

// RenameFile renames oldpath to newpath, returning an error if newpath already
Expand All @@ -51,7 +60,7 @@ func RenameFile(oldpath, newpath string) error {
return newFileExistsError(newpath)
}

return os.Rename(oldpath, newpath)
return RenameFileWithReplacement(oldpath, newpath)
}

// CreateFile creates a new file at newpath, returning an error if newpath already
Expand Down

0 comments on commit 4fd4bd0

Please sign in to comment.