1
- import type { DBTransaction } from '@matrixai/db' ;
1
+ import type { Crypto , DBTransaction } from '@matrixai/db' ;
2
2
import type {
3
3
Navigated ,
4
4
ParsedPath ,
@@ -93,17 +93,22 @@ class EncryptedFS {
93
93
logger ?: Logger ;
94
94
fresh ?: boolean ;
95
95
} ) : Promise < EncryptedFS > {
96
+ let crypto : {
97
+ key : Buffer ,
98
+ ops : Crypto
99
+ } | undefined ;
96
100
if ( db == null ) {
101
+ crypto = {
102
+ key : dbKey ! ,
103
+ ops : {
104
+ encrypt : utils . encrypt ,
105
+ decrypt : utils . decrypt ,
106
+ } ,
107
+ } ;
97
108
try {
98
109
db = await DB . createDB ( {
99
110
dbPath : dbPath ! ,
100
- crypto : {
101
- key : dbKey ! ,
102
- ops : {
103
- encrypt : utils . encrypt ,
104
- decrypt : utils . decrypt ,
105
- } ,
106
- } ,
111
+ crypto,
107
112
logger : logger . getChild ( DB . name ) ,
108
113
fresh,
109
114
} ) ;
@@ -158,6 +163,7 @@ class EncryptedFS {
158
163
fdMgr = fdMgr ?? new FileDescriptorManager ( iNodeMgr ) ;
159
164
const efs = new this ( {
160
165
db,
166
+ crypto,
161
167
iNodeMgr,
162
168
fdMgr,
163
169
rootIno,
@@ -175,6 +181,14 @@ class EncryptedFS {
175
181
public readonly blockSize : number ;
176
182
177
183
protected db : DB ;
184
+ /**
185
+ * If crypto is created and passed in.
186
+ * This means the DB object was created by ourselves.
187
+ * This means EFS will manage the lifecycle of the encapsulated DB.
188
+ * However if the DB was passed in from the outside.
189
+ * Then EFS will not manage the lifecycle of the DB.
190
+ */
191
+ protected crypto ?: { key : Buffer ; ops : Crypto } ;
178
192
protected iNodeMgr : INodeManager ;
179
193
protected fdMgr : FileDescriptorManager ;
180
194
protected logger : Logger ;
@@ -186,6 +200,7 @@ class EncryptedFS {
186
200
187
201
constructor ( {
188
202
db,
203
+ crypto,
189
204
iNodeMgr,
190
205
fdMgr,
191
206
rootIno,
@@ -196,6 +211,10 @@ class EncryptedFS {
196
211
chrootParent,
197
212
} : {
198
213
db : DB ;
214
+ crypto ?: {
215
+ key : Buffer ;
216
+ ops : Crypto ;
217
+ } ;
199
218
iNodeMgr : INodeManager ;
200
219
fdMgr : FileDescriptorManager ;
201
220
rootIno : INodeIndex ;
@@ -207,6 +226,7 @@ class EncryptedFS {
207
226
} ) {
208
227
this . logger = logger ;
209
228
this . db = db ;
229
+ this . crypto = crypto ;
210
230
this . iNodeMgr = iNodeMgr ;
211
231
this . fdMgr = fdMgr ;
212
232
this . rootIno = rootIno ;
@@ -236,7 +256,9 @@ class EncryptedFS {
236
256
} = { } ) : Promise < void > {
237
257
this . logger . info ( `Starting ${ this . constructor . name } ` ) ;
238
258
if ( this . chrootParent == null ) {
239
- await this . db . start ( { fresh } ) ;
259
+ if ( this . crypto != null ) {
260
+ await this . db . start ( { crypto : this . crypto , fresh } ) ;
261
+ }
240
262
await this . iNodeMgr . start ( { fresh } ) ;
241
263
} else {
242
264
// If chrooted instance, add itself to the chroots set
@@ -252,7 +274,9 @@ class EncryptedFS {
252
274
await efsChrooted . stop ( ) ;
253
275
}
254
276
await this . iNodeMgr . stop ( ) ;
255
- await this . db . stop ( ) ;
277
+ if ( this . crypto != null ) {
278
+ await this . db . stop ( ) ;
279
+ }
256
280
} else {
257
281
// If chrooted instance, delete itself from the chroots set
258
282
this . chroots . delete ( this ) ;
@@ -263,10 +287,13 @@ class EncryptedFS {
263
287
public async destroy ( ) : Promise < void > {
264
288
this . logger . info ( `Destroying ${ this . constructor . name } ` ) ;
265
289
if ( this . chrootParent == null ) {
266
- // No need to destroy with `this.iNodeMgr.destroy()`
267
- // It would require restarting the DB
268
- // It is sufficient to only destroy the database
269
- await this . db . destroy ( ) ;
290
+ if ( this . crypto != null ) {
291
+ await this . db . destroy ( ) ;
292
+ } else {
293
+ // If it is not an encapsulated DB instance,
294
+ // then we only clear the inodes
295
+ await this . iNodeMgr . destroy ( ) ;
296
+ }
270
297
}
271
298
// No destruction procedures for chrooted instances
272
299
this . logger . info ( `Destroyed ${ this . constructor . name } ` ) ;
@@ -1985,9 +2012,6 @@ class EncryptedFS {
1985
2012
throw e ;
1986
2013
}
1987
2014
} ;
1988
-
1989
- console . log ( 'WHAT IS THIS?' , path ) ;
1990
-
1991
2015
let navigated = await this . navigate ( path , false ) ;
1992
2016
// Mutable transaction contexts may be inherited across loop iterations
1993
2017
// During concurrent open calls with `O_CREAT`, calls may race to create the inode
@@ -2415,7 +2439,6 @@ class EncryptedFS {
2415
2439
let totalBuffer = Buffer . alloc ( 0 ) ;
2416
2440
let bytesRead : number | undefined = undefined ;
2417
2441
if ( typeof file !== 'number' ) {
2418
- console . log ( 'I AM HERE' , file , options . flag ) ;
2419
2442
fdIndex = await this . open ( file , options . flag ) ;
2420
2443
file = fdIndex ;
2421
2444
}
@@ -3427,7 +3450,6 @@ class EncryptedFS {
3427
3450
pathStack : [ ] ,
3428
3451
} ;
3429
3452
} else {
3430
- console . log ( '....' ) ;
3431
3453
return await this . navigateFrom (
3432
3454
this . rootIno ,
3433
3455
pathS ,
@@ -3438,11 +3460,6 @@ class EncryptedFS {
3438
3460
) ;
3439
3461
}
3440
3462
} else {
3441
- // ATO thing didn't work
3442
- // Signed license didn't work
3443
- // Not sure I really care about the qantas points
3444
- // The ATO portal thing is a bit more important
3445
- console . log ( '!!!!' , this . _cwd . ino , pathS , resolveLastLink , activeSymlinks , this . _cwd . pathStack , origPathS ) ;
3446
3463
return await this . navigateFrom (
3447
3464
this . _cwd . ino ,
3448
3465
pathS ,
@@ -3496,9 +3513,6 @@ class EncryptedFS {
3496
3513
DBTransaction ,
3497
3514
] ;
3498
3515
}
3499
-
3500
- console . log ( 'HERE' , curdir ) ;
3501
-
3502
3516
const curDirData = await this . iNodeMgr . get ( curdir , tran ) ;
3503
3517
if ( curDirData == null ) {
3504
3518
throw new errors . ErrorEncryptedFSError ( {
0 commit comments