Skip to content

Commit 225b25c

Browse files
Fix CopyTo same filepath truncate the file (#24)
1 parent 7d88805 commit 225b25c

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

paths.go

+4
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ func (p *Path) IsDirCheck() (bool, error) {
317317
// of the source file. The file mode will be copied from the source and
318318
// the copied data is synced/flushed to stable storage.
319319
func (p *Path) CopyTo(dst *Path) error {
320+
if p.EqualsTo(dst) {
321+
return fmt.Errorf("%s and %s are the same file", p.path, dst.path)
322+
}
323+
320324
in, err := os.Open(p.path)
321325
if err != nil {
322326
return err

paths_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,21 @@ func TestWriteToTempFile(t *testing.T) {
388388
require.NoError(t, err)
389389
require.Equal(t, tmpData, data)
390390
}
391+
392+
func TestCopyToSamePath(t *testing.T) {
393+
tmpDir := New(t.TempDir())
394+
srcFile := tmpDir.Join("test_file")
395+
dstFile := srcFile
396+
397+
// create the source file in tmp dir
398+
err := srcFile.WriteFile([]byte("hello"))
399+
require.NoError(t, err)
400+
content, err := srcFile.ReadFile()
401+
require.NoError(t, err)
402+
require.Equal(t, []byte("hello"), content)
403+
404+
// cannot copy the same file
405+
err = srcFile.CopyTo(dstFile)
406+
require.Error(t, err)
407+
require.Contains(t, err.Error(), "are the same file")
408+
}

0 commit comments

Comments
 (0)