Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utils: Renaming files across devices #5977

Merged
merged 8 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions util/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,37 @@
package util

import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"syscall"
)

func MoveFile(src, dst string) error {

Check failure on line 29 in util/io.go

View workflow job for this annotation

GitHub Actions / reviewdog-errors

[Lint Errors] reported by reviewdog 🐶 exported: exported function MoveFile should have comment or be unexported (revive) Raw Output: util/io.go:29:1: exported: exported function MoveFile should have comment or be unexported (revive) func MoveFile(src, dst string) error { ^
err := os.Rename(src, dst)
var errno syscall.Errno
if err != nil && errors.As(err, &errno) && errno == syscall.EXDEV {
jannotti marked this conversation as resolved.
Show resolved Hide resolved
jannotti marked this conversation as resolved.
Show resolved Hide resolved
// os.Rename() failed because src and dst are on different filesystems
// Must manually copy and delete the original file
return manuallyMoveFile(src, dst)
}
return err
}

func manuallyMoveFile(src, dst string) error {
jasonpaulos marked this conversation as resolved.
Show resolved Hide resolved
tmpDst := dst + ".tmp"
jasonpaulos marked this conversation as resolved.
Show resolved Hide resolved
if _, err := CopyFile(src, tmpDst); err != nil {
algorandskiy marked this conversation as resolved.
Show resolved Hide resolved
jasonpaulos marked this conversation as resolved.
Show resolved Hide resolved
return err
}
if err := os.Rename(tmpDst, dst); err != nil {
return err
jasonpaulos marked this conversation as resolved.
Show resolved Hide resolved
}
return os.Remove(src)
}

// CopyFile uses io.Copy() to copy a file to another location
// This was copied from https://opensource.com/article/18/6/copying-files-go
func CopyFile(src, dst string) (int64, error) {
Expand Down
57 changes: 57 additions & 0 deletions util/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ package util

import (
"os"
"os/exec"
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/algorand/go-algorand/test/partitiontest"
)

func TestIsEmpty(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

testPath := path.Join(os.TempDir(), "this", "is", "a", "long", "path")
err := os.MkdirAll(testPath, os.ModePerm)
Expand All @@ -39,3 +42,57 @@ func TestIsEmpty(t *testing.T) {
assert.NoError(t, err)
assert.False(t, IsEmpty(testPath))
}

func testMoveFile(t *testing.T, src, dst string) {
t.Helper()

require.NoFileExists(t, src)
require.NoFileExists(t, dst)

defer os.Remove(src)
defer os.Remove(dst)

f, err := os.Create(src)
require.NoError(t, err)

_, err = f.WriteString("test file contents")
require.NoError(t, err)
require.NoError(t, f.Close())

err = MoveFile(src, dst)
require.NoError(t, err)

require.FileExists(t, dst)
require.NoFileExists(t, src)

dstContents, err := os.ReadFile(dst)
require.NoError(t, err)
assert.Equal(t, "test file contents", string(dstContents))
}

func TestMoveFile(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

src := path.Join(os.TempDir(), "src.txt")
dst := path.Join(os.TempDir(), "dst.txt")
testMoveFile(t, src, dst)
}

func TestMoveFileAcrossFilesystems(t *testing.T) {
partitiontest.PartitionTest(t)

t.Skip("This is a manual test that must be run on a linux system")

os.Mkdir("/mnt/tmpfs", os.ModePerm)
defer os.Remove("/mnt/tmpfs")

err := exec.Command("mount", "-t", "tmpfs", "-o", "size=1K", "tmpfs", "/mnt/tmpfs").Run()
ohill marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
defer exec.Command("umount", "/mnt/tmpfs").Run()

src := path.Join(os.TempDir(), "src.txt")
dst := path.Join("/mnt/tmpfs", "dst.txt")

testMoveFile(t, src, dst)
}
Loading