Skip to content

Conversation

@te0d
Copy link
Contributor

@te0d te0d commented Jul 18, 2017

cp --force: add force option to files cp command

The force option was added to the files cp command. Specifying force
will overwrite an existing file with the source file.

License: MIT
Signed-off-by: Tom O'Donnell [email protected]

@te0d te0d force-pushed the feat/commands/cp-force branch from 77f314d to cd4e72e Compare July 18, 2017 16:45
@te0d
Copy link
Contributor Author

te0d commented Jul 19, 2017

Fixes: #2074


child, err := pdir.Child(name)
if err == nil && child.Type() == mfs.TFile {
pdir.Unlink(name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check the error returned from Unlink

}

child, err := pdir.Child(name)
if err == nil && child.Type() == mfs.TFile {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how should this behave in the context of symlinks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, wasn't aware of symlinks. I'll check it out.


pdir, ok := parent.(*mfs.Directory)
if !ok {
err = fmt.Errorf("No such file or directory: %s", dir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily true, this could be a file, but not a directory. Probably should have the error message be "PATH was not a directory" or something

Copy link
Member

@whyrusleeping whyrusleeping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comments, would also be good to add a test for this to test/sharness/t0250-files-api.sh

todizzle91 and others added 3 commits August 4, 2017 11:20
The force option was added to the `files cp` command. Specifying force
will overwrite an existing file with the source file.

License: MIT
Signed-off-by: Tom O'Donnell <[email protected]>
License: MIT
Signed-off-by: Tom O'Donnell <[email protected]>
The --force flag would produce an error when the target file did
not exist. It now ignores errors trying to find the node but
observes errors when attempting to unlink. Added a test case
to verify --force allows cp to overwrite a target.

License: MIT
Signed-off-by: Tom O'Donnell <[email protected]>
@te0d te0d force-pushed the feat/commands/cp-force branch from fd238f4 to 4f1889c Compare August 4, 2017 15:21
@schomatis schomatis self-assigned this Aug 15, 2018
@schomatis schomatis added kind/enhancement A net-new feature or improvement to an existing feature topic/files Topic files labels Aug 15, 2018
@schomatis schomatis self-requested a review August 15, 2018 01:10
@schomatis
Copy link
Contributor

@te0d So, if you're feeling adventurous and would like to learn some more about how IPFS work, particularly the MFS layer, I think we could leverage the Mv function (the one behind ipfs files mv) to adapt it to perform both operations , move and copy, as it would seem to me (at first sight at least) that basically the only difference between the two is that the first one unlinks the source and the second one doesn't (in theory, we would need to think this through).

If not, and you would just like to get this merged and get it over with (which is more than understandable since we made you wait more than an year as of now), please rebase this PR and we can give it the final touches to bring it home.

@te0d
Copy link
Contributor Author

te0d commented Aug 16, 2018

@schomatis Yes, I can look into adapting Mv to perform both operations. Haven't looked at ipfs in a while and a bit busy the upcoming week, but I'll submit something the following wee.

@schomatis
Copy link
Contributor

@te0d No problem, take your time. There's no need to submit a PR from the get-go, I would advice you to first analyze the implementation differences between ipfs files cp and ipfs files mv so we can discuss them here before you commit time to writing the PR. You can ping me with any questions that come up.

Comment on lines +298 to +314
pdir, ok := parent.(*mfs.Directory)
if !ok {
err = fmt.Errorf("No such directory: %s", dir)
return err
}

// Attempt to unlink if child is a file, ignore error since
// we are only concerned with unlinking an existing file.
child, err := pdir.Child(name)
if err == nil && child.Type() == mfs.TFile {
err := pdir.Unlink(name)
if err != nil {
return err
}
}

return nil
Copy link
Contributor

@gammazero gammazero Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pdir, ok := parent.(*mfs.Directory)
if !ok {
err = fmt.Errorf("No such directory: %s", dir)
return err
}
// Attempt to unlink if child is a file, ignore error since
// we are only concerned with unlinking an existing file.
child, err := pdir.Child(name)
if err == nil && child.Type() == mfs.TFile {
err := pdir.Unlink(name)
if err != nil {
return err
}
}
return nil
pdir, ok := parent.(*mfs.Directory)
if !ok {
return fmt.Errorf("not a directory: %s", dir)
}
// Attempt to unlink if child is a file, ignore error since
// we are only concerned with unlinking an existing file.
child, err := pdir.Child(name)
if err != nil {
return nil // no child file, nothing to unlink
}
if child.Type() != mfs.TFile {
return fmt.Errorf("not a file: %s", path)
}
return pdir.Unlink(name)

gammazero added a commit that referenced this pull request Jun 3, 2025
Adds a `--force` option to allow the `ipfs files cp` command to overwrite existig files. Returns error is trying to overwrite directories.

Replaces #4079

Closes #2074
gammazero added a commit that referenced this pull request Jun 3, 2025
Adds a `--force` option to allow the `ipfs files cp` command to overwrite existig files. Returns error is trying to overwrite directories.

Replaces #4079

Closes #2074
gammazero added a commit that referenced this pull request Jun 3, 2025
Adds a `--force` option to allow the `ipfs files cp` command to overwrite existig files. Returns error is trying to overwrite directories.

Replaces #4079

Closes #2074
@gammazero
Copy link
Contributor

Replaced by #10823

Thank you @te0d. Needed to open a new PR since this one was too far out of date and needed to make changes to make it current.

@gammazero gammazero closed this Jun 3, 2025
gammazero added a commit that referenced this pull request Jun 10, 2025
* commands: add `--force` option to `files cp` command

Adds a `--force` option to allow the `ipfs files cp` command to overwrite existig files. Returns error is trying to overwrite directories.

Replaces #4079

Closes #2074

* Update test/sharness/t0250-files-api.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement A net-new feature or improvement to an existing feature topic/files Topic files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants