From d32ae81fd0297ebe8b8838a8b6a6fd8da503203c Mon Sep 17 00:00:00 2001 From: Wang Heng Date: Tue, 14 May 2024 11:48:48 +0800 Subject: [PATCH 1/3] chore: Update cautionary message in README --- .../pages/getting-started/io-and-the-file-system.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/elixir/pages/getting-started/io-and-the-file-system.md b/lib/elixir/pages/getting-started/io-and-the-file-system.md index 57a4760633e..f0d6cab954a 100644 --- a/lib/elixir/pages/getting-started/io-and-the-file-system.md +++ b/lib/elixir/pages/getting-started/io-and-the-file-system.md @@ -27,6 +27,10 @@ hello world The `File` module contains functions that allow us to open files as IO devices. By default, files are opened in binary mode, which requires developers to use the specific `IO.binread/2` and `IO.binwrite/2` functions from the `IO` module: +> #### Potential Data Loss Warning {: .warning} +> +> The following code opens a file for writing. Be mindful that providing an incorrect file path will lead to the unintended deletion of the file's contents. Always verify the accuracy of the file path to avoid data loss. + ```elixir iex> {:ok, file} = File.open("path/to/file/hello", [:write]) {:ok, #PID<0.47.0>} @@ -95,6 +99,10 @@ With this, we have covered the main modules that Elixir provides for dealing wit You may have noticed that `File.open/2` returns a tuple like `{:ok, pid}`: +> #### Potential Data Loss Warning {: .warning} +> +> The following code opens a file for writing. Be mindful that providing an incorrect file path will lead to the unintended deletion of the file's contents. Always verify the accuracy of the file path to avoid data loss. + ```elixir iex> {:ok, file} = File.open("hello", [:write]) {:ok, #PID<0.47.0>} From 61e3544874364b6919c4eabb75c92c73a4584110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 14 May 2024 09:14:12 +0200 Subject: [PATCH 2/3] Update io-and-the-file-system.md --- .../pages/getting-started/io-and-the-file-system.md | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/elixir/pages/getting-started/io-and-the-file-system.md b/lib/elixir/pages/getting-started/io-and-the-file-system.md index f0d6cab954a..654aca1f73c 100644 --- a/lib/elixir/pages/getting-started/io-and-the-file-system.md +++ b/lib/elixir/pages/getting-started/io-and-the-file-system.md @@ -27,9 +27,9 @@ hello world The `File` module contains functions that allow us to open files as IO devices. By default, files are opened in binary mode, which requires developers to use the specific `IO.binread/2` and `IO.binwrite/2` functions from the `IO` module: -> #### Potential Data Loss Warning {: .warning} +> #### Potential data loss warning {: .warning} > -> The following code opens a file for writing. Be mindful that providing an incorrect file path will lead to the unintended deletion of the file's contents. Always verify the accuracy of the file path to avoid data loss. +> The following code opens a file for writing. If an existing file is available at the given path, its contents will be deleted. ```elixir iex> {:ok, file} = File.open("path/to/file/hello", [:write]) @@ -42,7 +42,7 @@ iex> File.read("path/to/file/hello") {:ok, "world"} ``` -A file can also be opened with `:utf8` encoding, which tells the `File` module to interpret the bytes read from the file as UTF-8-encoded bytes. +The file could be opened with the `:append` option, instead of `:write`, to preserve its contents. You may also pass the `:utf8` option, which tells the `File` module to interpret the bytes read from the file as UTF-8-encoded bytes. Besides functions for opening, reading and writing files, the `File` module has many functions to work with the file system. Those functions are named after their UNIX equivalents. For example, `File.rm/1` can be used to remove files, `File.mkdir/1` to create directories, `File.mkdir_p/1` to create directories and all their parent chain. There are even `File.cp_r/2` and `File.rm_rf/1` to respectively copy and remove files and directories recursively (i.e., copying and removing the contents of the directories too). @@ -99,12 +99,8 @@ With this, we have covered the main modules that Elixir provides for dealing wit You may have noticed that `File.open/2` returns a tuple like `{:ok, pid}`: -> #### Potential Data Loss Warning {: .warning} -> -> The following code opens a file for writing. Be mindful that providing an incorrect file path will lead to the unintended deletion of the file's contents. Always verify the accuracy of the file path to avoid data loss. - ```elixir -iex> {:ok, file} = File.open("hello", [:write]) +iex> {:ok, file} = File.open("hello") {:ok, #PID<0.47.0>} ``` From b7cb0442e3e0cd031445acfb70d851294b0a096d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 14 May 2024 09:15:53 +0200 Subject: [PATCH 3/3] Update lib/elixir/pages/getting-started/io-and-the-file-system.md --- lib/elixir/pages/getting-started/io-and-the-file-system.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/pages/getting-started/io-and-the-file-system.md b/lib/elixir/pages/getting-started/io-and-the-file-system.md index 654aca1f73c..50c9b333698 100644 --- a/lib/elixir/pages/getting-started/io-and-the-file-system.md +++ b/lib/elixir/pages/getting-started/io-and-the-file-system.md @@ -28,7 +28,7 @@ hello world The `File` module contains functions that allow us to open files as IO devices. By default, files are opened in binary mode, which requires developers to use the specific `IO.binread/2` and `IO.binwrite/2` functions from the `IO` module: > #### Potential data loss warning {: .warning} -> +> > The following code opens a file for writing. If an existing file is available at the given path, its contents will be deleted. ```elixir