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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/internal/fs/cp/cp-sync.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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