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
29 changes: 14 additions & 15 deletions lib/elixir/lib/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -310,24 +310,23 @@ defmodule File do
end

defp do_mkdir_p(path) do
if dir?(path) do
parent = Path.dirname(path)

if parent == path do
:ok
else
parent = Path.dirname(path)

if parent == path do
# Protect against infinite loop
{:error, :einval}
else
_ = do_mkdir_p(parent)

case :file.make_dir(path) do
{:error, :eexist} = error ->
if dir?(path), do: :ok, else: error
case do_mkdir_p(parent) do
:ok ->
case :file.make_dir(path) do
{:error, :eexist} ->
if dir?(path), do: :ok, else: {:error, :enotdir}

other ->
other
end

other ->
other
end
e ->
e
end
end
end
Expand Down
22 changes: 22 additions & 0 deletions lib/elixir/test/elixir/file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1146,6 +1146,28 @@ defmodule FileTest do
File.mkdir_p!(invalid)
end
end

@tag :unix
test "mkdir_p with non-accessible parent directory" do
fixture = tmp_path("tmp_test_parent")

try do
refute File.exists?(fixture)
assert File.mkdir_p!(fixture) == :ok
%File.Stat{mode: orig_mode} = File.stat!(fixture)
assert File.chmod!(fixture, 0o000) == :ok

child = Path.join(fixture, "child")
refute File.exists?(child)

assert File.mkdir_p(child) == {:error, :eacces}
refute File.exists?(child)

assert File.chmod!(fixture, orig_mode) == :ok
after
File.rm_rf(fixture)
end
end
end

describe "rm" do
Expand Down
Loading