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 all commits
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
56 changes: 36 additions & 20 deletions src/es_archiver/actions/rebuild_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@
* under the License.
*/

import { resolve } from 'path';
import {
resolve,
dirname,
relative
} from 'path';

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

export async function rebuildAllAction({ dataDir, log }) {
const archiveNames = await readDirectory(dataDir);
async function isDirectory(path) {
const stats = await fromNode(cb => stat(path, cb));
return stats.isDirectory();
}

export async function rebuildAllAction({ dataDir, log, rootDir = dataDir }) {
const childNames = prioritizeMappings(await readDirectory(dataDir));
for (const childName of childNames) {
const childPath = resolve(dataDir, childName);

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);
if (await isDirectory(childPath)) {
await rebuildAllAction({
dataDir: childPath,
log,
rootDir,
});
continue;
}

const path = resolve(inputDir, filename);
const gzip = isGzip(path);
const tempFile = path + (gzip ? '.rebuilding.gz' : '.rebuilding');
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(path),
...createParseArchiveStreams({ gzip }),
...createFormatArchiveStreams({ gzip }),
createWriteStream(tempFile),
]);
await createPromiseFromStreams([
createReadStream(childPath),
...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, childPath, cb));
log.info(`${archiveName} Rebuilt ${childName}`);
}
}