From 3502afc454291451ac5f30521d77c4d740d2ecd7 Mon Sep 17 00:00:00 2001 From: Yanis Benson Date: Thu, 27 Feb 2020 08:59:03 +0300 Subject: [PATCH] Stop preserving ownership, stop performing chmod after copyFileSync, remove related dead code and tests, solves #22 --- fs.js | 21 --------------------- index.js | 9 +-------- test/async.js | 24 ------------------------ test/sync.js | 42 +----------------------------------------- 4 files changed, 2 insertions(+), 94 deletions(-) diff --git a/fs.js b/fs.js index 281dd57..6f4f44e 100644 --- a/fs.js +++ b/fs.js @@ -9,7 +9,6 @@ const stat = promisify(fs.stat); const lstat = promisify(fs.lstat); const utimes = promisify(fs.utimes); const chmod = promisify(fs.chmod); -const chown = promisify(fs.chown); exports.closeSync = fs.closeSync.bind(fs); exports.createWriteStream = fs.createWriteStream.bind(fs); @@ -42,10 +41,6 @@ exports.chmod = (path, mode) => chmod(path, mode).catch(error => { throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error); }); -exports.chown = (path, uid, gid) => chown(path, uid, gid).catch(error => { - throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error); -}); - exports.statSync = path => { try { return fs.statSync(path); @@ -62,22 +57,6 @@ exports.utimesSync = (path, atime, mtime) => { } }; -exports.chmodSync = (path, mode) => { - try { - return fs.chmodSync(path, mode); - } catch (error) { - throw new CpFileError(`chmod \`${path}\` failed: ${error.message}`, error); - } -}; - -exports.chownSync = (path, uid, gid) => { - try { - return fs.chownSync(path, uid, gid); - } catch (error) { - throw new CpFileError(`chown \`${path}\` failed: ${error.message}`, error); - } -}; - exports.makeDir = path => makeDir(path, {fs}).catch(error => { throw new CpFileError(`Cannot create directory \`${path}\`: ${error.message}`, error); }); diff --git a/index.js b/index.js index 7209e96..551aa4c 100644 --- a/index.js +++ b/index.js @@ -44,8 +44,7 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => { return Promise.all([ fs.utimes(destination, stats.atime, stats.mtime), - fs.chmod(destination, stats.mode), - fs.chown(destination, stats.uid, stats.gid) + fs.chmod(destination, stats.mode) ]); } }; @@ -83,11 +82,6 @@ const checkSourceIsFile = (stat, source) => { } }; -const fixupAttributes = (destination, stat) => { - fs.chmodSync(destination, stat.mode); - fs.chownSync(destination, stat.uid, stat.gid); -}; - module.exports.sync = (source, destination, options) => { if (!source || !destination) { throw new CpFileError('`source` and `destination` required'); @@ -114,5 +108,4 @@ module.exports.sync = (source, destination, options) => { } fs.utimesSync(destination, stat.atime, stat.mtime); - fixupAttributes(destination, stat); }; diff --git a/test/async.js b/test/async.js index 2c87715..33bdd93 100644 --- a/test/async.js +++ b/test/async.js @@ -113,14 +113,6 @@ test('preserve mode', async t => { t.is(licenseStats.mode, tempStats.mode); }); -test('preserve ownership', async t => { - await cpFile('license', t.context.destination); - const licenseStats = fs.lstatSync('license'); - const tempStats = fs.lstatSync(t.context.destination); - t.is(licenseStats.gid, tempStats.gid); - t.is(licenseStats.uid, tempStats.uid); -}); - test('throw an Error if `source` does not exists', async t => { const error = await t.throwsAsync(cpFile('NO_ENTRY', t.context.destination)); t.is(error.name, 'CpFileError', error.message); @@ -226,22 +218,6 @@ test.serial('rethrow chmod errors', async t => { fs.chmod.restore(); }); -test.serial('rethrow chown errors', async t => { - const chownError = buildEPERM(t.context.destination, 'chown'); - - fs.chown = sinon.stub(fs, 'chown').throws(chownError); - - clearModule('../fs'); - const uncached = importFresh('..'); - const error = await t.throwsAsync(uncached('license', t.context.destination)); - t.is(error.name, 'CpFileError', error.message); - t.is(error.code, chownError.code, error.message); - t.is(error.path, chownError.path, error.message); - t.true(fs.chown.called); - - fs.chown.restore(); -}); - test.serial('rethrow read after open errors', async t => { const {createWriteStream, createReadStream} = fs; let calledWriteEnd = 0; diff --git a/test/sync.js b/test/sync.js index 83048af..9edaaf0 100644 --- a/test/sync.js +++ b/test/sync.js @@ -6,7 +6,7 @@ import test from 'ava'; import uuid from 'uuid'; import sinon from 'sinon'; import assertDateEqual from './helpers/_assert'; -import {buildEACCES, buildENOSPC, buildEBADF, buildEPERM} from './helpers/_fs-errors'; +import {buildEACCES, buildENOSPC, buildEBADF} from './helpers/_fs-errors'; import cpFile from '..'; const THREE_HUNDRED_KILO = (100 * 3 * 1024) + 1; @@ -128,14 +128,6 @@ test('preserve mode', t => { t.is(licenseStats.mode, tempStats.mode); }); -test('preserve ownership', t => { - cpFile.sync('license', t.context.destination); - const licenseStats = fs.lstatSync('license'); - const tempStats = fs.lstatSync(t.context.destination); - t.is(licenseStats.gid, tempStats.gid); - t.is(licenseStats.uid, tempStats.uid); -}); - test('throw an Error if `source` does not exists', t => { const error = t.throws(() => { cpFile.sync('NO_ENTRY', t.context.destination); @@ -215,35 +207,3 @@ test('rethrow utimes errors', t => { fs.utimesSync.restore(); }); - -test('rethrow chmod errors', t => { - const chmodError = buildEPERM(t.context.destination, 'chmod'); - - fs.chmodSync = sinon.stub(fs, 'chmodSync').throws(chmodError); - - const error = t.throws(() => { - cpFile.sync('license', t.context.destination); - }); - t.is(error.name, 'CpFileError', error.message); - t.is(error.errno, chmodError.errno, error.message); - t.is(error.code, chmodError.code, error.message); - t.true(fs.chmodSync.called); - - fs.chmodSync.restore(); -}); - -test('rethrow chown errors', t => { - const chownError = buildEPERM(t.context.destination, 'chown'); - - fs.chownSync = sinon.stub(fs, 'chownSync').throws(chownError); - - const error = t.throws(() => { - cpFile.sync('license', t.context.destination); - }); - t.is(error.name, 'CpFileError', error.message); - t.is(error.errno, chownError.errno, error.message); - t.is(error.code, chownError.code, error.message); - t.true(fs.chownSync.called); - - fs.chownSync.restore(); -});