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
5 changes: 3 additions & 2 deletions lib/elixir/lib/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ defmodule File do

Since v1.21, this function also accepts an open `t:io_device/0`. This lets
you stat a handle you already hold without reopening the file, and avoids
time-of-check to time-of-use races.
time-of-check to time-of-use races. For a device PID, closing the file causes
subsequent calls to return `{:error, :terminated}`.

## Options

Expand All @@ -500,7 +501,7 @@ defmodule File do
#=> {:error, :enoent}
"""
@spec stat(Path.t() | io_device, stat_options) ::
{:ok, File.Stat.t()} | {:error, posix | :badarg}
{:ok, File.Stat.t()} | {:error, posix | :badarg | :terminated}
def stat(path_or_io_device, opts \\ []) do
opts = Keyword.put_new(opts, :time, :universal)

Expand Down
6 changes: 6 additions & 0 deletions lib/elixir/test/elixir/file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,12 @@ defmodule FileTest do
end
end

test "stat with closed io_device" do
{:ok, io} = File.open(__ENV__.file, [:read])
assert :ok = File.close(io)
assert {:error, :terminated} = File.stat(io)
end

test "stat! with io_device" do
{:ok, io} = File.open(__ENV__.file, [:read])

Expand Down
Loading