diff --git a/src/node_file.cc b/src/node_file.cc index 52da072a32d..f210c611016 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -3791,6 +3791,7 @@ static void CpSyncCopyDir(const FunctionCallbackInfo& args) { auto dest_path = dest.ToPath(); std::error_code error; + const bool dest_existed = std::filesystem::exists(dest_path, error); std::filesystem::create_directories(dest_path, error); if (error) { return env->ThrowStdErrException(error, "cp", *dest); @@ -3910,11 +3911,28 @@ static void CpSyncCopyDir(const FunctionCallbackInfo& args) { } } else if (dir_entry.is_directory()) { auto entry_dir_path = src / dir_entry.path().filename(); - std::filesystem::create_directory(dest_file_path); + const bool created = + std::filesystem::create_directory(dest_file_path, error); + if (error) { + env->ThrowStdErrException( + error, "cp", ConvertPathToUTF8(dest_file_path).c_str()); + return false; + } auto success = copy_dir_contents(entry_dir_path, dest_file_path); if (!success) { return false; } + // A directory created by the copy gets the mode of its source once + // its contents are in (the source may be read-only). + if (created) { + std::filesystem::permissions( + dest_file_path, dir_entry.status().permissions(), error); + if (error) { + env->ThrowStdErrException( + error, "cp", ConvertPathToUTF8(dest_file_path).c_str()); + return false; + } + } } else if (dir_entry.is_regular_file()) { std::filesystem::copy_file( dir_entry.path(), dest_file_path, file_copy_opts, error); @@ -3939,7 +3957,13 @@ static void CpSyncCopyDir(const FunctionCallbackInfo& args) { return true; }; - copy_dir_contents(src_path, dest_path); + if (copy_dir_contents(src_path, dest_path) && !dest_existed) { + std::filesystem::permissions( + dest_path, std::filesystem::status(src_path).permissions(), error); + if (error) { + return env->ThrowStdErrException(error, "cp", *dest); + } + } } BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile( diff --git a/test/parallel/test-fs-cp-sync-directory-mode.mjs b/test/parallel/test-fs-cp-sync-directory-mode.mjs new file mode 100644 index 00000000000..f022af268f1 --- /dev/null +++ b/test/parallel/test-fs-cp-sync-directory-mode.mjs @@ -0,0 +1,64 @@ +// This tests that cpSync gives the directories it creates the mode of the +// corresponding source directory, as cp does. +import { mustNotMutateObjectDeep, isWindows, skip } from '../common/index.mjs'; +import { nextdir } from '../common/fs.js'; +import assert from 'node:assert'; +import { chmodSync, cpSync, mkdirSync, statSync, writeFileSync, promises } from 'node:fs'; +import { join } from 'node:path'; +import { isMainThread } from 'node:worker_threads'; +import tmpdir from '../common/tmpdir.js'; + +if (isWindows) + skip('directory modes are not meaningful on Windows'); +if (!isMainThread) + skip('process.umask() is not available in workers'); + +tmpdir.refresh(); +const mask = process.umask(0o022); + +const src = nextdir(); +mkdirSync(join(src, 'private', 'inner'), { recursive: true, mode: 0o700 }); +mkdirSync(join(src, 'shared'), { mode: 0o775 }); +writeFileSync(join(src, 'private', 'inner', 'file'), 'x', { mode: 0o600 }); + +function modes(root) { + return ['.', 'private', 'private/inner', 'shared', 'private/inner/file'] + .map((p) => (statSync(join(root, p)).mode & 0o777).toString(8)); +} + +const destSync = nextdir(); +cpSync(src, destSync, mustNotMutateObjectDeep({ recursive: true })); +assert.deepStrictEqual(modes(destSync), modes(src)); + +const destAsync = nextdir(); +await promises.cp(src, destAsync, { recursive: true }); +assert.deepStrictEqual(modes(destAsync), modes(src)); + +// A read-only source directory can still be copied; its copy ends up read-only too. +{ + const roSrc = nextdir(); + mkdirSync(join(roSrc, 'sub'), { recursive: true }); + writeFileSync(join(roSrc, 'sub', 'file'), 'x'); + chmodSync(join(roSrc, 'sub'), 0o555); + chmodSync(roSrc, 0o555); + const readOnly = [roSrc, join(roSrc, 'sub')]; + for (const copy of [(dest) => cpSync(roSrc, dest, { recursive: true }), + (dest) => promises.cp(roSrc, dest, { recursive: true })]) { + const dest = nextdir(); + await copy(dest); + assert.strictEqual(statSync(join(dest, 'sub', 'file')).size, 1); + assert.deepStrictEqual( + [dest, join(dest, 'sub')].map((p) => (statSync(p).mode & 0o777).toString(8)), ['555', '555']); + readOnly.push(dest, join(dest, 'sub')); + } + // Let tmpdir clean up. + for (const dir of readOnly) chmodSync(dir, 0o755); +} + +// An existing destination directory keeps its own mode. +const existing = nextdir(); +mkdirSync(existing, { mode: 0o711 }); +cpSync(src, existing, mustNotMutateObjectDeep({ recursive: true })); +assert.strictEqual((statSync(existing).mode & 0o777).toString(8), '711'); +assert.deepStrictEqual(modes(existing).slice(1), modes(src).slice(1)); +process.umask(mask);