Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mfs files.write command to create parent directories #5359

Conversation

achingbrain
Copy link
Member

Adds support for a -p/--parents flag to the files.write command similar to the one supported by the files.mkdir command. If this is true and the directory for the file is not "/", try to create the containing directory before writing to the file.

@achingbrain achingbrain requested a review from Kubuxu as a code owner August 9, 2018 11:14
achingbrain added a commit to ipfs-inactive/interface-js-ipfs-core that referenced this pull request Aug 9, 2018
Also adds a test.

Already supported in `js-ipfs`, `go-ipfs` requires ipfs/kubo#5359
to be merged and released
Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

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

Definitely something we want. My main concern is this might go better in getFileHandle but I won't be a stickler on that.

@@ -735,6 +736,7 @@ stat' on the file or any of its ancestors.
}

create, _ := req.Options["create"].(bool)
dashp, _ := req.Options["parents"].(bool)
Copy link
Member

Choose a reason for hiding this comment

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

Let's call this "mkParents" or something like that. Contrary to what the rest of the go-ipfs code might indicate, we don't pay by the letter in variable names 😄.

@@ -757,6 +759,22 @@ stat' on the file or any of its ancestors.
return
}

dirtomake := gopath.Dir(path)

if dirtomake != "/" && dashp == true {
Copy link
Member

Choose a reason for hiding this comment

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

No need for the dashp == true, dashp will suffice.

if dirtomake != "/" && dashp == true {
root := nd.FilesRoot

err = mfs.Mkdir(root, dirtomake, mfs.MkdirOpts{
Copy link
Member

Choose a reason for hiding this comment

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

Given that getFileHandle already has special logic for creating missing files, I'd prefer to add this logic to getFileHandle, replacing the the create bool with a constant. However, I don't feel too strongly about this.

root := nd.FilesRoot

err = mfs.Mkdir(root, dirtomake, mfs.MkdirOpts{
Mkparents: dashp,
Copy link
Member

Choose a reason for hiding this comment

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

I'd just use the true constant. Easier to follow.

@achingbrain achingbrain force-pushed the allow-files-write-to-create-parent-directories branch 2 times, most recently from e9bd6fb to e756c7d Compare August 10, 2018 07:15
@achingbrain
Copy link
Member Author

Thanks for the review - I was concerned that adding the mkdir type behaviour to getFileHandle might make it responsible for a little too much so split it out into a separate function. Let me know if you'd like any further changes made.

Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

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

One nit but this otherwise looks good to me.

@@ -757,6 +759,14 @@ stat' on the file or any of its ancestors.
return
}

if mkParents {
err := ensureContainingDirectoryExists(nd.FilesRoot, path, flush, prefix)
Copy link
Member

Choose a reason for hiding this comment

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

No point in flushing here. We'll flush when we actually create the file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I've changed it to not pass a Flush option to mfs.Mkdir

@achingbrain achingbrain force-pushed the allow-files-write-to-create-parent-directories branch 3 times, most recently from 5c0c918 to d439c5a Compare August 13, 2018 15:27
@Stebalien Stebalien requested a review from schomatis August 13, 2018 16:14
@Stebalien
Copy link
Member

@achingbrain mind rebasing?

@achingbrain achingbrain force-pushed the allow-files-write-to-create-parent-directories branch 5 times, most recently from bc1433b to 9b6eea9 Compare August 13, 2018 18:58
@Stebalien Stebalien requested a review from kevina August 13, 2018 19:16
@Stebalien
Copy link
Member

I'd like to get a second signoff from either @schomatis or @kevina and then I'll merge.

Copy link
Contributor

@kevina kevina left a comment

Choose a reason for hiding this comment

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

Needs one more test as I am unsure of the behavior of mfs.Mkdir when all the directories already exist.

test_expect_success "can write file and create intermediate directories with short flags $EXTRA" '
echo "ipfs rocks" | ipfs files write -e -p /parents/foo/bar/baz/qux/quux/garply/ipfs.txt &&
ipfs files stat "/parents/foo/bar/baz/qux/quux/garply/ipfs.txt" | grep -q "^Type: file"
'
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add another test something like:

  test_expect_success "can write another file in the same directory with -e -p $EXTRA" '
    echo "ipfs rocks" | ipfs files write -e -p /parents/foo/bar/baz/qux/quux/garply/ipfs2.txt &&
    ipfs files stat "/parents/foo/bar/baz/qux/quux/garply/ipfs2.txt" | grep -q "^Type: file"
  '

To make sure it won't error if you use -p (or --parents) and all the parent directory already exists.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added the extra test.

Copy link
Contributor

Choose a reason for hiding this comment

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

Okay. Assuming the test passes this LGTM.

Copy link
Member Author

Choose a reason for hiding this comment

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

It worked locally, hopefully CI will agree.

@achingbrain achingbrain force-pushed the allow-files-write-to-create-parent-directories branch from 9b6eea9 to bd7d437 Compare August 13, 2018 21:46
Copy link
Contributor

@kevina kevina left a comment

Choose a reason for hiding this comment

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

LGTM Now.

Copy link
Contributor

@kevina kevina left a comment

Choose a reason for hiding this comment

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

One more test change.

Sorry I didn't catch it the first time around.

@@ -597,9 +597,29 @@ test_files_api() {
ipfs files ls /adir | grep foobar
'

test_expect_failure "should fail to write file and create intermediate directories with no --parents flag set $EXTRA" '
echo "ipfs rocks" | ipfs files write --create /parents/foo/ipfs.txt
'
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not the correct way to do this. Use test_must_fail. Some thing like:

test_expect_success "should fail to write file and create intermediate directories with no --parents flag set $EXTRA" '
  echo "ipfs rocks" | test_must_fail ipfs files write --create /parents/foo/ipfs.txt
'

test_expect_failure is for broken test that need eventually be fixed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Contributor

@schomatis schomatis left a comment

Choose a reason for hiding this comment

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

I've always wanted this options since the first time I used the ipfs files write command, thanks @achingbrain!

@achingbrain achingbrain force-pushed the allow-files-write-to-create-parent-directories branch from bd7d437 to 4e61193 Compare August 14, 2018 06:01
Adds support for a `-p/--parents` flag to the `files.write` command
similar to the one supported by the `files.mkdir` command. If this
is true and the directory for the file is not `"/"`, try to create
the containing directory before writing to the file.

License: MIT
Signed-off-by: Alex Potsides <[email protected]>
@achingbrain achingbrain force-pushed the allow-files-write-to-create-parent-directories branch from 4e61193 to c97c44e Compare August 14, 2018 06:59
@achingbrain
Copy link
Member Author

@Stebalien @kevina @schomatis Do you think this will make it into the next release?

@Stebalien Stebalien merged commit 0be1f5f into ipfs:master Aug 23, 2018
@Stebalien
Copy link
Member

Stebalien commented Aug 23, 2018

@achingbrain yes.

alanshaw pushed a commit to ipfs-inactive/interface-js-ipfs-core that referenced this pull request Aug 28, 2018
Also adds a test.

Already supported in `js-ipfs`, `go-ipfs` requires ipfs/kubo#5359
to be merged and released
@achingbrain achingbrain deleted the allow-files-write-to-create-parent-directories branch September 20, 2018 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants