Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Added recursive directory creation to the fs.mkdir() function #513

Closed
wants to merge 9 commits into from
Closed

Added recursive directory creation to the fs.mkdir() function #513

wants to merge 9 commits into from

Conversation

bpedro
Copy link

@bpedro bpedro commented Dec 15, 2010

Implemented a polymorphic approach to the mkdir function making it also create directories recursively. This is the functionality offered by 'mkdir -p' and is often desirable to have it available from within node.

Here's the new mkdir() function synopsis:

  • mkdir(path, mode, [callback]): original implementation;
  • mkdir(path, mode, recursive, [callback]): if recursive == true, the directory will be created recursively.

This change doesn't break any existing test.

Example usage:

var fs = require('fs');

//
// Example with non-recursion.
//
fs.mkdir('example_dir/first/second/third/fourth/fifth', 0777, function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log('Directory created');
    }
});

//
// Example with recursion -- notice the parameter
// right before the callback function.
//
fs.mkdir('example_dir/first/second/third/fourth/fifth', 0777, true, function (err) {
    if (err) {
        console.log(err);
    } else {
        console.log('Directory created');
    }
});

@ry
Copy link

ry commented Dec 15, 2010

Thanks, but it needs to match style, have tests and docs.

@bpedro
Copy link
Author

bpedro commented Dec 16, 2010

Please see my latest commits and let me know what you think.

@thejh
Copy link

thejh commented Feb 2, 2011

I'm not sure, but wouldn't mode = mode || process.umask(); be better than mode = mode || 0777;? Or even throwing an error because a mandatory parameter wasn't set?

@bpedro
Copy link
Author

bpedro commented Feb 24, 2011

Hi thejh,

I agree and I'm changing to mode = mode || process.umask();. It makes sense and is definitely better than assuming 0777.

Other than this, what do you think of my implementation?

Thanks

@@ -26,8 +28,18 @@ fs.mkdir(d, 0666, function(err) {
}
});

fs.mkdir(recursive_d, 0766, true, function(err) {
Copy link

Choose a reason for hiding this comment

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

This is another hardcoded permission, right?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, that's a test. The directory name is also hardcoded and previous tests also had the permission hardcoded.

Do you see a problem with that?

Copy link

Choose a reason for hiding this comment

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

Oh, sorry, didn't look at the filename.

@bpedro bpedro closed this Mar 27, 2011
@ghost
Copy link

ghost commented Feb 16, 2012

When will this be added to the core?

@bpedro
Copy link
Author

bpedro commented May 23, 2012

I'm not sure if/when this will be added to the core. In the meanwhile I've created an npm module called node-fs: https://github.com/bpedro/node-fs

@ghost
Copy link

ghost commented May 23, 2012

Thanks!

@jfhbrook
Copy link

npm view mkdirp

@Mithgol
Copy link

Mithgol commented Jun 13, 2013

It's always easier to click than to copy+paste, so here's a hyperlink: https://npmjs.org/package/mkdirp

@treshugart
Copy link

It's baffling why this still isn't in the core.

@obastemur
Copy link

IMO This is the exact point that a specified 3rd party module should provide the solution. (ie mkdirp)

I don't remember a framework has this kind of functionality in the core.

@treshugart
Copy link

mkdir -p

It's a very common use case and I'd argue that any framework, or library should consider common use cases as something that should be in the core. Not really worth arguing about, but this is the single reason that it baffles me. I don't believe it's a good design choice is all.

@tjfontaine
Copy link

It's important that a design decision for node was to basically provide (where sensible) a javascript interface to the common posix interfaces that are consumed.

http://pubs.opengroup.org/onlinepubs/009695399/functions/mkdir.html does not define a recursive mechanism for it, and it's relatively trivial to implement in userland, so that's why node doesn't implement it.

Here's roughly the balance we try and strike:

  • Is there a standardized way to do this (at least on unicies) if so expose that mechanism in javascript.
  • Is this a common pattern that can be difficult to get right, we should write it and expose it for you

Sometimes we do well, and sometimes we don't, but that's the position we try and work from.

@treshugart
Copy link

Thanks for the link and kudos for a thorough explanation. It actually says to throw [ENOENT] if any part in the path prefix doesn't exist. I stand very corrected :)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants