Skip to content

Commit 4e536d5

Browse files
authoredMar 5, 2020
Stop preserving ownership, stop performing chmod after copyFileSync (#39)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 1cda1f2 commit 4e536d5

File tree

5 files changed

+3
-94
lines changed

5 files changed

+3
-94
lines changed
 

‎fs.js

-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const stat = promisify(fs.stat);
99
const lstat = promisify(fs.lstat);
1010
const utimes = promisify(fs.utimes);
1111
const chmod = promisify(fs.chmod);
12-
const chown = promisify(fs.chown);
1312

1413
exports.closeSync = fs.closeSync.bind(fs);
1514
exports.createWriteStream = fs.createWriteStream.bind(fs);
@@ -42,10 +41,6 @@ exports.chmod = (path, mode) => chmod(path, mode).catch(error => {
4241
throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error);
4342
});
4443

45-
exports.chown = (path, uid, gid) => chown(path, uid, gid).catch(error => {
46-
throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error);
47-
});
48-
4944
exports.statSync = path => {
5045
try {
5146
return fs.statSync(path);
@@ -62,22 +57,6 @@ exports.utimesSync = (path, atime, mtime) => {
6257
}
6358
};
6459

65-
exports.chmodSync = (path, mode) => {
66-
try {
67-
return fs.chmodSync(path, mode);
68-
} catch (error) {
69-
throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error);
70-
}
71-
};
72-
73-
exports.chownSync = (path, uid, gid) => {
74-
try {
75-
return fs.chownSync(path, uid, gid);
76-
} catch (error) {
77-
throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error);
78-
}
79-
};
80-
8160
exports.makeDir = path => makeDir(path, {fs}).catch(error => {
8261
throw new CpFileError(`Cannot create directory \`${path}\`: ${error.message}`, error);
8362
});

‎index.js

+1-8
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
4444

4545
return Promise.all([
4646
fs.utimes(destination, stats.atime, stats.mtime),
47-
fs.chmod(destination, stats.mode),
48-
fs.chown(destination, stats.uid, stats.gid)
47+
fs.chmod(destination, stats.mode)
4948
]);
5049
}
5150
};
@@ -83,11 +82,6 @@ const checkSourceIsFile = (stat, source) => {
8382
}
8483
};
8584

86-
const fixupAttributes = (destination, stat) => {
87-
fs.chmodSync(destination, stat.mode);
88-
fs.chownSync(destination, stat.uid, stat.gid);
89-
};
90-
9185
module.exports.sync = (source, destination, options) => {
9286
if (!source || !destination) {
9387
throw new CpFileError('`source` and `destination` required');
@@ -114,5 +108,4 @@ module.exports.sync = (source, destination, options) => {
114108
}
115109

116110
fs.utimesSync(destination, stat.atime, stat.mtime);
117-
fixupAttributes(destination, stat);
118111
};

‎readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Resilient by using [graceful-fs](https://github.com/isaacs/node-graceful-fs).
99
- User-friendly by creating non-existent destination directories for you.
1010
- Can be safe by turning off [overwriting](#optionsoverwrite).
11+
- Preserves file mode, [but not ownership](https://github.com/sindresorhus/cp-file/issues/22#issuecomment-502079547).
1112
- User-friendly errors.
1213

1314
## Install

‎test/async.js

-24
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,6 @@ test('preserve mode', async t => {
113113
t.is(licenseStats.mode, tempStats.mode);
114114
});
115115

116-
test('preserve ownership', async t => {
117-
await cpFile('license', t.context.destination);
118-
const licenseStats = fs.lstatSync('license');
119-
const tempStats = fs.lstatSync(t.context.destination);
120-
t.is(licenseStats.gid, tempStats.gid);
121-
t.is(licenseStats.uid, tempStats.uid);
122-
});
123-
124116
test('throw an Error if `source` does not exists', async t => {
125117
const error = await t.throwsAsync(cpFile('NO_ENTRY', t.context.destination));
126118
t.is(error.name, 'CpFileError', error.message);
@@ -226,22 +218,6 @@ test.serial('rethrow chmod errors', async t => {
226218
fs.chmod.restore();
227219
});
228220

229-
test.serial('rethrow chown errors', async t => {
230-
const chownError = buildEPERM(t.context.destination, 'chown');
231-
232-
fs.chown = sinon.stub(fs, 'chown').throws(chownError);
233-
234-
clearModule('../fs');
235-
const uncached = importFresh('..');
236-
const error = await t.throwsAsync(uncached('license', t.context.destination));
237-
t.is(error.name, 'CpFileError', error.message);
238-
t.is(error.code, chownError.code, error.message);
239-
t.is(error.path, chownError.path, error.message);
240-
t.true(fs.chown.called);
241-
242-
fs.chown.restore();
243-
});
244-
245221
test.serial('rethrow read after open errors', async t => {
246222
const {createWriteStream, createReadStream} = fs;
247223
let calledWriteEnd = 0;

‎test/sync.js

+1-41
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import test from 'ava';
66
import uuid from 'uuid';
77
import sinon from 'sinon';
88
import assertDateEqual from './helpers/_assert';
9-
import {buildEACCES, buildENOSPC, buildEBADF, buildEPERM} from './helpers/_fs-errors';
9+
import {buildEACCES, buildENOSPC, buildEBADF} from './helpers/_fs-errors';
1010
import cpFile from '..';
1111

1212
const THREE_HUNDRED_KILO = (100 * 3 * 1024) + 1;
@@ -128,14 +128,6 @@ test('preserve mode', t => {
128128
t.is(licenseStats.mode, tempStats.mode);
129129
});
130130

131-
test('preserve ownership', t => {
132-
cpFile.sync('license', t.context.destination);
133-
const licenseStats = fs.lstatSync('license');
134-
const tempStats = fs.lstatSync(t.context.destination);
135-
t.is(licenseStats.gid, tempStats.gid);
136-
t.is(licenseStats.uid, tempStats.uid);
137-
});
138-
139131
test('throw an Error if `source` does not exists', t => {
140132
const error = t.throws(() => {
141133
cpFile.sync('NO_ENTRY', t.context.destination);
@@ -215,35 +207,3 @@ test('rethrow utimes errors', t => {
215207

216208
fs.utimesSync.restore();
217209
});
218-
219-
test('rethrow chmod errors', t => {
220-
const chmodError = buildEPERM(t.context.destination, 'chmod');
221-
222-
fs.chmodSync = sinon.stub(fs, 'chmodSync').throws(chmodError);
223-
224-
const error = t.throws(() => {
225-
cpFile.sync('license', t.context.destination);
226-
});
227-
t.is(error.name, 'CpFileError', error.message);
228-
t.is(error.errno, chmodError.errno, error.message);
229-
t.is(error.code, chmodError.code, error.message);
230-
t.true(fs.chmodSync.called);
231-
232-
fs.chmodSync.restore();
233-
});
234-
235-
test('rethrow chown errors', t => {
236-
const chownError = buildEPERM(t.context.destination, 'chown');
237-
238-
fs.chownSync = sinon.stub(fs, 'chownSync').throws(chownError);
239-
240-
const error = t.throws(() => {
241-
cpFile.sync('license', t.context.destination);
242-
});
243-
t.is(error.name, 'CpFileError', error.message);
244-
t.is(error.errno, chownError.errno, error.message);
245-
t.is(error.code, chownError.code, error.message);
246-
t.true(fs.chownSync.called);
247-
248-
fs.chownSync.restore();
249-
});

0 commit comments

Comments
 (0)
Please sign in to comment.