Skip to content

Commit

Permalink
fix: better db location logic
Browse files Browse the repository at this point in the history
  • Loading branch information
FUDCo committed Jul 1, 2021
1 parent 07b300e commit a76d3b7
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions packages/SwingSet/bin/rekernelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,58 @@ import 'node-lmdb';
import '@agoric/babel-standalone';
import '@agoric/install-ses';

import fs from 'fs';
import path from 'path';
import process from 'process';

import bundleSource from '@agoric/bundle-source';
import { openLMDBSwingStore } from '@agoric/swing-store-lmdb';

function fail(message) {
console.error(message);
process.exit(1);
}

function dirContains(dirpath, suffix) {
try {
const files = fs.readdirSync(dirpath);
for (const file of files) {
if (file.endsWith(suffix)) {
return true;
}
}
return false;
} catch (e) {
return false;
}
}

async function main() {
const argv = process.argv.slice(2);

let dbDir = '.';
if (argv.length > 1) {
console.error('usage: rekernelize [DBDIR]');
process.exit(1);
fail('usage: rekernelize [DBDIR_OR_FILE]');
} else if (argv[0]) {
dbDir = argv[0];
}

const kernelStateDBDir = path.join(dbDir, 'swingset-kernel-state');
let kernelStateDBDir;
const dbSuffix = '.mdb';
if (dbDir.endsWith(dbSuffix)) {
kernelStateDBDir = path.dirname(dbDir);
} else if (dirContains(dbDir, dbSuffix)) {
kernelStateDBDir = dbDir;
} else {
kernelStateDBDir = path.join(dbDir, 'swingset-kernel-state');
if (!dirContains(kernelStateDBDir, dbSuffix)) {
kernelStateDBDir = null;
}
}
if (!kernelStateDBDir) {
fail(`can't find a database at ${dbDir}`);
}

const swingStore = openLMDBSwingStore(kernelStateDBDir);
const kvStore = swingStore.kvStore;
assert(kvStore.get('initialized'), 'kernel store not initialized');
Expand Down

0 comments on commit a76d3b7

Please sign in to comment.