Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 24 additions & 6 deletions lib/elixir/lib/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,9 @@ defmodule File do
if source_parts != dest_parts and List.starts_with?(dest_parts, source_parts) do
{:error, :einval, destination}
else
case do_cp_r(source, destination, on_conflict, dereference?, []) do
dereference = if dereference?, do: MapSet.new(), else: nil

case do_cp_r(source, destination, on_conflict, dereference, []) do
{:error, _, _} = error -> error
res -> {:ok, res}
end
Expand Down Expand Up @@ -1240,7 +1242,7 @@ defmodule File do
end
end

defp do_cp_r(src, dest, on_conflict, dereference?, acc) when is_list(acc) do
defp do_cp_r(src, dest, on_conflict, dereference, acc) when is_list(acc) do
case :elixir_utils.read_link_type(src) do
{:ok, :regular} ->
case do_cp_file(src, dest, on_conflict, acc) do
Expand All @@ -1253,8 +1255,15 @@ defmodule File do

{:ok, :symlink} ->
case :file.read_link(src) do
{:ok, link} when dereference? ->
do_cp_r(Path.expand(link, Path.dirname(src)), dest, on_conflict, dereference?, acc)
{:ok, link} when dereference != nil ->
resolved = Path.expand(link, Path.dirname(src))

if MapSet.member?(dereference, resolved) do
{:error, :eloop, src}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eloop is a new code, right? It may be better to use one of the existing ones, such as einval.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a standard posix error and I think the semantically correct for this case:

iex> :file.format_error(:eloop)
~c"too many levels of symbolic links"

else
dereference = MapSet.put(dereference, resolved)
do_cp_r(resolved, dest, on_conflict, dereference, acc)
end

{:ok, link} ->
do_cp_link(link, src, dest, on_conflict, acc)
Expand All @@ -1268,8 +1277,17 @@ defmodule File do
{:ok, files} ->
case mkdir(dest) do
success when success in [:ok, {:error, :eexist}] ->
Enum.reduce(files, [dest | acc], fn x, acc ->
do_cp_r(Path.join(src, x), Path.join(dest, x), on_conflict, dereference?, acc)
Enum.reduce_while(files, [dest | acc], fn x, acc ->
case do_cp_r(
Path.join(src, x),
Path.join(dest, x),
on_conflict,
dereference,
acc
) do
{:error, _, _} = error -> {:halt, error}
acc -> {:cont, acc}
end
end)

{:error, reason} ->
Expand Down
17 changes: 17 additions & 0 deletions lib/elixir/test/elixir/file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,23 @@ defmodule FileTest do
end
end

@tag :unix
test "cp_r with dereference symlink cycle returns eloop error" do
src = tmp_path("tmp/src")
dest = tmp_path("tmp/dest")

File.mkdir_p!(src)
:ok = :file.make_symlink(Path.join(src, "b"), Path.join(src, "a"))
:ok = :file.make_symlink(Path.join(src, "a"), Path.join(src, "b"))

try do
assert {:error, :eloop, _} = File.cp_r(src, dest, dereference_symlinks: true)
after
File.rm_rf(src)
File.rm_rf(dest)
end
end

test "cp_r with dir and file conflict" do
src = fixture_path("cp_r")
dest = tmp_path("tmp")
Expand Down