GeezerVison is showing github.com — fs: implement mkdir recursive (mkdirp) · nodejs/node@fdeace6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const fs = require('fs'); | | 5 | +const tmpdir = require('../../test/common/tmpdir'); | | 6 | +tmpdir.refresh(); | | 7 | +let dirc = 0; | | 8 | + | | 9 | +const bench = common.createBenchmark(main, { | | 10 | + n: [1e4], | | 11 | +}); | | 12 | + | | 13 | +function main({ n }) { | | 14 | + bench.start(); | | 15 | + (function r(cntr) { | | 16 | + if (cntr-- <= 0) | | 17 | + return bench.end(n); | | 18 | + const pathname = `${tmpdir.path}/${++dirc}/${++dirc}/${++dirc}/${++dirc}`; | | 19 | + fs.mkdir(pathname, { createParents: true }, (err) => { | | 20 | + r(cntr); | | 21 | + }); | | 22 | + }(n)); | | 23 | +} |
Collapse file Copy file name to clipboardExpand all lines: doc/api/fs.md +28-7Lines changed: 28 additions & 7 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2042,7 +2042,7 @@ changes: | 2042 | 2042 |
| 2043 | 2043 | Synchronous lstat(2). | 2044 | 2044 |
| 2045 | | -## fs.mkdir(path[, mode], callback) | | 2045 | +## fs.mkdir(path[, options], callback) | 2046 | 2046 | <!-- YAML | 2047 | 2047 | added: v0.1.8 | 2048 | 2048 | changes: | @@ -2061,16 +2061,29 @@ changes: | 2061 | 2061 | --> | 2062 | 2062 |
| 2063 | 2063 | * `path` {string|Buffer|URL} | 2064 | | -* `mode` {integer} Not supported on Windows. **Default:** `0o777`. | | 2064 | +* `options` {Object|integer} | | 2065 | + * `recursive` {boolean} **Default:** `false` | | 2066 | + * `mode` {integer} Not supported on Windows. **Default:** `0o777`. | 2065 | 2067 | * `callback` {Function} | 2066 | 2068 | * `err` {Error} | 2067 | 2069 |
| 2068 | 2070 | Asynchronously creates a directory. No arguments other than a possible exception | 2069 | 2071 | are given to the completion callback. | 2070 | 2072 |
| | 2073 | +The optional `options` argument can be an integer specifying mode (permission | | 2074 | +and sticky bits), or an object with a `mode` property and a `recursive` | | 2075 | +property indicating whether parent folders should be created. | | 2076 | + | | 2077 | +```js | | 2078 | +// Creates /tmp/a/apple, regardless of whether `/tmp` and /tmp/a exist. | | 2079 | +fs.mkdir('/tmp/a/apple', { recursive: true }, (err) => { | | 2080 | + if (err) throw err; | | 2081 | +}); | | 2082 | +``` | | 2083 | + | 2071 | 2084 | See also: mkdir(2). | 2072 | 2085 |
| 2073 | | -## fs.mkdirSync(path[, mode]) | | 2086 | +## fs.mkdirSync(path[, options]) | 2074 | 2087 | <!-- YAML | 2075 | 2088 | added: v0.1.21 | 2076 | 2089 | changes: | @@ -2081,7 +2094,9 @@ changes: | 2081 | 2094 | --> | 2082 | 2095 |
| 2083 | 2096 | * `path` {string|Buffer|URL} | 2084 | | -* `mode` {integer} Not supported on Windows. **Default:** `0o777`. | | 2097 | +* `options` {Object|integer} | | 2098 | + * `recursive` {boolean} **Default:** `false` | | 2099 | + * `mode` {integer} Not supported on Windows. **Default:** `0o777`. | 2085 | 2100 |
| 2086 | 2101 | Synchronously creates a directory. Returns `undefined`. | 2087 | 2102 | This is the synchronous version of [`fs.mkdir()`][]. | @@ -3974,18 +3989,24 @@ changes: | 3974 | 3989 | Asynchronous lstat(2). The `Promise` is resolved with the [`fs.Stats`][] object | 3975 | 3990 | for the given symbolic link `path`. | 3976 | 3991 |
| 3977 | | -### fsPromises.mkdir(path[, mode]) | | 3992 | +### fsPromises.mkdir(path[, options]) | 3978 | 3993 | <!-- YAML | 3979 | 3994 | added: v10.0.0 | 3980 | 3995 | --> | 3981 | 3996 |
| 3982 | 3997 | * `path` {string|Buffer|URL} | 3983 | | -* `mode` {integer} **Default:** `0o777` | | 3998 | +* `options` {Object|integer} | | 3999 | + * `recursive` {boolean} **Default:** `false` | | 4000 | + * `mode` {integer} Not supported on Windows. **Default:** `0o777`. | 3984 | 4001 | * Returns: {Promise} | 3985 | 4002 |
| 3986 | 4003 | Asynchronously creates a directory then resolves the `Promise` with no | 3987 | 4004 | arguments upon success. | 3988 | 4005 |
| | 4006 | +The optional `options` argument can be an integer specifying mode (permission | | 4007 | +and sticky bits), or an object with a `mode` property and a `recursive` | | 4008 | +property indicating whether parent folders should be created. | | 4009 | + | 3989 | 4010 | ### fsPromises.mkdtemp(prefix[, options]) | 3990 | 4011 | <!-- YAML | 3991 | 4012 | added: v10.0.0 | @@ -4622,7 +4643,7 @@ the file contents. | 4622 | 4643 | [`fs.ftruncate()`]: #fs_fs_ftruncate_fd_len_callback | 4623 | 4644 | [`fs.futimes()`]: #fs_fs_futimes_fd_atime_mtime_callback | 4624 | 4645 | [`fs.lstat()`]: #fs_fs_lstat_path_options_callback | 4625 | | -[`fs.mkdir()`]: #fs_fs_mkdir_path_mode_callback | | 4646 | +[`fs.mkdir()`]: #fs_fs_mkdir_path_options_callback | 4626 | 4647 | [`fs.mkdtemp()`]: #fs_fs_mkdtemp_prefix_options_callback | 4627 | 4648 | [`fs.open()`]: #fs_fs_open_path_flags_mode_callback | 4628 | 4649 | [`fs.read()`]: #fs_fs_read_fd_buffer_offset_length_position_callback | |
Collapse file Copy file name to clipboardExpand all lines: lib/fs.js +32-13Lines changed: 32 additions & 13 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -703,29 +703,48 @@ function fsyncSync(fd) { | 703 | 703 | handleErrorFromBinding(ctx); | 704 | 704 | } | 705 | 705 |
| 706 | | -function mkdir(path, mode, callback) { | | 706 | +function mkdir(path, options, callback) { | | 707 | + if (typeof options === 'function') { | | 708 | + callback = options; | | 709 | + options = {}; | | 710 | + } else if (typeof options === 'number' || typeof options === 'string') { | | 711 | + options = { mode: options }; | | 712 | + } | | 713 | + const { | | 714 | + recursive = false, | | 715 | + mode = 0o777 | | 716 | + } = options || {}; | | 717 | + callback = makeCallback(callback); | 707 | 718 | path = getPathFromURL(path); | 708 | | - validatePath(path); | 709 | 719 |
| 710 | | - if (arguments.length < 3) { | 711 | | - callback = makeCallback(mode); | 712 | | - mode = 0o777; | 713 | | - } else { | 714 | | - callback = makeCallback(callback); | 715 | | - mode = validateMode(mode, 'mode', 0o777); | 716 | | - } | | 720 | + validatePath(path); | | 721 | + if (typeof recursive !== 'boolean') | | 722 | + throw new ERR_INVALID_ARG_TYPE('recursive', 'boolean', recursive); | 717 | 723 |
| 718 | 724 | const req = new FSReqWrap(); | 719 | 725 | req.oncomplete = callback; | 720 | | - binding.mkdir(pathModule.toNamespacedPath(path), mode, req); | | 726 | + binding.mkdir(pathModule.toNamespacedPath(path), | | 727 | + validateMode(mode, 'mode', 0o777), recursive, req); | 721 | 728 | } | 722 | 729 |
| 723 | | -function mkdirSync(path, mode) { | | 730 | +function mkdirSync(path, options) { | | 731 | + if (typeof options === 'number' || typeof options === 'string') { | | 732 | + options = { mode: options }; | | 733 | + } | 724 | 734 | path = getPathFromURL(path); | | 735 | + const { | | 736 | + recursive = false, | | 737 | + mode = 0o777 | | 738 | + } = options || {}; | | 739 | + | 725 | 740 | validatePath(path); | 726 | | - mode = validateMode(mode, 'mode', 0o777); | | 741 | + if (typeof recursive !== 'boolean') | | 742 | + throw new ERR_INVALID_ARG_TYPE('recursive', 'boolean', recursive); | | 743 | + | 727 | 744 | const ctx = { path }; | 728 | | - binding.mkdir(pathModule.toNamespacedPath(path), mode, undefined, ctx); | | 745 | + binding.mkdir(pathModule.toNamespacedPath(path), | | 746 | + validateMode(mode, 'mode', 0o777), recursive, undefined, | | 747 | + ctx); | 729 | 748 | handleErrorFromBinding(ctx); | 730 | 749 | } | 731 | 750 |
| |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js +15-3Lines changed: 15 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -283,11 +283,23 @@ async function fsync(handle) { | 283 | 283 | return binding.fsync(handle.fd, kUsePromises); | 284 | 284 | } | 285 | 285 |
| 286 | | -async function mkdir(path, mode) { | | 286 | +async function mkdir(path, options) { | | 287 | + if (typeof options === 'number' || typeof options === 'string') { | | 288 | + options = { mode: options }; | | 289 | + } | | 290 | + const { | | 291 | + recursive = false, | | 292 | + mode = 0o777 | | 293 | + } = options || {}; | 287 | 294 | path = getPathFromURL(path); | | 295 | + | 288 | 296 | validatePath(path); | 289 | | - mode = validateMode(mode, 'mode', 0o777); | 290 | | - return binding.mkdir(pathModule.toNamespacedPath(path), mode, kUsePromises); | | 297 | + if (typeof recursive !== 'boolean') | | 298 | + throw new ERR_INVALID_ARG_TYPE('recursive', 'boolean', recursive); | | 299 | + | | 300 | + return binding.mkdir(pathModule.toNamespacedPath(path), | | 301 | + validateMode(mode, 'mode', 0o777), recursive, | | 302 | + kUsePromises); | 291 | 303 | } | 292 | 304 |
| 293 | 305 | async function readdir(path, options) { | |
Collapse file Copy file name to clipboardExpand all lines: src/node_file.cc +151-7Lines changed: 151 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -76,6 +76,16 @@ using v8::Value; | 76 | 76 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) | 77 | 77 | #endif | 78 | 78 |
| | 79 | +#ifndef S_ISDIR | | 80 | +# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) | | 81 | +#endif | | 82 | + | | 83 | +#ifdef __POSIX__ | | 84 | +const char* kPathSeparator = "/"; | | 85 | +#else | | 86 | +const char* kPathSeparator = "\\/"; | | 87 | +#endif | | 88 | + | 79 | 89 | #define GET_OFFSET(a) ((a)->IsNumber() ? (a).As<Integer>()->Value() : -1) | 80 | 90 | #define TRACE_NAME(name) "fs.sync." #name | 81 | 91 | #define GET_TRACE_ENABLED \ | @@ -1148,28 +1158,162 @@ static void RMDir(const FunctionCallbackInfo<Value>& args) { | 1148 | 1158 | } | 1149 | 1159 | } | 1150 | 1160 |
| | 1161 | +int MKDirpSync(uv_loop_t* loop, uv_fs_t* req, const std::string& path, int mode, | | 1162 | + uv_fs_cb cb = nullptr) { | | 1163 | + FSContinuationData continuation_data(req, mode, cb); | | 1164 | + continuation_data.PushPath(std::move(path)); | | 1165 | + | | 1166 | + while (continuation_data.paths.size() > 0) { | | 1167 | + std::string next_path = continuation_data.PopPath(); | | 1168 | + int err = uv_fs_mkdir(loop, req, next_path.c_str(), mode, nullptr); | | 1169 | + while (true) { | | 1170 | + switch (err) { | | 1171 | + case 0: | | 1172 | + if (continuation_data.paths.size() == 0) { | | 1173 | + return 0; | | 1174 | + } | | 1175 | + break; | | 1176 | + case UV_ENOENT: { | | 1177 | + std::string dirname = next_path.substr(0, | | 1178 | + next_path.find_last_of(kPathSeparator)); | | 1179 | + if (dirname != next_path) { | | 1180 | + continuation_data.PushPath(std::move(next_path)); | | 1181 | + continuation_data.PushPath(std::move(dirname)); | | 1182 | + } else if (continuation_data.paths.size() == 0) { | | 1183 | + err = UV_EEXIST; | | 1184 | + continue; | | 1185 | + } | | 1186 | + break; | | 1187 | + } | | 1188 | + case UV_EPERM: { | | 1189 | + return err; | | 1190 | + } | | 1191 | + default: | | 1192 | + uv_fs_req_cleanup(req); | | 1193 | + err = uv_fs_stat(loop, req, next_path.c_str(), nullptr); | | 1194 | + if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) return UV_EEXIST; | | 1195 | + if (err < 0) return err; | | 1196 | + break; | | 1197 | + } | | 1198 | + break; | | 1199 | + } | | 1200 | + uv_fs_req_cleanup(req); | | 1201 | + } | | 1202 | + | | 1203 | + return 0; | | 1204 | +} | | 1205 | + | | 1206 | +int MKDirpAsync(uv_loop_t* loop, | | 1207 | + uv_fs_t* req, | | 1208 | + const char* path, | | 1209 | + int mode, | | 1210 | + uv_fs_cb cb) { | | 1211 | + FSReqBase* req_wrap = FSReqBase::from_req(req); | | 1212 | + // on the first iteration of algorithm, stash state information. | | 1213 | + if (req_wrap->continuation_data == nullptr) { | | 1214 | + req_wrap->continuation_data = std::unique_ptr<FSContinuationData>{ | | 1215 | + new FSContinuationData(req, mode, cb)}; | | 1216 | + req_wrap->continuation_data->PushPath(std::move(path)); | | 1217 | + } | | 1218 | + | | 1219 | + // on each iteration of algorithm, mkdir directory on top of stack. | | 1220 | + std::string next_path = req_wrap->continuation_data->PopPath(); | | 1221 | + int err = uv_fs_mkdir(loop, req, next_path.c_str(), mode, | | 1222 | + uv_fs_callback_t{[](uv_fs_t* req) { | | 1223 | + FSReqBase* req_wrap = FSReqBase::from_req(req); | | 1224 | + Environment* env = req_wrap->env(); | | 1225 | + uv_loop_t* loop = env->event_loop(); | | 1226 | + std::string path = req->path; | | 1227 | + int err = req->result; | | 1228 | + | | 1229 | + while (true) { | | 1230 | + switch (err) { | | 1231 | + case 0: { | | 1232 | + if (req_wrap->continuation_data->paths.size() == 0) { | | 1233 | + req_wrap->continuation_data->Done(0); | | 1234 | + } else { | | 1235 | + uv_fs_req_cleanup(req); | | 1236 | + MKDirpAsync(loop, req, path.c_str(), | | 1237 | + req_wrap->continuation_data->mode, nullptr); | | 1238 | + } | | 1239 | + break; | | 1240 | + } | | 1241 | + case UV_ENOENT: { | | 1242 | + std::string dirname = path.substr(0, | | 1243 | + path.find_last_of(kPathSeparator)); | | 1244 | + if (dirname != path) { | | 1245 | + req_wrap->continuation_data->PushPath(std::move(path)); | | 1246 | + req_wrap->continuation_data->PushPath(std::move(dirname)); | | 1247 | + } else if (req_wrap->continuation_data->paths.size() == 0) { | | 1248 | + err = UV_EEXIST; | | 1249 | + continue; | | 1250 | + } | | 1251 | + uv_fs_req_cleanup(req); | | 1252 | + MKDirpAsync(loop, req, path.c_str(), | | 1253 | + req_wrap->continuation_data->mode, nullptr); | | 1254 | + break; | | 1255 | + } | | 1256 | + case UV_EPERM: { | | 1257 | + req_wrap->continuation_data->Done(err); | | 1258 | + break; | | 1259 | + } | | 1260 | + default: | | 1261 | + if (err == UV_EEXIST && | | 1262 | + req_wrap->continuation_data->paths.size() > 0) { | | 1263 | + uv_fs_req_cleanup(req); | | 1264 | + MKDirpAsync(loop, req, path.c_str(), | | 1265 | + req_wrap->continuation_data->mode, nullptr); | | 1266 | + } else { | | 1267 | + // verify that the path pointed to is actually a directory. | | 1268 | + uv_fs_req_cleanup(req); | | 1269 | + int err = uv_fs_stat(loop, req, path.c_str(), | | 1270 | + uv_fs_callback_t{[](uv_fs_t* req) { | | 1271 | + FSReqBase* req_wrap = FSReqBase::from_req(req); | | 1272 | + int err = req->result; | | 1273 | + if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) err = UV_EEXIST; | | 1274 | + req_wrap->continuation_data->Done(err); | | 1275 | + }}); | | 1276 | + if (err < 0) req_wrap->continuation_data->Done(err); | | 1277 | + } | | 1278 | + break; | | 1279 | + } | | 1280 | + break; | | 1281 | + } | | 1282 | + }}); | | 1283 | + | | 1284 | + return err; | | 1285 | +} | | 1286 | + | 1151 | 1287 | static void MKDir(const FunctionCallbackInfo<Value>& args) { | 1152 | 1288 | Environment* env = Environment::GetCurrent(args); | 1153 | 1289 |
| 1154 | 1290 | const int argc = args.Length(); | 1155 | | - CHECK_GE(argc, 3); | | 1291 | + CHECK_GE(argc, 4); | 1156 | 1292 |
| 1157 | 1293 | BufferValue path(env->isolate(), args[0]); | 1158 | 1294 | CHECK_NOT_NULL(*path); | 1159 | 1295 |
| 1160 | 1296 | CHECK(args[1]->IsInt32()); | 1161 | 1297 | const int mode = args[1].As<Int32>()->Value(); | 1162 | 1298 |
| 1163 | | - FSReqBase* req_wrap_async = GetReqWrap(env, args[2]); | | 1299 | + CHECK(args[2]->IsBoolean()); | | 1300 | + bool mkdirp = args[2]->IsTrue(); | | 1301 | + | | 1302 | + FSReqBase* req_wrap_async = GetReqWrap(env, args[3]); | 1164 | 1303 | if (req_wrap_async != nullptr) { // mkdir(path, mode, req) | 1165 | | - AsyncCall(env, req_wrap_async, args, "mkdir", UTF8, AfterNoArgs, | 1166 | | - uv_fs_mkdir, *path, mode); | | 1304 | + AsyncCall(env, req_wrap_async, args, "mkdir", UTF8, | | 1305 | + AfterNoArgs, mkdirp ? MKDirpAsync : uv_fs_mkdir, *path, mode); | 1167 | 1306 | } else { // mkdir(path, mode, undefined, ctx) | 1168 | | - CHECK_EQ(argc, 4); | | 1307 | + CHECK_EQ(argc, 5); | 1169 | 1308 | FSReqWrapSync req_wrap_sync; | 1170 | 1309 | FS_SYNC_TRACE_BEGIN(mkdir); | 1171 | | - SyncCall(env, args[3], &req_wrap_sync, "mkdir", | 1172 | | - uv_fs_mkdir, *path, mode); | | 1310 | + if (mkdirp) { | | 1311 | + SyncCall(env, args[4], &req_wrap_sync, "mkdir", | | 1312 | + MKDirpSync, *path, mode); | | 1313 | + } else { | | 1314 | + SyncCall(env, args[4], &req_wrap_sync, "mkdir", | | 1315 | + uv_fs_mkdir, *path, mode); | | 1316 | + } | 1173 | 1317 | FS_SYNC_TRACE_END(mkdir); | 1174 | 1318 | } | 1175 | 1319 | } | |
|