@@ -6,24 +6,58 @@ import 'node-lmdb';
6
6
import '@agoric/babel-standalone' ;
7
7
import '@agoric/install-ses' ;
8
8
9
+ import fs from 'fs' ;
9
10
import path from 'path' ;
10
11
import process from 'process' ;
11
12
12
13
import bundleSource from '@agoric/bundle-source' ;
13
14
import { openLMDBSwingStore } from '@agoric/swing-store-lmdb' ;
14
15
16
+ function fail ( message ) {
17
+ console . error ( message ) ;
18
+ process . exit ( 1 ) ;
19
+ }
20
+
21
+ function dirContains ( dirpath , suffix ) {
22
+ try {
23
+ const files = fs . readdirSync ( dirpath ) ;
24
+ for ( const file of files ) {
25
+ if ( file . endsWith ( suffix ) ) {
26
+ return true ;
27
+ }
28
+ }
29
+ return false ;
30
+ } catch ( e ) {
31
+ return false ;
32
+ }
33
+ }
34
+
15
35
async function main ( ) {
16
36
const argv = process . argv . slice ( 2 ) ;
17
37
18
38
let dbDir = '.' ;
19
39
if ( argv . length > 1 ) {
20
- console . error ( 'usage: rekernelize [DBDIR]' ) ;
21
- process . exit ( 1 ) ;
40
+ fail ( 'usage: rekernelize [DBDIR_OR_FILE]' ) ;
22
41
} else if ( argv [ 0 ] ) {
23
42
dbDir = argv [ 0 ] ;
24
43
}
25
44
26
- const kernelStateDBDir = path . join ( dbDir , 'swingset-kernel-state' ) ;
45
+ let kernelStateDBDir ;
46
+ const dbSuffix = '.mdb' ;
47
+ if ( dbDir . endsWith ( dbSuffix ) ) {
48
+ kernelStateDBDir = path . dirname ( dbDir ) ;
49
+ } else if ( dirContains ( dbDir , dbSuffix ) ) {
50
+ kernelStateDBDir = dbDir ;
51
+ } else {
52
+ kernelStateDBDir = path . join ( dbDir , 'swingset-kernel-state' ) ;
53
+ if ( ! dirContains ( kernelStateDBDir , dbSuffix ) ) {
54
+ kernelStateDBDir = null ;
55
+ }
56
+ }
57
+ if ( ! kernelStateDBDir ) {
58
+ fail ( `can't find a database at ${ dbDir } ` ) ;
59
+ }
60
+
27
61
const swingStore = openLMDBSwingStore ( kernelStateDBDir ) ;
28
62
const kvStore = swingStore . kvStore ;
29
63
assert ( kvStore . get ( 'initialized' ) , 'kernel store not initialized' ) ;
0 commit comments