Skip to content

Commit 78a5151

Browse files
authored
Add support for [:raw] opts in File.read (#15172)
1 parent c464eb8 commit 78a5151

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

lib/elixir/lib/file.ex

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,13 @@ defmodule File do
400400
401401
You can use `:file.format_error/1` to get a descriptive string of the error.
402402
403+
## Options (since v1.20)
404+
405+
The supported options are:
406+
407+
* `:raw` - a single atom to bypass the file server and only check
408+
for the file locally
409+
403410
## Examples
404411
405412
File.read("hello.txt")
@@ -408,15 +415,24 @@ defmodule File do
408415
File.read("non_existing.txt")
409416
#=> {:error, :enoent}
410417
"""
411-
@spec read(Path.t()) :: {:ok, binary} | {:error, posix | :badarg | :terminated | :system_limit}
412-
def read(path) do
413-
:file.read_file(IO.chardata_to_string(path))
418+
@spec read(Path.t(), [exists_option]) ::
419+
{:ok, binary} | {:error, posix | :badarg | :terminated | :system_limit}
420+
when exists_option: :raw
421+
def read(path, opts \\ []) do
422+
:file.read_file(IO.chardata_to_string(path), opts)
414423
end
415424

416425
@doc """
417426
Returns a binary with the contents of the given filename,
418427
or raises a `File.Error` exception if an error occurs.
419428
429+
## Options (since v1.20)
430+
431+
The supported options are:
432+
433+
* `:raw` - a single atom to bypass the file server and only check
434+
for the file locally
435+
420436
## Examples
421437
422438
File.read!("hello.txt")
@@ -425,9 +441,9 @@ defmodule File do
425441
File.read!("non_existing.txt")
426442
** (File.Error) could not read file "non_existing.txt": no such file or directory
427443
"""
428-
@spec read!(Path.t()) :: binary
429-
def read!(path) do
430-
case read(path) do
444+
@spec read!(Path.t(), [exists_option]) :: binary when exists_option: :raw
445+
def read!(path, opts \\ []) do
446+
case read(path, opts) do
431447
{:ok, binary} ->
432448
binary
433449

lib/elixir/test/elixir/file_test.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,10 @@ defmodule FileTest do
983983
assert {:ok, "Русский\n\n"} = File.read(Path.expand(~c"fixtures/utf8.txt", __DIR__))
984984
end
985985

986+
test "read with :raw options" do
987+
assert {:ok, "FOO\n"} = File.read(fixture_path("file.txt"), [:raw])
988+
end
989+
986990
test "read!" do
987991
assert File.read!(fixture_path("file.txt")) == "FOO\n"
988992
expected_message = "could not read file \"fixtures/missing.txt\": no such file or directory"

0 commit comments

Comments
 (0)