File tree 2 files changed +22
-0
lines changed
2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change @@ -317,6 +317,10 @@ func (p *Path) IsDirCheck() (bool, error) {
317
317
// of the source file. The file mode will be copied from the source and
318
318
// the copied data is synced/flushed to stable storage.
319
319
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
+
320
324
in , err := os .Open (p .path )
321
325
if err != nil {
322
326
return err
Original file line number Diff line number Diff line change @@ -388,3 +388,21 @@ func TestWriteToTempFile(t *testing.T) {
388
388
require .NoError (t , err )
389
389
require .Equal (t , tmpData , data )
390
390
}
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
+ }
You can’t perform that action at this time.
0 commit comments