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

fs: fix infinite loop with async recursive mkdir on Windows #27207

Merged
merged 2 commits into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 32 additions & 16 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1270,8 +1270,15 @@ int MKDirpSync(uv_loop_t* loop,
}
default:
uv_fs_req_cleanup(req);
int orig_err = err;
err = uv_fs_stat(loop, req, next_path.c_str(), nullptr);
if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) return UV_EEXIST;
if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) {
uv_fs_req_cleanup(req);
if (orig_err == UV_EEXIST && continuation_data.paths.size() > 0) {
return UV_ENOTDIR;
}
return UV_EEXIST;
}
if (err < 0) return err;
break;
}
Expand Down Expand Up @@ -1338,23 +1345,32 @@ int MKDirpAsync(uv_loop_t* loop,
break;
}
default:
if (err == UV_EEXIST &&
req_wrap->continuation_data->paths.size() > 0) {
uv_fs_req_cleanup(req);
MKDirpAsync(loop, req, path.c_str(),
req_wrap->continuation_data->mode, nullptr);
} else {
uv_fs_req_cleanup(req);
// Stash err for use in the callback.
req->data = reinterpret_cast<void*>(static_cast<intptr_t>(err));
int err = uv_fs_stat(loop, req, path.c_str(),
uv_fs_callback_t{[](uv_fs_t* req) {
FSReqBase* req_wrap = FSReqBase::from_req(req);
int err = req->result;
if (reinterpret_cast<intptr_t>(req->data) == UV_EEXIST &&
req_wrap->continuation_data->paths.size() > 0) {
if (err == 0 && S_ISDIR(req->statbuf.st_mode)) {
Environment* env = req_wrap->env();
uv_loop_t* loop = env->event_loop();
std::string path = req->path;
uv_fs_req_cleanup(req);
MKDirpAsync(loop, req, path.c_str(),
req_wrap->continuation_data->mode, nullptr);
return;
}
err = UV_ENOTDIR;
}
// verify that the path pointed to is actually a directory.
if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) err = UV_EEXIST;
uv_fs_req_cleanup(req);
int err = uv_fs_stat(loop, req, path.c_str(),
uv_fs_callback_t{[](uv_fs_t* req) {
FSReqBase* req_wrap = FSReqBase::from_req(req);
int err = req->result;
if (err == 0 && !S_ISDIR(req->statbuf.st_mode)) err = UV_EEXIST;
req_wrap->continuation_data->Done(err);
}});
if (err < 0) req_wrap->continuation_data->Done(err);
}
req_wrap->continuation_data->Done(err);
}});
if (err < 0) req_wrap->continuation_data->Done(err);
break;
}
break;
Expand Down
71 changes: 53 additions & 18 deletions test/parallel/test-fs-mkdir.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,34 @@ function nextdir() {
fs.mkdirSync(path.dirname(pathname));
fs.writeFileSync(pathname, '', 'utf8');

try {
fs.mkdirSync(pathname, { recursive: true });
throw new Error('unreachable');
} catch (err) {
assert.notStrictEqual(err.message, 'unreachable');
assert.strictEqual(err.code, 'EEXIST');
assert.strictEqual(err.syscall, 'mkdir');
}
assert.throws(
() => { fs.mkdirSync(pathname, { recursive: true }); },
{
code: 'EEXIST',
message: /EEXIST: .*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
}

// mkdirpSync when part of the path is a file.
{
const filename = path.join(tmpdir.path, nextdir(), nextdir());
const pathname = path.join(filename, nextdir(), nextdir());

fs.mkdirSync(path.dirname(filename));
fs.writeFileSync(filename, '', 'utf8');

assert.throws(
() => { fs.mkdirSync(pathname, { recursive: true }); },
{
code: 'ENOTDIR',
message: /ENOTDIR: .*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
}

// `mkdirp` when folder does not yet exist.
Expand All @@ -149,11 +169,25 @@ function nextdir() {

fs.mkdirSync(path.dirname(pathname));
fs.writeFileSync(pathname, '', 'utf8');
fs.mkdir(pathname, { recursive: true }, (err) => {
fs.mkdir(pathname, { recursive: true }, common.mustCall((err) => {
assert.strictEqual(err.code, 'EEXIST');
assert.strictEqual(err.syscall, 'mkdir');
assert.strictEqual(fs.statSync(pathname).isDirectory(), false);
});
}));
}

// `mkdirp` when part of the path is a file.
Copy link
Contributor

Choose a reason for hiding this comment

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

tests look great 👍

{
const filename = path.join(tmpdir.path, nextdir(), nextdir());
const pathname = path.join(filename, nextdir(), nextdir());

fs.mkdirSync(path.dirname(filename));
fs.writeFileSync(filename, '', 'utf8');
fs.mkdir(pathname, { recursive: true }, common.mustCall((err) => {
assert.strictEqual(err.code, 'ENOTDIR');
assert.strictEqual(err.syscall, 'mkdir');
assert.strictEqual(fs.existsSync(pathname), false);
}));
}

// mkdirpSync dirname loop
Expand All @@ -163,14 +197,15 @@ if (common.isMainThread && (common.isLinux || common.isOSX)) {
fs.mkdirSync(pathname);
process.chdir(pathname);
fs.rmdirSync(pathname);
try {
fs.mkdirSync('X', { recursive: true });
throw new Error('unreachable');
} catch (err) {
assert.notStrictEqual(err.message, 'unreachable');
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'mkdir');
}
assert.throws(
() => { fs.mkdirSync('X', { recursive: true }); },
{
code: 'ENOENT',
message: /ENOENT: .*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
fs.mkdir('X', { recursive: true }, (err) => {
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'mkdir');
Expand Down
34 changes: 26 additions & 8 deletions test/parallel/test-fs-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,32 @@ async function getHandle(dest) {
const dir = path.join(tmpDir, nextdir(), nextdir());
await mkdir(path.dirname(dir));
await writeFile(dir);
try {
await mkdir(dir, { recursive: true });
throw new Error('unreachable');
} catch (err) {
assert.notStrictEqual(err.message, 'unreachable');
assert.strictEqual(err.code, 'EEXIST');
assert.strictEqual(err.syscall, 'mkdir');
}
assert.rejects(
mkdir(dir, { recursive: true }),
{
code: 'EEXIST',
message: /EEXIST: .*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
}

// `mkdirp` when part of the path is a file.
{
const file = path.join(tmpDir, nextdir(), nextdir());
const dir = path.join(file, nextdir(), nextdir());
await mkdir(path.dirname(file));
await writeFile(file);
assert.rejects(
mkdir(dir, { recursive: true }),
{
code: 'ENOTDIR',
message: /ENOTDIR: .*mkdir/,
name: 'Error',
syscall: 'mkdir',
}
);
}

// mkdirp ./
Expand Down