-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Allow mfs files.write command to create parent directories #5359
Conversation
Also adds a test. Already supported in `js-ipfs`, `go-ipfs` requires ipfs/kubo#5359 to be merged and released
There was a problem hiding this 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.
core/commands/files.go
Outdated
@@ -735,6 +736,7 @@ stat' on the file or any of its ancestors. | |||
} | |||
|
|||
create, _ := req.Options["create"].(bool) | |||
dashp, _ := req.Options["parents"].(bool) |
There was a problem hiding this comment.
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 😄.
core/commands/files.go
Outdated
@@ -757,6 +759,22 @@ stat' on the file or any of its ancestors. | |||
return | |||
} | |||
|
|||
dirtomake := gopath.Dir(path) | |||
|
|||
if dirtomake != "/" && dashp == true { |
There was a problem hiding this comment.
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.
core/commands/files.go
Outdated
if dirtomake != "/" && dashp == true { | ||
root := nd.FilesRoot | ||
|
||
err = mfs.Mkdir(root, dirtomake, mfs.MkdirOpts{ |
There was a problem hiding this comment.
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.
core/commands/files.go
Outdated
root := nd.FilesRoot | ||
|
||
err = mfs.Mkdir(root, dirtomake, mfs.MkdirOpts{ | ||
Mkparents: dashp, |
There was a problem hiding this comment.
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.
e9bd6fb
to
e756c7d
Compare
Thanks for the review - I was concerned that adding the mkdir type behaviour to |
There was a problem hiding this 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.
core/commands/files.go
Outdated
@@ -757,6 +759,14 @@ stat' on the file or any of its ancestors. | |||
return | |||
} | |||
|
|||
if mkParents { | |||
err := ensureContainingDirectoryExists(nd.FilesRoot, path, flush, prefix) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
5c0c918
to
d439c5a
Compare
@achingbrain mind rebasing? |
bc1433b
to
9b6eea9
Compare
I'd like to get a second signoff from either @schomatis or @kevina and then I'll merge. |
There was a problem hiding this 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" | ||
' |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
9b6eea9
to
bd7d437
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM Now.
There was a problem hiding this 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.
test/sharness/t0250-files-api.sh
Outdated
@@ -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 | |||
' |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
There was a problem hiding this 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!
bd7d437
to
4e61193
Compare
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]>
4e61193
to
c97c44e
Compare
@Stebalien @kevina @schomatis Do you think this will make it into the next release? |
@achingbrain yes. |
Also adds a test. Already supported in `js-ipfs`, `go-ipfs` requires ipfs/kubo#5359 to be merged and released
Adds support for a
-p/--parents
flag to thefiles.write
command similar to the one supported by thefiles.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.