Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix es_archiver rebuild_all action to support nested directories #27592

Merged
merged 3 commits into from
Dec 21, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/es_archiver/actions/rebuild_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { resolve } from 'path';
import {
stat,
rename,
createReadStream,
createWriteStream
Expand All @@ -38,28 +39,37 @@ import {
createFormatArchiveStreams,
} from '../lib';

export async function rebuildAllAction({ dataDir, log }) {
const archiveNames = await readDirectory(dataDir);

for (const name of archiveNames) {
const inputDir = resolve(dataDir, name);
const files = prioritizeMappings(await readDirectory(inputDir));
for (const filename of files) {
log.info('[%s] Rebuilding %j', name, filename);

const path = resolve(inputDir, filename);
const gzip = isGzip(path);
const tempFile = path + (gzip ? '.rebuilding.gz' : '.rebuilding');
async function rebuildFilesInDir({ log, folderPath, archiveName }) {
const files = prioritizeMappings(await readDirectory(folderPath));
for (const filename of files) {
const filePath = resolve(folderPath, filename);
const stats = await fromNode(cb => stat(filePath, cb));
if (stats.isDirectory()) {
// Recursion
await rebuildFilesInDir({ log, archiveName, folderPath: filePath });
} else {
log.info(`${archiveName} Rebuilding ${filename}`);
const gzip = isGzip(filePath);
const tempFile = filePath + (gzip ? '.rebuilding.gz' : '.rebuilding');

await createPromiseFromStreams([
createReadStream(path),
createReadStream(filePath),
...createParseArchiveStreams({ gzip }),
...createFormatArchiveStreams({ gzip }),
createWriteStream(tempFile),
]);

await fromNode(cb => rename(tempFile, path, cb));
log.info('[%s] Rebuilt %j', name, filename);
await fromNode(cb => rename(tempFile, filePath, cb));
log.info(`${archiveName} Rebuilt ${filename}`);
}
}
}

export async function rebuildAllAction({ dataDir, log }) {
const archiveNames = await readDirectory(dataDir);

for (const name of archiveNames) {
spalger marked this conversation as resolved.
Show resolved Hide resolved
const inputDir = resolve(dataDir, name);
await rebuildFilesInDir({ log, archiveName: name, folderPath: inputDir });
}
}