Skip to content

Commit 545c3b6

Browse files
authored
Merge pull request #65 from rnd-debug/feat/compress-with-rights
feat: enable to set mode of the compressed archive
2 parents 2f8f1bd + 488dc14 commit 545c3b6

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

lib/RollingFileWriteStream.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,14 @@ class RollingFileWriteStream extends Writable {
177177
index: i + 1
178178
});
179179

180+
const moveAndCompressOptions = {
181+
compress: this.options.compress && i === 0,
182+
mode: this.options.mode
183+
}
180184
await moveAndMaybeCompressFile(
181185
sourceFilePath,
182186
targetFilePath,
183-
this.options.compress && i === 0
187+
moveAndCompressOptions
184188
);
185189
}
186190

lib/moveAndMaybeCompressFile.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@ const debug = require('debug')('streamroller:moveAndMaybeCompressFile');
22
const fs = require('fs-extra');
33
const zlib = require('zlib');
44

5+
const _parseOption = function(rawOptions){
6+
const defaultOptions = {
7+
mode: parseInt("0644", 8),
8+
compress: false,
9+
};
10+
const options = Object.assign({}, defaultOptions, rawOptions);
11+
debug(
12+
`_parseOption: moveAndMaybeCompressFile called with option=${JSON.stringify(options)}`
13+
);
14+
return options;
15+
}
16+
517
const moveAndMaybeCompressFile = async (
618
sourceFilePath,
719
targetFilePath,
8-
needCompress
20+
options
921
) => {
22+
options = _parseOption(options);
1023
if (sourceFilePath === targetFilePath) {
1124
debug(
1225
`moveAndMaybeCompressFile: source and target are the same, not doing anything`
@@ -17,14 +30,14 @@ const moveAndMaybeCompressFile = async (
1730

1831
debug(
1932
`moveAndMaybeCompressFile: moving file from ${sourceFilePath} to ${targetFilePath} ${
20-
needCompress ? "with" : "without"
33+
options.compress ? "with" : "without"
2134
} compress`
2235
);
23-
if (needCompress) {
36+
if (options.compress) {
2437
await new Promise((resolve, reject) => {
2538
fs.createReadStream(sourceFilePath)
2639
.pipe(zlib.createGzip())
27-
.pipe(fs.createWriteStream(targetFilePath))
40+
.pipe(fs.createWriteStream(targetFilePath, {mode: options.mode}))
2841
.on("finish", () => {
2942
debug(
3043
`moveAndMaybeCompressFile: finished compressing ${targetFilePath}, deleting ${sourceFilePath}`

test/moveAndMaybeCompressFile-test.js

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ describe('moveAndMaybeCompressFile', () => {
3434
const source = path.join(TEST_DIR, 'test.log');
3535
const destination = path.join(TEST_DIR, 'moved-test.log.gz');
3636
await fs.outputFile(source, 'This is the test file.');
37-
await moveAndMaybeCompressFile(source, destination, true);
37+
const moveAndCompressOptions = {compress: true}
38+
await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions);
3839

3940
const zippedContents = await fs.readFile(destination);
4041
const contents = await new Promise(resolve => {
@@ -101,7 +102,8 @@ describe('moveAndMaybeCompressFile', () => {
101102
const source = path.join(TEST_DIR, 'test.log');
102103
const destination = path.join(TEST_DIR, 'moved-test.log.gz');
103104
await fs.outputFile(source, 'This is the test file.');
104-
await moveWithMock(source, destination, true);
105+
const options = {compress: true};
106+
await moveWithMock(source, destination, options);
105107

106108
const zippedContents = await fs.readFile(destination);
107109
const contents = await new Promise(resolve => {
@@ -115,4 +117,50 @@ describe('moveAndMaybeCompressFile', () => {
115117
(await fs.readFile(source, 'utf8')).should.be.empty()
116118

117119
});
120+
121+
it('should compress the source file at the new destination with 0o775 rights', async () => {
122+
const source = path.join(TEST_DIR, 'test.log');
123+
const destination = path.join(TEST_DIR, 'moved-test.log.gz');
124+
await fs.outputFile(source, 'This is the test file.');
125+
const moveAndCompressOptions = {compress: true, mode:0o775}
126+
await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions);
127+
128+
const destinationStats = await fs.stat(destination);
129+
const destMode = (destinationStats.mode & 0o775).toString(8);
130+
destMode.should.equal('775');
131+
132+
const zippedContents = await fs.readFile(destination);
133+
const contents = await new Promise(resolve => {
134+
zlib.gunzip(zippedContents, (e, data) => {
135+
resolve(data.toString());
136+
});
137+
});
138+
contents.should.equal('This is the test file.');
139+
140+
const exists = await fs.pathExists(source);
141+
exists.should.be.false();
142+
});
143+
144+
it('should compress the source file at the new destination with 0o400 rights', async () => {
145+
const source = path.join(TEST_DIR, 'test.log');
146+
const destination = path.join(TEST_DIR, 'moved-test.log.gz');
147+
await fs.outputFile(source, 'This is the test file.');
148+
const moveAndCompressOptions = {compress: true, mode:0o400}
149+
await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions);
150+
151+
const destinationStats = await fs.stat(destination);
152+
const destMode = (destinationStats.mode & 0o400).toString(8);
153+
destMode.should.equal('400');
154+
155+
const zippedContents = await fs.readFile(destination);
156+
const contents = await new Promise(resolve => {
157+
zlib.gunzip(zippedContents, (e, data) => {
158+
resolve(data.toString());
159+
});
160+
});
161+
contents.should.equal('This is the test file.');
162+
163+
const exists = await fs.pathExists(source);
164+
exists.should.be.false();
165+
});
118166
});

0 commit comments

Comments
 (0)