@@ -461,7 +461,7 @@ defmodule File do
461
461
error will be returned.
462
462
"""
463
463
@ spec cp ( Path . t , Path . t , ( Path . t , Path . t -> boolean ) ) :: :ok | { :error , posix }
464
- def cp ( source , destination , callback \\ fn ( _ , _ ) -> true end ) do
464
+ def cp ( source , destination , callback \\ & path_differs? / 2 ) do
465
465
source = IO . chardata_to_string ( source )
466
466
destination = IO . chardata_to_string ( destination )
467
467
@@ -471,12 +471,19 @@ defmodule File do
471
471
end
472
472
end
473
473
474
+ defp path_differs? ( path , path ) ,
475
+ do: false
476
+
477
+ defp path_differs? ( p1 , p2 ) do
478
+ Path . expand ( p1 ) !== Path . expand ( p2 )
479
+ end
480
+
474
481
@ doc """
475
482
The same as `cp/3`, but raises `File.CopyError` if it fails.
476
483
Returns the list of copied files otherwise.
477
484
"""
478
485
@ spec cp! ( Path . t , Path . t , ( Path . t , Path . t -> boolean ) ) :: :ok | no_return
479
- def cp! ( source , destination , callback \\ fn ( _ , _ ) -> true end ) do
486
+ def cp! ( source , destination , callback \\ & path_differs? / 2 ) do
480
487
case cp ( source , destination , callback ) do
481
488
:ok -> :ok
482
489
{ :error , reason } ->
@@ -599,8 +606,6 @@ defmodule File do
599
606
[ dest | acc ]
600
607
{ :error , :eexist } ->
601
608
if callback . ( src , dest ) do
602
- # If rm/1 fails, copy/2 will fail
603
- _ = rm ( dest )
604
609
case copy ( src , dest ) do
605
610
{ :ok , _ } ->
606
611
copy_file_mode! ( src , dest )
@@ -621,7 +626,7 @@ defmodule File do
621
626
[ dest | acc ]
622
627
{ :error , :eexist } ->
623
628
if callback . ( src , dest ) do
624
- # If rm/1 fails, iF .make_symlink/2 will fail
629
+ # If rm/1 fails, F .make_symlink/2 will fail
625
630
_ = rm ( dest )
626
631
case F . make_symlink ( link , dest ) do
627
632
:ok -> [ dest | acc ]
0 commit comments