diff --git a/lib/RollingFileWriteStream.js b/lib/RollingFileWriteStream.js index 34e60e4..c672464 100644 --- a/lib/RollingFileWriteStream.js +++ b/lib/RollingFileWriteStream.js @@ -177,10 +177,14 @@ class RollingFileWriteStream extends Writable { index: i + 1 }); + const moveAndCompressOptions = { + compress: this.options.compress && i === 0, + mode: this.options.mode + } await moveAndMaybeCompressFile( sourceFilePath, targetFilePath, - this.options.compress && i === 0 + moveAndCompressOptions ); } diff --git a/lib/moveAndMaybeCompressFile.js b/lib/moveAndMaybeCompressFile.js index b9b6068..3da4ca3 100644 --- a/lib/moveAndMaybeCompressFile.js +++ b/lib/moveAndMaybeCompressFile.js @@ -2,11 +2,24 @@ const debug = require('debug')('streamroller:moveAndMaybeCompressFile'); const fs = require('fs-extra'); const zlib = require('zlib'); +const _parseOption = function(rawOptions){ + const defaultOptions = { + mode: parseInt("0644", 8), + compress: false, + }; + const options = Object.assign({}, defaultOptions, rawOptions); + debug( + `_parseOption: moveAndMaybeCompressFile called with option=${JSON.stringify(options)}` + ); + return options; +} + const moveAndMaybeCompressFile = async ( sourceFilePath, targetFilePath, - needCompress + options ) => { + options = _parseOption(options); if (sourceFilePath === targetFilePath) { debug( `moveAndMaybeCompressFile: source and target are the same, not doing anything` @@ -17,14 +30,14 @@ const moveAndMaybeCompressFile = async ( debug( `moveAndMaybeCompressFile: moving file from ${sourceFilePath} to ${targetFilePath} ${ - needCompress ? "with" : "without" + options.compress ? "with" : "without" } compress` ); - if (needCompress) { + if (options.compress) { await new Promise((resolve, reject) => { fs.createReadStream(sourceFilePath) .pipe(zlib.createGzip()) - .pipe(fs.createWriteStream(targetFilePath)) + .pipe(fs.createWriteStream(targetFilePath, {mode: options.mode})) .on("finish", () => { debug( `moveAndMaybeCompressFile: finished compressing ${targetFilePath}, deleting ${sourceFilePath}` diff --git a/test/moveAndMaybeCompressFile-test.js b/test/moveAndMaybeCompressFile-test.js index acbe84b..775dc17 100644 --- a/test/moveAndMaybeCompressFile-test.js +++ b/test/moveAndMaybeCompressFile-test.js @@ -34,7 +34,8 @@ describe('moveAndMaybeCompressFile', () => { const source = path.join(TEST_DIR, 'test.log'); const destination = path.join(TEST_DIR, 'moved-test.log.gz'); await fs.outputFile(source, 'This is the test file.'); - await moveAndMaybeCompressFile(source, destination, true); + const moveAndCompressOptions = {compress: true} + await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions); const zippedContents = await fs.readFile(destination); const contents = await new Promise(resolve => { @@ -101,7 +102,8 @@ describe('moveAndMaybeCompressFile', () => { const source = path.join(TEST_DIR, 'test.log'); const destination = path.join(TEST_DIR, 'moved-test.log.gz'); await fs.outputFile(source, 'This is the test file.'); - await moveWithMock(source, destination, true); + const options = {compress: true}; + await moveWithMock(source, destination, options); const zippedContents = await fs.readFile(destination); const contents = await new Promise(resolve => { @@ -115,4 +117,50 @@ describe('moveAndMaybeCompressFile', () => { (await fs.readFile(source, 'utf8')).should.be.empty() }); + + it('should compress the source file at the new destination with 0o775 rights', async () => { + const source = path.join(TEST_DIR, 'test.log'); + const destination = path.join(TEST_DIR, 'moved-test.log.gz'); + await fs.outputFile(source, 'This is the test file.'); + const moveAndCompressOptions = {compress: true, mode:0o775} + await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions); + + const destinationStats = await fs.stat(destination); + const destMode = (destinationStats.mode & 0o775).toString(8); + destMode.should.equal('775'); + + const zippedContents = await fs.readFile(destination); + const contents = await new Promise(resolve => { + zlib.gunzip(zippedContents, (e, data) => { + resolve(data.toString()); + }); + }); + contents.should.equal('This is the test file.'); + + const exists = await fs.pathExists(source); + exists.should.be.false(); + }); + + it('should compress the source file at the new destination with 0o400 rights', async () => { + const source = path.join(TEST_DIR, 'test.log'); + const destination = path.join(TEST_DIR, 'moved-test.log.gz'); + await fs.outputFile(source, 'This is the test file.'); + const moveAndCompressOptions = {compress: true, mode:0o400} + await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions); + + const destinationStats = await fs.stat(destination); + const destMode = (destinationStats.mode & 0o400).toString(8); + destMode.should.equal('400'); + + const zippedContents = await fs.readFile(destination); + const contents = await new Promise(resolve => { + zlib.gunzip(zippedContents, (e, data) => { + resolve(data.toString()); + }); + }); + contents.should.equal('This is the test file.'); + + const exists = await fs.pathExists(source); + exists.should.be.false(); + }); });