Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit c269bc3

Browse files
committed
docs: make docs around copying/moving files/dirs clearer
1 parent 889e63f commit c269bc3

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

SPEC/FILES.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ The Mutable File System (MFS) is a virtual file system on top of IPFS that expos
601601
Where:
602602

603603
- `from` is the path(s) of the source to copy. It might be:
604-
- An existing MFS path (e.g. `/my-dir/my-file.txt`)
604+
- An existing MFS path to a file or a directory (e.g. `/my-dir/my-file.txt`)
605605
- An IPFS path (e.g. `/ipfs/QmWGeRAEgtsHW3ec7U4qW2CyVy7eA2mFRVbk1nb24jFyks`)
606606
- `to` is the path of the destination to copy to
607607
- `options` is an optional Object that might contain the following keys:
@@ -611,7 +611,11 @@ Where:
611611
- `flush` is a Boolean value to decide whether or not to immediately flush MFS changes to disk (default: true)
612612
- `callback` is an optional function with the signature `function (error) {}`, where `error` may be an Error that occured if the operation was not successful
613613

614-
If there are multiple sources, `to` will be treated as a directory, otherwise it will be treated as the same type as `from`.
614+
If `from` has multiple values then `to` must be a directory.
615+
616+
If `from` has a single value and `to` exists and is a directory, `from` will be copied into `to`.
617+
618+
If `from` has a single value and `to` exists and is a file, `from` must be a file and the contents of `to` will be replaced with the contents of `from` otherwise an error will be returned.
615619

616620
If `from` is an IPFS path, and an MFS path exists with the same name, the IPFS path will be chosen.
617621

@@ -620,18 +624,21 @@ If no `callback` is passed, a promise is returned.
620624
**Example:**
621625

622626
```JavaScript
623-
ipfs.files.cp('/src-file', '/dst-file', (err) => {s
627+
// To copy a file
628+
ipfs.files.cp('/src-file', '/dst-file', (err) => {
624629
if (err) {
625630
console.error(err)
626631
}
627632
})
628633

629-
ipfs.files.cp('/src-dir', '/dst-dir', (err) => {s
634+
// To copy a directory
635+
ipfs.files.cp('/src-dir', '/dst-dir', (err) => {
630636
if (err) {
631637
console.error(err)
632638
}
633639
})
634640

641+
// To copy multiple files to a directory
635642
ipfs.files.cp('/src-file1', '/src-file2', '/dst-dir', (err) => {
636643
if (err) {
637644
console.error(err)
@@ -684,7 +691,7 @@ Where:
684691
- `hash` is a Boolean value to return only the hash (default: false)
685692
- `size` is a Boolean value to return only the size (default: false)
686693
- `withLocal` is a Boolean value to compute the amount of the dag that is local, and if possible the total size (default: false)
687-
- `callback` is an optional function with the signature `function (error, stats) {}`, where `error` may be an Error that occured if the operation was not successful and `stat` is an Object with the following keys:
694+
- `callback` is an optional function with the signature `function (error, stats) {}`, where `error` may be an Error that occured if the operation was not successful and `stats` is an Object with the following keys:
688695

689696
- `hash` is a string with the hash
690697
- `size` is an integer with the file size in Bytes
@@ -765,7 +772,7 @@ ipfs.files.rm('/my/beautiful/directory', { recursive: true }, (err) => {
765772

766773
Where:
767774

768-
- `path` is the path of the file to read and must point to a file
775+
- `path` is the path of the file to read and must point to a file (and not a directory)
769776
- `options` is an optional Object that might contain the following keys:
770777
- `offset` is an Integer with the byte offset to begin reading from (default: 0)
771778
- `length` is an Integer with the maximum number of bytes to read (default: Read to end of stream)
@@ -795,7 +802,7 @@ ipfs.files.read('/hello-world', (error, buf) => {
795802

796803
Where:
797804

798-
- `path` is the path of the file to read and must point to a file
805+
- `path` is the path of the file to read and must point to a file (and not a directory)
799806
- `options` is an optional Object that might contain the following keys:
800807
- `offset` is an Integer with the byte offset to begin reading from (default: 0)
801808
- `length` is an Integer with the maximum number of bytes to read (default: Read to end of stream)
@@ -821,7 +828,7 @@ stream.on('data', (buf) => console.log(buf.toString('utf8')))
821828

822829
Where:
823830

824-
- `path` is the path of the file to read and must point to a file
831+
- `path` is the path of the file to read and must point to a file (and not a directory)
825832
- `options` is an optional Object that might contain the following keys:
826833
- `offset` is an Integer with the byte offset to begin reading from (default: 0)
827834
- `length` is an Integer with the maximum number of bytes to read (default: Read to end of stream)
@@ -893,7 +900,15 @@ Where:
893900
- `flush` is a Boolean value to decide whether or not to immediately flush MFS changes to disk (default: true)
894901
- `callback` is an optional function with the signature `function (error) {}`, where `error` may be an Error that occured if the operation was not successful
895902

896-
If there are multiple sources, `to` will be treated as a directory, otherwise it will be treated as the same type as `from`.
903+
If `from` has multiple values then `to` must be a directory.
904+
905+
If `from` has a single value and `to` exists and is a directory, `from` will be moved into `to`.
906+
907+
If `from` has a single value and `to` exists and is a file, `from` must be a file and the contents of `to` will be replaced with the contents of `from` otherwise an error will be returned.
908+
909+
If `from` is an IPFS path, and an MFS path exists with the same name, the IPFS path will be chosen.
910+
911+
All values of `from` will be removed after the operation is complete, unless they are an IPFS path.
897912

898913
If no `callback` is passed, a promise is returned.
899914

0 commit comments

Comments
 (0)