57
57
defmodule File do
58
58
@ moduledoc """
59
59
This module contains function to manipulate files.
60
- Many of the functions that interact with the filesystem
61
- have their naming based on its UNIX variants. For
62
- example, deleting a file is done with `File.rm`.
63
- Getting its stats with `File.stat`.
64
-
65
- In order to write and read files, one must use the
66
- functions in the IO module. By default, a file is
67
- opened in binary mode which requires the functions
68
- `IO.binread`, `IO.binwrite` and `IO.binreadline` to
69
- interact with the file. A developer may pass `:utf8`
70
- as an option when opening the file and then all other
71
- functions from IO are available, since they work directly
60
+
61
+ Some of those functions are low-level, allowing the user
62
+ to interact with the file or IO devices, like `File.open/2`,
63
+ `File.copy/3` and others. This module also provides higher
64
+ level functions, with their naming based on its UNIX variants.
65
+ For example, one can copy a file via `File.cp/3` and remove
66
+ files and directories recursively via `File.rm_rf/2`
67
+
68
+ In order to write and read files, one must use the functions
69
+ in the IO module. By default, a file is opened in binary mode
70
+ which requires the functions `IO.binread`, `IO.binwrite` and
71
+ `IO.binreadline` to interact with the file. A developer may
72
+ pass `:utf8` as an option when opening the file and then all
73
+ other functions from IO are available, since they work directly
72
74
with Unicode data.
73
75
74
- Most of the functions in this module return `:ok`
75
- or `{ :ok, result }` in case of success, `{ :error, reason }`
76
- otherwise. Those function are also followed by
77
- a variant that ends with `!` which returns the
78
- result (without the `{ :ok, result }` tuple) in
79
- case of success or raises an exception in case it
80
- fails. For example:
76
+ Most of the functions in this module return `:ok` or
77
+ `{ :ok, result }` in case of success, `{ :error, reason }`
78
+ otherwise. Those function are also followed by a variant
79
+ that ends with `!` which returns the result (without the
80
+ `{ :ok, result }` tuple) in case of success or raises an
81
+ exception in case it fails. For example:
81
82
82
83
File.read("hello.txt")
83
84
#=> { :ok, "World" }
@@ -91,16 +92,10 @@ defmodule File do
91
92
File.read!("invalid.txt")
92
93
#=> raises File.Error
93
94
94
- In general, a developer should use the former in case
95
- he wants to react in the fie does not exist. The latter
96
- should be used when the developer expects his software
97
- to fail in case the file cannot be read (i.e. it is
98
- literally an exception).
99
-
100
- Finally, the functions in this module accept either
101
- a char lists or a binary. When manipulating paths, a char
102
- list is returned if one is given as argument. However,
103
- when reading files, binaries are always returned.
95
+ In general, a developer should use the former in case he wants
96
+ to react in the fie does not exist. The latter should be used
97
+ when the developer expects his software to fail in case the
98
+ file cannot be read (i.e. it is literally an exception).
104
99
"""
105
100
106
101
alias :file , as: F
@@ -312,17 +307,24 @@ defmodule File do
312
307
end
313
308
314
309
@ doc """
315
- Copies the contents of `source` to `destination`. Both
316
- parameters can be a filename or an io device opened with `File.open`.
317
- `bytes_count` specifies the number of bytes to count, the default
318
- being `:infinity`.
310
+ Copies the contents of `source` to `destination`.
311
+
312
+ Both parameters can be a filename or an io device opened
313
+ with `File.open/2`. `bytes_count` specifies the number of
314
+ bytes to copy, the default being `:infinity`.
319
315
320
316
If file `destination` already exists, it is overriden
321
317
by the contents in `source`.
322
318
323
319
Returns `{ :ok, bytes_copied }` if successful,
324
320
`{ :error, reason }` otherwise.
325
321
322
+ Compared to the `File.cp/3`, this function is more low-level,
323
+ allowing a copy from device to device limited by a number of
324
+ bytes. On the other hand, `File.cp/3` performs more extensive
325
+ checks on both source and destination and it also preserves
326
+ the file mode after copy.
327
+
326
328
Typical error reasons are the same as in `open/2`,
327
329
`read/1` and `write/2`.
328
330
"""
@@ -331,7 +333,7 @@ defmodule File do
331
333
end
332
334
333
335
@ doc """
334
- The same as `copy/3` but raises an File.CopyError if it fails.
336
+ The same as `copy/3` but raises an ` File.CopyError` if it fails.
335
337
Returns the `bytes_copied` otherwise.
336
338
"""
337
339
def copy! ( source , destination , bytes_count // :infinity ) do
@@ -344,40 +346,36 @@ defmodule File do
344
346
end
345
347
346
348
@ doc """
347
- Copies the contents in `source` to `destination`.
348
- Similar to the command `cp -r` in Unix systems,
349
- this function behaves differently depending
350
- if `source` and `destination` are a file or a directory.
349
+ Copies the contents in `source` to `destination` preserving its mode.
351
350
352
- If both are files, it simply copies `source` to
353
- `destination`. However, if `destination` is a directory,
354
- it copies the contents of `source` to `destination/source`
355
- recursively .
351
+ Similar to the command `cp` in Unix systems, this function
352
+ behaves differently depending if `destination` is a directory
353
+ or not. In paritcular, if `destination` is a directory, it
354
+ copies the contents of `source` to `destination/source` .
356
355
357
- If a file already exists in the destination,
358
- it invokes a callback which should return
359
- true if the existing file should be overriden,
360
- false otherwise. It defaults to return true.
356
+ If a file already exists in the destination, it invokes a
357
+ callback which should return true if the existing file
358
+ should be overriden, false otherwise. It defaults to return true.
361
359
362
360
It returns `:ok` in case of success, returns
363
361
`{ :error, reason }` otherwise.
362
+
363
+ If you want to copy contents from an io device to another device
364
+ or do a straight copy from a source to a destination without
365
+ preserving modes, check `File.copy/3` instead.
364
366
"""
365
367
def cp ( source , destination , callback // fn ( _ , _ ) -> true end ) do
366
- if dir? ( source ) do
367
- { :error , :eisdir }
368
- else
369
- output =
370
- if dir? ( destination ) do
371
- mkdir ( destination )
372
- FN . join ( destination , FN . basename ( source ) )
373
- else
374
- destination
375
- end
376
-
377
- case do_cp_file ( source , output , callback , [ ] ) do
378
- { :error , reason , _ } -> { :error , reason }
379
- _ -> :ok
368
+ output =
369
+ if dir? ( destination ) do
370
+ mkdir ( destination )
371
+ FN . join ( destination , FN . basename ( source ) )
372
+ else
373
+ destination
380
374
end
375
+
376
+ case do_cp_file ( source , output , callback , [ ] ) do
377
+ { :error , reason , _ } -> { :error , reason }
378
+ _ -> :ok
381
379
end
382
380
end
383
381
0 commit comments