Skip to content

Commit

Permalink
If parents is not set, please avoid creating this closure in the firs…
Browse files Browse the repository at this point in the history
…t place.
  • Loading branch information
kaizhu256 committed Jan 19, 2021
1 parent 528331a commit 480eaf7
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,21 +481,25 @@ function open(path, flag, mode, callback) {
callback = makeCallback(callback);

const req = new FSReqCallback();
req.oncomplete = (err, fd) => {
// Lazily create parent-subdirectories if they do not exist
if (err?.code === 'ENOENT' && parents && (flag & O_CREAT)) {
mkdir(pathModule.dirname(path), { recursive: true }, (err2) => {
if (err2) {
callback(err2);
return;
}
// Retry open() after lazily creating parent-subdirectories
open(path, { flag, mode }, callback);
});
return;
}
callback(err, fd);
};
if (parents) {
req.oncomplete = (err, fd) => {
// Lazily create parent-subdirectories if they do not exist
if (err?.code === 'ENOENT' && parents && (flag & O_CREAT)) {
mkdir(pathModule.dirname(path), { recursive: true }, (err2) => {
if (err2) {
callback(err2);
return;
}
// Retry open() after lazily creating parent-subdirectories
open(path, { flag, mode }, callback);
});
return;
}
callback(err, fd);
};
} else {
req.oncomplete = callback;
}

binding.open(pathModule.toNamespacedPath(path),
flag,
Expand Down

0 comments on commit 480eaf7

Please sign in to comment.