From e3881b1d12491db037dc447c972e6bde6a31541e Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 20 Dec 2018 10:52:30 -0500 Subject: [PATCH 1/2] Fix es_archiver rebuild_all action to support nested directories --- src/es_archiver/actions/rebuild_all.js | 40 ++++++++++++++++---------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/es_archiver/actions/rebuild_all.js b/src/es_archiver/actions/rebuild_all.js index ea4260db62900..d1e1fe9c740b3 100644 --- a/src/es_archiver/actions/rebuild_all.js +++ b/src/es_archiver/actions/rebuild_all.js @@ -19,6 +19,7 @@ import { resolve } from 'path'; import { + stat, rename, createReadStream, createWriteStream @@ -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) { + const inputDir = resolve(dataDir, name); + await rebuildFilesInDir({ log, archiveName: name, folderPath: inputDir }); + } +} From 1da0c33a7b51950aabb727360eff77e3ddd0a489 Mon Sep 17 00:00:00 2001 From: Mike Cote Date: Thu, 20 Dec 2018 15:55:19 -0500 Subject: [PATCH 2/2] Merge two functions into one --- src/es_archiver/actions/rebuild_all.js | 62 ++++++++++++++------------ 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/src/es_archiver/actions/rebuild_all.js b/src/es_archiver/actions/rebuild_all.js index d1e1fe9c740b3..c2e593c2cbd94 100644 --- a/src/es_archiver/actions/rebuild_all.js +++ b/src/es_archiver/actions/rebuild_all.js @@ -17,7 +17,12 @@ * under the License. */ -import { resolve } from 'path'; +import { + resolve, + dirname, + relative +} from 'path'; + import { stat, rename, @@ -39,37 +44,38 @@ import { createFormatArchiveStreams, } from '../lib'; -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'); +async function isDirectory(path) { + const stats = await fromNode(cb => stat(path, cb)); + return stats.isDirectory(); +} - await createPromiseFromStreams([ - createReadStream(filePath), - ...createParseArchiveStreams({ gzip }), - ...createFormatArchiveStreams({ gzip }), - createWriteStream(tempFile), - ]); +export async function rebuildAllAction({ dataDir, log, rootDir = dataDir }) { + const childNames = prioritizeMappings(await readDirectory(dataDir)); + for (const childName of childNames) { + const childPath = resolve(dataDir, childName); - await fromNode(cb => rename(tempFile, filePath, cb)); - log.info(`${archiveName} Rebuilt ${filename}`); + if (await isDirectory(childPath)) { + await rebuildAllAction({ + dataDir: childPath, + log, + rootDir, + }); + continue; } - } -} -export async function rebuildAllAction({ dataDir, log }) { - const archiveNames = await readDirectory(dataDir); + const archiveName = dirname(relative(rootDir, childPath)); + log.info(`${archiveName} Rebuilding ${childName}`); + const gzip = isGzip(childPath); + const tempFile = childPath + (gzip ? '.rebuilding.gz' : '.rebuilding'); + + await createPromiseFromStreams([ + createReadStream(childPath), + ...createParseArchiveStreams({ gzip }), + ...createFormatArchiveStreams({ gzip }), + createWriteStream(tempFile), + ]); - for (const name of archiveNames) { - const inputDir = resolve(dataDir, name); - await rebuildFilesInDir({ log, archiveName: name, folderPath: inputDir }); + await fromNode(cb => rename(tempFile, childPath, cb)); + log.info(`${archiveName} Rebuilt ${childName}`); } }