diff options
author | sabiwara <sabiwara@gmail.com> | 2022-07-11 19:20:41 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-11 12:20:41 +0200 |
commit | 195d7cd12d66291f942fc783e69883b372e78888 (patch) | |
tree | 4db1ecd5cce8baeca83e527df1261a68d712a3cb /lib/elixir | |
parent | d5a2233cc4eb4331189f26f3629a2f989066f0cf (diff) | |
download | elixir-195d7cd12d66291f942fc783e69883b372e78888.tar.gz |
Have File.rename/2 and File.copy/2 check path types (#11977)
* File.rename/2 checks path type
* File.copy/2 checks path type
Diffstat (limited to 'lib/elixir')
-rw-r--r-- | lib/elixir/lib/file.ex | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/lib/elixir/lib/file.ex b/lib/elixir/lib/file.ex index 0f761ab34..2fec0c0a0 100644 --- a/lib/elixir/lib/file.ex +++ b/lib/elixir/lib/file.ex @@ -694,7 +694,10 @@ defmodule File do @spec copy(Path.t() | io_device, Path.t() | io_device, pos_integer | :infinity) :: {:ok, non_neg_integer} | {:error, posix} def copy(source, destination, bytes_count \\ :infinity) do - :file.copy(maybe_to_string(source), maybe_to_string(destination), bytes_count) + source = normalize_path_or_io_device(source) + destination = normalize_path_or_io_device(destination) + + :file.copy(source, destination, bytes_count) end @doc """ @@ -712,8 +715,8 @@ defmodule File do raise File.CopyError, reason: reason, action: "copy", - source: maybe_to_string(source), - destination: maybe_to_string(destination) + source: normalize_path_or_io_device(source), + destination: normalize_path_or_io_device(destination) end end @@ -741,6 +744,8 @@ defmodule File do @doc since: "1.1.0" @spec rename(Path.t(), Path.t()) :: :ok | {:error, posix} def rename(source, destination) do + source = IO.chardata_to_string(source) + destination = IO.chardata_to_string(destination) :file.rename(source, destination) end @@ -1801,7 +1806,8 @@ defmodule File do defp normalize_modes([], true), do: [:binary] defp normalize_modes([], false), do: [] - defp maybe_to_string(path) when is_list(path), do: IO.chardata_to_string(path) - defp maybe_to_string(path) when is_binary(path), do: path - defp maybe_to_string(path), do: path + defp normalize_path_or_io_device(path) when is_list(path), do: IO.chardata_to_string(path) + defp normalize_path_or_io_device(path) when is_binary(path), do: path + defp normalize_path_or_io_device(io_device) when is_pid(io_device), do: io_device + defp normalize_path_or_io_device(io_device = {:file_descriptor, _, _}), do: io_device end |