|
| 1 | +// This tests that cpSync gives the directories it creates the mode of the |
| 2 | +// corresponding source directory, as cp does. |
| 3 | +import { mustNotMutateObjectDeep, isWindows, skip } from '../common/index.mjs'; |
| 4 | +import { nextdir } from '../common/fs.js'; |
| 5 | +import assert from 'node:assert'; |
| 6 | +import { chmodSync, cpSync, mkdirSync, statSync, writeFileSync, promises } from 'node:fs'; |
| 7 | +import { join } from 'node:path'; |
| 8 | +import { isMainThread } from 'node:worker_threads'; |
| 9 | +import tmpdir from '../common/tmpdir.js'; |
| 10 | + |
| 11 | +if (isWindows) |
| 12 | + skip('directory modes are not meaningful on Windows'); |
| 13 | +if (!isMainThread) |
| 14 | + skip('process.umask() is not available in workers'); |
| 15 | + |
| 16 | +tmpdir.refresh(); |
| 17 | +const mask = process.umask(0o022); |
| 18 | + |
| 19 | +const src = nextdir(); |
| 20 | +mkdirSync(join(src, 'private', 'inner'), { recursive: true, mode: 0o700 }); |
| 21 | +mkdirSync(join(src, 'shared'), { mode: 0o775 }); |
| 22 | +writeFileSync(join(src, 'private', 'inner', 'file'), 'x', { mode: 0o600 }); |
| 23 | + |
| 24 | +function modes(root) { |
| 25 | + return ['.', 'private', 'private/inner', 'shared', 'private/inner/file'] |
| 26 | + .map((p) => (statSync(join(root, p)).mode & 0o777).toString(8)); |
| 27 | +} |
| 28 | + |
| 29 | +const destSync = nextdir(); |
| 30 | +cpSync(src, destSync, mustNotMutateObjectDeep({ recursive: true })); |
| 31 | +assert.deepStrictEqual(modes(destSync), modes(src)); |
| 32 | + |
| 33 | +const destAsync = nextdir(); |
| 34 | +await promises.cp(src, destAsync, { recursive: true }); |
| 35 | +assert.deepStrictEqual(modes(destAsync), modes(src)); |
| 36 | + |
| 37 | +// A read-only source directory can still be copied; its copy ends up read-only too. |
| 38 | +{ |
| 39 | + const roSrc = nextdir(); |
| 40 | + mkdirSync(join(roSrc, 'sub'), { recursive: true }); |
| 41 | + writeFileSync(join(roSrc, 'sub', 'file'), 'x'); |
| 42 | + chmodSync(join(roSrc, 'sub'), 0o555); |
| 43 | + chmodSync(roSrc, 0o555); |
| 44 | + const readOnly = [roSrc, join(roSrc, 'sub')]; |
| 45 | + for (const copy of [(dest) => cpSync(roSrc, dest, { recursive: true }), |
| 46 | + (dest) => promises.cp(roSrc, dest, { recursive: true })]) { |
| 47 | + const dest = nextdir(); |
| 48 | + await copy(dest); |
| 49 | + assert.strictEqual(statSync(join(dest, 'sub', 'file')).size, 1); |
| 50 | + assert.deepStrictEqual( |
| 51 | + [dest, join(dest, 'sub')].map((p) => (statSync(p).mode & 0o777).toString(8)), ['555', '555']); |
| 52 | + readOnly.push(dest, join(dest, 'sub')); |
| 53 | + } |
| 54 | + // Let tmpdir clean up. |
| 55 | + for (const dir of readOnly) chmodSync(dir, 0o755); |
| 56 | +} |
| 57 | + |
| 58 | +// An existing destination directory keeps its own mode. |
| 59 | +const existing = nextdir(); |
| 60 | +mkdirSync(existing, { mode: 0o711 }); |
| 61 | +cpSync(src, existing, mustNotMutateObjectDeep({ recursive: true })); |
| 62 | +assert.strictEqual((statSync(existing).mode & 0o777).toString(8), '711'); |
| 63 | +assert.deepStrictEqual(modes(existing).slice(1), modes(src).slice(1)); |
| 64 | +process.umask(mask); |
0 commit comments