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
28 changes: 22 additions & 6 deletions lib/elixir/lib/file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,13 @@ defmodule File do

You can use `:file.format_error/1` to get a descriptive string of the error.

## Options (since v1.20)

The supported options are:

* `:raw` - a single atom to bypass the file server and only check
for the file locally

## Examples

File.read("hello.txt")
Expand All @@ -408,15 +415,24 @@ defmodule File do
File.read("non_existing.txt")
#=> {:error, :enoent}
"""
@spec read(Path.t()) :: {:ok, binary} | {:error, posix | :badarg | :terminated | :system_limit}
def read(path) do
:file.read_file(IO.chardata_to_string(path))
@spec read(Path.t(), [exists_option]) ::
{:ok, binary} | {:error, posix | :badarg | :terminated | :system_limit}
when exists_option: :raw
def read(path, opts \\ []) do
:file.read_file(IO.chardata_to_string(path), opts)

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.

I went with the easy solution of using the default argument (and consistent with other existing functions), but should I split the /1 and /2 arity implementations?

Or possibly:

Suggested change
:file.read_file(IO.chardata_to_string(path), opts)
case opts do
[] -> :file.read_file(IO.chardata_to_string(path))
_ -> :file.read_file(IO.chardata_to_string(path), opts)
end

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.

Erlang is already doing the change you propose, so I think this fine as is.

end

@doc """
Returns a binary with the contents of the given filename,
or raises a `File.Error` exception if an error occurs.

## Options (since v1.20)

The supported options are:

* `:raw` - a single atom to bypass the file server and only check
for the file locally

## Examples

File.read!("hello.txt")
Expand All @@ -425,9 +441,9 @@ defmodule File do
File.read!("non_existing.txt")
** (File.Error) could not read file "non_existing.txt": no such file or directory
"""
@spec read!(Path.t()) :: binary
def read!(path) do
case read(path) do
@spec read!(Path.t(), [exists_option]) :: binary when exists_option: :raw
def read!(path, opts \\ []) do
case read(path, opts) do
{:ok, binary} ->
binary

Expand Down
4 changes: 4 additions & 0 deletions lib/elixir/test/elixir/file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,10 @@ defmodule FileTest do
assert {:ok, "Русский\n日\n"} = File.read(Path.expand(~c"fixtures/utf8.txt", __DIR__))
end

test "read with :raw options" do
assert {:ok, "FOO\n"} = File.read(fixture_path("file.txt"), [:raw])
end

test "read!" do
assert File.read!(fixture_path("file.txt")) == "FOO\n"
expected_message = "could not read file \"fixtures/missing.txt\": no such file or directory"
Expand Down