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: use streaming directory processing in cp() #41351

Merged
merged 2 commits into from Dec 31, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/internal/fs/cp/cp-sync.js
Expand Up @@ -32,7 +32,7 @@ const {
existsSync,
lstatSync,
mkdirSync,
readdirSync,
opendirSync,
readlinkSync,
statSync,
symlinkSync,
Expand Down Expand Up @@ -275,14 +275,22 @@ function mkDirAndCopy(srcMode, src, dest, opts) {
}

function copyDir(src, dest, opts) {
readdirSync(src).forEach((item) => copyDirItem(item, src, dest, opts));
}
const dir = opendirSync(src);

try {
let dirent;

function copyDirItem(item, src, dest, opts) {
const srcItem = join(src, item);
const destItem = join(dest, item);
const { destStat } = checkPathsSync(srcItem, destItem, opts);
return startCopy(destStat, srcItem, destItem, opts);
while ((dirent = dir.readSync()) !== null) {
const { name } = dirent;
const srcItem = join(src, name);
const destItem = join(dest, name);
const { destStat } = checkPathsSync(srcItem, destItem, opts);

startCopy(destStat, srcItem, destItem, opts);
}
} finally {
dir.closeSync();
}
}

function onLink(destStat, src, dest) {
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/fs/cp/cp.js
Expand Up @@ -41,7 +41,7 @@ const {
copyFile,
lstat,
mkdir,
readdir,
opendir,
readlink,
stat,
symlink,
Expand Down Expand Up @@ -325,11 +325,11 @@ async function mkDirAndCopy(srcMode, src, dest, opts) {
}

async function copyDir(src, dest, opts) {
const dir = await readdir(src);
for (let i = 0; i < dir.length; i++) {
const item = dir[i];
const srcItem = join(src, item);
const destItem = join(dest, item);
const dir = await opendir(src);

for await (const { name } of dir) {
const srcItem = join(src, name);
const destItem = join(dest, name);
const { destStat } = await checkPaths(srcItem, destItem, opts);
await startCopy(destStat, srcItem, destItem, opts);
}
Expand Down