Skip to content

Commit bae9441

Browse files
committed
WIP
1 parent 6f22fd2 commit bae9441

File tree

4 files changed

+47
-49
lines changed

4 files changed

+47
-49
lines changed

src/EncryptedFS.ts

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { DBTransaction } from '@matrixai/db';
1+
import type { Crypto, DBTransaction } from '@matrixai/db';
22
import type {
33
Navigated,
44
ParsedPath,
@@ -93,17 +93,22 @@ class EncryptedFS {
9393
logger?: Logger;
9494
fresh?: boolean;
9595
}): Promise<EncryptedFS> {
96+
let crypto: {
97+
key: Buffer,
98+
ops: Crypto
99+
} | undefined;
96100
if (db == null) {
101+
crypto = {
102+
key: dbKey!,
103+
ops: {
104+
encrypt: utils.encrypt,
105+
decrypt: utils.decrypt,
106+
},
107+
};
97108
try {
98109
db = await DB.createDB({
99110
dbPath: dbPath!,
100-
crypto: {
101-
key: dbKey!,
102-
ops: {
103-
encrypt: utils.encrypt,
104-
decrypt: utils.decrypt,
105-
},
106-
},
111+
crypto,
107112
logger: logger.getChild(DB.name),
108113
fresh,
109114
});
@@ -158,6 +163,7 @@ class EncryptedFS {
158163
fdMgr = fdMgr ?? new FileDescriptorManager(iNodeMgr);
159164
const efs = new this({
160165
db,
166+
crypto,
161167
iNodeMgr,
162168
fdMgr,
163169
rootIno,
@@ -175,6 +181,14 @@ class EncryptedFS {
175181
public readonly blockSize: number;
176182

177183
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 };
178192
protected iNodeMgr: INodeManager;
179193
protected fdMgr: FileDescriptorManager;
180194
protected logger: Logger;
@@ -186,6 +200,7 @@ class EncryptedFS {
186200

187201
constructor({
188202
db,
203+
crypto,
189204
iNodeMgr,
190205
fdMgr,
191206
rootIno,
@@ -196,6 +211,10 @@ class EncryptedFS {
196211
chrootParent,
197212
}: {
198213
db: DB;
214+
crypto?: {
215+
key: Buffer;
216+
ops: Crypto;
217+
};
199218
iNodeMgr: INodeManager;
200219
fdMgr: FileDescriptorManager;
201220
rootIno: INodeIndex;
@@ -207,6 +226,7 @@ class EncryptedFS {
207226
}) {
208227
this.logger = logger;
209228
this.db = db;
229+
this.crypto = crypto;
210230
this.iNodeMgr = iNodeMgr;
211231
this.fdMgr = fdMgr;
212232
this.rootIno = rootIno;
@@ -236,7 +256,9 @@ class EncryptedFS {
236256
} = {}): Promise<void> {
237257
this.logger.info(`Starting ${this.constructor.name}`);
238258
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+
}
240262
await this.iNodeMgr.start({ fresh });
241263
} else {
242264
// If chrooted instance, add itself to the chroots set
@@ -252,7 +274,9 @@ class EncryptedFS {
252274
await efsChrooted.stop();
253275
}
254276
await this.iNodeMgr.stop();
255-
await this.db.stop();
277+
if (this.crypto != null) {
278+
await this.db.stop();
279+
}
256280
} else {
257281
// If chrooted instance, delete itself from the chroots set
258282
this.chroots.delete(this);
@@ -263,10 +287,13 @@ class EncryptedFS {
263287
public async destroy(): Promise<void> {
264288
this.logger.info(`Destroying ${this.constructor.name}`);
265289
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+
}
270297
}
271298
// No destruction procedures for chrooted instances
272299
this.logger.info(`Destroyed ${this.constructor.name}`);
@@ -1985,9 +2012,6 @@ class EncryptedFS {
19852012
throw e;
19862013
}
19872014
};
1988-
1989-
console.log('WHAT IS THIS?', path);
1990-
19912015
let navigated = await this.navigate(path, false);
19922016
// Mutable transaction contexts may be inherited across loop iterations
19932017
// During concurrent open calls with `O_CREAT`, calls may race to create the inode
@@ -2415,7 +2439,6 @@ class EncryptedFS {
24152439
let totalBuffer = Buffer.alloc(0);
24162440
let bytesRead: number | undefined = undefined;
24172441
if (typeof file !== 'number') {
2418-
console.log('I AM HERE', file, options.flag);
24192442
fdIndex = await this.open(file, options.flag);
24202443
file = fdIndex;
24212444
}
@@ -3427,7 +3450,6 @@ class EncryptedFS {
34273450
pathStack: [],
34283451
};
34293452
} else {
3430-
console.log('....');
34313453
return await this.navigateFrom(
34323454
this.rootIno,
34333455
pathS,
@@ -3438,11 +3460,6 @@ class EncryptedFS {
34383460
);
34393461
}
34403462
} 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);
34463463
return await this.navigateFrom(
34473464
this._cwd.ino,
34483465
pathS,
@@ -3496,9 +3513,6 @@ class EncryptedFS {
34963513
DBTransaction,
34973514
];
34983515
}
3499-
3500-
console.log('HERE', curdir);
3501-
35023516
const curDirData = await this.iNodeMgr.get(curdir, tran);
35033517
if (curDirData == null) {
35043518
throw new errors.ErrorEncryptedFSError({

src/inodes/INodeManager.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,6 @@ class INodeManager {
375375
}
376376
nlink = 1;
377377
}
378-
console.log('CREATING INODE');
379378
await this.iNodeCreate(
380379
'Directory',
381380
{
@@ -423,9 +422,6 @@ class INodeManager {
423422
params: INodeParams,
424423
tran: DBTransaction,
425424
): Promise<void> {
426-
427-
console.log('CREATING INODE', type, params);
428-
429425
const statPath = [...this.statsDbPath, params.ino.toString()];
430426
params.dev = params.dev ?? 0;
431427
params.nlink = params.nlink ?? 0;
@@ -584,20 +580,10 @@ class INodeManager {
584580
if (tran == null) {
585581
return this.withTransactionF(ino, async (tran) => this.get(ino, tran));
586582
}
587-
588-
console.log(
589-
'WHAT IS THIS?',
590-
this.iNodesDbPath,
591-
inodesUtils.iNodeId(ino as INodeIndex),
592-
);
593-
594583
const type = await tran.get<INodeType>([
595584
...this.iNodesDbPath,
596585
inodesUtils.iNodeId(ino as INodeIndex),
597586
]);
598-
599-
console.log('Acquired type', type);
600-
601587
if (type == null) {
602588
return;
603589
}

tests/EncryptedFS.concurrent.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4037,7 +4037,11 @@ describe(`${EncryptedFS.name} Concurrency`, () => {
40374037
results.every((result) => {
40384038
return (
40394039
result.status === 'fulfilled' &&
4040-
(result.value === [] || result.value === undefined)
4040+
(
4041+
(Array.isArray(result.value) && result.value.length === 0)
4042+
||
4043+
result.value === undefined
4044+
)
40414045
);
40424046
})
40434047
) {

tests/EncryptedFS.test.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,18 @@ describe(EncryptedFS.name, () => {
4242
await efs.chdir('.');
4343
}).rejects.toThrow(errors.ErrorEncryptedFSNotRunning);
4444
});
45-
test.only('efs is persistent across restarts', async () => {
45+
test('efs is persistent across restarts', async () => {
4646
const efs1 = await EncryptedFS.createEncryptedFS({
4747
dbPath: dataDir,
4848
dbKey,
4949
logger,
5050
});
51-
console.log('1');
5251
await efs1.writeFile('testfile', 'hello world');
53-
console.log('------------READFILE', await efs1.readFile('testfile', { encoding: 'utf-8' }));
54-
console.log('2');
5552
await efs1.stop();
56-
console.log('3');
5753
await efs1.start();
58-
console.log('4');
5954
expect(await efs1.readFile('testfile', { encoding: 'utf-8' })).toBe(
6055
'hello world',
6156
);
62-
console.log('5');
6357
await efs1.stop();
6458
const efs2 = await EncryptedFS.createEncryptedFS({
6559
dbPath: dataDir,

0 commit comments

Comments
 (0)